Added path_push_subdir function to path

This commit is contained in:
Mateusz Faderewski 2023-08-17 22:26:18 +02:00
parent 1bdbfef8ac
commit 33a1729792
2 changed files with 22 additions and 11 deletions

View File

@ -88,16 +88,6 @@ bool path_is_root (path_t *path) {
return (strcmp(path->root, "/") == 0); return (strcmp(path->root, "/") == 0);
} }
void path_push (path_t *path, char *string) {
if (path->buffer[strlen(path->buffer) - 1] != '/') {
path_append(path, "/");
}
if (string[0] == '/') {
string += 1;
}
path_append(path, string);
}
void path_pop (path_t *path) { void path_pop (path_t *path) {
if (path_is_root(path)) { if (path_is_root(path)) {
return; return;
@ -110,6 +100,25 @@ void path_pop (path_t *path) {
} }
} }
void path_push (path_t *path, char *string) {
if (path->buffer[strlen(path->buffer) - 1] != '/') {
path_append(path, "/");
}
if (string[0] == '/') {
string += 1;
}
path_append(path, string);
}
void path_push_subdir (path_t *path, char *string) {
char *file = path_last_get(path);
char *tmp = alloca(strlen(file) + 1);
strcpy(tmp, file);
path_pop(path);
path_push(path, string);
path_push(path, tmp);
}
char *path_ext_get (path_t *path) { char *path_ext_get (path_t *path) {
char *buffer = path_last_get(path); char *buffer = path_last_get(path);
char *last_dot = strrchr(buffer, '.'); char *last_dot = strrchr(buffer, '.');

View File

@ -9,6 +9,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
/** @brief Path Structure */ /** @brief Path Structure */
@ -26,8 +27,9 @@ path_t *path_clone_push (path_t *path, char *string);
char *path_get (path_t *path); char *path_get (path_t *path);
char *path_last_get (path_t *path); char *path_last_get (path_t *path);
bool path_is_root (path_t *path); bool path_is_root (path_t *path);
void path_push (path_t *path, char *string);
void path_pop (path_t *path); void path_pop (path_t *path);
void path_push (path_t *path, char *string);
void path_push_subdir (path_t *path, char *string);
char *path_ext_get (path_t *path); char *path_ext_get (path_t *path);
void path_ext_remove (path_t *path); void path_ext_remove (path_t *path);
void path_ext_replace (path_t *path, char *ext); void path_ext_replace (path_t *path, char *ext);