Rename `path` to `string_utils`.

pull/9/head
Joshua Potter 2023-11-25 15:29:35 -07:00
parent eb598fe639
commit 6f77c400e9
10 changed files with 45 additions and 52 deletions

View File

@ -1,9 +1,10 @@
/** /**
@file @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 <stdlib.h> #include <stdlib.h>
@ -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 The concatenation of the path components via `/`. The caller takes ownership
of this value. 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 */

View File

@ -6,7 +6,7 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "path.h" #include "string_utils.h"
struct Error *config_new( struct Error *config_new(
const char *cwd, const char *cwd,
@ -26,8 +26,7 @@ struct Error *config_new(
} }
const char *segments[] = {root_dir, target}; const char *segments[] = {root_dir, target};
const char *filepath = const char *filepath = join(sizeof(segments) / sizeof(char *), segments, '/');
join_path_segments(sizeof(segments) / sizeof(char *), segments);
struct stat sb; struct stat sb;
int stat_res = stat(filepath, &sb); int stat_res = stat(filepath, &sb);

View File

@ -7,8 +7,8 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "path.h"
#include "string_buf.h" #include "string_buf.h"
#include "string_utils.h"
#include "validator.h" #include "validator.h"
static struct Error *find_run_exec(const struct Config *const config) { 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; struct stat sb;
const char *segments[] = {config->root_dir, config->target, "runner"}; const char *segments[] = {config->root_dir, config->target, "runner"};
char *filepath = char *filepath = join(sizeof(segments) / sizeof(char *), segments, '/');
join_path_segments(sizeof(segments) / sizeof(char *), segments);
int stat_res = stat(filepath, &sb); int stat_res = stat(filepath, &sb);
free(filepath); free(filepath);
@ -103,8 +102,7 @@ int evaluate_runner(
} }
const char *segments[] = {config->root_dir, config->target, "runner"}; const char *segments[] = {config->root_dir, config->target, "runner"};
const char *filepath = const char *filepath = join(sizeof(segments) / sizeof(char *), segments, '/');
join_path_segments(sizeof(segments) / sizeof(char *), segments);
const char *env = string_buf_convert(env_buf); const char *env = string_buf_convert(env_buf);
struct StringBuf *command_buf = string_buf_new(1024); struct StringBuf *command_buf = string_buf_new(1024);

View File

@ -5,14 +5,13 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include "path.h" #include "string_utils.h"
static struct Error *find_spec_json( static struct Error *find_spec_json(
const struct Config *const config, FILE **handle const struct Config *const config, FILE **handle
) { ) {
const char *segments[] = {config->root_dir, config->target, "spec.json"}; const char *segments[] = {config->root_dir, config->target, "spec.json"};
char *filepath = char *filepath = join(sizeof(segments) / sizeof(char *), segments, '/');
join_path_segments(sizeof(segments) / sizeof(char *), segments);
struct Error *error = 0; struct Error *error = 0;
// It is ok if the file does not exist. It is not ok if we couldn't open the // It is ok if the file does not exist. It is not ok if we couldn't open the

View File

@ -1,9 +1,9 @@
#include "path.h" #include "string_utils.h"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
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); assert(n > 0);
size_t length = 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); memcpy(joined + offset, segments[i], segment_len);
offset += segment_len; offset += segment_len;
if (i < n - 1) { if (i < n - 1) {
joined[offset++] = '/'; joined[offset++] = sep;
} }
} }

View File

@ -2,8 +2,8 @@
#include "test_config.h" #include "test_config.h"
#include "test_dyn_array.h" #include "test_dyn_array.h"
#include "test_parser.h" #include "test_parser.h"
#include "test_path.h"
#include "test_string_buf.h" #include "test_string_buf.h"
#include "test_string_utils.h"
#include "test_validator.h" #include "test_validator.h"
int main(int argc, char *argv[]) { 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_sappend);
sput_run_test(test_string_buf_cappend); sput_run_test(test_string_buf_cappend);
sput_enter_suite("path"); sput_enter_suite("string_utils");
sput_run_test(test_join_path_single_segments); sput_run_test(test_join_single);
sput_run_test(test_join_path_multiple_segments); sput_run_test(test_join_multiple);
sput_enter_suite("parser"); sput_enter_suite("parser");
sput_run_test(test_parser_missing); sput_run_test(test_parser_missing);

View File

@ -4,8 +4,8 @@
#include <unistd.h> #include <unistd.h>
#include "config.h" #include "config.h"
#include "path.h"
#include "sput.h" #include "sput.h"
#include "string_utils.h"
struct TestConfigFixture { struct TestConfigFixture {
char *cwd; char *cwd;
@ -16,8 +16,7 @@ struct TestConfigFixture {
static struct TestConfigFixture *test_config_setup() { static struct TestConfigFixture *test_config_setup() {
char *cwd = getcwd(0, 0); char *cwd = getcwd(0, 0);
const char *segments[] = {cwd, "test", "specs"}; const char *segments[] = {cwd, "test", "specs"};
char *root_dir = char *root_dir = join(sizeof(segments) / sizeof(char *), segments, '/');
join_path_segments(sizeof(segments) / sizeof(char *), segments);
struct TestConfigFixture *fixture = malloc(sizeof(struct TestConfigFixture)); struct TestConfigFixture *fixture = malloc(sizeof(struct TestConfigFixture));
fixture->cwd = getcwd(0, 0); fixture->cwd = getcwd(0, 0);

View File

@ -6,8 +6,8 @@
#include "cJSON.h" #include "cJSON.h"
#include "config.h" #include "config.h"
#include "parser.h" #include "parser.h"
#include "path.h"
#include "sput.h" #include "sput.h"
#include "string_utils.h"
struct TestParserFixture { struct TestParserFixture {
char *cwd; char *cwd;
@ -19,8 +19,7 @@ struct TestParserFixture {
static struct TestParserFixture *test_parser_setup(const char *target) { static struct TestParserFixture *test_parser_setup(const char *target) {
char *cwd = getcwd(0, 0); char *cwd = getcwd(0, 0);
const char *segments[] = {cwd, "test", "specs"}; const char *segments[] = {cwd, "test", "specs"};
char *root_dir = char *root_dir = join(sizeof(segments) / sizeof(char *), segments, '/');
join_path_segments(sizeof(segments) / sizeof(char *), segments);
struct TestParserFixture *fixture = malloc(sizeof(struct TestParserFixture)); struct TestParserFixture *fixture = malloc(sizeof(struct TestParserFixture));
fixture->cwd = getcwd(0, 0); fixture->cwd = getcwd(0, 0);

View File

@ -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 */

21
test/test_string_utils.h Normal file
View File

@ -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 */