From b85ed706741d8325c78a1a4d85e52b6dd580a683 Mon Sep 17 00:00:00 2001 From: Al Date: Fri, 10 Mar 2017 13:39:43 -0500 Subject: [PATCH] [utils] adding a function for checking if files exists (yay C), or at least the closest agreed-upon method for it (may return false if the user doesn't have permissions, but that's ok for our purposes here) --- src/crf_trainer.h | 1 - src/file_utils.c | 7 +++++++ src/shuffle.c | 4 +--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/crf_trainer.h b/src/crf_trainer.h index 498aad8e..9b8b302e 100644 --- a/src/crf_trainer.h +++ b/src/crf_trainer.h @@ -31,7 +31,6 @@ bool crf_trainer_hash_feature_to_id_exists(crf_trainer_t *self, char *feature, u bool crf_trainer_hash_prev_tag_feature_to_id(crf_trainer_t *self, char *feature, uint32_t *feature_id); bool crf_trainer_hash_prev_tag_feature_to_id_exists(crf_trainer_t *self, char *feature, uint32_t *feature_id, bool *exists); - bool crf_trainer_get_feature_id(crf_trainer_t *self, char *feature, uint32_t *feature_id); bool crf_trainer_get_prev_tag_feature_id(crf_trainer_t *self, char *feature, uint32_t *feature_id); diff --git a/src/file_utils.c b/src/file_utils.c index af0aa3b8..f25e5ee6 100644 --- a/src/file_utils.c +++ b/src/file_utils.c @@ -32,6 +32,13 @@ char *file_getline(FILE * f) return ret; } +bool file_exists(char *filename) { + FILE *f = fopen(filename, "r"); + bool exists = f != NULL; + if (exists) fclose(f); + return exists; +} + bool is_relative_path(struct dirent *ent) { return strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0; } diff --git a/src/shuffle.c b/src/shuffle.c index a9704b4b..0a4293c7 100644 --- a/src/shuffle.c +++ b/src/shuffle.c @@ -47,11 +47,9 @@ bool shuffle_file_chunked(char *filename, size_t parts) { } // Make sure the input file exists - FILE *f = fopen(filename, "r"); - if (f == NULL) { + if (!file_exists(filename)) { return false; } - fclose(f); // This is an in-place shuffle to keep the API simple char *outfile = filename;