Mercurial > mplayer.hg
annotate gui/util/string.c @ 37065:b28b632efeef
Cosmetic: Revise a few comments.
author | ib |
---|---|
date | Thu, 24 Apr 2014 11:31:31 +0000 |
parents | 0790f864cea2 |
children |
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 | |
37024 | 24 #include <stdio.h> |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
25 #include <stdlib.h> |
33737 | 26 #include <string.h> |
27 | |
33048 | 28 #include "string.h" |
35525 | 29 #include "gui/app/gui.h" |
34175 | 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') | |
36986 | 46 *p += 0x20; |
33052 | 47 |
48 p++; | |
49 } | |
50 | |
51 return in; | |
52 } | |
53 | |
33982 | 54 /** |
36987 | 55 * @brief Convert a string to upper case. |
56 * | |
57 * @param string to be converted | |
58 * | |
59 * @return converted string | |
60 * | |
61 * @note Only characters from a to z will be converted and this is an in-place conversion. | |
62 */ | |
63 char *strupper(char *in) | |
64 { | |
65 char *p = in; | |
66 | |
67 while (*p) { | |
68 if (*p >= 'a' && *p <= 'z') | |
69 *p -= 0x20; | |
70 | |
71 p++; | |
72 } | |
73 | |
74 return in; | |
75 } | |
76 | |
77 /** | |
33982 | 78 * @brief Swap characters in a string. |
79 * | |
80 * @param in string to be processed | |
81 * @param from character to be swapped | |
82 * @param to character to swap in | |
83 * | |
84 * @return processed string | |
85 * | |
86 * @note All occurrences will be swapped and this is an in-place processing. | |
87 */ | |
33048 | 88 char *strswap(char *in, char from, char to) |
89 { | |
33049 | 90 char *p = in; |
33048 | 91 |
33049 | 92 while (*p) { |
93 if (*p == from) | |
94 *p = to; | |
33048 | 95 |
33049 | 96 p++; |
97 } | |
33048 | 98 |
99 return in; | |
100 } | |
101 | |
33982 | 102 /** |
103 * @brief Remove all space characters from a string, | |
104 * but leave text enclosed in quotation marks untouched. | |
105 * | |
106 * @param in string to be processed | |
107 * | |
108 * @return processed string | |
109 * | |
110 * @note This is an in-place processing. | |
111 */ | |
33048 | 112 char *trim(char *in) |
113 { | |
33051 | 114 char *src, *dest; |
35493 | 115 int freeze = False; |
33048 | 116 |
33051 | 117 src = dest = in; |
33048 | 118 |
33051 | 119 while (*src) { |
120 if (*src == '"') | |
121 freeze = !freeze; | |
33048 | 122 |
33051 | 123 if (freeze || (*src != ' ')) |
124 *dest++ = *src; | |
125 | |
126 src++; | |
33048 | 127 } |
128 | |
33051 | 129 *dest = 0; |
130 | |
33048 | 131 return in; |
132 } | |
33073 | 133 |
33982 | 134 /** |
135 * @brief Remove a comment from a string, | |
136 * but leave text enclosed in quotation marks untouched. | |
137 * | |
138 * A comment starts either with a semicolon anywhere in the string | |
139 * or with a number sign character at the very beginning. | |
140 * | |
141 * @param in string to be processed | |
142 * | |
143 * @return string without comment | |
144 * | |
145 * @note This is an in-place processing, i.e. @a in will be shortened. | |
146 */ | |
33073 | 147 char *decomment(char *in) |
148 { | |
149 char *p; | |
35493 | 150 int nap = False; |
33073 | 151 |
152 p = in; | |
153 | |
33080 | 154 if (*p == '#') |
155 *p = 0; | |
156 | |
33073 | 157 while (*p) { |
158 if (*p == '"') | |
159 nap = !nap; | |
160 | |
161 if ((*p == ';') && !nap) { | |
162 *p = 0; | |
163 break; | |
164 } | |
165 | |
166 p++; | |
167 } | |
168 | |
169 return in; | |
170 } | |
33737 | 171 |
35479 | 172 /** |
36989 | 173 * @brief Extract a part of a string delimited by a separator character. |
174 * | |
175 * @param in string to be analyzed | |
37065 | 176 * @param out memory location of a buffer suitable to store the extracted part |
36989 | 177 * @param sep separator character |
178 * @param num number of separator characters to be skipped before extraction starts | |
179 * @param maxout maximum length of extracted part (including the trailing null byte) | |
180 */ | |
36991 | 181 void cutString(char *in, char *out, char sep, int num, size_t maxout) |
36989 | 182 { |
183 int n; | |
184 unsigned int i, c; | |
185 | |
186 for (c = 0, n = 0, i = 0; in[i]; i++) { | |
187 if (in[i] == sep) | |
188 n++; | |
189 if (n >= num && in[i] != sep && c + 1 < maxout) | |
190 out[c++] = in[i]; | |
191 if (n >= num && in[i + 1] == sep) | |
192 break; | |
193 } | |
194 | |
195 if (c < maxout) | |
196 out[c] = 0; | |
197 } | |
198 | |
199 /** | |
200 * @brief Extract a numeric part of a string delimited by a separator character. | |
201 * | |
202 * @param in string to be analyzed | |
203 * @param sep separator character | |
204 * @param num number of separator characters to be skipped before extraction starts | |
205 * | |
206 * @return extracted number (numeric part) | |
207 */ | |
36991 | 208 int cutInt(char *in, char sep, int num) |
36989 | 209 { |
210 char tmp[64]; | |
211 | |
36991 | 212 cutStr(in, tmp, sep, num); |
36989 | 213 |
214 return atoi(tmp); | |
215 } | |
216 | |
217 /** | |
37065 | 218 * @brief A strchr() that can handle NULL pointer arguments. |
35479 | 219 * |
220 * @param str string to examine | |
221 * @param c character to find | |
222 * | |
223 * @return return value of strchr() or NULL, if @a str is NULL | |
224 */ | |
33737 | 225 char *gstrchr(const char *str, int c) |
226 { | |
227 if (!str) | |
228 return NULL; | |
229 | |
230 return strchr(str, c); | |
231 } | |
232 | |
35459 | 233 /** |
37065 | 234 * @brief A strcmp() that can handle NULL pointer arguments. |
35459 | 235 * |
236 * @param a string to be compared | |
237 * @param b string which is compared | |
238 * | |
35479 | 239 * @return return value of strcmp() or -1, if @a a or @a b are NULL |
35459 | 240 */ |
33737 | 241 int gstrcmp(const char *a, const char *b) |
242 { | |
243 if (!a && !b) | |
244 return 0; | |
245 if (!a || !b) | |
246 return -1; | |
247 | |
248 return strcmp(a, b); | |
249 } | |
250 | |
34628 | 251 /** |
37065 | 252 * @brief A strncmp() that can handle NULL pointer arguments. |
34628 | 253 * |
254 * @param a string to be compared | |
255 * @param b string which is compared | |
256 * @param n number of characters compared at the most | |
257 * | |
35479 | 258 * @return return value of strncmp() or -1, if @a a or @a b are NULL |
34628 | 259 */ |
260 int gstrncmp(const char *a, const char *b, size_t n) | |
33737 | 261 { |
262 if (!a && !b) | |
263 return 0; | |
264 if (!a || !b) | |
265 return -1; | |
266 | |
267 return strncmp(a, b, n); | |
268 } | |
269 | |
33982 | 270 /** |
271 * @brief Duplicate a string. | |
272 * | |
273 * If @a str is NULL, it returns NULL. | |
274 * The string is duplicated by calling strdup(). | |
275 * | |
276 * @param str string to be duplicated | |
277 * | |
278 * @return duplicated string (newly allocated) | |
279 */ | |
33737 | 280 char *gstrdup(const char *str) |
281 { | |
282 if (!str) | |
283 return NULL; | |
284 | |
285 return strdup(str); | |
286 } | |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
287 |
33982 | 288 /** |
289 * @brief Assign a duplicated string. | |
290 * | |
291 * The string is duplicated by calling #gstrdup(). | |
292 * | |
37065 | 293 * @param old memory location to store the new pointer |
33982 | 294 * @param str string to be duplicated |
35475
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
295 * |
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
296 * @note @a *old is freed prior to the assignment. |
33982 | 297 */ |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
298 void setdup(char **old, const char *str) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
299 { |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
300 free(*old); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
301 *old = gstrdup(str); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
302 } |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
303 |
33982 | 304 /** |
305 * @brief Assign a newly allocated string | |
306 * containing the path created from a directory and a filename. | |
307 * | |
37065 | 308 * @param old memory location to store the new pointer |
33982 | 309 * @param dir directory |
310 * @param name filename | |
35475
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
311 * |
49f29de2ff10
Cosmetic: Place doxygen note at the end of the comment.
ib
parents:
35459
diff
changeset
|
312 * @note @a *old is freed prior to the assignment. |
33982 | 313 */ |
33740
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
314 void setddup(char **old, const char *dir, const char *name) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
315 { |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
316 free(*old); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
317 *old = malloc(strlen(dir) + strlen(name) + 2); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
318 if (*old) |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
319 sprintf(*old, "%s/%s", dir, name); |
2c02269701bd
Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents:
33737
diff
changeset
|
320 } |