annotate gui/util/string.c @ 34560:abcf26dcec6b

Add fgetstr() to read from files without end-of-line characters. Use it where fgets'ed lines are cleared of EOLs. Remove gfgets() and replace these calls by fgetstr().
author ib
date Fri, 03 Feb 2012 13:38:42 +0000
parents 5a45efc630b8
children ee78c9c66508
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
1 /*
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
2 * This file is part of MPlayer.
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
3 *
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
7 * (at your option) any later version.
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
8 *
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
12 * GNU General Public License for more details.
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
13 *
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
14 * You should have received a copy of the GNU General Public License along
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
17 */
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
18
33740
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
19 #include <stdlib.h>
33737
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
20 #include <string.h>
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
21
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
22 #include "string.h"
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
23 #include "gui/interface.h"
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
24
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
25 #include "config.h"
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
26 #include "help_mp.h"
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
27 #include "libavutil/avstring.h"
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
28 #include "stream/stream.h"
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
29
33982
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
30 /**
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
31 * @brief Convert a string to lower case.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
32 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
33 * @param string to be converted
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
34 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
35 * @return converted string
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
36 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
37 * @note Only characters from A to Z will be converted and this is an in-place conversion.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
38 */
33052
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
39 char *strlower(char *in)
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
40 {
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
41 char *p = in;
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
42
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
43 while (*p) {
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
44 if (*p >= 'A' && *p <= 'Z')
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
45 *p += 'a' - 'A';
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
46
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
47 p++;
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
48 }
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
49
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
50 return in;
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
51 }
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
52
33982
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
53 /**
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
54 * @brief Swap characters in a string.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
55 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
56 * @param in string to be processed
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
57 * @param from character to be swapped
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
58 * @param to character to swap in
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
59 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
60 * @return processed string
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
61 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
62 * @note All occurrences will be swapped and this is an in-place processing.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
63 */
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
64 char *strswap(char *in, char from, char to)
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
65 {
33049
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
66 char *p = in;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
67
33049
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
68 while (*p) {
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
69 if (*p == from)
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
70 *p = to;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
71
33049
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
72 p++;
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
73 }
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
74
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
75 return in;
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
76 }
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
77
33982
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
78 /**
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
79 * @brief Remove all space characters from a string,
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
80 * but leave text enclosed in quotation marks untouched.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
81 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
82 * @param in string to be processed
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
83 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
84 * @return processed string
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
85 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
86 * @note This is an in-place processing.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
87 */
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
88 char *trim(char *in)
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
89 {
33051
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
90 char *src, *dest;
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
91 int freeze = 0;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
92
33051
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
93 src = dest = in;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
94
33051
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
95 while (*src) {
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
96 if (*src == '"')
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
97 freeze = !freeze;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
98
33051
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
99 if (freeze || (*src != ' '))
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
100 *dest++ = *src;
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
101
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
102 src++;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
103 }
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
104
33051
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
105 *dest = 0;
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
106
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
107 return in;
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
108 }
33073
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
109
33982
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
110 /**
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
111 * @brief Remove a comment from a string,
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
112 * but leave text enclosed in quotation marks untouched.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
113 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
114 * A comment starts either with a semicolon anywhere in the string
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
115 * or with a number sign character at the very beginning.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
116 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
117 * @param in string to be processed
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
118 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
119 * @return string without comment
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
120 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
121 * @note This is an in-place processing, i.e. @a in will be shortened.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
122 */
33073
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
123 char *decomment(char *in)
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
124 {
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
125 char *p;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
126 int nap = 0;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
127
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
128 p = in;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
129
33080
60b2e408bd78 Allow number sign as comment character.
ib
parents: 33073
diff changeset
130 if (*p == '#')
60b2e408bd78 Allow number sign as comment character.
ib
parents: 33073
diff changeset
131 *p = 0;
60b2e408bd78 Allow number sign as comment character.
ib
parents: 33073
diff changeset
132
33073
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
133 while (*p) {
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
134 if (*p == '"')
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
135 nap = !nap;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
136
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
137 if ((*p == ';') && !nap) {
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
138 *p = 0;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
139 break;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
140 }
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
141
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
142 p++;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
143 }
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
144
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
145 return in;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
146 }
33737
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
147
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
148 char *gstrchr(const char *str, int c)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
149 {
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
150 if (!str)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
151 return NULL;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
152
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
153 return strchr(str, c);
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
154 }
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
155
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
156 int gstrcmp(const char *a, const char *b)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
157 {
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
158 if (!a && !b)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
159 return 0;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
160 if (!a || !b)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
161 return -1;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
162
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
163 return strcmp(a, b);
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
164 }
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
165
33786
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
166 int gstrcasecmp(const char *a, const char *b)
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
167 {
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
168 if (!a && !b)
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
169 return 0;
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
170 if (!a || !b)
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
171 return -1;
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
172
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
173 return strcasecmp(a, b);
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
174 }
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
175
33737
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
176 int gstrncmp(const char *a, const char *b, int n)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
177 {
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
178 if (!a && !b)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
179 return 0;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
180 if (!a || !b)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
181 return -1;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
182
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
183 return strncmp(a, b, n);
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
184 }
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
185
33982
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
186 /**
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
187 * @brief Duplicate a string.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
188 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
189 * If @a str is NULL, it returns NULL.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
190 * The string is duplicated by calling strdup().
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
191 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
192 * @param str string to be duplicated
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
193 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
194 * @return duplicated string (newly allocated)
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
195 */
33737
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
196 char *gstrdup(const char *str)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
197 {
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
198 if (!str)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
199 return NULL;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
200
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
201 return strdup(str);
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
202 }
33740
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
203
33982
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
204 /**
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
205 * @brief Assign a duplicated string.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
206 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
207 * The string is duplicated by calling #gstrdup().
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
208 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
209 * @note @a *old is freed prior to the assignment.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
210 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
211 * @param old pointer to a variable suitable to store the new pointer
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
212 * @param str string to be duplicated
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
213 */
33740
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
214 void setdup(char **old, const char *str)
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
215 {
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
216 free(*old);
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
217 *old = gstrdup(str);
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
218 }
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
219
33982
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
220 /**
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
221 * @brief Assign a newly allocated string
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
222 * containing the path created from a directory and a filename.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
223 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
224 * @note @a *old is freed prior to the assignment.
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
225 *
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
226 * @param old pointer to a variable suitable to store the new pointer
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
227 * @param dir directory
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
228 * @param name filename
d7527ee45784 Add doxygen comments to string.c.
ib
parents: 33786
diff changeset
229 */
33740
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
230 void setddup(char **old, const char *dir, const char *name)
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
231 {
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
232 free(*old);
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
233 *old = malloc(strlen(dir) + strlen(name) + 2);
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
234 if (*old)
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
235 sprintf(*old, "%s/%s", dir, name);
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
236 }
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
237
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
238 /**
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
239 * @brief Convert #guiInfo member Filename.
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
240 *
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
241 * @param how 0 (cut file path and extension),
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
242 * 1 (additionally, convert lower case) or
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
243 * 2 (additionally, convert upper case)
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
244 * @param fname pointer to a buffer to receive the converted Filename
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
245 * @param maxlen size of @a fname buffer
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
246 *
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
247 * @return pointer to @a fname buffer
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
248 */
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
249 char *TranslateFilename(int how, char *fname, size_t maxlen)
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
250 {
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
251 char *p;
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
252 size_t len;
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
253
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
254 switch (guiInfo.StreamType) {
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
255 case STREAMTYPE_FILE:
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
256 if (guiInfo.Filename && *guiInfo.Filename) {
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
257 p = strrchr(guiInfo.Filename,
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
258 #if HAVE_DOS_PATHS
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
259 '\\');
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
260 #else
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
261 '/');
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
262 #endif
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
263
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
264 if (p)
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
265 av_strlcpy(fname, p + 1, maxlen);
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
266 else
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
267 av_strlcpy(fname, guiInfo.Filename, maxlen);
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
268
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
269 len = strlen(fname);
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
270
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
271 if (len > 3 && fname[len - 3] == '.')
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
272 fname[len - 3] = 0;
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
273 else if (len > 4 && fname[len - 4] == '.')
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
274 fname[len - 4] = 0;
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
275 else if (len > 5 && fname[len - 5] == '.')
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
276 fname[len - 5] = 0;
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
277 } else
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
278 av_strlcpy(fname, MSGTR_NoFileLoaded, maxlen);
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
279 break;
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
280
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
281 case STREAMTYPE_STREAM:
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
282 av_strlcpy(fname, guiInfo.Filename, maxlen);
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
283 break;
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
284
34387
0ba85cad4c7e Add audio CD playback support to the X11/GTK GUI.
ib
parents: 34177
diff changeset
285 case STREAMTYPE_CDDA:
0ba85cad4c7e Add audio CD playback support to the X11/GTK GUI.
ib
parents: 34177
diff changeset
286 snprintf(fname, maxlen, MSGTR_Title, guiInfo.Track);
0ba85cad4c7e Add audio CD playback support to the X11/GTK GUI.
ib
parents: 34177
diff changeset
287 break;
0ba85cad4c7e Add audio CD playback support to the X11/GTK GUI.
ib
parents: 34177
diff changeset
288
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
289 case STREAMTYPE_VCD:
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
290 snprintf(fname, maxlen, MSGTR_Title, guiInfo.Track - 1);
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
291 break;
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
292
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
293 case STREAMTYPE_DVD:
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
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
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
296 else
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
297 av_strlcat(fname, MSGTR_NoChapter, maxlen);
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
298 break;
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
299
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
300 default:
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
301 av_strlcpy(fname, MSGTR_NoMediaOpened, maxlen);
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
302 break;
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
303 }
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
304
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
305 if (how) {
34177
fadc00cc31d3 Optimize TranslateFilename().
ib
parents: 34176
diff changeset
306 p = fname;
fadc00cc31d3 Optimize TranslateFilename().
ib
parents: 34176
diff changeset
307
fadc00cc31d3 Optimize TranslateFilename().
ib
parents: 34176
diff changeset
308 while (*p) {
fadc00cc31d3 Optimize TranslateFilename().
ib
parents: 34176
diff changeset
309 char t = 0;
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
310
34177
fadc00cc31d3 Optimize TranslateFilename().
ib
parents: 34176
diff changeset
311 if (how == 1 && *p >= 'A' && *p <= 'Z')
fadc00cc31d3 Optimize TranslateFilename().
ib
parents: 34176
diff changeset
312 t = 32;
fadc00cc31d3 Optimize TranslateFilename().
ib
parents: 34176
diff changeset
313 if (how == 2 && *p >= 'a' && *p <= 'z')
fadc00cc31d3 Optimize TranslateFilename().
ib
parents: 34176
diff changeset
314 t = -32;
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
315
34177
fadc00cc31d3 Optimize TranslateFilename().
ib
parents: 34176
diff changeset
316 *p = *p + t;
fadc00cc31d3 Optimize TranslateFilename().
ib
parents: 34176
diff changeset
317 p++;
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
318 }
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
319 }
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
320
34176
d52b0ad317d5 Cosmetic: Rename TranslateFilename()'s parameter names.
ib
parents: 34175
diff changeset
321 return fname;
34175
a345e7162d0a Move TranslateFilename() to util/string.c.
ib
parents: 33982
diff changeset
322 }
34560
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 /**
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
325 * @brief Read characters from @a file.
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
326 *
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
327 * @note Reading stops with '\\r', '\\n' or EOF.
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
328 *
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
329 * @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
330 * @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
331 * @param file file to read from
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
332 *
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
333 * @return str (success) or NULL (error)
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
334 */
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
335 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
336 {
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
337 char *s;
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
338
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
339 s = fgets(str, size, file);
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
340
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
341 if (s)
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
342 s[strcspn(s, "\n\r")] = 0;
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 return s;
abcf26dcec6b Add fgetstr() to read from files without end-of-line characters.
ib
parents: 34454
diff changeset
345 }