Skip to content

Commit

Permalink
Adds aws_normalize_directory_separator (#1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 authored Mar 30, 2023
1 parent 8060c22 commit b8c51f2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/aws/common/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ bool aws_is_any_directory_separator(char value);
AWS_COMMON_API
char aws_get_platform_directory_separator(void);

/**
* Normalizes the path by replacing any directory separator with the local platform's directory separator.
* @param path path to normalize. Must be writeable.
*/
AWS_COMMON_API
void aws_normalize_directory_separator(struct aws_byte_buf *path);

/**
* Returns the current user's home directory.
*/
Expand Down
13 changes: 13 additions & 0 deletions source/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ bool aws_is_any_directory_separator(char value) {
return value == '\\' || value == '/';
}

void aws_normalize_directory_separator(struct aws_byte_buf *path) {
AWS_PRECONDITION(aws_byte_buf_is_valid(path));

const char local_platform_separator = aws_get_platform_directory_separator();
for (size_t i = 0; i < path->len; ++i) {
if (aws_is_any_directory_separator((char)path->buffer[i])) {
path->buffer[i] = local_platform_separator;
}
}

AWS_POSTCONDITION(aws_byte_buf_is_valid(path));
}

struct aws_directory_iterator {
struct aws_linked_list list_data;
struct aws_allocator *allocator;
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ add_test_case(directory_non_empty_deletion_recursively_succeeds_test)
add_test_case(directory_move_succeeds_test)
add_test_case(directory_move_src_non_existent_test)
add_test_case(test_home_directory_not_null)
add_test_case(test_normalize_posix_directory_separator)
add_test_case(test_normalize_windows_directory_separator)

add_test_case(promise_test_wait_forever)
add_test_case(promise_test_wait_for_a_bit)
Expand Down
35 changes: 35 additions & 0 deletions tests/file_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,38 @@ static int s_test_home_directory_not_null(struct aws_allocator *allocator, void
}

AWS_TEST_CASE(test_home_directory_not_null, s_test_home_directory_not_null);

static int s_test_normalize_posix_directory_separator(struct aws_allocator *allocator, void *ctx) {
(void)ctx;

struct aws_string *buffer = aws_string_new_from_c_str(allocator, "./test/path/abc");
struct aws_byte_buf path_buf = aws_byte_buf_from_array(buffer->bytes, buffer->len);
aws_normalize_directory_separator(&path_buf);
for (size_t i = 0; i < path_buf.len; ++i) {
if (aws_is_any_directory_separator((char)path_buf.buffer[i])) {
ASSERT_INT_EQUALS(aws_get_platform_directory_separator(), path_buf.buffer[i]);
}
}

aws_string_destroy(buffer);
return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(test_normalize_posix_directory_separator, s_test_normalize_posix_directory_separator);

static int s_test_normalize_windows_directory_separator(struct aws_allocator *allocator, void *ctx) {
(void)ctx;

struct aws_string *buffer = aws_string_new_from_c_str(allocator, ".\\test\\path\\abc");
struct aws_byte_buf path_buf = aws_byte_buf_from_array(buffer->bytes, buffer->len);
aws_normalize_directory_separator(&path_buf);
for (size_t i = 0; i < path_buf.len; ++i) {
if (aws_is_any_directory_separator((char)path_buf.buffer[i])) {
ASSERT_INT_EQUALS(aws_get_platform_directory_separator(), path_buf.buffer[i]);
}
}
aws_string_destroy(buffer);
return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(test_normalize_windows_directory_separator, s_test_normalize_windows_directory_separator);

0 comments on commit b8c51f2

Please sign in to comment.