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/mysql.c \
src/indexer.c \
src/common/strbuf.c \
src/common/path.c \
src/common/rbtree.c \
src/notify/inotify.c \

2
TODO
View file

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

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;
strbuf_t sb = STRBUF_INIT;
if (base == NULL || !is_abspath(base) || has_delim(name))
if (base == NULL || !is_abspath(base) || has_delim(name))
return NULL;
size = pathlen(base);
strbuf_append_str(&sb, base);
if (name != NULL) {
size += strlen(name);
if (dir)
size++;
}
if (sb.buf[sb.len] != '/')
strbuf_append_ch(&sb, '/');
if (*(base+size) != '/')
size++;
if (name) {
strbuf_append_str(&sb, name);
ptr = alloc_path(size);
if (dir)
strbuf_append_ch(&sb, '/');
}
if (ptr == NULL)
return NULL;
strbuf_squeeze(&sb, '/');
ret = ptr;
ptr = cpy_path(ptr, base);
if (name != NULL) {
memcpy(ptr, name, strlen(name));
if (dir)
*(ptr+strlen(name)) = '/';
}
return ret;
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);

View file

@ -12,7 +12,7 @@ strbuf :
$(CC) $(CFLAGS) ../src/common/strbuf.c t_strbuf.c -o test_strbuf
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 :
$(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 "../src/common/path.h"
void test_fmt_path() {
void test_normalize() {
char *ptr;
ptr = fmt_path("usr/", "include/", 0);
ptr = path_normalize("usr/", "include/", 0);
assert(ptr == NULL);
ptr = fmt_path("/usr/src/", "linux", 0);
ptr = path_normalize("/usr/src/", "linux", 0);
assert_string(ptr, "/usr/src/linux");
free(ptr);
ptr = fmt_path("/segment1/segment2/", "segment3", 1);
ptr = path_normalize("/segment1///segment2//", "segment3", 1);
assert_string(ptr, "/segment1/segment2/segment3/");
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/");
free(ptr);
ptr = fmt_path("/mnt/cdrom", "keff", 0);
ptr = path_normalize("/mnt/cdrom", "keff", 0);
assert_string(ptr, "/mnt/cdrom/keff");
free(ptr);
}
@ -98,7 +98,7 @@ void test_dirname() {
int main(int argc, char *argv[]) {
test_isabspath();
test_fmt_path();
test_normalize();
test_pathlen();
test_basename();
test_dirname();