diff --git a/src/util/cstr_helper.c b/src/util/cstr_helper.c index 9266551..2e33d50 100644 --- a/src/util/cstr_helper.c +++ b/src/util/cstr_helper.c @@ -2,21 +2,33 @@ #include #include +#include "cstr_helper.h" -int copy_cstr(const char *source, char **destination, size_t *size) + +int copy_cstr(const char *source, char **dest) // Determines length, allocates memory, and returns a null terminated copy // Be Aware: caller is responsible for freeing memory { size_t len; len = strlen(source); - *destination = (char *) calloc(len + 1, sizeof(char)); + *dest = (char *) calloc((len + 1), sizeof(char)); - if (*destination == NULL) + if (*dest == NULL) return -1; else { - strncpy(*destination, source, len); - *size = len + 1; + strncpy(*dest, source, (len + 1)); + strncat(*dest, "\0", 1); } return 0; } + + +bool isnullterm_cstr(const char *source) +{ + if (strchr(source, '\0')) + return true; + else + return false; + +} diff --git a/src/util/cstr_helper.h b/src/util/cstr_helper.h index 1b435c1..55f71d0 100644 --- a/src/util/cstr_helper.h +++ b/src/util/cstr_helper.h @@ -3,7 +3,11 @@ #define CSTR_HELPER_H_ -int copy_cstr(const char *source, char **destination, size_t *size); +#include + + +int copy_cstr(const char *source, char **destination); +bool isnullterm_cstr(const char *source); #endif /* CSTR_HELPER_H_ */ diff --git a/src/util/filemanager.c b/src/util/filemanager.c index 98d775c..49ca4a3 100644 --- a/src/util/filemanager.c +++ b/src/util/filemanager.c @@ -27,15 +27,14 @@ typedef struct file_s { - char *filename; - size_t size; + char *filename; // Assumes this is a null terminated string FILE *file; } file_handle_t; // local (private) functions int _fopen(FILE **f, const char *name, const char *mode); -int _get_temp_filename(char **tempname, size_t *size); +int _get_temp_filename(char **tempname); file_handle_t *create_file_manager() { @@ -44,14 +43,13 @@ file_handle_t *create_file_manager() { file_handle = (file_handle_t *)calloc(1, sizeof(file_handle_t)); file_handle->filename = NULL; - file_handle->size = 0; file_handle->file = NULL; return file_handle; } void delete_file_manager(file_handle_t *file_handle) { - + if (file_handle->file != NULL) close_file(file_handle); @@ -65,7 +63,7 @@ void get_filename(file_handle_t *file_handle, char **filename, size_t *size) // BE AWARE: The memory allocated here must be freed by the caller // { - copy_cstr(file_handle->filename, filename, size); + copy_cstr(file_handle->filename, filename); } @@ -73,9 +71,9 @@ int open_file(file_handle_t *file_handle, const char *filename, const char *file int error = 0; if (filename == NULL) - _get_temp_filename(&(file_handle->filename), &(file_handle->size)); + _get_temp_filename(&(file_handle->filename)); else - copy_cstr(filename, &(file_handle->filename), &(file_handle->size)); + copy_cstr(filename, &(file_handle->filename)); if (file_mode == NULL) error = -1; @@ -173,7 +171,7 @@ int _fopen(FILE **f, const char *name, const char *mode) return ret; } -int _get_temp_filename(char **tempname, size_t *size) +int _get_temp_filename(char **tempname) { int error = 0; @@ -187,7 +185,7 @@ int _get_temp_filename(char **tempname, size_t *size) return error; } else - copy_cstr(name, tempname, size); + copy_cstr(name, tempname); // --- free the pointer returned by _tempnam if (name) @@ -201,3 +199,12 @@ int _get_temp_filename(char **tempname, size_t *size) #endif return error; } + +bool is_valid(file_handle_t *file_handle) +{ + if ((file_handle->filename == NULL && file_handle->file == NULL) || + (isnullterm_cstr(file_handle->filename) && file_handle != NULL)) + return true; + else + return false; +} diff --git a/src/util/filemanager.h b/src/util/filemanager.h index f0b00d2..93ee8d9 100644 --- a/src/util/filemanager.h +++ b/src/util/filemanager.h @@ -71,6 +71,9 @@ int close_file(file_handle_t *file_handle); int remove_file(file_handle_t *file_handle); +bool is_valid(file_handle_t *file_handle); + + #if defined(__cplusplus) } #endif diff --git a/tests/util/test_filemanager.cpp b/tests/util/test_filemanager.cpp index 310c620..70ff6c6 100644 --- a/tests/util/test_filemanager.cpp +++ b/tests/util/test_filemanager.cpp @@ -40,6 +40,7 @@ BOOST_AUTO_TEST_CASE (test_create_destroy) file_handle = create_file_manager(); BOOST_CHECK(file_handle != NULL); + BOOST_CHECK(is_valid(file_handle) == true); delete_file_manager(file_handle); } @@ -54,6 +55,7 @@ BOOST_AUTO_TEST_CASE(test_open_close) error = open_file(file_handle, DATA_PATH_OUTPUT, "rb"); BOOST_REQUIRE(error == 0); + BOOST_CHECK(is_valid(file_handle) == true); error = close_file(file_handle); BOOST_REQUIRE(error == 0); @@ -85,7 +87,7 @@ BOOST_FIXTURE_TEST_CASE(test_temp_file, Fixture) printf_file(file_handle, "%s", "This is a test."); get_filename(file_handle, &filename, &size); - //BOOST_CHECK(check_string(filename, "./test_file.txt")); + BOOST_CHECK(is_valid(file_handle) == true); BOOST_CHECK(boost::filesystem::exists(filename) == true);