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

@ -21,6 +21,7 @@ SOURCES := \
src/arch.c \ src/arch.c \
src/arch/mysql.c \ src/arch/mysql.c \
src/indexer.c \ src/indexer.c \
src/common/strbuf.c \
src/common/path.c \ src/common/path.c \
src/common/rbtree.c \ src/common/rbtree.c \
src/notify/inotify.c \ src/notify/inotify.c \

2
TODO
View file

@ -4,7 +4,5 @@
################## ##################
* Use queue in inotify_read(), tree and remove indexer * Use queue in inotify_read(), tree and remove indexer
* path:
* normalize
* ini-like config support * ini-like config support
* fix mysql module * fix mysql module

View file

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

View file

@ -18,6 +18,6 @@ int is_abspath(const char *path);
size_t pathlen(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 */ #endif /* __COMMON_PATH_H */

View file

@ -87,7 +87,7 @@ static int rm() {
void indexer_register(const char *base, const char *name) { 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 (!indexer_pending()) {
#if __DEBUG__ #if __DEBUG__

View file

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

View file

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

View file

@ -12,7 +12,7 @@ strbuf :
$(CC) $(CFLAGS) ../src/common/strbuf.c t_strbuf.c -o test_strbuf $(CC) $(CFLAGS) ../src/common/strbuf.c t_strbuf.c -o test_strbuf
path : path :
$(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/path.c t_path.c -o test_path $(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/strbuf.c ../src/common/path.c t_path.c -o test_path
rbtree : rbtree :
$(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/rbtree.c t_rbtree.c -o test_rbtree $(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/rbtree.c t_rbtree.c -o test_rbtree

View file

@ -4,26 +4,26 @@
#include "unit.h" #include "unit.h"
#include "../src/common/path.h" #include "../src/common/path.h"
void test_fmt_path() { void test_normalize() {
char *ptr; char *ptr;
ptr = fmt_path("usr/", "include/", 0); ptr = path_normalize("usr/", "include/", 0);
assert(ptr == NULL); assert(ptr == NULL);
ptr = fmt_path("/usr/src/", "linux", 0); ptr = path_normalize("/usr/src/", "linux", 0);
assert_string(ptr, "/usr/src/linux"); assert_string(ptr, "/usr/src/linux");
free(ptr); free(ptr);
ptr = fmt_path("/segment1/segment2/", "segment3", 1); ptr = path_normalize("/segment1///segment2//", "segment3", 1);
assert_string(ptr, "/segment1/segment2/segment3/"); assert_string(ptr, "/segment1/segment2/segment3/");
free(ptr); free(ptr);
ptr = fmt_path("/stuff/with/ahell/lot/of/slashes/at/the/", "end", 1); ptr = path_normalize("/stuff/with/ahell/lot/of/slashes/at/the///", "end", 1);
assert_string(ptr, "/stuff/with/ahell/lot/of/slashes/at/the/end/"); assert_string(ptr, "/stuff/with/ahell/lot/of/slashes/at/the/end/");
free(ptr); free(ptr);
ptr = fmt_path("/mnt/cdrom", "keff", 0); ptr = path_normalize("/mnt/cdrom", "keff", 0);
assert_string(ptr, "/mnt/cdrom/keff"); assert_string(ptr, "/mnt/cdrom/keff");
free(ptr); free(ptr);
} }
@ -98,7 +98,7 @@ void test_dirname() {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
test_isabspath(); test_isabspath();
test_fmt_path(); test_normalize();
test_pathlen(); test_pathlen();
test_basename(); test_basename();
test_dirname(); test_dirname();