diff options
author | Stef Walter <stef@thewalter.net> | 2013-07-03 10:38:19 +0200 |
---|---|---|
committer | Stef Walter <stef@thewalter.net> | 2013-07-03 11:49:26 +0200 |
commit | 81431ffd8cbf55175b1b9a9ed130fc67d0d4000b (patch) | |
tree | b6715f32115c537dd71a488048a9241a037024ad /common | |
parent | 1c4522e5df79bd197feab8448008fc2bf6b4ea2e (diff) |
path: Add p11_path_canon() function
Cleans up a filename with readable characters.
Diffstat (limited to 'common')
-rw-r--r-- | common/path.c | 15 | ||||
-rw-r--r-- | common/path.h | 2 | ||||
-rw-r--r-- | common/tests/test-path.c | 17 |
3 files changed, 34 insertions, 0 deletions
diff --git a/common/path.c b/common/path.c index 8362765..89d9a67 100644 --- a/common/path.c +++ b/common/path.c @@ -315,3 +315,18 @@ p11_path_prefix (const char *string, strncmp (string, prefix, b) == 0 && is_path_component_or_null (string[b]); } + +void +p11_path_canon (char *name) +{ + static const char *VALID = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_"; + int i; + + return_if_fail (name != NULL); + + for (i = 0; name[i] != '\0'; i++) { + if (strchr (VALID, name[i]) == NULL) + name[i] = '_'; + } +} diff --git a/common/path.h b/common/path.h index cd135cb..0b19a5d 100644 --- a/common/path.h +++ b/common/path.h @@ -64,4 +64,6 @@ char * p11_path_parent (const char *path); bool p11_path_prefix (const char *string, const char *prefix); +void p11_path_canon (char *name); + #endif /* P11_PATH_H__ */ diff --git a/common/tests/test-path.c b/common/tests/test-path.c index 1671381..1f85dbb 100644 --- a/common/tests/test-path.c +++ b/common/tests/test-path.c @@ -201,6 +201,22 @@ test_prefix (void) assert (p11_path_prefix ("/test//other//second", "/test")); } +static void +test_canon (void) +{ + char *test; + + test = strdup ("2309haonutb;AOE@#$O "); + p11_path_canon (test); + assert_str_eq (test, "2309haonutb_AOE___O_"); + free (test); + + test = strdup ("22@# %ATI@#$onot"); + p11_path_canon (test); + assert_str_eq (test, "22____ATI___onot"); + free (test); +} + int main (int argc, char *argv[]) @@ -211,6 +227,7 @@ main (int argc, p11_test (test_absolute, "/path/absolute"); p11_test (test_parent, "/path/parent"); p11_test (test_prefix, "/path/prefix"); + p11_test (test_canon, "/path/canon"); return p11_test_run (argc, argv); } |