Work in progress

Refactoring cstr_copy and adding test
This commit is contained in:
Michael Tryby
2019-04-16 15:51:19 -04:00
parent ac56971ef2
commit 02ec735c58
4 changed files with 31 additions and 12 deletions

View File

@@ -17,13 +17,10 @@
#include "cstr_helper.h"
int cstr_copy(const char *source, char **dest)
// Determines length, allocates memory, and returns a null terminated copy
// Be Aware: caller is responsible for freeing memory
int cstr_duplicate(char **dest, const char *source)
// Duplicates source string
{
size_t size;
size = 1 + strlen(source);
size_t size = 1 + strlen(source);
*dest = (char *) calloc(size, sizeof(char));
if (*dest == NULL)

View File

@@ -23,7 +23,7 @@ extern "C" {
#endif
int cstr_copy(const char *source, char **destination);
int cstr_duplicate(char **dest, const char *source);
bool cstr_validate_id(const char *element_id);

View File

@@ -63,7 +63,7 @@ int get_filename(file_handle_t *file_handle, char **filename)
// BE AWARE: The memory allocated here must be freed by the caller
//
{
return cstr_copy(file_handle->filename, filename);
return cstr_duplicate(filename, file_handle->filename);
}
@@ -73,7 +73,7 @@ int open_file(file_handle_t *file_handle, const char *filename, const char *file
if (filename == NULL)
_get_temp_filename(&(file_handle->filename));
else
cstr_copy(filename, &(file_handle->filename));
cstr_duplicate(&(file_handle->filename), filename);
if (file_mode == NULL)
error = -1;
@@ -195,7 +195,7 @@ int _get_temp_filename(char **tempname)
return error;
}
else
cstr_copy(name, tempname);
cstr_duplicate(tempname, name);
// --- free the pointer returned by _tempnam
if (name)
@@ -204,7 +204,7 @@ int _get_temp_filename(char **tempname)
// --- for non-Windows systems:
#else
// --- use system function mkstemp() to create a temporary file name
cstr_copy("enXXXXXX", tempname);
cstr_duplicate(tempname, "enXXXXXX");
error = mkstemp(*tempname);
#endif
return error;