Updating built in Io code to use += instead of x = x + y
[io/quag.git] / libs / basekit / source / UArray_path.c
blob9ea31415a627946aa230053c5fd001f6b244a4b0
1 /*
2 copyright: Steve Dekorte, 2006. All rights reserved.
3 license: See _BSDLicense.txt.
4 */
6 #include "UArray.h"
7 #include <string.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stddef.h>
12 void UArray_appendPath_(UArray *self, const UArray *path)
14 const UArray sep = UArray_stackAllocedWithCString_(IO_PATH_SEPARATOR);
16 int selfEndsWithSep = IS_PATH_SEPERATOR(UArray_lastLong(self));
17 int pathStartsWithSep = IS_PATH_SEPERATOR(UArray_firstLong(path));
19 if (!selfEndsWithSep && !pathStartsWithSep)
21 if(self->size != 0) UArray_append_(self, &sep);
22 UArray_append_(self, path);
24 else if (selfEndsWithSep && pathStartsWithSep)
26 const UArray pathPart = UArray_stackRange(path, 1, path->size - 1);
27 UArray_append_(self, &pathPart);
29 else
31 UArray_append_(self, path);
35 void UArray_removeLastPathComponent(UArray *self)
37 UArray seps = UArray_stackAllocedWithCString_(IO_PATH_SEPARATORS);
38 long pos = UArray_findLastPathComponent(self);
39 if (pos) pos --;
40 UArray_setSize_(self, pos);
43 void UArray_clipBeforeLastPathComponent(UArray *self)
45 long pos = UArray_findLastPathComponent(self);
47 if (pos != -1)
49 UArray_removeRange(self, 0, pos);
53 long UArray_findLastPathComponent(const UArray *self)
55 if (self->size)
57 UArray seps = UArray_stackAllocedWithCString_(IO_PATH_SEPARATORS);
58 UArray s = UArray_stackRange(self, 0, self->size);
59 long pos = 0;
61 while (s.size && (pos = UArray_rFindAnyValue_(&s, &seps)) == s.size - 1)
63 s.size = pos;
66 if (pos == -1) { pos = 0; } else { pos ++; }
67 return pos;
70 return 0;
73 UArray *UArray_lastPathComponent(const UArray *self)
75 long pos = UArray_findLastPathComponent(self);
76 return UArray_range(self, pos, self->size - pos);
79 long UArray_findPathExtension(UArray *self)
81 UArray dot = UArray_stackAllocedWithCString_(IO_PATH_SEPARATOR_DOT);
82 return UArray_rFind_(self, &dot);
85 void UArray_removePathExtension(UArray *self)
87 long pos = UArray_findPathExtension(self);
89 if (pos != -1)
91 UArray_setSize_(self, pos);
95 UArray *UArray_pathExtension(UArray *self)
97 long pos = UArray_findPathExtension(self);
99 if (pos == -1 || pos == self->size - 1)
101 return UArray_newWithCString_copy_("", 1);
104 return UArray_range(self, pos + 1, self->size - pos - 1);
107 UArray *UArray_fileName(UArray *self)
109 long extPos = UArray_findLastPathComponent(self);
110 long dotPos = UArray_findPathExtension(self);
112 //if (extPos == -1) { extPos = 0; } else { extPos ++; }
113 if (dotPos == -1) dotPos = self->size;
115 return UArray_range(self, extPos, dotPos - extPos);
118 // to/from os path - always returns a copy
120 int UArray_OSPathSeparatorIsUnixSeparator(void)
122 return strcmp(OS_PATH_SEPARATOR, "/") == 0;
125 UArray *UArray_asOSPath(UArray *self)
127 UArray *a = UArray_clone(self);
128 UArray_replaceCString_withCString_(a, IO_PATH_SEPARATOR, OS_PATH_SEPARATOR);
129 return a;
132 UArray *UArray_asUnixPath(UArray *self)
134 UArray *a = UArray_clone(self);
135 UArray_replaceCString_withCString_(a, OS_PATH_SEPARATOR, IO_PATH_SEPARATOR);
136 return a;