~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to gui/util/string.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
17
 */
18
18
 
 
19
#include <stdio.h>
 
20
#include <stdlib.h>
 
21
#include <string.h>
 
22
 
19
23
#include "string.h"
20
 
 
 
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"
 
30
 
 
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
 */
21
40
char *strlower(char *in)
22
41
{
23
42
    char *p = in;
32
51
    return in;
33
52
}
34
53
 
 
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
 */
35
65
char *strswap(char *in, char from, char to)
36
66
{
37
67
    char *p = in;
46
76
    return in;
47
77
}
48
78
 
 
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
 */
49
89
char *trim(char *in)
50
90
{
51
91
    char *src, *dest;
68
108
    return in;
69
109
}
70
110
 
 
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
 */
71
124
char *decomment(char *in)
72
125
{
73
126
    char *p;
92
145
 
93
146
    return in;
94
147
}
 
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
 
 
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
 
 
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
 
 
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
 */
 
197
char *gstrdup(const char *str)
 
198
{
 
199
    if (!str)
 
200
        return NULL;
 
201
 
 
202
    return strdup(str);
 
203
}
 
204
 
 
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
 */
 
215
void setdup(char **old, const char *str)
 
216
{
 
217
    free(*old);
 
218
    *old = gstrdup(str);
 
219
}
 
220
 
 
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
 */
 
231
void setddup(char **old, const char *dir, const char *name)
 
232
{
 
233
    free(*old);
 
234
    *old = malloc(strlen(dir) + strlen(name) + 2);
 
235
    if (*old)
 
236
        sprintf(*old, "%s/%s", dir, name);
 
237
}
 
238
 
 
239
/**
 
240
 * @brief Convert #guiInfo member Filename.
 
241
 *
 
242
 * @param how 0 (cut file path and extension),
 
243
 *            1 (additionally, convert lower case) or
 
244
 *            2 (additionally, convert upper case)
 
245
 * @param fname pointer to a buffer to receive the converted Filename
 
246
 * @param maxlen size of @a fname buffer
 
247
 *
 
248
 * @return pointer to @a fname buffer
 
249
 */
 
250
char *TranslateFilename(int how, char *fname, size_t maxlen)
 
251
{
 
252
    char *p;
 
253
    size_t len;
 
254
 
 
255
    switch (guiInfo.StreamType) {
 
256
    case STREAMTYPE_FILE:
 
257
        if (guiInfo.Filename && *guiInfo.Filename) {
 
258
            p = strrchr(guiInfo.Filename,
 
259
#if HAVE_DOS_PATHS
 
260
                        '\\');
 
261
#else
 
262
                        '/');
 
263
#endif
 
264
 
 
265
            if (p)
 
266
                av_strlcpy(fname, p + 1, maxlen);
 
267
            else
 
268
                av_strlcpy(fname, guiInfo.Filename, maxlen);
 
269
 
 
270
            len = strlen(fname);
 
271
 
 
272
            if (len > 3 && fname[len - 3] == '.')
 
273
                fname[len - 3] = 0;
 
274
            else if (len > 4 && fname[len - 4] == '.')
 
275
                fname[len - 4] = 0;
 
276
            else if (len > 5 && fname[len - 5] == '.')
 
277
                fname[len - 5] = 0;
 
278
        } else
 
279
            av_strlcpy(fname, MSGTR_NoFileLoaded, maxlen);
 
280
        break;
 
281
 
 
282
    case STREAMTYPE_STREAM:
 
283
        av_strlcpy(fname, guiInfo.Filename, maxlen);
 
284
        break;
 
285
 
 
286
#ifdef CONFIG_CDDA
 
287
    case STREAMTYPE_CDDA:
 
288
        snprintf(fname, maxlen, MSGTR_Title, guiInfo.Track);
 
289
        break;
 
290
#endif
 
291
 
 
292
#ifdef CONFIG_VCD
 
293
    case STREAMTYPE_VCD:
 
294
        snprintf(fname, maxlen, MSGTR_Title, guiInfo.Track - 1);
 
295
        break;
 
296
#endif
 
297
 
 
298
#ifdef CONFIG_DVDREAD
 
299
    case STREAMTYPE_DVD:
 
300
        if (guiInfo.Chapter)
 
301
            snprintf(fname, maxlen, MSGTR_Chapter, guiInfo.Chapter);
 
302
        else
 
303
            av_strlcat(fname, MSGTR_NoChapter, maxlen);
 
304
        break;
 
305
#endif
 
306
 
 
307
    default:
 
308
        av_strlcpy(fname, MSGTR_NoMediaOpened, maxlen);
 
309
        break;
 
310
    }
 
311
 
 
312
    if (how) {
 
313
        p = fname;
 
314
 
 
315
        while (*p) {
 
316
            char t = 0;
 
317
 
 
318
            if (how == 1 && *p >= 'A' && *p <= 'Z')
 
319
                t = 32;
 
320
            if (how == 2 && *p >= 'a' && *p <= 'z')
 
321
                t = -32;
 
322
 
 
323
            *p = *p + t;
 
324
            p++;
 
325
        }
 
326
    }
 
327
 
 
328
    return fname;
 
329
}