Work in progress
Refactoring cstr_copy and adding test
This commit is contained in:
@@ -17,13 +17,10 @@
|
|||||||
#include "cstr_helper.h"
|
#include "cstr_helper.h"
|
||||||
|
|
||||||
|
|
||||||
int cstr_copy(const char *source, char **dest)
|
int cstr_duplicate(char **dest, const char *source)
|
||||||
// Determines length, allocates memory, and returns a null terminated copy
|
// Duplicates source string
|
||||||
// Be Aware: caller is responsible for freeing memory
|
|
||||||
{
|
{
|
||||||
size_t size;
|
size_t size = 1 + strlen(source);
|
||||||
|
|
||||||
size = 1 + strlen(source);
|
|
||||||
*dest = (char *) calloc(size, sizeof(char));
|
*dest = (char *) calloc(size, sizeof(char));
|
||||||
|
|
||||||
if (*dest == NULL)
|
if (*dest == NULL)
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ extern "C" {
|
|||||||
#endif
|
#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);
|
bool cstr_validate_id(const char *element_id);
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
// 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)
|
if (filename == NULL)
|
||||||
_get_temp_filename(&(file_handle->filename));
|
_get_temp_filename(&(file_handle->filename));
|
||||||
else
|
else
|
||||||
cstr_copy(filename, &(file_handle->filename));
|
cstr_duplicate(&(file_handle->filename), filename);
|
||||||
|
|
||||||
if (file_mode == NULL)
|
if (file_mode == NULL)
|
||||||
error = -1;
|
error = -1;
|
||||||
@@ -195,7 +195,7 @@ int _get_temp_filename(char **tempname)
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
cstr_copy(name, tempname);
|
cstr_duplicate(tempname, name);
|
||||||
|
|
||||||
// --- free the pointer returned by _tempnam
|
// --- free the pointer returned by _tempnam
|
||||||
if (name)
|
if (name)
|
||||||
@@ -204,7 +204,7 @@ int _get_temp_filename(char **tempname)
|
|||||||
// --- for non-Windows systems:
|
// --- for non-Windows systems:
|
||||||
#else
|
#else
|
||||||
// --- use system function mkstemp() to create a temporary file name
|
// --- use system function mkstemp() to create a temporary file name
|
||||||
cstr_copy("enXXXXXX", tempname);
|
cstr_duplicate(tempname, "enXXXXXX");
|
||||||
error = mkstemp(*tempname);
|
error = mkstemp(*tempname);
|
||||||
#endif
|
#endif
|
||||||
return error;
|
return error;
|
||||||
|
|||||||
@@ -17,9 +17,31 @@
|
|||||||
#include "util/cstr_helper.h"
|
#include "util/cstr_helper.h"
|
||||||
|
|
||||||
|
|
||||||
|
boost::test_tools::predicate_result check_string(std::string test, std::string ref)
|
||||||
|
{
|
||||||
|
if (ref.compare(test) == 0)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(test_cstrhelper)
|
BOOST_AUTO_TEST_SUITE(test_cstrhelper)
|
||||||
|
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_duplicate) {
|
||||||
|
char source[] = "I will be rewarded for good behavior.";
|
||||||
|
char *dest = NULL;
|
||||||
|
|
||||||
|
cstr_duplicate(&dest, source);
|
||||||
|
BOOST_CHECK(check_string(dest, source));
|
||||||
|
BOOST_CHECK(cstr_isnullterm(dest) == true);
|
||||||
|
|
||||||
|
free(dest);
|
||||||
|
free(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(test_validate_id) {
|
BOOST_AUTO_TEST_CASE(test_validate_id) {
|
||||||
|
|
||||||
BOOST_CHECK(cstr_validate_id("big tank") == false);
|
BOOST_CHECK(cstr_validate_id("big tank") == false);
|
||||||
|
|||||||
Reference in New Issue
Block a user