This commit is contained in:
Michael Tryby
2019-04-17 10:53:08 -04:00
13 changed files with 231 additions and 24 deletions

View File

@@ -17,32 +17,43 @@
#include "cstr_helper.h"
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
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)
return -1;
else {
#ifdef _MSC_VER
strncpy_s(*dest, size, source, size);
strncpy_s(*dest, size, source, size);
#else
strncpy(*dest, source, size);
strncpy(*dest, source, size);
#endif
}
return 0;
}
bool isnullterm_cstr(const char *source)
bool cstr_isvalid(const char *element_id)
// Determines if invalid characters are present in an element id string
{
if (strchr(source, '\0'))
return true;
else
return false;
const char *invalid_chars = " \";";
// if invalid char is present a pointer to it is returned else NULL
if (strpbrk(element_id, invalid_chars))
return false;
else
return true;
}
bool cstr_isnullterm(const char *source)
// Determines if the string passed is null terminated or not
{
if (strchr(source, '\0'))
return true;
else
return false;
}

View File

@@ -18,8 +18,21 @@
#include <stdbool.h>
int copy_cstr(const char *source, char **destination);
bool isnullterm_cstr(const char *source);
#if defined(__cplusplus)
extern "C" {
#endif
int cstr_duplicate(char **dest, const char *source);
bool cstr_isvalid(const char *element_id);
bool cstr_isnullterm(const char *source);
#if defined(__cplusplus)
}
#endif
#endif /* CSTR_HELPER_H_ */

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 copy_cstr(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
copy_cstr(filename, &(file_handle->filename));
cstr_duplicate(&(file_handle->filename), filename);
if (file_mode == NULL)
error = -1;
@@ -156,7 +156,7 @@ int remove_file(file_handle_t *file_handle) {
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))
(cstr_isnullterm(file_handle->filename) && file_handle != NULL))
return true;
else
return false;
@@ -195,7 +195,7 @@ int _get_temp_filename(char **tempname)
return error;
}
else
copy_cstr(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
copy_cstr("enXXXXXX", tempname);
cstr_duplicate(tempname, "enXXXXXX");
error = mkstemp(*tempname);
#endif
return error;