annotate gui/util/string.c @ 33786:0d3d8db05a59

Compare encodings case insensitively. Add and use new function gstrcasecmp(). This closes Bugzilla #644.
author ib
date Mon, 11 Jul 2011 12:40:30 +0000
parents 2c02269701bd
children d7527ee45784
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 <stdio.h>
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
20 #include <stdlib.h>
33737
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
21 #include <string.h>
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
22
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
23 #include "string.h"
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
24
33052
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
25 char *strlower(char *in)
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
26 {
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
27 char *p = in;
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
28
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
29 while (*p) {
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
30 if (*p >= 'A' && *p <= 'Z')
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
31 *p += 'a' - 'A';
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
32
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
33 p++;
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
34 }
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
35
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
36 return in;
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
37 }
956c67bb5198 Move strlower() into auxiliary string function file.
ib
parents: 33051
diff changeset
38
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
39 char *strswap(char *in, char from, char to)
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
40 {
33049
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
41 char *p = in;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
42
33049
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
43 while (*p) {
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
44 if (*p == from)
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
45 *p = to;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
46
33049
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
47 p++;
fc7a3f9f74f8 Simplify strswap().
ib
parents: 33048
diff changeset
48 }
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
49
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
50 return in;
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
51 }
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
52
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
53 char *trim(char *in)
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
54 {
33051
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
55 char *src, *dest;
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
56 int freeze = 0;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
57
33051
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
58 src = dest = in;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
59
33051
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
60 while (*src) {
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
61 if (*src == '"')
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
62 freeze = !freeze;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
63
33051
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
64 if (freeze || (*src != ' '))
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
65 *dest++ = *src;
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
66
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
67 src++;
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
68 }
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
69
33051
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
70 *dest = 0;
cec61c9f27f4 Simplify trim().
ib
parents: 33049
diff changeset
71
33048
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
72 return in;
c6d0adf896ea Move auxiliary string functions into separate file.
ib
parents:
diff changeset
73 }
33073
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
74
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
75 char *decomment(char *in)
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
76 {
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
77 char *p;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
78 int nap = 0;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
79
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
80 p = in;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
81
33080
60b2e408bd78 Allow number sign as comment character.
ib
parents: 33073
diff changeset
82 if (*p == '#')
60b2e408bd78 Allow number sign as comment character.
ib
parents: 33073
diff changeset
83 *p = 0;
60b2e408bd78 Allow number sign as comment character.
ib
parents: 33073
diff changeset
84
33073
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
85 while (*p) {
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
86 if (*p == '"')
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
87 nap = !nap;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
88
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
89 if ((*p == ';') && !nap) {
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
90 *p = 0;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
91 break;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
92 }
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
93
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
94 p++;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
95 }
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
96
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
97 return in;
334e19411421 Improve handling of the comment character.
ib
parents: 33052
diff changeset
98 }
33737
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
99
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
100 char *gstrchr(const char *str, int c)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
101 {
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
102 if (!str)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
103 return NULL;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
104
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
105 return strchr(str, c);
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
106 }
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
107
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
108 int gstrcmp(const char *a, const char *b)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
109 {
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
110 if (!a && !b)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
111 return 0;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
112 if (!a || !b)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
113 return -1;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
114
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
115 return strcmp(a, b);
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
116 }
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
117
33786
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
118 int gstrcasecmp(const char *a, const char *b)
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
119 {
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
120 if (!a && !b)
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
121 return 0;
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
122 if (!a || !b)
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
123 return -1;
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
124
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
125 return strcasecmp(a, b);
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
126 }
0d3d8db05a59 Compare encodings case insensitively.
ib
parents: 33740
diff changeset
127
33737
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
128 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
129 {
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
130 if (!a && !b)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
131 return 0;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
132 if (!a || !b)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
133 return -1;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
134
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
135 return strncmp(a, b, n);
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
136 }
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
137
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
138 char *gstrdup(const char *str)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
139 {
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
140 if (!str)
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
141 return NULL;
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
142
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
143 return strdup(str);
71c29e8ec68f Move string functions from interface.c to string.c.
ib
parents: 33080
diff changeset
144 }
33740
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
145
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
146 void setdup(char **old, const char *str)
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
147 {
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
148 free(*old);
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
149 *old = gstrdup(str);
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
150 }
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
151
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
152 void setddup(char **old, const char *dir, const char *name)
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
153 {
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
154 free(*old);
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
155 *old = malloc(strlen(dir) + strlen(name) + 2);
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
156 if (*old)
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
157 sprintf(*old, "%s/%s", dir, name);
2c02269701bd Remove macros guiSetFilename() and guiSetDF() from interface.h.
ib
parents: 33737
diff changeset
158 }