updated dirname/basepath to follow a modification of IEEE standard.
This commit is contained in:
parent
304049064e
commit
654f4bff80
6 changed files with 217 additions and 194 deletions
|
|
@ -1,33 +1,24 @@
|
|||
|
||||
/*
|
||||
* Copyright (C) 2010 Archived
|
||||
/* common/path.c - path string handling routines
|
||||
*
|
||||
* Copyright (C) 2010 Hernrik Hautakoski <henrik.hautakoski@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* The design goal here is to not use fixed size buffers because
|
||||
* path's can be extremely long (slightly overexaggerated but extreme cases are extreme).
|
||||
* so we use funky heap memory based algorithms instead :)
|
||||
*/
|
||||
|
||||
/*
|
||||
* the design goal here is to not use buffers because
|
||||
* path's can be extremely long (slightly overexaggerated but extreme cases are extreme).
|
||||
* so we use funky heap memory based algorithms instead :)
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "path.h"
|
||||
|
||||
static char defpath[2];
|
||||
|
||||
/*
|
||||
* allocates and initilizes a path
|
||||
*/
|
||||
|
|
@ -37,8 +28,10 @@ static char* alloc_path(size_t s) {
|
|||
|
||||
if (s < 1)
|
||||
return NULL;
|
||||
|
||||
if ((ptr = malloc(s+1)) == NULL)
|
||||
|
||||
ptr = malloc(s+1);
|
||||
|
||||
if (ptr == NULL)
|
||||
return NULL;
|
||||
|
||||
*ptr = '/';
|
||||
|
|
@ -69,7 +62,7 @@ static inline int has_delim(const char *str) {
|
|||
*/
|
||||
static char* cpy_path(char *buf, const char *path) {
|
||||
|
||||
if(!*path)
|
||||
if (*path == 0)
|
||||
return buf;
|
||||
|
||||
while(*path) {
|
||||
|
|
@ -90,7 +83,31 @@ static char* cpy_path(char *buf, const char *path) {
|
|||
return buf;
|
||||
}
|
||||
|
||||
int abspath(const char *path) {
|
||||
static char* split_path(char *path) {
|
||||
|
||||
char *last = path+strlen(path)-1, *slash = NULL;
|
||||
|
||||
while(*last == '/' && (last--) > path);
|
||||
|
||||
while(last > path) {
|
||||
|
||||
if (*last == '/') {
|
||||
slash = last;
|
||||
} else if (slash != NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
last--;
|
||||
}
|
||||
|
||||
if (slash == NULL)
|
||||
return path;
|
||||
|
||||
return slash;
|
||||
}
|
||||
|
||||
|
||||
int is_abspath(const char *path) {
|
||||
|
||||
if (*path != '/')
|
||||
return 0;
|
||||
|
|
@ -109,8 +126,8 @@ size_t pathlen(const char *path) {
|
|||
|
||||
if (*path == '/')
|
||||
path = path_clear(path);
|
||||
else if (*(++path) == 0)
|
||||
size++;
|
||||
else
|
||||
path++;
|
||||
}
|
||||
|
||||
return size;
|
||||
|
|
@ -121,17 +138,20 @@ char* fmt_path(const char *base, const char *name, unsigned char dir) {
|
|||
char *ptr, *ret;
|
||||
size_t size;
|
||||
|
||||
if (base == NULL || !abspath(base) || has_delim(name))
|
||||
if (base == NULL || !is_abspath(base) || has_delim(name))
|
||||
return NULL;
|
||||
|
||||
size = pathlen(base);
|
||||
|
||||
if (name != NULL) {
|
||||
size += strlen(name);
|
||||
if (dir) size++;
|
||||
if (dir)
|
||||
size++;
|
||||
}
|
||||
|
||||
if((ptr = alloc_path(size)) == NULL)
|
||||
|
||||
ptr = alloc_path(size);
|
||||
|
||||
if (ptr == NULL)
|
||||
return NULL;
|
||||
|
||||
ret = ptr;
|
||||
|
|
@ -142,51 +162,68 @@ char* fmt_path(const char *base, const char *name, unsigned char dir) {
|
|||
|
||||
if (name != NULL) {
|
||||
memcpy(ptr, name, strlen(name));
|
||||
if (dir) *(ptr+strlen(name)) = '/';
|
||||
if (dir)
|
||||
*(ptr+strlen(name)) = '/';
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
char* split_path(const char *path) {
|
||||
return NULL;
|
||||
char* basename(char *path) {
|
||||
|
||||
char *pos = path;
|
||||
|
||||
if (path == NULL || *path == '\0') {
|
||||
defpath[0] = '.';
|
||||
defpath[1] = '\0';
|
||||
return defpath;
|
||||
}
|
||||
|
||||
while(*path != '\0') {
|
||||
|
||||
if (*path == '/') {
|
||||
|
||||
if (*(path+1) == '\0') {
|
||||
|
||||
if (pos >= path)
|
||||
break;
|
||||
|
||||
*(path--) = '\0';
|
||||
continue;
|
||||
}
|
||||
|
||||
if (*(path+1) != '/')
|
||||
pos = path+1;
|
||||
}
|
||||
|
||||
path++;
|
||||
}
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
const char* basename(const char *path) {
|
||||
|
||||
const char *last = path+strlen(path);
|
||||
char* dirname(char *path) {
|
||||
|
||||
if (*last == '/')
|
||||
last--;
|
||||
|
||||
while(path != last) {
|
||||
if(*last == '/')
|
||||
break;
|
||||
last--;
|
||||
}
|
||||
|
||||
return last;
|
||||
}
|
||||
|
||||
|
||||
char* dirname(char *fullpath) {
|
||||
|
||||
char *dirname, *sep;
|
||||
int pos;
|
||||
|
||||
if (fullpath[strlen(fullpath)-1] == '/')
|
||||
fullpath[strlen(fullpath)-1] = '\0';
|
||||
|
||||
sep = strrchr(fullpath,'/');
|
||||
pos = sep-fullpath+1;
|
||||
|
||||
if(sep == NULL)
|
||||
return NULL;
|
||||
|
||||
dirname = malloc ( pos );
|
||||
|
||||
memset(dirname,0,pos);
|
||||
memcpy (dirname, fullpath, pos-1 );
|
||||
|
||||
return dirname;
|
||||
char *last, *slash = NULL;
|
||||
size_t len;
|
||||
|
||||
if (path == NULL || *path == '\0') {
|
||||
defpath[0] = '.';
|
||||
defpath[1] = '\0';
|
||||
return defpath;
|
||||
}
|
||||
|
||||
len = split_path(path) - path;
|
||||
|
||||
if (len <= 1) {
|
||||
|
||||
if (*path != '/')
|
||||
path[0] = '.';
|
||||
|
||||
path[1] = '\0';
|
||||
} else {
|
||||
path[len] = '\0';
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue