1 /* path-elt.c: return the stuff between colons.
2
3 Copyright 1993, 1996 2008 Karl Berry.
4 Copyright 1997, 2001, 2005 Olaf Weber.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, see <http://www.gnu.org/licenses/>. */
18
19 #include <kpathsea/config.h>
20
21 #include <kpathsea/c-pathch.h>
22 #include <kpathsea/pathsearch.h>
23
24
25 /* Upon entry, the static `path' is at the first (and perhaps last)
26 character of the return value, or else NULL if we're at the end (or
27 haven't been called). I make no provision for caching the results;
28 thus, we parse the same path over and over, on every lookup. If that
29 turns out to be a significant lose, it can be fixed, but I'm guessing
30 disk accesses overwhelm everything else. If ENV_P is true, use
31 IS_ENV_SEP; else use IS_DIR_SEP. */
32
33 static string
element(kpathsea kpse,const_string passed_path,boolean env_p)34 element (kpathsea kpse, const_string passed_path, boolean env_p)
35 {
36 const_string p;
37 string ret;
38 int brace_level;
39 unsigned len;
40
41 if (passed_path)
42 kpse->path = passed_path;
43 /* Check if called with NULL, and no previous path (perhaps we reached
44 the end). */
45 else if (!kpse->path)
46 return NULL;
47
48 /* OK, we have a non-null `path' if we get here. */
49 assert (kpse->path);
50 p = kpse->path;
51
52 /* Find the next colon not enclosed by braces (or the end of the path). */
53 brace_level = 0;
54 while (*p != 0 && !(brace_level == 0
55 && (env_p ? IS_ENV_SEP (*p) : IS_DIR_SEP (*p)))) {
56 if (*p == '{') ++brace_level;
57 else if (*p == '}') --brace_level;
58 #if defined(WIN32)
59 else if (IS_KANJI(p))
60 p++;
61 #endif
62 p++;
63 }
64
65 /* Return the substring starting at `path'. */
66 len = p - kpse->path;
67
68 /* Make sure we have enough space (including the null byte). */
69 if (len + 1 > kpse->elt_alloc)
70 {
71 kpse->elt_alloc = len + 1;
72 kpse->elt = (string)xrealloc (kpse->elt, kpse->elt_alloc);
73 }
74
75 strncpy (kpse->elt, kpse->path, len);
76 kpse->elt[len] = 0;
77 ret = kpse->elt;
78
79 /* If we are at the end, return NULL next time. */
80 if (kpse->path[len] == 0)
81 kpse->path = NULL;
82 else
83 kpse->path += len + 1;
84
85 return ret;
86 }
87
88 string
kpathsea_path_element(kpathsea kpse,const_string p)89 kpathsea_path_element (kpathsea kpse, const_string p)
90 {
91 return element (kpse, p, true);
92 }
93
94 string
kpathsea_filename_component(kpathsea kpse,const_string p)95 kpathsea_filename_component (kpathsea kpse, const_string p)
96 {
97 return element (kpse, p, false);
98 }
99
100 #ifdef TEST
101
102 void
print_path_elements(const_string path)103 print_path_elements (const_string path)
104 {
105 string elt;
106 printf ("Elements of `%s':", path ? path : "(null)");
107
108 for (elt = kpathsea_path_element (kpse_def, path); elt != NULL;
109 elt = kpathsea_path_element (kpse_def, NULL))
110 {
111 printf (" %s", *elt ? elt : "`'");
112 }
113
114 puts (".");
115 }
116
117 int
main()118 main ()
119 {
120 /* All lists end with NULL. */
121 print_path_elements (NULL); /* */
122 print_path_elements (""); /* "" */
123 print_path_elements ("a"); /* a */
124 print_path_elements (ENV_SEP_STRING); /* "", "" */
125 print_path_elements (ENV_SEP_STRING ENV_SEP_STRING); /* "", "", "" */
126 print_path_elements ("a" ENV_SEP_STRING); /* a, "" */
127 print_path_elements (ENV_SEP_STRING "b"); /* "", b */
128 print_path_elements ("a" ENV_SEP_STRING "b"); /* a, b */
129
130 return 0;
131 }
132
133 #endif /* TEST */
134
135
136 /*
137 Local variables:
138 standalone-compile-command: "gcc -g -I. -I.. -DTEST path-elt.c kpathsea.a"
139 End:
140 */
141