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