Archived
1
0
Fork 0

common/path.c: normalize function (old fmt_path) using strbuf.

This commit is contained in:
H Hautakoski 2010-09-14 12:58:39 +02:00 committed by Henrik Hautakoski
parent ccdad9c6aa
commit 25d789c77b
9 changed files with 30 additions and 43 deletions

View file

@ -15,6 +15,7 @@
#include <string.h>
#include "debug.h"
#include "strbuf.h"
#include "path.h"
/*
@ -117,39 +118,26 @@ size_t pathlen(const char *path) {
return size;
}
char* fmt_path(const char *base, const char *name, unsigned char dir) {
char* path_normalize(const char *base, const char *name, unsigned char dir) {
char *ptr, *ret;
size_t size;
if (base == NULL || !is_abspath(base) || has_delim(name))
strbuf_t sb = STRBUF_INIT;
if (base == NULL || !is_abspath(base) || has_delim(name))
return NULL;
size = pathlen(base);
if (name != NULL) {
size += strlen(name);
if (dir)
size++;
}
if (*(base+size) != '/')
size++;
strbuf_append_str(&sb, base);
ptr = alloc_path(size);
if (sb.buf[sb.len] != '/')
strbuf_append_ch(&sb, '/');
if (name) {
strbuf_append_str(&sb, name);
if (ptr == NULL)
return NULL;
ret = ptr;
ptr = cpy_path(ptr, base);
if (dir)
strbuf_append_ch(&sb, '/');
}
if (name != NULL) {
memcpy(ptr, name, strlen(name));
if (dir)
*(ptr+strlen(name)) = '/';
}
return ret;
strbuf_squeeze(&sb, '/');
return strbuf_release(&sb);
}

View file

@ -18,6 +18,6 @@ int is_abspath(const char *path);
size_t pathlen(const char *path);
char* fmt_path(const char *base, const char *name, unsigned char dir);
char* path_normalize(const char *base, const char *name, unsigned char dir);
#endif /* __COMMON_PATH_H */

View file

@ -87,7 +87,7 @@ static int rm() {
void indexer_register(const char *base, const char *name) {
char *fullpath = fmt_path(base, name, 1);
char *fullpath = path_normalize(base, name, 1);
if (!indexer_pending()) {
#if __DEBUG__

View file

@ -53,7 +53,7 @@ static int addwatch(const char *path, const char *name) {
char *cpath;
int wd;
cpath = fmt_path(path, name, 1);
cpath = path_normalize(path, name, 1);
if (cpath == NULL)
return -1;

View file

@ -43,7 +43,7 @@ struct tree* tree_new(const char *path) {
if (t == NULL)
return NULL;
t->path = fmt_path(path, NULL, 0);
t->path = path_normalize(path, NULL, 0);
if (t->path == NULL) {
free(t);