Work in progress

Creating function for validateing element ID strings
This commit is contained in:
Michael Tryby
2019-04-16 14:44:06 -04:00
parent 8989be193a
commit ac56971ef2
5 changed files with 74 additions and 9 deletions

View File

@@ -17,7 +17,7 @@
#include "cstr_helper.h" #include "cstr_helper.h"
int copy_cstr(const char *source, char **dest) int cstr_copy(const char *source, char **dest)
// Determines length, allocates memory, and returns a null terminated copy // Determines length, allocates memory, and returns a null terminated copy
// Be Aware: caller is responsible for freeing memory // Be Aware: caller is responsible for freeing memory
{ {
@@ -39,7 +39,20 @@ int copy_cstr(const char *source, char **dest)
} }
bool isnullterm_cstr(const char *source) bool cstr_validate_id(const char *element_id)
// Determines if invalid characters are present in an element id string
{
const char *invalid_chars = " \";";
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')) if (strchr(source, '\0'))
return true; return true;

View File

@@ -18,8 +18,21 @@
#include <stdbool.h> #include <stdbool.h>
int copy_cstr(const char *source, char **destination); #if defined(__cplusplus)
bool isnullterm_cstr(const char *source); extern "C" {
#endif
int cstr_copy(const char *source, char **destination);
bool cstr_validate_id(const char *element_id);
bool cstr_isnullterm(const char *source);
#if defined(__cplusplus)
}
#endif
#endif /* CSTR_HELPER_H_ */ #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 // BE AWARE: The memory allocated here must be freed by the caller
// //
{ {
return copy_cstr(file_handle->filename, filename); return cstr_copy(file_handle->filename, 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
copy_cstr(filename, &(file_handle->filename)); cstr_copy(filename, &(file_handle->filename));
if (file_mode == NULL) if (file_mode == NULL)
error = -1; error = -1;
@@ -156,7 +156,7 @@ int remove_file(file_handle_t *file_handle) {
bool is_valid(file_handle_t *file_handle) bool is_valid(file_handle_t *file_handle)
{ {
if ((file_handle->filename == NULL && file_handle->file == NULL) || 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; return true;
else else
return false; return false;
@@ -195,7 +195,7 @@ int _get_temp_filename(char **tempname)
return error; return error;
} }
else else
copy_cstr(name, tempname); cstr_copy(name, tempname);
// --- 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
copy_cstr("enXXXXXX", tempname); cstr_copy("enXXXXXX", tempname);
error = mkstemp(*tempname); error = mkstemp(*tempname);
#endif #endif
return error; return error;

View File

@@ -7,6 +7,12 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
add_executable(test_cstrhelper ./test_cstrhelper.cpp
../../src/util/cstr_helper.c)
target_include_directories(test_cstrhelper PUBLIC ../../src/)
target_link_libraries(test_cstrhelper ${Boost_LIBRARIES})
add_executable(test_errormanager ./test_errormanager.cpp add_executable(test_errormanager ./test_errormanager.cpp
../../src/util/errormanager.c) ../../src/util/errormanager.c)
target_include_directories(test_errormanager PUBLIC ../../src/) target_include_directories(test_errormanager PUBLIC ../../src/)

View File

@@ -0,0 +1,33 @@
/*
******************************************************************************
Project: OWA EPANET
Version: 2.2
Module: test_cstrhelper.cpp
Description: tests for C string helper functions
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 04/16/2019
******************************************************************************
*/
#define BOOST_TEST_MODULE cstr_helper
#include <boost/test/unit_test.hpp>
#include "util/cstr_helper.h"
BOOST_AUTO_TEST_SUITE(test_cstrhelper)
BOOST_AUTO_TEST_CASE(test_validate_id){
BOOST_CHECK(cstr_validate_id("big tank") == false);
BOOST_CHECK(cstr_validate_id("big\"tank") == false);
BOOST_CHECK(cstr_validate_id("big;tank") == false);
BOOST_CHECK(cstr_validate_id("big-tank") == true);
}
BOOST_AUTO_TEST_SUITE_END()