Browse Source

Added libpath to fs module

master
parent
commit
1978fafaac
  1. 177
      src/fs/libpath.c
  2. 41
      src/fs/libpath.h
  3. 41
      testing/test.c

177
src/fs/libpath.c

@ -0,0 +1,177 @@
/*
The MIT License (MIT)
Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// File path separator. '/' by default and '\\' for Windows and DOS
const char PATH_SEPARATOR =
#if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(__DOS__) || defined(__SYMBIAN32__)
'\\';
#else
'/';
#endif
/*
Clears allocated path from duplicate separators.
Reallocates memory if needed.
*/
void path_clean(char* path) {
size_t path_len;
size_t i;
if (!path) {
return;
}
path_len = strlen(path);
if (path_len <= 1) {
return;
}
// remove trailing separator if present
if (path[path_len-1] == PATH_SEPARATOR) {
path[path_len-1] = '\0';
path_len--;
}
if (path_len <= 1) {
return;
}
// get rid of duplicate separators
i = 1;
while (i < path_len) {
if (path[i-1] == PATH_SEPARATOR && path[i] == PATH_SEPARATOR) {
// move to the left
for (size_t j = i; j < path_len-1; j++) {
path[j] = path[j + 1];
}
path[path_len-1] = '\0';
path_len--;
i--;
}
i++;
}
path = realloc(path, sizeof(char) * path_len);
}
/*
Clears allocated path from duplicate separators
and returns allocated clean path.
Be sure to path_free() after use.
*/
char* path_clean_copy(const char* path) {
char* clean_path = NULL;
size_t path_len;
if (!path) {
return NULL;
}
path_len = strlen(path);
clean_path = malloc(sizeof(char) * (path_len + 1));
clean_path = memcpy(clean_path, path, sizeof(char) * (path_len + 1));
path_clean(clean_path);
return clean_path;
}
/*
Joins path parts together and allocates the resulting path.
Takes counter for the amount of path parts; path parts must be of char* type.
Calls path_clean on the appended parts.
Be sure to path_free() after use.
*/
char* path_join(unsigned int count, ...) {
char* resulting_path = NULL;
const char* part = NULL;
unsigned long part_len = 0;
unsigned long current_len = 0;
va_list list;
unsigned int i = 0;
va_start(list, count);
while (i < count) {
part = va_arg(list, const char*);
part_len = strlen(part);
resulting_path = realloc(resulting_path, current_len + part_len + 1);
for (unsigned long i = current_len; i < current_len+part_len; i++) {
resulting_path[i] = part[i-current_len];
}
resulting_path[current_len+part_len] = PATH_SEPARATOR;
current_len += part_len + 1;
i++;
}
resulting_path[current_len] = '\0';
va_end(list);
path_clean(resulting_path);
return resulting_path;
}
// Frees path and sets pointer to NULL
void path_free(char** path) {
free(*path);
*path = NULL;
}
/*
Creates a new path pointing to the parent.
Be sure to path_free() on the base as well after use.
*/
char* path_parent(const char* path) {
if (!path) {
return NULL;
}
char* base_path = NULL;
char* clean_path = NULL;
size_t path_len;
size_t last_separator_index = 0;
clean_path = path_clean_copy(path);
if (!clean_path) {
return NULL;
}
path_len = strlen(clean_path);
if (path_len <= 1) {
return NULL;
}
// find the last separator index in clean path
for (size_t i = 0; i < path_len; i++) {
if (clean_path[i] == PATH_SEPARATOR) {
last_separator_index = i;
}
}
// allocate memory to hold up to last separator bytes
base_path = malloc(sizeof(char) * last_separator_index);
if (!base_path) {
return NULL;
}
// copy over
memcpy(base_path, clean_path, sizeof(char) * last_separator_index);
base_path[last_separator_index] = '\0';
// free temporary cleaned path
path_free(&clean_path);
return base_path;
}

41
src/fs/libpath.h

@ -0,0 +1,41 @@
/*
The MIT License (MIT)
Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
Clears allocated path from duplicate separators.
Reallocates memory if needed.
*/
void path_clean(char* path);
/*
Clears allocated path from duplicate separators
and returns allocated clean path.
Be sure to path_free() after use.
*/
char* path_clean_copy(const char* path);
/*
Joins path parts together and allocates the resulting path.
Takes counter for the amount of path parts; path parts must be of char* type.
Calls path_clean on the appended parts.
Be sure to path_free() after use.
*/
char* path_join(unsigned int count, ...);
// Frees path and sets pointer to NULL
void path_free(char** path);
/*
Creates a new path pointing to the parent.
Be sure to path_free() on the base as well after use.
*/
char* path_parent(const char* path);

41
testing/test.c

@ -30,6 +30,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include "../src/strings/levenshtein.h" #include "../src/strings/levenshtein.h"
#include "../src/strings/auxistr.h" #include "../src/strings/auxistr.h"
#include "../src/crypt/xorcipher.h" #include "../src/crypt/xorcipher.h"
#include "../src/fs/libpath.h"
int test_rng() { int test_rng() {
lcg(76); lcg(76);
@ -127,7 +128,7 @@ int test_endian() {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
int test_fs() { int test_fs_fs() {
if (file_size("test_img512x512.ppm") == UINT64_MAX) { if (file_size("test_img512x512.ppm") == UINT64_MAX) {
printf("[ERROR] Failed to determine file size of test ppm image\n"); printf("[ERROR] Failed to determine file size of test ppm image\n");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -152,6 +153,44 @@ int test_fs() {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
int test_libpath() {
char* path = path_join(2, ".////", "//test_nonexistent_file.txt");
if (strcmp(path, "./test_nonexistent_file.txt") != 0) {
printf("[ERROR] Expected path to be %s; got %s\n", "./test_nonexistent_file.txt", path);
path_free(&path);
return EXIT_FAILURE;
}
char* parent = path_parent(path);
if (strcmp(parent, "./") != 0 && strcmp(parent, ".") != 0) {
printf("[ERROR] Failed to find path's parent: got %s; expected %s\n", parent, ".");
path_free(&path);
return EXIT_FAILURE;
}
path_free(&path);
if (path != NULL) {
printf("[ERROR] Free'd path memory still has a pointer to it: %p\n", path);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int test_fs() {
if (test_fs_fs() == EXIT_FAILURE) {
printf("[ERROR] FS test failed\n");
return EXIT_FAILURE;
}
if (test_libpath() == EXIT_FAILURE) {
printf("[ERROR] Libpath test failed\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int test_bits() { int test_bits() {
// 11001000 // 11001000
uint8_t test_byte = 200; uint8_t test_byte = 200;

Loading…
Cancel
Save