common/path.c: normalize function (old fmt_path) using strbuf.
This commit is contained in:
parent
ccdad9c6aa
commit
25d789c77b
9 changed files with 30 additions and 43 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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__
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Reference in a new issue