Mercurial > hg > octave-max
comparison kpathsea/path-elt.c @ 2999:faa5d0421460
[project @ 1997-05-23 03:02:09 by jwe]
author | jwe |
---|---|
date | Fri, 23 May 1997 03:02:36 +0000 |
parents | |
children | 1f0b06020e36 |
comparison
equal
deleted
inserted
replaced
2998:692ba9d441ec | 2999:faa5d0421460 |
---|---|
1 /* path-elt.c: Return the stuff between colons. | |
2 | |
3 Copyright (C) 1993, 96 Karl Berry. | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free Software | |
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
18 | |
19 #include <kpathsea/config.h> | |
20 | |
21 #include <kpathsea/c-pathch.h> | |
22 #include <kpathsea/pathsearch.h> | |
23 | |
24 | |
25 /* The static (but dynamically allocated) area we return the answer in, | |
26 and how much we've currently allocated for it. */ | |
27 static string elt = NULL; | |
28 static unsigned elt_alloc = 0; | |
29 | |
30 /* The path we're currently working on. */ | |
31 static const_string path = NULL; | |
32 | |
33 | |
34 /* Upon entry, the static `path' is at the first (and perhaps last) | |
35 character of the return value, or else NULL if we're at the end (or | |
36 haven't been called). I make no provision for caching the results; | |
37 thus, we parse the same path over and over, on every lookup. If that | |
38 turns out to be a significant lose, it can be fixed, but I'm guessing | |
39 disk accesses overwhelm everything else. If ENV_P is true, use | |
40 IS_ENV_SEP; else use IS_DIR_SEP. */ | |
41 | |
42 static string | |
43 element P2C(const_string, passed_path, boolean, env_p) | |
44 { | |
45 const_string p; | |
46 string ret; | |
47 | |
48 if (passed_path) | |
49 path = passed_path; | |
50 | |
51 /* Check if called with NULL, and no previous path (perhaps we reached | |
52 the end). */ | |
53 else if (!path) | |
54 return NULL; | |
55 | |
56 /* OK, we have a non-null `path' if we get here. */ | |
57 assert (path); | |
58 p = path; | |
59 | |
60 /* Find the next colon (or the end of the path). */ | |
61 while (*p != 0 && (env_p ? !IS_ENV_SEP (*p) : !IS_DIR_SEP (*p))) | |
62 p++; | |
63 | |
64 /* If there were no separators, return the whole thing this time, and | |
65 return NULL next time. */ | |
66 if (*p == 0) | |
67 { | |
68 ret = (string) path; | |
69 path = NULL; | |
70 } | |
71 | |
72 /* Otherwise, return the substring starting at `path'. */ | |
73 else | |
74 { | |
75 unsigned len = p - path; | |
76 | |
77 /* Make sure we have enough space (including the null byte). */ | |
78 if (len + 1 > elt_alloc) | |
79 { | |
80 elt_alloc = len + 1; | |
81 elt = xrealloc (elt, elt_alloc); | |
82 } | |
83 | |
84 strncpy (elt, path, len); | |
85 elt[len] = 0; | |
86 ret = elt; | |
87 | |
88 path += len + 1; | |
89 } | |
90 | |
91 return ret; | |
92 } | |
93 | |
94 string | |
95 kpse_path_element P1C(const_string, p) | |
96 { | |
97 return element (p, true); | |
98 } | |
99 | |
100 string | |
101 kpse_filename_component P1C(const_string, p) | |
102 { | |
103 return element (p, false); | |
104 } | |
105 | |
106 #ifdef TEST | |
107 | |
108 void | |
109 print_path_elements (const_string path) | |
110 { | |
111 string elt; | |
112 printf ("Elements of `%s':", path ? path : "(null)"); | |
113 | |
114 for (elt = kpse_path_element (path); elt != NULL; | |
115 elt = kpse_path_element (NULL)) | |
116 { | |
117 printf (" %s", *elt ? elt : "`'"); | |
118 } | |
119 | |
120 puts ("."); | |
121 } | |
122 | |
123 int | |
124 main () | |
125 { | |
126 /* All lists end with NULL. */ | |
127 print_path_elements (NULL); /* */ | |
128 print_path_elements (""); /* "" */ | |
129 print_path_elements ("a"); /* a */ | |
130 print_path_elements (":"); /* "", "" */ | |
131 print_path_elements ("::"); /* "", "", "" */ | |
132 print_path_elements ("a:"); /* a, "" */ | |
133 print_path_elements (":b"); /* "", b */ | |
134 print_path_elements ("a:b"); /* a, b */ | |
135 | |
136 return 0; | |
137 } | |
138 | |
139 #endif /* TEST */ | |
140 | |
141 | |
142 /* | |
143 Local variables: | |
144 standalone-compile-command: "gcc -g -I. -I.. -DTEST path-elt.c kpathsea.a" | |
145 End: | |
146 */ |