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