diff options
author | Paul Duncan <pabs@pablotron.org> | 2019-02-05 00:22:15 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2019-02-05 00:22:15 -0500 |
commit | f557d1f49a2914c6084dd18efc783395228d8ce0 (patch) | |
tree | 51111fc899f01956cbab948b87c241478576870d /km-load.c | |
parent | b5065ea43cb13c0b553874305b53963176c70f59 (diff) | |
download | kmeans-f557d1f49a2914c6084dd18efc783395228d8ce0.tar.bz2 kmeans-f557d1f49a2914c6084dd18efc783395228d8ce0.zip |
mv *.[hc] src/
Diffstat (limited to 'km-load.c')
-rw-r--r-- | km-load.c | 112 |
1 files changed, 0 insertions, 112 deletions
diff --git a/km-load.c b/km-load.c deleted file mode 100644 index 3615aa9..0000000 --- a/km-load.c +++ /dev/null @@ -1,112 +0,0 @@ -#include <stdbool.h> // bool -#include <stdio.h> // fscanf() -#include <string.h> // strerror() -#include <errno.h> // errno -#include "util.h" -#include "km.h" - -#define FAIL(...) do { \ - if (cbs && cbs->on_error) { \ - char buf[1024]; \ - snprintf(buf, sizeof(buf), __VA_ARGS__); \ - cbs->on_error(buf, cb_data); \ - } \ - return false; \ -} while (0) - -_Bool -km_load( - FILE * const fh, - const km_load_cbs_t * const cbs, - void * const cb_data -) { - // read shape - km_shape_t shape = { 0, 0 }; - if (fscanf(fh, "%zu %zu", &(shape.num_floats), &(shape.num_ints)) != 2) { - FAIL("shape fscanf() failed: %s", strerror(errno)); - } - - if (cbs && cbs->on_shape) { - // emit shape - if (!cbs->on_shape(&shape, cb_data)) { - // return failure - return false; - } - } - - // alloc floats buffer - float *floats = NULL; - if (shape.num_floats > 0) { - floats = malloc(sizeof(float) * shape.num_floats); - if (!floats) { - FAIL("floats malloc() failed: %s", strerror(errno)); - } - } - - // alloc ints buffer - int *ints = NULL; - if (shape.num_ints > 0) { - ints = malloc(sizeof(int) * shape.num_ints); - if (!ints) { - FAIL("ints malloc() failed: %s", strerror(errno)); - } - } - - for (size_t row = 0; !feof(fh); row++) { - for (size_t i = 0; i < shape.num_floats; i++) { - if (fscanf(fh, " %f ", floats + i) != 1) { - FAIL("[%zu, %zu] float fscanf() failed: %s", row, i, strerror(errno)); - } - } - - // read ints - for (size_t i = 0; i < shape.num_ints; i++) { - if (fscanf(fh, " %d ", ints + i) != 1) { - FAIL("[%zu, %zu] int fscanf() failed: %s", row, i, strerror(errno)); - } - } - - if (cbs && cbs->on_row) { - // emit row - if (!cbs->on_row(floats, ints, cb_data)) { - // return failure - return false; - } - } - } - - if (shape.num_floats > 0) { - // free float buffer - free(floats); - } - - if (shape.num_ints > 0) { - // free int buffer - free(ints); - } - - // return success - return true; -} - -_Bool -km_load_path( - const char * const path, - const km_load_cbs_t * const cbs, - void * const cb_data -) { - // open file - FILE *fh = fopen(path, "rb"); - if (!fh) { - FAIL("fopen(\"%s\") failed: %s", path, strerror(errno)); - } - - // load file, get result - const bool r = km_load(fh, cbs, cb_data); - - // close file - fclose(fh); - - // return result - return r; -} |