diff --git a/include/path.h b/include/string_utils.h similarity index 64% rename from include/path.h rename to include/string_utils.h index e4417fd..2447ed0 100644 --- a/include/path.h +++ b/include/string_utils.h @@ -1,9 +1,10 @@ /** @file -@brief Path-related utility functions. +@brief String-related utility functions. */ -#ifndef _BOOTSTRAP_PATH_H -#define _BOOTSTRAP_PATH_H + +#ifndef _BOOTSTRAP_STRING_UTILS_H +#define _BOOTSTRAP_STRING_UTILS_H #include @@ -20,6 +21,6 @@ This function assumes a forward slash path separator (i.e. `/`). The concatenation of the path components via `/`. The caller takes ownership of this value. */ -char *join_path_segments(size_t n, const char *segments[static n]); +char *join(size_t n, const char *segments[static n], char sep); -#endif /* _BOOTSTRAP_PATH_H */ +#endif /* _BOOTSTRAP_STRING_UTILS_H */ diff --git a/src/config.c b/src/config.c index 4a55a97..aceecd1 100644 --- a/src/config.c +++ b/src/config.c @@ -6,7 +6,7 @@ #include #include -#include "path.h" +#include "string_utils.h" struct Error *config_new( const char *cwd, @@ -26,8 +26,7 @@ struct Error *config_new( } const char *segments[] = {root_dir, target}; - const char *filepath = - join_path_segments(sizeof(segments) / sizeof(char *), segments); + const char *filepath = join(sizeof(segments) / sizeof(char *), segments, '/'); struct stat sb; int stat_res = stat(filepath, &sb); diff --git a/src/evaluator.c b/src/evaluator.c index 228a99c..efed46c 100644 --- a/src/evaluator.c +++ b/src/evaluator.c @@ -7,8 +7,8 @@ #include #include -#include "path.h" #include "string_buf.h" +#include "string_utils.h" #include "validator.h" static struct Error *find_run_exec(const struct Config *const config) { @@ -16,8 +16,7 @@ static struct Error *find_run_exec(const struct Config *const config) { struct stat sb; const char *segments[] = {config->root_dir, config->target, "runner"}; - char *filepath = - join_path_segments(sizeof(segments) / sizeof(char *), segments); + char *filepath = join(sizeof(segments) / sizeof(char *), segments, '/'); int stat_res = stat(filepath, &sb); free(filepath); @@ -103,8 +102,7 @@ int evaluate_runner( } const char *segments[] = {config->root_dir, config->target, "runner"}; - const char *filepath = - join_path_segments(sizeof(segments) / sizeof(char *), segments); + const char *filepath = join(sizeof(segments) / sizeof(char *), segments, '/'); const char *env = string_buf_convert(env_buf); struct StringBuf *command_buf = string_buf_new(1024); diff --git a/src/parser.c b/src/parser.c index 8443005..0b32409 100644 --- a/src/parser.c +++ b/src/parser.c @@ -5,14 +5,13 @@ #include #include -#include "path.h" +#include "string_utils.h" static struct Error *find_spec_json( const struct Config *const config, FILE **handle ) { const char *segments[] = {config->root_dir, config->target, "spec.json"}; - char *filepath = - join_path_segments(sizeof(segments) / sizeof(char *), segments); + char *filepath = join(sizeof(segments) / sizeof(char *), segments, '/'); struct Error *error = 0; // It is ok if the file does not exist. It is not ok if we couldn't open the diff --git a/src/path.c b/src/string_utils.c similarity index 79% rename from src/path.c rename to src/string_utils.c index e4c6cf7..897132c 100644 --- a/src/path.c +++ b/src/string_utils.c @@ -1,9 +1,9 @@ -#include "path.h" +#include "string_utils.h" #include #include -char *join_path_segments(size_t n, const char *segments[static n]) { +char *join(size_t n, const char *segments[static n], char sep) { assert(n > 0); size_t length = 0; @@ -20,7 +20,7 @@ char *join_path_segments(size_t n, const char *segments[static n]) { memcpy(joined + offset, segments[i], segment_len); offset += segment_len; if (i < n - 1) { - joined[offset++] = '/'; + joined[offset++] = sep; } } diff --git a/test/test.c b/test/test.c index df9d68d..a6dddd8 100644 --- a/test/test.c +++ b/test/test.c @@ -2,8 +2,8 @@ #include "test_config.h" #include "test_dyn_array.h" #include "test_parser.h" -#include "test_path.h" #include "test_string_buf.h" +#include "test_string_utils.h" #include "test_validator.h" int main(int argc, char *argv[]) { @@ -23,9 +23,9 @@ int main(int argc, char *argv[]) { sput_run_test(test_string_buf_sappend); sput_run_test(test_string_buf_cappend); - sput_enter_suite("path"); - sput_run_test(test_join_path_single_segments); - sput_run_test(test_join_path_multiple_segments); + sput_enter_suite("string_utils"); + sput_run_test(test_join_single); + sput_run_test(test_join_multiple); sput_enter_suite("parser"); sput_run_test(test_parser_missing); diff --git a/test/test_config.h b/test/test_config.h index 3769de4..5fae6f0 100644 --- a/test/test_config.h +++ b/test/test_config.h @@ -4,8 +4,8 @@ #include #include "config.h" -#include "path.h" #include "sput.h" +#include "string_utils.h" struct TestConfigFixture { char *cwd; @@ -16,8 +16,7 @@ struct TestConfigFixture { static struct TestConfigFixture *test_config_setup() { char *cwd = getcwd(0, 0); const char *segments[] = {cwd, "test", "specs"}; - char *root_dir = - join_path_segments(sizeof(segments) / sizeof(char *), segments); + char *root_dir = join(sizeof(segments) / sizeof(char *), segments, '/'); struct TestConfigFixture *fixture = malloc(sizeof(struct TestConfigFixture)); fixture->cwd = getcwd(0, 0); diff --git a/test/test_parser.h b/test/test_parser.h index ad1033e..7abccbb 100644 --- a/test/test_parser.h +++ b/test/test_parser.h @@ -6,8 +6,8 @@ #include "cJSON.h" #include "config.h" #include "parser.h" -#include "path.h" #include "sput.h" +#include "string_utils.h" struct TestParserFixture { char *cwd; @@ -19,8 +19,7 @@ struct TestParserFixture { static struct TestParserFixture *test_parser_setup(const char *target) { char *cwd = getcwd(0, 0); const char *segments[] = {cwd, "test", "specs"}; - char *root_dir = - join_path_segments(sizeof(segments) / sizeof(char *), segments); + char *root_dir = join(sizeof(segments) / sizeof(char *), segments, '/'); struct TestParserFixture *fixture = malloc(sizeof(struct TestParserFixture)); fixture->cwd = getcwd(0, 0); diff --git a/test/test_path.h b/test/test_path.h deleted file mode 100644 index eb9911a..0000000 --- a/test/test_path.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef _BOOTSTRAP_TEST_PATH -#define _BOOTSTRAP_TEST_PATH - -#include "path.h" -#include "sput.h" - -static void test_join_path_single_segments() { - const char *segments[] = {"abc"}; - char *joined = - join_path_segments(sizeof(segments) / sizeof(char *), segments); - sput_fail_unless(strcmp(joined, "abc") == 0, "abc"); - free(joined); -} - -static void test_join_path_multiple_segments() { - const char *segments[] = {"abc", "def", "ghi"}; - char *joined = - join_path_segments(sizeof(segments) / sizeof(char *), segments); - sput_fail_unless(strcmp(joined, "abc/def/ghi") == 0, "abc/def/ghi"); - free(joined); -} - -#endif /* _BOOTSTRAP_TEST_PATH */ diff --git a/test/test_string_utils.h b/test/test_string_utils.h new file mode 100644 index 0000000..694fa49 --- /dev/null +++ b/test/test_string_utils.h @@ -0,0 +1,21 @@ +#ifndef _BOOTSTRAP_TEST_STRING_UTILS +#define _BOOTSTRAP_TEST_STRING_UTILS + +#include "sput.h" +#include "string_utils.h" + +static void test_join_single() { + const char *segments[] = {"abc"}; + char *joined = join(sizeof(segments) / sizeof(char *), segments, '/'); + sput_fail_unless(strcmp(joined, "abc") == 0, "abc"); + free(joined); +} + +static void test_join_multiple() { + const char *segments[] = {"abc", "def", "ghi"}; + char *joined = join(sizeof(segments) / sizeof(char *), segments, '/'); + sput_fail_unless(strcmp(joined, "abc/def/ghi") == 0, "abc/def/ghi"); + free(joined); +} + +#endif /* _BOOTSTRAP_TEST_STRING_UTILS */