contrib/OWB: add correct SDL dependency, fix compilers used
[AROS-Contrib.git] / freetype1 / contrib / ttf2pk / emtexdir.c
blob9a35570adb3e16fe2d74d5cf360f63faa5bef6ab
1 /* emtexdir.c -- written by Eberhard Mattes, donated to the public domain */
3 #if defined (__EMX__)
4 #include <emx/syscalls.h>
5 #else
6 #include "emdir.h"
7 #endif
8 #if defined(DJGPP) || defined(GO32)
9 #include <unistd.h>
10 #endif
11 #include <stdlib.h>
12 #include <string.h>
13 #include <fcntl.h>
14 #include <io.h>
15 #include "emtexdir.h"
17 #define FALSE 0
18 #define TRUE 1
20 void (*emtex_dir_find_callback)(const char *name, int ok) = NULL;
22 static int setup_add (struct emtex_dir *dst, const char *path)
24 char *p;
26 if (dst->used >= dst->alloc)
28 dst->alloc += 8;
29 dst->list = realloc (dst->list, dst->alloc * sizeof (*dst->list));
30 if (dst->list == NULL)
31 return (FALSE);
33 p = strdup (path);
34 if (p == NULL)
35 return (FALSE);
36 dst->list[dst->used++] = p;
37 return (TRUE);
41 static int setup_subdir (struct emtex_dir *dst, char *path, size_t add,
42 unsigned flags, int recurse)
44 int ok, i, end, len;
45 #if defined (__EMX__)
46 struct _find find;
47 #else
48 struct ll_findbuffer find;
49 #endif
51 i = dst->used;
52 strcpy (path + add, "*.*");
53 #if defined (__EMX__)
54 ok = __findfirst (path, 0x10, &find) == 0;
55 #else
56 ok = ll_findfirst (path, 0x10, &find);
57 #endif
58 while (ok)
60 if ((find.attr & 0x10)
61 && !(strcmp (find.name, ".") == 0 || strcmp (find.name, "..") == 0))
63 len = strlen (find.name);
64 memcpy (path + add, find.name, len);
65 path[add+len] = '\\';
66 path[add+len+1] = 0;
67 if (!setup_add (dst, path))
68 return (FALSE);
70 #if defined (__EMX__)
71 ok = __findnext (&find) == 0;
72 #else
73 ok = ll_findnext (&find);
74 #endif
76 if (recurse)
78 end = dst->used;
79 while (i < end)
81 strcpy (path, dst->list[i]);
82 if (!setup_subdir (dst, path, strlen (path), flags, TRUE))
83 return (FALSE);
84 ++i;
87 return (TRUE);
91 static int setup_dir (struct emtex_dir *dst, char *path,
92 const char *base_dir, size_t base_dir_len,
93 const char *sub_dir,
94 unsigned flags)
96 size_t i, len;
98 memcpy (path, base_dir, base_dir_len);
99 i = base_dir_len;
100 if ((flags & EDS_BANG) && sub_dir == NULL)
102 flags &= ~(EDS_ONESUBDIR|EDS_ALLSUBDIR);
103 if (i >= 2 && path[i-1] == '!' && path[i-2] == '!')
105 flags |= EDS_ALLSUBDIR;
106 i -= 2;
108 else if (i >= 1 && path[i-1] == '!')
110 flags |= EDS_ONESUBDIR;
111 --i;
114 if (sub_dir != NULL && *sub_dir != 0)
116 if (i != 0 && path[i-1] != ':' && path[i-1] != '/' && path[i-1] != '\\')
117 path[i++] = '\\';
118 len = strlen (sub_dir);
119 memcpy (path+i, sub_dir, len);
120 i += len;
122 if (path[i-1] != ':' && path[i-1] != '/' && path[i-1] != '\\')
123 path[i++] = '\\';
124 path[i] = 0;
125 if (!setup_add (dst, path))
126 return (FALSE);
127 if (flags & EDS_ALLSUBDIR)
128 return (setup_subdir (dst, path, i, flags, TRUE));
129 else if (flags & EDS_ONESUBDIR)
130 return (setup_subdir (dst, path, i, flags, FALSE));
131 else
132 return (TRUE);
136 /*static */int setup_list (struct emtex_dir *dst, char *path,
137 const char *list, unsigned flags)
139 const char *end;
140 size_t i;
142 for (;;)
144 while (*list == ' ' || *list == '\t')
145 ++list;
146 if (*list == 0)
147 return (TRUE);
148 end = list;
149 while (*end != 0 && *end != ';')
150 ++end;
151 i = end - list;
152 while (i > 0 && (list[i-1] == ' ' || list[i-1] == '\t'))
153 --i;
154 if (i != 0 && !setup_dir (dst, path, list, i, NULL, flags))
155 return (FALSE);
156 if (*end == 0)
157 return (TRUE);
158 list = end + 1;
163 int emtex_dir_setup (struct emtex_dir *ed, const char *env, const char *dir,
164 unsigned flags)
166 const char *val;
167 char path[260];
169 ed->alloc = 0;
170 ed->used = 0;
171 ed->list = NULL;
172 if (env != NULL && (val = getenv (env)) != NULL)
173 return (setup_list (ed, path, val, flags));
174 else if ((val = getenv ("EMTEXDIR")) != NULL)
175 return (setup_dir (ed, path, val, strlen (val), dir, flags));
176 else
177 return (setup_dir (ed, path, "\\emtex", 6, dir, flags));
181 static void pretty (char *path, unsigned flags)
183 char *p;
185 if (flags & EDF_FSLASH)
186 for (p = path; *p != 0; ++p)
187 if (*p == '\\')
188 *p = '/';
192 #define ADDCHAR(C) \
193 if (dst_size < 1) return (EDT_TOOLONG); \
194 *dst++ = (C); --dst_size
196 int emtex_dir_trunc (char *dst, size_t dst_size, const char *src,
197 unsigned flags, int method)
199 int len, truncated, dot;
201 if (src[0] != 0 && src[1] == ':')
203 ADDCHAR (src[0]);
204 ADDCHAR (src[1]);
205 src += 2;
208 truncated = FALSE; dot = FALSE; len = 0;
209 for (;;)
211 switch (*src)
213 case 0:
214 ADDCHAR (0);
215 return (truncated ? EDT_CHANGED : EDT_UNCHANGED);
217 case ':':
218 return (EDT_INVALID);
220 case '/':
221 case '\\':
222 ADDCHAR (*src);
223 len = 0; dot = FALSE;
224 break;
226 case '.':
227 if (dot)
228 return (EDT_INVALID);
229 ADDCHAR (*src);
231 /* ".." is allowed -- don't return EDT_INVALID for the next
232 dot. */
234 if (!(len == 0 && src[1] == '.'
235 && (src[2] == 0 || src[2] == '/' || src[2] == '\\')))
237 len = 0; dot = TRUE;
239 break;
241 default:
242 if (dot && len == 3)
243 truncated = TRUE;
244 else if (!dot && len == 8)
246 truncated = TRUE;
247 if (method == 0)
249 dst[-3] = dst[-2];
250 dst[-2] = dst[-1];
251 dst[-1] = *src;
254 else
256 ADDCHAR (*src);
257 ++len;
259 break;
261 ++src;
266 static int find2 (const char *name, unsigned flags)
268 int ok;
270 ok = (access (name, 4) == 0);
271 if (flags & EDF_TRACE)
272 emtex_dir_find_callback (name, ok);
273 return (ok);
277 static int find1 (char *path, size_t path_size, const char *dir,
278 const char *fname, unsigned flags)
280 char buf[260];
281 int method, rc;
282 size_t len, tmp;
284 len = 0;
285 if (dir != NULL)
287 tmp = strlen (dir);
288 if (tmp >= sizeof (buf))
289 return (FALSE);
290 memcpy (buf, dir, tmp);
291 len = tmp;
293 tmp = strlen (fname);
294 if (len + tmp >= sizeof (buf))
295 return (FALSE);
296 memcpy (buf + len, fname, tmp + 1);
297 len += tmp;
298 #if 0 /* wkim */
299 /* disabled for Win95's long file name support */
300 /* -- Wonkoo Kim (wkim+@pitt.edu), May 18, 1997 */
301 if (_osmode == DOS_MODE)
303 rc = emtex_dir_trunc (path, path_size, buf, flags, EDT_5_PLUS_3);
304 if ((rc == EDT_UNCHANGED || rc == EDT_CHANGED) && find2 (path, flags))
306 pretty (path, flags);
307 return (TRUE);
309 rc = emtex_dir_trunc (path, path_size, buf, flags, EDT_8);
310 if (rc == EDT_CHANGED && find2 (path, flags))
312 pretty (path, flags);
313 return (TRUE);
315 return (FALSE);
317 else
318 #endif /* wkim */
320 if (len < path_size && find2 (buf, flags))
322 memcpy (path, buf, len + 1);
323 pretty (path, flags);
324 return (TRUE);
326 for (method = 0; method < 2; ++method)
328 rc = emtex_dir_trunc (path, path_size, buf, flags, method);
329 if (rc == EDT_CHANGED && find2 (path, flags))
331 pretty (path, flags);
332 return (TRUE);
335 return (FALSE);
340 int emtex_dir_find (char *path, size_t path_size,
341 const struct emtex_dir *ed,
342 const char *fname, unsigned flags)
344 int i, absp;
345 const char *p;
347 absp = FALSE;
348 for (p = fname; *p != 0; ++p)
349 if (*p == ':' || *p == '/' || *p == '\\')
351 absp = TRUE;
352 break;
355 if (absp)
356 return (find1 (path, path_size, NULL, fname, flags));
358 if ((flags & EDF_CWD) && find1 (path, path_size, NULL, fname, flags))
359 return (TRUE);
361 for (i = 0; i < ed->used; ++i)
362 if (find1 (path, path_size, ed->list[i], fname, flags))
363 return (TRUE);
364 return (FALSE);
368 #if defined (TEST)
370 #include <stdio.h>
372 int main (int argc, char *argv[])
374 struct emtex_dir ed;
375 int i;
376 unsigned flags1, flags2;
377 char path[260];
379 if (argc != 6)
381 puts ("Usage: emtexdir <flags> <flags> <env> <dir> <fname>");
382 return (1);
385 flags1 = (unsigned)strtol (argv[1], NULL, 0);
386 flags2 = (unsigned)strtol (argv[2], NULL, 0);
388 if (!emtex_dir_setup (&ed, argv[3], argv[4], flags1))
390 fputs ("emtex_dir_setup failed\n", stderr);
391 return (2);
394 printf ("Directories:\n");
395 for (i = 0; i < ed.used; ++i)
396 printf (" %s\n", ed.list[i]);
398 if (!emtex_dir_find (path, sizeof (path), &ed, argv[5], flags2))
399 puts ("File not found");
400 else
401 printf ("Path: %s\n", path);
402 return (0);
405 #endif