lazysphere/lib/buffer.c
2023-05-14 21:43:02 -04:00

77 lines
1.6 KiB
C

#include "lslib.h"
#include <limits.h>
#include <string.h>
static int push_path_buffer_b(char* buf, int* index, const char* string) {
int save, string_len;
save = *index;
if (*index > 1 || (*index == 1 && buf[0] != '/')) {
buf[(*index)++] = '/';
}
string_len = strlen(string);
memcpy(buf + *index, string, string_len + 1);
*index += string_len;
return save;
}
static void pop_path_buffer_b(char* buf, int* index, int i) {
*index = i;
buf[*index] = '\0';
}
static char path_buffer[PATH_MAX + 1];
static int path_buffer_index = 0;
char* get_path_buffer(void) {
return path_buffer;
}
int push_path_buffer(const char* string) {
return push_path_buffer_b(path_buffer, &path_buffer_index, string);
}
void pop_path_buffer(int i) {
pop_path_buffer_b(path_buffer, &path_buffer_index, i);
}
static char path_buffer_2[PATH_MAX + 1];
static int path_buffer_index_2 = 0;
char* get_path_buffer_2(void) {
return path_buffer_2;
}
int push_path_buffer_2(const char* string) {
return push_path_buffer_b(path_buffer_2, &path_buffer_index_2, string);
}
void pop_path_buffer_2(int i) {
pop_path_buffer_b(path_buffer_2, &path_buffer_index_2, i);
}
bool is_dot_dir(const char* path) {
return streql(path, ".") || streql(path, "..");
}
const char* get_last_component(const char* path) {
const char* last;
char c;
last = NULL;
while (c = *path, true) {
if (c == '\\') {
last = path;
} else if (c == '\0') {
break;
}
path++;
}
return last;
}