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