Mercurial > mplayer.hg
annotate gui/util/string.c @ 35473:d61151719945
Remove unused defines that cause redefinition warnings.
author | reimar |
---|---|
date | Sun, 02 Dec 2012 22:02:51 +0000 |
parents | 1edb306bc754 |
children | 49f29de2ff10 |
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 | |
35459 | 156 /** |
157 * @brief A strcmp() that can handle NULL pointers. | |
158 * | |
159 * @param a string to be compared | |
160 * @param b string which is compared | |
161 * | |
162 * @return return value of strcmp() or -1, if a or b are NULL | |
163 */ | |
33737 | 164 int gstrcmp(const char *a, const char *b) |
165 { | |
166 if (!a && !b) | |
167 return 0; | |
168 if (!a || !b) | |
169 return -1; | |
170 | |
171 return strcmp(a, b); | |
172 } | |
173 | |
34628 | 174 /** |
175 * @brief A strncmp() that can handle NULL pointers. | |
176 * | |
177 * @param a string to be compared | |
178 * @param b string which is compared | |
179 * @param n number of characters compared at the most | |
180 * | |
181 * @return return value of strncmp() or -1, if a or b are NULL | |
182 */ | |
183 int gstrncmp(const char *a, const char *b, size_t n) | |
33737 | 184 { |
185 if (!a && !b) | |
186 return 0; | |
187 if (!a || !b) | |
188 return -1; | |
189 | |
190 return strncmp(a, b, n); | |
191 } | |
192 | |
33982 | 193 /** |
194 * @brief Duplicate a string. | |
195 * | |
196 * If @a str is NULL, it returns NULL. | |
197 * The string is duplicated by calling strdup(). | |
198 * | |
199 * @param str string to be duplicated | |
200 * | |
201 * @return duplicated string (newly allocated) | |
202 */ | |
33737 | 203 char *gstrdup(const char *str) |
204 { | |
205 if (!str) | |
206 return NULL; | |
207 | |
208 return strdup(str); | |
209 } | |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
210 |
33982 | 211 /** |
212 * @brief Assign a duplicated string. | |
213 * | |
214 * The string is duplicated by calling #gstrdup(). | |
215 * | |
216 * @note @a *old is freed prior to the assignment. | |
217 * | |
218 * @param old pointer to a variable suitable to store the new pointer | |
219 * @param str string to be duplicated | |
220 */ | |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
221 void setdup(char **old, const char *str) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
222 { |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
223 free(*old); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
224 *old = gstrdup(str); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
225 } |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
226 |
33982 | 227 /** |
228 * @brief Assign a newly allocated string | |
229 * containing the path created from a directory and a filename. | |
230 * | |
231 * @note @a *old is freed prior to the assignment. | |
232 * | |
233 * @param old pointer to a variable suitable to store the new pointer | |
234 * @param dir directory | |
235 * @param name filename | |
236 */ | |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
237 void setddup(char **old, const char *dir, const char *name) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
238 { |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
239 free(*old); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
240 *old = malloc(strlen(dir) + strlen(name) + 2); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
241 if (*old) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
242 sprintf(*old, "%s/%s", dir, name); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
243 } |
34175 | 244 |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
245 /** |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
246 * @brief Convert #guiInfo member Filename. |
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 * @param how 0 (cut file path and extension), |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
249 * 1 (additionally, convert lower case) or |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
250 * 2 (additionally, convert upper case) |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
251 * @param fname pointer to a buffer to receive the converted Filename |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
252 * @param maxlen size of @a fname buffer |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
253 * |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
254 * @return pointer to @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 char *TranslateFilename(int how, char *fname, size_t maxlen) |
34175 | 257 { |
258 char *p; | |
259 size_t len; | |
260 | |
261 switch (guiInfo.StreamType) { | |
262 case STREAMTYPE_FILE: | |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
263 if (guiInfo.Filename && *guiInfo.Filename) { |
34175 | 264 p = strrchr(guiInfo.Filename, |
265 #if HAVE_DOS_PATHS | |
266 '\\'); | |
267 #else | |
268 '/'); | |
269 #endif | |
270 | |
271 if (p) | |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
272 av_strlcpy(fname, p + 1, maxlen); |
34175 | 273 else |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
274 av_strlcpy(fname, guiInfo.Filename, maxlen); |
34175 | 275 |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
276 len = strlen(fname); |
34175 | 277 |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
278 if (len > 3 && fname[len - 3] == '.') |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
279 fname[len - 3] = 0; |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
280 else if (len > 4 && fname[len - 4] == '.') |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
281 fname[len - 4] = 0; |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
282 else if (len > 5 && fname[len - 5] == '.') |
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
283 fname[len - 5] = 0; |
34175 | 284 } else |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
285 av_strlcpy(fname, MSGTR_NoFileLoaded, maxlen); |
34175 | 286 break; |
287 | |
288 case STREAMTYPE_STREAM: | |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
289 av_strlcpy(fname, guiInfo.Filename, maxlen); |
34175 | 290 break; |
291 | |
34387 | 292 case STREAMTYPE_CDDA: |
293 snprintf(fname, maxlen, MSGTR_Title, guiInfo.Track); | |
294 break; | |
295 | |
34175 | 296 case STREAMTYPE_VCD: |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
297 snprintf(fname, maxlen, MSGTR_Title, guiInfo.Track - 1); |
34175 | 298 break; |
299 | |
300 case STREAMTYPE_DVD: | |
301 if (guiInfo.Chapter) | |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
302 snprintf(fname, maxlen, MSGTR_Chapter, guiInfo.Chapter); |
34175 | 303 else |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
304 av_strlcat(fname, MSGTR_NoChapter, maxlen); |
34175 | 305 break; |
306 | |
307 default: | |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
308 av_strlcpy(fname, MSGTR_NoMediaOpened, maxlen); |
34175 | 309 break; |
310 } | |
311 | |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
312 if (how) { |
34177 | 313 p = fname; |
314 | |
315 while (*p) { | |
316 char t = 0; | |
34175 | 317 |
34177 | 318 if (how == 1 && *p >= 'A' && *p <= 'Z') |
319 t = 32; | |
320 if (how == 2 && *p >= 'a' && *p <= 'z') | |
321 t = -32; | |
34175 | 322 |
34177 | 323 *p = *p + t; |
324 p++; | |
34175 | 325 } |
326 } | |
327 | |
34176
d52b0ad317d5
Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents:
34175
diff
changeset
|
328 return fname; |
34175 | 329 } |
34560
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
330 |
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 * @brief Read characters from @a file. |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
333 * |
34629 | 334 * @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
|
335 * |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
336 * @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
|
337 * @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
|
338 * @param file file to read from |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
339 * |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
340 * @return str (success) or NULL (error) |
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 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
|
343 { |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
344 char *s; |
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 s = fgets(str, size, file); |
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 if (s) |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
349 s[strcspn(s, "\n\r")] = 0; |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
350 |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
351 return s; |
abcf26dcec6b
Add fgetstr() to read from files without end-of-line characters.
ib
parents:
34454
diff
changeset
|
352 } |