diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2024-02-09 15:59:37 +0100 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2024-02-09 15:59:37 +0100 |
commit | ae048f7ac4a9ab6576ca6842aa13e5c9c31e35a7 (patch) | |
tree | 79aa627a2689e76f23f856c85b9f86d9e651bfd2 /tests/libutil/fix_win32_filename.c | |
parent | 48b3355c7a887530a9bd17a1ad571e402102dd95 (diff) |
Add utility function to fixup Windows file paths
The idea is to iterate over a (canonicalized) path with forward
slashes by components, i.e. file and directory names. Each name
is then looked at by iterating over components, i.e. everything
between dots.
If a component is an illegal name, like COM1 or AUX, we add an
underscore. If it contains illegal characters, like : or \, we
re-map that character into unicode private use area.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tests/libutil/fix_win32_filename.c')
-rw-r--r-- | tests/libutil/fix_win32_filename.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/libutil/fix_win32_filename.c b/tests/libutil/fix_win32_filename.c new file mode 100644 index 0000000..a4f71e8 --- /dev/null +++ b/tests/libutil/fix_win32_filename.c @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * fix_win32_filename.c + * + * Copyright (C) 2024 David Oberhollenzer <goliath@infraroot.at> + */ +#include "config.h" + +#include "util/test.h" +#include "util/util.h" + +static const struct { + const char *path; + const char *result; +} test_data[] = { + { "foo", "foo" }, + { "foo/bar", "foo/bar" }, + { "foo/bar.txt", "foo/bar.txt" }, + { "COM1", "COM1_" }, + { "COM1.txt", "COM1_.txt" }, + { "foo.aux", "foo.aux_" }, + { "foo/bar/test.LPT1/bla", "foo/bar/test.LPT1_/bla" }, + { "C:\\/foo/COM1.bla/bar", + "C\xEF\x80\xBA\xEF\x81\x9c/foo/COM1_.bla/bar" }, +}; + +int main(int argc, char **argv) +{ + (void)argc; (void)argv; + + for (size_t i = 0; i < sizeof(test_data) / sizeof(test_data[0]); ++i) { + char *result = fix_win32_filename(test_data[i].path); + size_t out_len = strlen(test_data[i].result); + + if (result == NULL) { + fprintf(stderr, "OOM for test case %u (%s)?\n", + (unsigned int)i, test_data[i].path); + return EXIT_FAILURE; + } + + if (out_len != strlen(result) || + memcmp(result, test_data[i].result, out_len) != 0) { + fprintf(stderr, + "Mismatch for %s -> %s, got %s instead!\n", + test_data[i].path, test_data[i].result, + result); + free(result); + return EXIT_FAILURE; + } + + free(result); + } + + return EXIT_SUCCESS; +} |