Mercurial > mplayer.hg
annotate gui/util/string.c @ 36989:0b80003f6542
Relocate the cut functions.
Put them into the string functions file.
author | ib |
---|---|
date | Thu, 27 Mar 2014 09:15:47 +0000 |
parents | b33ff300128e |
children | fa8b6892b0bf |
rev | line source |
---|---|
33048 | 1 /* |
2 * This file is part of MPlayer. | |
3 * | |
4 * MPlayer is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; either version 2 of the License, or | |
7 * (at your option) any later version. | |
8 * | |
9 * MPlayer is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License along | |
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 */ | |
18 | |
35479 | 19 /** |
20 * @file | |
21 * @brief String utilities | |
22 */ | |
23 | |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
24 #include <stdlib.h> |
33737 | 25 #include <string.h> |
26 | |
33048 | 27 #include "string.h" |
35525 | 28 #include "gui/app/gui.h" |
34175 | 29 |
33982 | 30 /** |
31 * @brief Convert a string to lower case. | |
32 * | |
33 * @param string to be converted | |
34 * | |
35 * @return converted string | |
36 * | |
37 * @note Only characters from A to Z will be converted and this is an in-place conversion. | |
38 */ | |
33052 | 39 char *strlower(char *in) |
40 { | |
41 char *p = in; | |
42 | |
43 while (*p) { | |
44 if (*p >= 'A' && *p <= 'Z') | |
36986 | 45 *p += 0x20; |
33052 | 46 |
47 p++; | |
48 } | |
49 | |
50 return in; | |
51 } | |
52 | |
33982 | 53 /** |
36987 | 54 * @brief Convert a string to upper case. |
55 * | |
56 * @param string to be converted | |
57 * | |
58 * @return converted string | |
59 * | |
60 * @note Only characters from a to z will be converted and this is an in-place conversion. | |
61 */ | |
62 char *strupper(char *in) | |
63 { | |
64 char *p = in; | |
65 | |
66 while (*p) { | |
67 if (*p >= 'a' && *p <= 'z') | |
68 *p -= 0x20; | |
69 | |
70 p++; | |
71 } | |
72 | |
73 return in; | |
74 } | |
75 | |
76 /** | |
33982 | 77 * @brief Swap characters in a string. |
78 * | |
79 * @param in string to be processed | |
80 * @param from character to be swapped | |
81 * @param to character to swap in | |
82 * | |
83 * @return processed string | |
84 * | |
85 * @note All occurrences will be swapped and this is an in-place processing. | |
86 */ | |
33048 | 87 char *strswap(char *in, char from, char to) |
88 { | |
33049 | 89 char *p = in; |
33048 | 90 |
33049 | 91 while (*p) { |
92 if (*p == from) | |
93 *p = to; | |
33048 | 94 |
33049 | 95 p++; |
96 } | |
33048 | 97 |
98 return in; | |
99 } | |
100 | |
33982 | 101 /** |
102 * @brief Remove all space characters from a string, | |
103 * but leave text enclosed in quotation marks untouched. | |
104 * | |
105 * @param in string to be processed | |
106 * | |
107 * @return processed string | |
108 * | |
109 * @note This is an in-place processing. | |
110 */ | |
33048 | 111 char *trim(char *in) |
112 { | |
33051 | 113 char *src, *dest; |
35493 | 114 int freeze = False; |
33048 | 115 |
33051 | 116 src = dest = in; |
33048 | 117 |
33051 | 118 while (*src) { |
119 if (*src == '"') | |
120 freeze = !freeze; | |
33048 | 121 |
33051 | 122 if (freeze || (*src != ' ')) |
123 *dest++ = *src; | |
124 | |
125 src++; | |
33048 | 126 } |
127 | |
33051 | 128 *dest = 0; |
129 | |
33048 | 130 return in; |
131 } | |
33073 | 132 |
33982 | 133 /** |
134 * @brief Remove a comment from a string, | |
135 * but leave text enclosed in quotation marks untouched. | |
136 * | |
137 * A comment starts either with a semicolon anywhere in the string | |
138 * or with a number sign character at the very beginning. | |
139 * | |
140 * @param in string to be processed | |
141 * | |
142 * @return string without comment | |
143 * | |
144 * @note This is an in-place processing, i.e. @a in will be shortened. | |
145 */ | |
33073 | 146 char *decomment(char *in) |
147 { | |
148 char *p; | |
35493 | 149 int nap = False; |
33073 | 150 |
151 p = in; | |
152 | |
33080 | 153 if (*p == '#') |
154 *p = 0; | |
155 | |
33073 | 156 while (*p) { |
157 if (*p == '"') | |
158 nap = !nap; | |
159 | |
160 if ((*p == ';') && !nap) { | |
161 *p = 0; | |
162 break; | |
163 } | |
164 | |
165 p++; | |
166 } | |
167 | |
168 return in; | |
169 } | |
33737 | 170 |
35479 | 171 /** |
36989 | 172 * @brief Extract a part of a string delimited by a separator character. |
173 * | |
174 * @param in string to be analyzed | |
175 * @param out pointer suitable to store the extracted part | |
176 * @param sep separator character | |
177 * @param num number of separator characters to be skipped before extraction starts | |
178 * @param maxout maximum length of extracted part (including the trailing null byte) | |
179 */ | |
180 void cutItemString(char *in, char *out, char sep, int num, size_t maxout) | |
181 { | |
182 int n; | |
183 unsigned int i, c; | |
184 | |
185 for (c = 0, n = 0, i = 0; in[i]; i++) { | |
186 if (in[i] == sep) | |
187 n++; | |
188 if (n >= num && in[i] != sep && c + 1 < maxout) | |
189 out[c++] = in[i]; | |
190 if (n >= num && in[i + 1] == sep) | |
191 break; | |
192 } | |
193 | |
194 if (c < maxout) | |
195 out[c] = 0; | |
196 } | |
197 | |
198 /** | |
199 * @brief Extract a numeric part of a string delimited by a separator character. | |
200 * | |
201 * @param in string to be analyzed | |
202 * @param sep separator character | |
203 * @param num number of separator characters to be skipped before extraction starts | |
204 * | |
205 * @return extracted number (numeric part) | |
206 */ | |
207 int cutItemToInt(char *in, char sep, int num) | |
208 { | |
209 char tmp[64]; | |
210 | |
211 cutItem(in, tmp, sep, num); | |
212 | |
213 return atoi(tmp); | |
214 } | |
215 | |
216 /** | |
35479 | 217 * @brief A strchr() that can handle NULL pointers. |
218 * | |
219 * @param str string to examine | |
220 * @param c character to find | |
221 * | |
222 * @return return value of strchr() or NULL, if @a str is NULL | |
223 */ | |
33737 | 224 char *gstrchr(const char *str, int c) |
225 { | |
226 if (!str) | |
227 return NULL; | |
228 | |
229 return strchr(str, c); | |
230 } | |
231 | |
35459 | 232 /** |
233 * @brief A strcmp() that can handle NULL pointers. | |
234 * | |
235 * @param a string to be compared | |
236 * @param b string which is compared | |
237 * | |
35479 | 238 * @return return value of strcmp() or -1, if @a a or @a b are NULL |
35459 | 239 */ |
33737 | 240 int gstrcmp(const char *a, const char *b) |
241 { | |
242 if (!a && !b) | |
243 return 0; | |
244 if (!a || !b) | |
245 return -1; | |
246 | |
247 return strcmp(a, b); | |
248 } | |
249 | |
34628 | 250 /** |
251 * @brief A strncmp() that can handle NULL pointers. | |
252 * | |
253 * @param a string to be compared | |
254 * @param b string which is compared | |
255 * @param n number of characters compared at the most | |
256 * | |
35479 | 257 * @return return value of strncmp() or -1, if @a a or @a b are NULL |
34628 | 258 */ |
259 int gstrncmp(const char *a, const char *b, size_t n) | |
33737 | 260 { |
261 if (!a && !b) | |
262 return 0; | |
263 if (!a || !b) | |
264 return -1; | |
265 | |
266 return strncmp(a, b, n); | |
267 } | |
268 | |
33982 | 269 /** |
270 * @brief Duplicate a string. | |
271 * | |
272 * If @a str is NULL, it returns NULL. | |
273 * The string is duplicated by calling strdup(). | |
274 * | |
275 * @param str string to be duplicated | |
276 * | |
277 * @return duplicated string (newly allocated) | |
278 */ | |
33737 | 279 char *gstrdup(const char *str) |
280 { | |
281 if (!str) | |
282 return NULL; | |
283 | |
284 return strdup(str); | |
285 } | |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
286 |
33982 | 287 /** |
288 * @brief Assign a duplicated string. | |
289 * | |
290 * The string is duplicated by calling #gstrdup(). | |
291 * | |
292 * @param old pointer to a variable suitable to store the new pointer | |
293 * @param str string to be duplicated | |
35475
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
294 * |
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
295 * @note @a *old is freed prior to the assignment. |
33982 | 296 */ |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
297 void setdup(char **old, const char *str) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
298 { |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
299 free(*old); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
300 *old = gstrdup(str); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
301 } |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
302 |
33982 | 303 /** |
304 * @brief Assign a newly allocated string | |
305 * containing the path created from a directory and a filename. | |
306 * | |
307 * @param old pointer to a variable suitable to store the new pointer | |
308 * @param dir directory | |
309 * @param name filename | |
35475
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
310 * |
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
311 * @note @a *old is freed prior to the assignment. |
33982 | 312 */ |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
313 void setddup(char **old, const char *dir, const char *name) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
314 { |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
315 free(*old); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
316 *old = malloc(strlen(dir) + strlen(name) + 2); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
317 if (*old) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
318 sprintf(*old, "%s/%s", dir, name); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
319 } |
34175 | 320 |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
321 /** |
34560
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
322 * @brief Read characters from @a file. |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
323 * |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
324 * @param str pointer to a buffer to receive the read characters |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
325 * @param size number of characters read at the most (including a terminating null-character) |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
326 * @param file file to read from |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
327 * |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
328 * @return str (success) or NULL (error) |
35475
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
329 * |
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
330 * @note Reading stops with an end-of-line character or at end of file. |
34560
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
331 */ |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
332 char *fgetstr(char *str, int size, FILE *file) |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
333 { |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
334 char *s; |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
335 |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
336 s = fgets(str, size, file); |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
337 |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
338 if (s) |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
339 s[strcspn(s, "\n\r")] = 0; |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
340 |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
341 return s; |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
342 } |