star-sp

Random number generators and distributions
git clone git://git.meso-star.fr/star-sp.git
Log | Files | Refs | README | LICENSE

commit 83676812d584443a5a44537dc6589d896c475343
parent 27ba5fe7e4ce4126adba08b2040f477efe20ac88
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 23 Nov 2015 10:55:02 +0100

Update the KISS deserialisation routine

Rewrite the read function of the KISS RNG. This should fix the bug of
the CL19 compiler.

Diffstat:
Msrc/ssp_rng.c | 30++++++++++++++++++++++++++----
1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/src/ssp_rng.c b/src/ssp_rng.c @@ -31,6 +31,7 @@ #include "ssp_rng_c.h" +#include <rsys/dynamic_array_char.h> #include <rsys/mem_allocator.h> #ifdef COMPILER_CL @@ -149,10 +150,31 @@ static res_T rng_kiss_read(void* data, FILE* file) { struct rng_kiss* rng = (struct rng_kiss*)data; - int res; + struct darray_char buf; + res_T res = RES_OK; + int err; ASSERT(data && file); - res = fscanf(file, "%u %u %u %u", &rng->x, &rng->y, &rng->z, &rng->c); - return res == EOF || !res/* Required by CL O_o !!*/ ? RES_IO_ERR : RES_OK; + + darray_char_init(&mem_default_allocator, &buf); + res = darray_char_resize(&buf, 64); + if(res != RES_OK) goto error; + + while(fgets(darray_char_data_get(&buf), (int)darray_char_size_get(&buf), file)) { + /* Ensure that the whole line is read */ + if(strrchr(darray_char_cdata_get(&buf), '\n')) break; + res = darray_char_resize(&buf, darray_char_size_get(&buf) * 2); + if(res != RES_OK) goto error; + } + + err = sscanf(darray_char_cdata_get(&buf), "%u %u %u %u", + &rng->x, &rng->y, &rng->z, &rng->c); + res = err == EOF || !err ? RES_IO_ERR : RES_OK; + +exit: + darray_char_release(&buf); + return res; +error: + goto exit; } static res_T @@ -161,7 +183,7 @@ rng_kiss_write(const void* data, FILE* file) const struct rng_kiss* rng = (const struct rng_kiss*)data; int res; ASSERT(data && file); - res = fprintf(file, "%u %u %u %u", rng->x, rng->y, rng->z, rng->c); + res = fprintf(file, "%u %u %u %u\n", rng->x, rng->y, rng->z, rng->c); return res < 0 ? RES_IO_ERR : RES_OK; }