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