Mercurial > mplayer.hg
annotate gui/util/string.c @ 35561:8889d320734f
Cosmetic: Adjust indent.
author | ib |
---|---|
date | Mon, 10 Dec 2012 13:54:56 +0000 |
parents | ded4a8c2aa70 |
children | c667e34fb941 |
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 #include "gui/interface.h" |
30 | |
31 #include "config.h" | |
32 #include "help_mp.h" | |
33 #include "libavutil/avstring.h" | |
34 #include "stream/stream.h" | |
33048 | 35 |
33982 | 36 /** |
37 * @brief Convert a string to lower case. | |
38 * | |
39 * @param string to be converted | |
40 * | |
41 * @return converted string | |
42 * | |
43 * @note Only characters from A to Z will be converted and this is an in-place conversion. | |
44 */ | |
33052 | 45 char *strlower(char *in) |
46 { | |
47 char *p = in; | |
48 | |
49 while (*p) { | |
50 if (*p >= 'A' && *p <= 'Z') | |
51 *p += 'a' - 'A'; | |
52 | |
53 p++; | |
54 } | |
55 | |
56 return in; | |
57 } | |
58 | |
33982 | 59 /** |
60 * @brief Swap characters in a string. | |
61 * | |
62 * @param in string to be processed | |
63 * @param from character to be swapped | |
64 * @param to character to swap in | |
65 * | |
66 * @return processed string | |
67 * | |
68 * @note All occurrences will be swapped and this is an in-place processing. | |
69 */ | |
33048 | 70 char *strswap(char *in, char from, char to) |
71 { | |
33049 | 72 char *p = in; |
33048 | 73 |
33049 | 74 while (*p) { |
75 if (*p == from) | |
76 *p = to; | |
33048 | 77 |
33049 | 78 p++; |
79 } | |
33048 | 80 |
81 return in; | |
82 } | |
83 | |
33982 | 84 /** |
85 * @brief Remove all space characters from a string, | |
86 * but leave text enclosed in quotation marks untouched. | |
87 * | |
88 * @param in string to be processed | |
89 * | |
90 * @return processed string | |
91 * | |
92 * @note This is an in-place processing. | |
93 */ | |
33048 | 94 char *trim(char *in) |
95 { | |
33051 | 96 char *src, *dest; |
35493 | 97 int freeze = False; |
33048 | 98 |
33051 | 99 src = dest = in; |
33048 | 100 |
33051 | 101 while (*src) { |
102 if (*src == '"') | |
103 freeze = !freeze; | |
33048 | 104 |
33051 | 105 if (freeze || (*src != ' ')) |
106 *dest++ = *src; | |
107 | |
108 src++; | |
33048 | 109 } |
110 | |
33051 | 111 *dest = 0; |
112 | |
33048 | 113 return in; |
114 } | |
33073 | 115 |
33982 | 116 /** |
117 * @brief Remove a comment from a string, | |
118 * but leave text enclosed in quotation marks untouched. | |
119 * | |
120 * A comment starts either with a semicolon anywhere in the string | |
121 * or with a number sign character at the very beginning. | |
122 * | |
123 * @param in string to be processed | |
124 * | |
125 * @return string without comment | |
126 * | |
127 * @note This is an in-place processing, i.e. @a in will be shortened. | |
128 */ | |
33073 | 129 char *decomment(char *in) |
130 { | |
131 char *p; | |
35493 | 132 int nap = False; |
33073 | 133 |
134 p = in; | |
135 | |
33080 | 136 if (*p == '#') |
137 *p = 0; | |
138 | |
33073 | 139 while (*p) { |
140 if (*p == '"') | |
141 nap = !nap; | |
142 | |
143 if ((*p == ';') && !nap) { | |
144 *p = 0; | |
145 break; | |
146 } | |
147 | |
148 p++; | |
149 } | |
150 | |
151 return in; | |
152 } | |
33737 | 153 |
35479 | 154 /** |
155 * @brief A strchr() that can handle NULL pointers. | |
156 * | |
157 * @param str string to examine | |
158 * @param c character to find | |
159 * | |
160 * @return return value of strchr() or NULL, if @a str is NULL | |
161 */ | |
33737 | 162 char *gstrchr(const char *str, int c) |
163 { | |
164 if (!str) | |
165 return NULL; | |
166 | |
167 return strchr(str, c); | |
168 } | |
169 | |
35459 | 170 /** |
171 * @brief A strcmp() that can handle NULL pointers. | |
172 * | |
173 * @param a string to be compared | |
174 * @param b string which is compared | |
175 * | |
35479 | 176 * @return return value of strcmp() or -1, if @a a or @a b are NULL |
35459 | 177 */ |
33737 | 178 int gstrcmp(const char *a, const char *b) |
179 { | |
180 if (!a && !b) | |
181 return 0; | |
182 if (!a || !b) | |
183 return -1; | |
184 | |
185 return strcmp(a, b); | |
186 } | |
187 | |
34628 | 188 /** |
189 * @brief A strncmp() that can handle NULL pointers. | |
190 * | |
191 * @param a string to be compared | |
192 * @param b string which is compared | |
193 * @param n number of characters compared at the most | |
194 * | |
35479 | 195 * @return return value of strncmp() or -1, if @a a or @a b are NULL |
34628 | 196 */ |
197 int gstrncmp(const char *a, const char *b, size_t n) | |
33737 | 198 { |
199 if (!a && !b) | |
200 return 0; | |
201 if (!a || !b) | |
202 return -1; | |
203 | |
204 return strncmp(a, b, n); | |
205 } | |
206 | |
33982 | 207 /** |
208 * @brief Duplicate a string. | |
209 * | |
210 * If @a str is NULL, it returns NULL. | |
211 * The string is duplicated by calling strdup(). | |
212 * | |
213 * @param str string to be duplicated | |
214 * | |
215 * @return duplicated string (newly allocated) | |
216 */ | |
33737 | 217 char *gstrdup(const char *str) |
218 { | |
219 if (!str) | |
220 return NULL; | |
221 | |
222 return strdup(str); | |
223 } | |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
224 |
33982 | 225 /** |
226 * @brief Assign a duplicated string. | |
227 * | |
228 * The string is duplicated by calling #gstrdup(). | |
229 * | |
230 * @param old pointer to a variable suitable to store the new pointer | |
231 * @param str string to be duplicated | |
35475
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
232 * |
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
233 * @note @a *old is freed prior to the assignment. |
33982 | 234 */ |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
235 void setdup(char **old, const char *str) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
236 { |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
237 free(*old); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
238 *old = gstrdup(str); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
239 } |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
240 |
33982 | 241 /** |
242 * @brief Assign a newly allocated string | |
243 * containing the path created from a directory and a filename. | |
244 * | |
245 * @param old pointer to a variable suitable to store the new pointer | |
246 * @param dir directory | |
247 * @param name filename | |
35475
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
248 * |
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
249 * @note @a *old is freed prior to the assignment. |
33982 | 250 */ |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
251 void setddup(char **old, const char *dir, const char *name) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
252 { |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
253 free(*old); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
254 *old = malloc(strlen(dir) + strlen(name) + 2); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
255 if (*old) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
256 sprintf(*old, "%s/%s", dir, name); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
257 } |
34175 | 258 |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
259 /** |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
260 * @brief Convert #guiInfo member Filename. |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
261 * |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
262 * @param how 0 (cut file path and extension), |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
263 * 1 (additionally, convert lower case) or |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
264 * 2 (additionally, convert upper case) |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
265 * @param fname pointer to a buffer to receive the converted Filename |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
266 * @param maxlen size of @a fname buffer |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
267 * |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
268 * @return pointer to @a fname buffer |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
269 */ |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
270 char *TranslateFilename(int how, char *fname, size_t maxlen) |
34175 | 271 { |
272 char *p; | |
273 size_t len; | |
274 | |
275 switch (guiInfo.StreamType) { | |
276 case STREAMTYPE_FILE: | |
35541 | 277 |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
278 if (guiInfo.Filename && *guiInfo.Filename) { |
34175 | 279 p = strrchr(guiInfo.Filename, |
280 #if HAVE_DOS_PATHS | |
281 '\\'); | |
282 #else | |
283 '/'); | |
284 #endif | |
285 | |
286 if (p) | |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
287 av_strlcpy(fname, p + 1, maxlen); |
34175 | 288 else |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
289 av_strlcpy(fname, guiInfo.Filename, maxlen); |
34175 | 290 |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
291 len = strlen(fname); |
34175 | 292 |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
293 if (len > 3 && fname[len - 3] == '.') |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
294 fname[len - 3] = 0; |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
295 else if (len > 4 && fname[len - 4] == '.') |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
296 fname[len - 4] = 0; |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
297 else if (len > 5 && fname[len - 5] == '.') |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
298 fname[len - 5] = 0; |
34175 | 299 } else |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
300 av_strlcpy(fname, MSGTR_NoFileLoaded, maxlen); |
35541 | 301 |
34175 | 302 break; |
303 | |
304 case STREAMTYPE_STREAM: | |
35541 | 305 |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
306 av_strlcpy(fname, guiInfo.Filename, maxlen); |
34175 | 307 break; |
308 | |
34387 | 309 case STREAMTYPE_CDDA: |
35541 | 310 |
34387 | 311 snprintf(fname, maxlen, MSGTR_Title, guiInfo.Track); |
312 break; | |
313 | |
34175 | 314 case STREAMTYPE_VCD: |
35541 | 315 |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
316 snprintf(fname, maxlen, MSGTR_Title, guiInfo.Track - 1); |
34175 | 317 break; |
318 | |
319 case STREAMTYPE_DVD: | |
35541 | 320 |
34175 | 321 if (guiInfo.Chapter) |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
322 snprintf(fname, maxlen, MSGTR_Chapter, guiInfo.Chapter); |
34175 | 323 else |
35482 | 324 av_strlcpy(fname, MSGTR_NoChapter, maxlen); |
35541 | 325 |
34175 | 326 break; |
327 | |
328 default: | |
35541 | 329 |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
330 av_strlcpy(fname, MSGTR_NoMediaOpened, maxlen); |
34175 | 331 break; |
332 } | |
333 | |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
334 if (how) { |
34177 | 335 p = fname; |
336 | |
337 while (*p) { | |
338 char t = 0; | |
34175 | 339 |
34177 | 340 if (how == 1 && *p >= 'A' && *p <= 'Z') |
341 t = 32; | |
342 if (how == 2 && *p >= 'a' && *p <= 'z') | |
343 t = -32; | |
34175 | 344 |
34177 | 345 *p = *p + t; |
346 p++; | |
34175 | 347 } |
348 } | |
349 | |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
350 return fname; |
34175 | 351 } |
34560
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
352 |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
353 /** |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
354 * @brief Read characters from @a file. |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
355 * |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
356 * @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
|
357 * @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
|
358 * @param file file to read from |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
359 * |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
360 * @return str (success) or NULL (error) |
35475
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
361 * |
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
362 * @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
|
363 */ |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
364 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
|
365 { |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
366 char *s; |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
367 |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
368 s = fgets(str, size, file); |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
369 |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
370 if (s) |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
371 s[strcspn(s, "\n\r")] = 0; |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
372 |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
373 return s; |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
374 } |