Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 138 lines (111 sloc) 2.978 kb
bf7cf8b initial commit
Steve Dekorte authored
1 /*
2 copyright: Steve Dekorte, 2006. All rights reserved.
3 license: See _BSDLicense.txt.
4 */
5
6 #include "UArray.h"
7 #include <string.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stddef.h>
11
12 void UArray_appendPath_(UArray *self, const UArray *path)
13 {
75b2211 adding hash files
Steve Dekorte authored
14 const UArray sep = UArray_stackAllocedWithCString_(OS_PATH_SEPARATOR);
bf7cf8b initial commit
Steve Dekorte authored
15
16 int selfEndsWithSep = IS_PATH_SEPERATOR(UArray_lastLong(self));
17 int pathStartsWithSep = IS_PATH_SEPERATOR(UArray_firstLong(path));
18
19 if (!selfEndsWithSep && !pathStartsWithSep)
20 {
21 if(self->size != 0) UArray_append_(self, &sep);
22 UArray_append_(self, path);
23 }
24 else if (selfEndsWithSep && pathStartsWithSep)
25 {
26 const UArray pathPart = UArray_stackRange(path, 1, path->size - 1);
27 UArray_append_(self, &pathPart);
28 }
29 else
30 {
31 UArray_append_(self, path);
32 }
33 }
34
35 void UArray_removeLastPathComponent(UArray *self)
36 {
37 long pos = UArray_findLastPathComponent(self);
38 if (pos) pos --;
39 UArray_setSize_(self, pos);
40 }
41
42 void UArray_clipBeforeLastPathComponent(UArray *self)
43 {
44 long pos = UArray_findLastPathComponent(self);
45
46 if (pos != -1)
47 {
48 UArray_removeRange(self, 0, pos);
49 }
50 }
51
52 long UArray_findLastPathComponent(const UArray *self)
53 {
54 if (self->size)
55 {
56 UArray seps = UArray_stackAllocedWithCString_(IO_PATH_SEPARATORS);
57 UArray s = UArray_stackRange(self, 0, self->size);
58 long pos = 0;
59
60 while (s.size && (pos = UArray_rFindAnyValue_(&s, &seps)) == s.size - 1)
61 {
62 s.size = pos;
63 }
64
65 if (pos == -1) { pos = 0; } else { pos ++; }
66 return pos;
67 }
68
69 return 0;
70 }
71
72 UArray *UArray_lastPathComponent(const UArray *self)
73 {
74 long pos = UArray_findLastPathComponent(self);
75 return UArray_range(self, pos, self->size - pos);
76 }
77
78 long UArray_findPathExtension(UArray *self)
79 {
80 UArray dot = UArray_stackAllocedWithCString_(IO_PATH_SEPARATOR_DOT);
81 return UArray_rFind_(self, &dot);
82 }
83
84 void UArray_removePathExtension(UArray *self)
85 {
86 long pos = UArray_findPathExtension(self);
87
88 if (pos != -1)
89 {
90 UArray_setSize_(self, pos);
91 }
92 }
93
94 UArray *UArray_pathExtension(UArray *self)
95 {
96 long pos = UArray_findPathExtension(self);
97
98 if (pos == -1 || pos == self->size - 1)
99 {
100 return UArray_newWithCString_copy_("", 1);
101 }
102
103 return UArray_range(self, pos + 1, self->size - pos - 1);
104 }
105
106 UArray *UArray_fileName(UArray *self)
107 {
108 long extPos = UArray_findLastPathComponent(self);
109 long dotPos = UArray_findPathExtension(self);
110
111 //if (extPos == -1) { extPos = 0; } else { extPos ++; }
112 if (dotPos == -1) dotPos = self->size;
113
114 return UArray_range(self, extPos, dotPos - extPos);
115 }
116
117 // to/from os path - always returns a copy
118
119 int UArray_OSPathSeparatorIsUnixSeparator(void)
120 {
121 return strcmp(OS_PATH_SEPARATOR, "/") == 0;
122 }
123
124 UArray *UArray_asOSPath(UArray *self)
125 {
126 UArray *a = UArray_clone(self);
127 UArray_replaceCString_withCString_(a, IO_PATH_SEPARATOR, OS_PATH_SEPARATOR);
128 return a;
129 }
130
131 UArray *UArray_asUnixPath(UArray *self)
132 {
133 UArray *a = UArray_clone(self);
134 UArray_replaceCString_withCString_(a, OS_PATH_SEPARATOR, IO_PATH_SEPARATOR);
135 return a;
136 }
137
Something went wrong with that request. Please try again.