1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include "pathutils.h"
24
25 #include "nedit_malloc.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29
30
31 char* ConcatPath(
const char *parent,
const char *name)
32 {
33 size_t parentlen = strlen(parent);
34 size_t namelen = strlen(name);
35
36 size_t pathlen = parentlen + namelen +
2;
37 char *path = NEditMalloc(pathlen);
38
39 memcpy(path, parent, parentlen);
40 if(parentlen >
0 && parent[parentlen-
1] !=
'/') {
41 path[parentlen] =
'/';
42 parentlen++;
43 }
44 if(name[
0] ==
'/') {
45 name++;
46 namelen--;
47 }
48 memcpy(path+parentlen, name, namelen);
49 path[parentlen+namelen] =
'\0';
50 return path;
51 }
52
53 char* FileName(
char *path) {
54 int si =
0;
55 int osi =
0;
56 int i =
0;
57 int p =
0;
58 char c;
59 while((c = path[i]) !=
0) {
60 if(c ==
'/') {
61 osi = si;
62 si = i;
63 p =
1;
64 }
65 i++;
66 }
67
68 char *name = path + si + p;
69 if(name[
0] ==
0) {
70 name = path + osi + p;
71 if(name[
0] ==
0) {
72 return path;
73 }
74 }
75
76 return name;
77 }
78
79 char* ParentPath(
char *path) {
80 char *name = FileName(path);
81 size_t namelen = strlen(name);
82 size_t pathlen = strlen(path);
83 size_t parentlen = pathlen - namelen;
84 if(parentlen ==
0) {
85 parentlen++;
86 }
87 char *parent = NEditMalloc(parentlen +
1);
88 memcpy(parent, path, parentlen);
89 parent[parentlen] =
'\0';
90 return parent;
91 }
92