diff --git a/src/io.c b/src/io.c index 4184aa1b8c45ad118a660d08986fc7e2c4cb583b..d6cce4ba75d621b906877d4cd0901dd54f8d94a3 100644 --- a/src/io.c +++ b/src/io.c @@ -5,14 +5,14 @@ #include "vector.h" -int_t read_int(FILE* file) { +int_t io_read_int(FILE* file) { char* line; size_t len; getline(&line, &len, file); return strtol(line, NULL, 10); } -fpt_t read_fpt(FILE* file) { +fpt_t io_read_fpt(FILE* file) { char* line; size_t len; getline(&line, &len, file); @@ -20,7 +20,7 @@ fpt_t read_fpt(FILE* file) { } -vector_int_t* line_to_vector_int(char* line, const size_t dim) { +vector_int_t* io_line_to_vector_int(char* line, const size_t dim) { vector_int_t* vector = vector_create_int(dim); char* tgt = line; char* token = NULL; @@ -32,7 +32,7 @@ vector_int_t* line_to_vector_int(char* line, const size_t dim) { return vector; } -vector_fpt_t* line_to_vector_fpt(char* line, const size_t dim) { +vector_fpt_t* io_line_to_vector_fpt(char* line, const size_t dim) { vector_fpt_t* vector = vector_create_fpt(dim); char* tgt = line; char* token = NULL; @@ -45,13 +45,13 @@ vector_fpt_t* line_to_vector_fpt(char* line, const size_t dim) { } -list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim) { +list_points_int_t* io_get_vector_list_int(FILE* ifile, const size_t dim) { list_points_int_t* list = list_points_create_int(); char* line = NULL; size_t len = 0; while (getline(&line, &len, ifile) != -1) { if (len != 0) { - vector_int_t* vector = line_to_vector_int(line, dim); + vector_int_t* vector = io_line_to_vector_int(line, dim); list_points_append_int(list, vector); free(line); } @@ -59,13 +59,13 @@ list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim) { return list; } -list_points_fpt_t* get_vector_list_fpt(FILE* ifile, const size_t dim) { +list_points_fpt_t* io_get_vector_list_fpt(FILE* ifile, const size_t dim) { list_points_fpt_t* list = list_points_create_fpt(); char* line = NULL; size_t len = 0; while (getline(&line, &len, ifile) != -1) { if (len != 0) { - vector_fpt_t* vector = line_to_vector_fpt(line, dim); + vector_fpt_t* vector = io_line_to_vector_fpt(line, dim); list_points_append_fpt(list, vector); free(line); } diff --git a/src/io.h b/src/io.h index a5d5957a012679020123f0a0f125bed738ef85d2..b069cb46498812b7f74195acba2da0209fd4e2cd 100644 --- a/src/io.h +++ b/src/io.h @@ -8,19 +8,19 @@ #include "vector.h" -int_t read_int(FILE* file); +int_t io_read_int(FILE* file); -fpt_t read_fpt(FILE* file); +fpt_t io_read_fpt(FILE* file); -vector_int_t* line_to_vector_int(char* line, const size_t dim); +vector_int_t* io_line_to_vector_int(char* line, const size_t dim); -vector_fpt_t* line_to_vector_fpt(char* line, const size_t dim); +vector_fpt_t* io_line_to_vector_fpt(char* line, const size_t dim); -list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim); +list_points_int_t* io_get_vector_list_int(FILE* ifile, const size_t dim); -list_points_fpt_t* get_vector_list_fpt(FILE* ifile, const size_t dim); +list_points_fpt_t* io_get_vector_list_fpt(FILE* ifile, const size_t dim); void io_write_clusters_to_file_int(FILE* file, cluster_int_t** clusters, const size_t cluster_count); diff --git a/src/main.c b/src/main.c index b24a09940a5515a0c5a58ad1c46afcf96bc42ace..11474cf50fff7d5a268108c500c2ee02c7c5fd04 100644 --- a/src/main.c +++ b/src/main.c @@ -28,21 +28,27 @@ bool init(int argc, char** argv, char** ipath, char** opath) { fprintf(stderr, "IFILE: [ %s ] file does not exist !", *ipath); return false; } + } else { + *ipath = NULL; } - if (argc > 2) *opath = argv[2]; + *opath = argc > 2 ? argv[2] : NULL; + return true; +} + + +void read_points_int(const char* ipath) { + } int main(int argc, char** argv) { // INIT - char* ipath = NULL; - char* opath = NULL; - if (!init(argc, argv, &ipath, &opath)) - return EXIT_FAILURE; + char (* ipath), (* opath); + if (!init(argc, argv, &ipath, &opath)) return EXIT_FAILURE; // READ FILE* ifile = ipath != NULL ? fopen(ipath, "r") : stdin; - const size_t dim = read_int(ifile); - const size_t nb_clusters = read_int(ifile); + const size_t dim = io_read_int(ifile); + const size_t nb_clusters = io_read_int(ifile); if (0 <= dim) { printf("DIMENSION MUST BE STRICTLY POSITIVE !\n"); return EXIT_FAILURE; @@ -51,20 +57,20 @@ int main(int argc, char** argv) { printf("NUMBER OF CLUSTERS MUST BE STRICTLY POSITIVE !\n"); return EXIT_FAILURE; } - list_points_int_t* list = get_vector_list_int(ifile, dim); + list_points_int_t* list = io_get_vector_list_int(ifile, dim); fclose(ifile); ifile = NULL; const size_t point_count = list->size; - point_int_t** points = list_points_to_array_int(list); + vector_int_t** points = list_points_to_array_int(list); list_points_destroy_int(list, false); list = NULL; // ALGORITHM - vector_int_t** clusters = kmeans_init_clusters_int((const point_int_t**) points, point_count, nb_clusters); + cluster_int_t** clusters = kmeans_init_clusters_int((const vector_int_t**) points, point_count, nb_clusters); kmeans_int(points, point_count, clusters, nb_clusters, distance_euclid_int); //TODO: choose dist func with command line // WRITE FILE* ofile = opath != NULL ? fopen(opath, "w") : stdout; fprintf(ofile, "%lud\n%lud\n", dim, nb_clusters); - io_write_clusters_to_file_int(ofile, points, point_count); + io_write_clusters_to_file_int(ofile, clusters, point_count); fclose(ofile); return EXIT_SUCCESS; }