~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/blenlib/intern/string.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* util.c
2
 
 *
3
 
 * various string, file, list operations.
4
 
 *
5
 
 *
6
 
 * $Id: string.c 28667 2010-05-08 15:37:29Z campbellbarton $
7
 
 *
 
1
/*
8
2
 * ***** BEGIN GPL LICENSE BLOCK *****
9
3
 *
10
4
 * This program is free software; you can redistribute it and/or
32
26
 * 
33
27
 */
34
28
 
 
29
/** \file blender/blenlib/intern/string.c
 
30
 *  \ingroup bli
 
31
 */
 
32
 
 
33
 
35
34
#include <string.h>
36
35
#include <stdlib.h>
37
36
#include <stdarg.h>
42
41
#include "BLI_dynstr.h"
43
42
#include "BLI_string.h"
44
43
 
45
 
char *BLI_strdupn(const char *str, int len) {
 
44
char *BLI_strdupn(const char *str, const size_t len)
 
45
{
46
46
        char *n= MEM_mallocN(len+1, "strdup");
47
47
        memcpy(n, str, len);
48
48
        n[len]= '\0';
49
49
        
50
50
        return n;
51
51
}
52
 
char *BLI_strdup(const char *str) {
 
52
char *BLI_strdup(const char *str)
 
53
{
53
54
        return BLI_strdupn(str, strlen(str));
54
55
}
55
56
 
56
57
char *BLI_strdupcat(const char *str1, const char *str2)
57
58
{
58
 
        int len;
 
59
        size_t len;
59
60
        char *n;
60
61
        
61
62
        len= strlen(str1)+strlen(str2);
66
67
        return n;
67
68
}
68
69
 
69
 
char *BLI_strncpy(char *dst, const char *src, int maxncpy) {
70
 
        int srclen= strlen(src);
71
 
        int cpylen= (srclen>(maxncpy-1))?(maxncpy-1):srclen;
 
70
char *BLI_strncpy(char *dst, const char *src, const size_t maxncpy)
 
71
{
 
72
        size_t srclen= strlen(src);
 
73
        size_t cpylen= (srclen>(maxncpy-1))?(maxncpy-1):srclen;
72
74
        
73
75
        memcpy(dst, src, cpylen);
74
76
        dst[cpylen]= '\0';
76
78
        return dst;
77
79
}
78
80
 
79
 
int BLI_snprintf(char *buffer, size_t count, const char *format, ...)
 
81
size_t BLI_snprintf(char *buffer, size_t count, const char *format, ...)
80
82
{
81
 
        int n;
 
83
        size_t n;
82
84
        va_list arg;
83
85
 
84
86
        va_start(arg, format);
86
88
        
87
89
        if (n != -1 && n < count) {
88
90
                buffer[n] = '\0';
89
 
        } else {
 
91
        }
 
92
        else {
90
93
                buffer[count-1] = '\0';
91
94
        }
92
95
        
112
115
        return n;
113
116
}
114
117
 
 
118
 
 
119
/* match pythons string escaping, assume double quotes - (")
 
120
 * TODO: should be used to create RNA animation paths.
 
121
 * TODO: support more fancy string escaping. current code is primitive
 
122
 *    this basically is an ascii version of PyUnicode_EncodeUnicodeEscape()
 
123
 *    which is a useful reference. */
 
124
size_t BLI_strescape(char *dst, const char *src, const size_t maxlen)
 
125
{
 
126
        size_t len= 0;
 
127
        while (len < maxlen) {
 
128
                switch(*src) {
 
129
                        case '\0':
 
130
                                goto escape_finish;
 
131
                        case '\\':
 
132
                        case '"':
 
133
 
 
134
                                /* less common but should also be support */
 
135
                        case '\t':
 
136
                        case '\n':
 
137
                        case '\r':
 
138
                                if (len + 1 <  maxlen) {
 
139
                                        *dst++ = '\\';
 
140
                                        len++;
 
141
                                }
 
142
                                else {
 
143
                                        /* not enough space to escape */
 
144
                                        break;
 
145
                                }
 
146
                                /* intentionally pass through */
 
147
                        default:
 
148
                                *dst = *src;
 
149
                }
 
150
                dst++;
 
151
                src++;
 
152
                len++;
 
153
        }
 
154
 
 
155
escape_finish:
 
156
 
 
157
        *dst= '\0';
 
158
 
 
159
        return len;
 
160
}
 
161
 
 
162
 
115
163
/* Makes a copy of the text within the "" that appear after some text 'blahblah'
116
164
 * i.e. for string 'pose["apples"]' with prefix 'pose[', it should grab "apples"
117
165
 * 
123
171
 */
124
172
char *BLI_getQuotedStr (const char *str, const char *prefix)
125
173
{
126
 
        int prefixLen = strlen(prefix);
 
174
        size_t prefixLen = strlen(prefix);
127
175
        char *startMatch, *endMatch;
128
176
        
129
177
        /* get the starting point (i.e. where prefix starts, and add prefixLen+1 to it to get be after the first " */
133
181
        endMatch= strchr(startMatch, '"'); // "  NOTE: this comment here is just so that my text editor still shows the functions ok...
134
182
        
135
183
        /* return the slice indicated */
136
 
        return BLI_strdupn(startMatch, (int)(endMatch-startMatch));
 
184
        return BLI_strdupn(startMatch, (size_t)(endMatch-startMatch));
137
185
}
138
186
 
139
 
/* Replaces all occurances of oldText with newText in str, returning a new string that doesn't 
140
 
 * contain the 'replaced' occurances.
 
187
/* Replaces all occurrences of oldText with newText in str, returning a new string that doesn't 
 
188
 * contain the 'replaced' occurrences.
141
189
 */
142
190
// A rather wasteful string-replacement utility, though this shall do for now...
143
191
// Feel free to replace this with an even safe + nicer alternative 
144
192
char *BLI_replacestr(char *str, const char *oldText, const char *newText)
145
193
{
146
194
        DynStr *ds= NULL;
147
 
        int lenOld= strlen(oldText);
 
195
        size_t lenOld= strlen(oldText);
148
196
        char *match;
149
197
        
150
198
        /* sanity checks */
183
231
                str += lenOld;
184
232
        }
185
233
        
186
 
        /* finish off and return a new string that has had all occurances of */
 
234
        /* finish off and return a new string that has had all occurrences of */
187
235
        if (ds) {
188
236
                char *newStr;
189
237
                
207
255
        }
208
256
209
257
 
210
 
int BLI_streq(const char *a, const char *b) 
211
 
{
212
 
        return (strcmp(a, b)==0);
213
 
}
214
 
 
215
258
int BLI_strcaseeq(const char *a, const char *b) 
216
259
{
217
260
        return (BLI_strcasecmp(a, b)==0);
239
282
}
240
283
 
241
284
 
242
 
int BLI_strcasecmp(const char *s1, const char *s2) {
 
285
int BLI_strcasecmp(const char *s1, const char *s2)
 
286
{
243
287
        int i;
244
288
 
245
289
        for (i=0; ; i++) {
248
292
 
249
293
                if (c1<c2) {
250
294
                        return -1;
251
 
                } else if (c1>c2) {
 
295
                }
 
296
                else if (c1>c2) {
252
297
                        return 1;
253
 
                } else if (c1==0) {
 
298
                }
 
299
                else if (c1==0) {
254
300
                        break;
255
301
                }
256
302
        }
258
304
        return 0;
259
305
}
260
306
 
261
 
int BLI_strncasecmp(const char *s1, const char *s2, int n) {
 
307
int BLI_strncasecmp(const char *s1, const char *s2, size_t len)
 
308
{
262
309
        int i;
263
310
 
264
 
        for (i=0; i<n; i++) {
 
311
        for (i=0; i<len; i++) {
265
312
                char c1 = tolower(s1[i]);
266
313
                char c2 = tolower(s2[i]);
267
314
 
268
315
                if (c1<c2) {
269
316
                        return -1;
270
 
                } else if (c1>c2) {
 
317
                }
 
318
                else if (c1>c2) {
271
319
                        return 1;
272
 
                } else if (c1==0) {
 
320
                }
 
321
                else if (c1==0) {
273
322
                        break;
274
323
                }
275
324
        }
281
330
int BLI_natstrcmp(const char *s1, const char *s2)
282
331
{
283
332
        int d1= 0, d2= 0;
284
 
        
 
333
 
285
334
        /* if both chars are numeric, to a strtol().
286
 
           then increase string deltas as long they are 
287
 
           numeric, else do a tolower and char compare */
288
 
        
289
 
        while(1) {
 
335
         * then increase string deltas as long they are 
 
336
         * numeric, else do a tolower and char compare */
 
337
 
 
338
        while (1) {
290
339
                char c1 = tolower(s1[d1]);
291
340
                char c2 = tolower(s2[d2]);
292
341
                
293
 
                if( isdigit(c1) && isdigit(c2) ) {
 
342
                if ( isdigit(c1) && isdigit(c2) ) {
294
343
                        int val1, val2;
295
344
                        
296
345
                        val1= (int)strtol(s1+d1, (char **)NULL, 10);
298
347
                        
299
348
                        if (val1<val2) {
300
349
                                return -1;
301
 
                        } else if (val1>val2) {
 
350
                        }
 
351
                        else if (val1>val2) {
302
352
                                return 1;
303
353
                        }
304
354
                        d1++;
305
 
                        while( isdigit(s1[d1]) )
 
355
                        while ( isdigit(s1[d1]) )
306
356
                                d1++;
307
357
                        d2++;
308
 
                        while( isdigit(s2[d2]) )
 
358
                        while ( isdigit(s2[d2]) )
309
359
                                d2++;
310
360
                        
311
361
                        c1 = tolower(s1[d1]);
312
362
                        c2 = tolower(s2[d2]);
313
363
                }
314
 
                
315
 
                if (c1<c2) {
316
 
                        return -1;
317
 
                } else if (c1>c2) {
318
 
                        return 1;
319
 
                } else if (c1==0) {
 
364
        
 
365
                /* first check for '.' so "foo.bar" comes before "foo 1.bar" */ 
 
366
                if (c1=='.' && c2!='.')
 
367
                        return -1;
 
368
                if (c1!='.' && c2=='.')
 
369
                        return 1;
 
370
                else if (c1<c2) {
 
371
                        return -1;
 
372
                }
 
373
                else if (c1>c2) {
 
374
                        return 1;
 
375
                }
 
376
                else if (c1==0) {
320
377
                        break;
321
378
                }
322
379
                d1++;
335
392
        
336
393
        if (hr) {
337
394
                sprintf(str, "%.2d:%.2d:%.2d.%.2d",hr,min,sec,hun);
338
 
        } else {
 
395
        }
 
396
        else {
339
397
                sprintf(str, "%.2d:%.2d.%.2d",min,sec,hun);
340
398
        }
341
399
        
348
406
        const char *end = memchr(str, '\0', maxlen);
349
407
        return end ? (size_t) (end - str) : maxlen;
350
408
}
 
409
 
 
410
void BLI_ascii_strtolower(char *str, int len)
 
411
{
 
412
        int i;
 
413
 
 
414
        for (i=0; i<len; i++)
 
415
                if (str[i] >= 'A' && str[i] <= 'Z')
 
416
                        str[i] += 'a' - 'A';
 
417
}
 
418
 
 
419
void BLI_ascii_strtoupper(char *str, int len)
 
420
{
 
421
        int i;
 
422
 
 
423
        for (i=0; i<len; i++)
 
424
                if (str[i] >= 'a' && str[i] <= 'z')
 
425
                        str[i] -= 'a' - 'A';
 
426
}
 
427