]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /************************************************************************** |
2 | * | |
374ca955 | 3 | * Copyright (C) 2000-2004, International Business Machines |
b75a7d8f A |
4 | * Corporation and others. All Rights Reserved. |
5 | * | |
6 | *************************************************************************** | |
7 | * file name: pkgdata.c | |
8 | * encoding: ANSI X3.4 (1968) | |
9 | * tab size: 8 (not used) | |
10 | * indentation:4 | |
11 | * | |
12 | * created on: 2000may16 | |
13 | * created by: Steven \u24C7 Loomis | |
14 | * | |
15 | * common types for pkgdata | |
16 | */ | |
17 | ||
18 | #include <stdio.h> | |
19 | #include <stdlib.h> | |
20 | #include "unicode/utypes.h" | |
374ca955 | 21 | #include "unicode/putil.h" |
b75a7d8f A |
22 | #include "cmemory.h" |
23 | #include "cstring.h" | |
24 | #include "pkgtypes.h" | |
25 | ||
26 | ||
27 | const char *pkg_writeCharListWrap(FileStream *s, CharList *l, const char *delim, const char *brk, int32_t quote) | |
28 | { | |
29 | int32_t ln = 0; | |
30 | char buffer[1024]; | |
374ca955 | 31 | const CharList *ol = NULL; |
b75a7d8f A |
32 | while(l != NULL) |
33 | { | |
34 | if(l->str) | |
35 | { | |
36 | uprv_strncpy(buffer, l->str, 1020); | |
37 | buffer[1019]=0; | |
38 | ||
39 | if(quote < 0) { /* remove quotes */ | |
40 | if(buffer[uprv_strlen(buffer)-1] == '"') { | |
41 | buffer[uprv_strlen(buffer)-1] = '\0'; | |
42 | } | |
43 | if(buffer[0] == '"') { | |
44 | uprv_strcpy(buffer, buffer+1); | |
45 | } | |
46 | } else if(quote > 0) { /* add quotes */ | |
47 | if(l->str[0] != '"') { | |
48 | uprv_strcpy(buffer, "\""); | |
49 | uprv_strncat(buffer, l->str,1020); | |
50 | } | |
51 | if(l->str[uprv_strlen(l->str)-1] != '"') { | |
52 | uprv_strcat(buffer, "\""); | |
53 | } | |
54 | } | |
374ca955 | 55 | T_FileStream_write(s, buffer, (int32_t)uprv_strlen(buffer)); |
b75a7d8f A |
56 | } |
57 | ||
374ca955 | 58 | ln += (int32_t)uprv_strlen(l->str); |
b75a7d8f | 59 | |
374ca955 | 60 | ol = l; |
b75a7d8f A |
61 | |
62 | if(l->next && delim) | |
63 | { | |
64 | if(ln > 60 && brk) { | |
65 | ln = 0; | |
374ca955 | 66 | T_FileStream_write(s, brk, (int32_t)uprv_strlen(brk)); |
b75a7d8f | 67 | } |
374ca955 | 68 | T_FileStream_write(s, delim, (int32_t)uprv_strlen(delim)); |
b75a7d8f A |
69 | } |
70 | l = l->next; | |
71 | } | |
72 | return NULL; | |
73 | } | |
74 | ||
75 | ||
76 | const char *pkg_writeCharList(FileStream *s, CharList *l, const char *delim, int32_t quote) | |
77 | { | |
78 | char buffer[1024]; | |
79 | while(l != NULL) | |
80 | { | |
81 | if(l->str) | |
82 | { | |
83 | uprv_strncpy(buffer, l->str, 1023); | |
84 | buffer[1023]=0; | |
85 | if(uprv_strlen(l->str) >= 1023) | |
86 | { | |
87 | fprintf(stderr, "%s:%d: Internal error, line too long (greater than 1023 chars)\n", | |
88 | __FILE__, __LINE__); | |
89 | exit(0); | |
90 | } | |
91 | if(quote < 0) { /* remove quotes */ | |
92 | if(buffer[uprv_strlen(buffer)-1] == '"') { | |
93 | buffer[uprv_strlen(buffer)-1] = '\0'; | |
94 | } | |
95 | if(buffer[0] == '"') { | |
96 | uprv_strcpy(buffer, buffer+1); | |
97 | } | |
98 | } else if(quote > 0) { /* add quotes */ | |
99 | if(l->str[0] != '"') { | |
100 | uprv_strcpy(buffer, "\""); | |
101 | uprv_strcat(buffer, l->str); | |
102 | } | |
103 | if(l->str[uprv_strlen(l->str)-1] != '"') { | |
104 | uprv_strcat(buffer, "\""); | |
105 | } | |
106 | } | |
374ca955 | 107 | T_FileStream_write(s, buffer, (int32_t)uprv_strlen(buffer)); |
b75a7d8f A |
108 | } |
109 | ||
110 | if(l->next && delim) | |
111 | { | |
374ca955 | 112 | T_FileStream_write(s, delim, (int32_t)uprv_strlen(delim)); |
b75a7d8f A |
113 | } |
114 | l = l->next; | |
115 | } | |
116 | return NULL; | |
117 | } | |
118 | ||
119 | ||
120 | /* | |
121 | * Count items . 0 if null | |
122 | */ | |
123 | uint32_t pkg_countCharList(CharList *l) | |
124 | { | |
125 | uint32_t c = 0; | |
126 | while(l != NULL) | |
127 | { | |
128 | c++; | |
129 | l = l->next; | |
130 | } | |
131 | ||
132 | return c; | |
133 | } | |
134 | ||
135 | /* | |
136 | * Prepend string to CharList | |
137 | */ | |
138 | CharList *pkg_prependToList(CharList *l, const char *str) | |
139 | { | |
140 | CharList *newList; | |
141 | newList = uprv_malloc(sizeof(CharList)); | |
142 | ||
143 | /* test for NULL */ | |
144 | if(newList == NULL) { | |
145 | return NULL; | |
146 | } | |
147 | ||
148 | newList->str = str; | |
149 | newList->next = l; | |
150 | return newList; | |
151 | } | |
152 | ||
153 | /* | |
154 | * append string to CharList. *end or even end can be null if you don't | |
155 | * know it.[slow] | |
156 | * Str is adopted! | |
157 | */ | |
158 | CharList *pkg_appendToList(CharList *l, CharList** end, const char *str) | |
159 | { | |
160 | CharList *endptr = NULL, *tmp; | |
161 | ||
162 | if(end == NULL) | |
163 | { | |
164 | end = &endptr; | |
165 | } | |
166 | ||
167 | /* FIND the end */ | |
168 | if((*end == NULL) && (l != NULL)) | |
169 | { | |
170 | tmp = l; | |
171 | while(tmp->next) | |
172 | { | |
173 | tmp = tmp->next; | |
174 | } | |
175 | ||
176 | *end = tmp; | |
177 | } | |
178 | ||
179 | /* Create a new empty list and append it */ | |
180 | if(l == NULL) | |
181 | { | |
182 | l = pkg_prependToList(NULL, str); | |
183 | } | |
184 | else | |
185 | { | |
186 | (*end)->next = pkg_prependToList(NULL, str); | |
187 | } | |
188 | ||
189 | /* Move the end pointer. */ | |
190 | if(*end) | |
191 | { | |
192 | (*end) = (*end)->next; | |
193 | } | |
194 | else | |
195 | { | |
196 | *end = l; | |
197 | } | |
198 | ||
199 | return l; | |
200 | } | |
201 | ||
374ca955 A |
202 | CharList *pkg_appendUniqueDirToList(CharList *l, CharList** end, const char *strAlias) { |
203 | char aBuf[1024]; | |
204 | char *rPtr; | |
205 | rPtr = uprv_strrchr(strAlias, U_FILE_SEP_CHAR); | |
206 | #if defined(U_FILE_ALT_SEP_CHAR) && (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR) | |
207 | { | |
208 | char *aPtr = uprv_strrchr(strAlias, U_FILE_ALT_SEP_CHAR); | |
209 | if(!rPtr || /* regular char wasn't found or.. */ | |
210 | (aPtr && (aPtr > rPtr))) | |
211 | { /* alt ptr exists and is to the right of r ptr */ | |
212 | rPtr = aPtr; /* may copy NULL which is OK */ | |
213 | } | |
214 | } | |
215 | #endif | |
216 | if(!rPtr) { | |
217 | return l; /* no dir path */ | |
218 | } | |
219 | if((rPtr-strAlias) > (sizeof(aBuf)/sizeof(aBuf[0]))) { | |
220 | fprintf(stderr, "## ERR: Path too long [%d chars]: %s\n", sizeof(aBuf), strAlias); | |
221 | return l; | |
222 | } | |
223 | strncpy(aBuf, strAlias,(rPtr-strAlias)); | |
224 | aBuf[rPtr-strAlias]=0; /* no trailing slash */ | |
225 | ||
226 | if(!pkg_listContains(l, aBuf)) { | |
227 | return pkg_appendToList(l, end, uprv_strdup(aBuf)); | |
228 | } else { | |
229 | return l; /* already found */ | |
230 | } | |
231 | } | |
232 | ||
233 | #if 0 | |
234 | static CharList * | |
235 | pkg_appendFromStrings(CharList *l, CharList** end, const char *s, int32_t len) | |
236 | { | |
237 | CharList *endptr = NULL; | |
238 | const char *p; | |
239 | char *t; | |
240 | const char *targ; | |
241 | if(end == NULL) { | |
242 | end = &endptr; | |
243 | } | |
244 | ||
245 | if(len==-1) { | |
246 | len = uprv_strlen(s); | |
247 | } | |
248 | targ = s+len; | |
249 | ||
250 | while(*s && s<targ) { | |
251 | while(s<targ&&isspace(*s)) s++; | |
252 | for(p=s;s<targ&&!isspace(*p);p++); | |
253 | if(p!=s) { | |
254 | t = uprv_malloc(p-s+1); | |
255 | uprv_strncpy(t,s,p-s); | |
256 | t[p-s]=0; | |
257 | l=pkg_appendToList(l,end,t); | |
258 | fprintf(stderr, " P %s\n", t); | |
259 | } | |
260 | s=p; | |
261 | } | |
262 | ||
263 | return l; | |
264 | } | |
265 | #endif | |
266 | ||
267 | ||
b75a7d8f A |
268 | /* |
269 | * Delete list | |
270 | */ | |
271 | void pkg_deleteList(CharList *l) | |
272 | { | |
273 | ||
274 | while(l != NULL) | |
275 | { | |
276 | uprv_free((void*)l->str); | |
277 | l = l->next; | |
278 | } | |
279 | } | |
280 | ||
281 | UBool pkg_listContains(CharList *l, const char *str) | |
282 | { | |
283 | for(;l;l=l->next){ | |
284 | if(!uprv_strcmp(l->str, str)) { | |
285 | return TRUE; | |
286 | } | |
287 | } | |
288 | ||
289 | return FALSE; | |
290 | } |