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