~ubuntu-branches/ubuntu/saucy/blender/saucy-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
#include "BLI_dynstr.h"
42
42
#include "BLI_string.h"
43
43
 
 
44
#include "BLI_utildefines.h"
 
45
 
44
46
char *BLI_strdupn(const char *str, const size_t len)
45
47
{
46
 
        char *n= MEM_mallocN(len+1, "strdup");
 
48
        char *n = MEM_mallocN(len + 1, "strdup");
47
49
        memcpy(n, str, len);
48
 
        n[len]= '\0';
 
50
        n[len] = '\0';
49
51
        
50
52
        return n;
51
53
}
54
56
        return BLI_strdupn(str, strlen(str));
55
57
}
56
58
 
57
 
char *BLI_strdupcat(const char *str1, const char *str2)
 
59
char *BLI_strdupcat(const char *__restrict str1, const char *__restrict str2)
58
60
{
59
61
        size_t len;
60
62
        char *n;
61
63
        
62
 
        len= strlen(str1)+strlen(str2);
63
 
        n= MEM_mallocN(len+1, "strdupcat");
 
64
        len = strlen(str1) + strlen(str2);
 
65
        n = MEM_mallocN(len + 1, "strdupcat");
64
66
        strcpy(n, str1);
65
67
        strcat(n, str2);
66
68
        
67
69
        return n;
68
70
}
69
71
 
70
 
char *BLI_strncpy(char *dst, const char *src, const size_t maxncpy)
 
72
char *BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
71
73
{
72
 
        size_t srclen= strlen(src);
73
 
        size_t cpylen= (srclen>(maxncpy-1))?(maxncpy-1):srclen;
 
74
        size_t srclen = strlen(src);
 
75
        size_t cpylen = (srclen > (maxncpy - 1)) ? (maxncpy - 1) : srclen;
 
76
        BLI_assert(maxncpy != 0);
74
77
        
75
78
        memcpy(dst, src, cpylen);
76
 
        dst[cpylen]= '\0';
 
79
        dst[cpylen] = '\0';
77
80
        
78
81
        return dst;
79
82
}
80
83
 
81
 
size_t BLI_snprintf(char *buffer, size_t count, const char *format, ...)
 
84
size_t BLI_vsnprintf(char *__restrict buffer, size_t count, const char *__restrict format, va_list arg)
82
85
{
83
86
        size_t n;
84
 
        va_list arg;
85
 
 
86
 
        va_start(arg, format);
 
87
 
 
88
        BLI_assert(buffer != NULL);
 
89
        BLI_assert(count > 0);
 
90
        BLI_assert(format != NULL);
 
91
 
87
92
        n = vsnprintf(buffer, count, format, arg);
88
 
        
 
93
 
89
94
        if (n != -1 && n < count) {
90
95
                buffer[n] = '\0';
91
96
        }
92
97
        else {
93
 
                buffer[count-1] = '\0';
 
98
                buffer[count - 1] = '\0';
94
99
        }
95
 
        
 
100
 
 
101
        return n;
 
102
}
 
103
 
 
104
size_t BLI_snprintf(char *__restrict buffer, size_t count, const char *__restrict format, ...)
 
105
{
 
106
        size_t n;
 
107
        va_list arg;
 
108
 
 
109
        va_start(arg, format);
 
110
        n = BLI_vsnprintf(buffer, count, format, arg);
96
111
        va_end(arg);
 
112
 
97
113
        return n;
98
114
}
99
115
 
100
 
char *BLI_sprintfN(const char *format, ...)
 
116
char *BLI_sprintfN(const char *__restrict format, ...)
101
117
{
102
118
        DynStr *ds;
103
119
        va_list arg;
104
120
        char *n;
105
121
 
 
122
        BLI_assert(format != NULL);
 
123
 
106
124
        va_start(arg, format);
107
125
 
108
 
        ds= BLI_dynstr_new();
 
126
        ds = BLI_dynstr_new();
109
127
        BLI_dynstr_vappendf(ds, format, arg);
110
 
        n= BLI_dynstr_get_cstring(ds);
 
128
        n = BLI_dynstr_get_cstring(ds);
111
129
        BLI_dynstr_free(ds);
112
130
 
113
131
        va_end(arg);
121
139
 * TODO: support more fancy string escaping. current code is primitive
122
140
 *    this basically is an ascii version of PyUnicode_EncodeUnicodeEscape()
123
141
 *    which is a useful reference. */
124
 
size_t BLI_strescape(char *dst, const char *src, const size_t maxlen)
 
142
size_t BLI_strescape(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
125
143
{
126
 
        size_t len= 0;
127
 
        while (len < maxlen) {
128
 
                switch(*src) {
 
144
        size_t len = 0;
 
145
 
 
146
        BLI_assert(maxncpy != 0);
 
147
 
 
148
        while (len < maxncpy) {
 
149
                switch (*src) {
129
150
                        case '\0':
130
151
                                goto escape_finish;
131
152
                        case '\\':
132
153
                        case '"':
133
154
 
134
 
                                /* less common but should also be support */
 
155
                        /* less common but should also be support */
135
156
                        case '\t':
136
157
                        case '\n':
137
158
                        case '\r':
138
 
                                if (len + 1 <  maxlen) {
 
159
                                if (len + 1 < maxncpy) {
139
160
                                        *dst++ = '\\';
140
161
                                        len++;
141
162
                                }
143
164
                                        /* not enough space to escape */
144
165
                                        break;
145
166
                                }
146
 
                                /* intentionally pass through */
 
167
                        /* intentionally pass through */
147
168
                        default:
148
169
                                *dst = *src;
149
170
                }
154
175
 
155
176
escape_finish:
156
177
 
157
 
        *dst= '\0';
 
178
        *dst = '\0';
158
179
 
159
180
        return len;
160
181
}
163
184
/* Makes a copy of the text within the "" that appear after some text 'blahblah'
164
185
 * i.e. for string 'pose["apples"]' with prefix 'pose[', it should grab "apples"
165
186
 * 
166
 
 *      - str: is the entire string to chop
 
187
 *  - str: is the entire string to chop
167
188
 *      - prefix: is the part of the string to leave out 
168
189
 *
169
190
 * Assume that the strings returned must be freed afterwards, and that the inputs will contain 
170
191
 * data we want...
 
192
 *
 
193
 * TODO, return the offset and a length so as to avoid doing an allocation.
171
194
 */
172
 
char *BLI_getQuotedStr (const char *str, const char *prefix)
 
195
char *BLI_str_quoted_substrN(const char *__restrict str, const char *__restrict prefix)
173
196
{
174
197
        size_t prefixLen = strlen(prefix);
175
198
        char *startMatch, *endMatch;
176
199
        
177
200
        /* get the starting point (i.e. where prefix starts, and add prefixLen+1 to it to get be after the first " */
178
 
        startMatch= strstr(str, prefix) + prefixLen + 1;
179
 
        
180
 
        /* get the end point (i.e. where the next occurance of " is after the starting point) */
181
 
        endMatch= strchr(startMatch, '"'); // "  NOTE: this comment here is just so that my text editor still shows the functions ok...
182
 
        
183
 
        /* return the slice indicated */
184
 
        return BLI_strdupn(startMatch, (size_t)(endMatch-startMatch));
 
201
        startMatch = strstr(str, prefix) + prefixLen + 1;
 
202
        if (startMatch) {
 
203
                /* get the end point (i.e. where the next occurance of " is after the starting point) */
 
204
                endMatch = strchr(startMatch, '"'); /* "  NOTE: this comment here is just so that my text editor still shows the functions ok... */
 
205
                
 
206
                if (endMatch)
 
207
                        /* return the slice indicated */
 
208
                        return BLI_strdupn(startMatch, (size_t)(endMatch - startMatch));
 
209
        }
 
210
        return BLI_strdupn("", 0);
185
211
}
186
212
 
187
213
/* Replaces all occurrences of oldText with newText in str, returning a new string that doesn't 
188
214
 * contain the 'replaced' occurrences.
189
215
 */
190
 
// A rather wasteful string-replacement utility, though this shall do for now...
191
 
// Feel free to replace this with an even safe + nicer alternative 
192
 
char *BLI_replacestr(char *str, const char *oldText, const char *newText)
 
216
 
 
217
/* A rather wasteful string-replacement utility, though this shall do for now...
 
218
 * Feel free to replace this with an even safe + nicer alternative */
 
219
char *BLI_replacestr(char *__restrict str, const char *__restrict oldText, const char *__restrict newText)
193
220
{
194
 
        DynStr *ds= NULL;
195
 
        size_t lenOld= strlen(oldText);
 
221
        DynStr *ds = NULL;
 
222
        size_t lenOld = strlen(oldText);
196
223
        char *match;
197
224
        
198
225
        /* sanity checks */
199
 
        if ((str == NULL) || (str[0]==0))
 
226
        if ((str == NULL) || (str[0] == 0))
200
227
                return NULL;
201
 
        else if ((oldText == NULL) || (newText == NULL) || (oldText[0]==0))
 
228
        else if ((oldText == NULL) || (newText == NULL) || (oldText[0] == 0))
202
229
                return BLI_strdup(str);
203
230
        
204
231
        /* while we can still find a match for the old substring that we're searching for, 
207
234
        while ( (match = strstr(str, oldText)) ) {
208
235
                /* the assembly buffer only gets created when we actually need to rebuild the string */
209
236
                if (ds == NULL)
210
 
                        ds= BLI_dynstr_new();
 
237
                        ds = BLI_dynstr_new();
211
238
                        
212
239
                /* if the match position does not match the current position in the string, 
213
240
                 * copy the text up to this position and advance the current position in the string
216
243
                        /* replace the token at the 'match' position with \0 so that the copied string will be ok,
217
244
                         * add the segment of the string from str to match to the buffer, then restore the value at match
218
245
                         */
219
 
                        match[0]= 0;
 
246
                        match[0] = 0;
220
247
                        BLI_dynstr_append(ds, str);
221
 
                        match[0]= oldText[0];
 
248
                        match[0] = oldText[0];
222
249
                        
223
250
                        /* now our current position should be set on the start of the match */
224
 
                        str= match;
 
251
                        str = match;
225
252
                }
226
253
                
227
254
                /* add the replacement text to the accumulation buffer */
242
269
                        BLI_dynstr_append(ds, str);
243
270
                
244
271
                /* convert to new c-string (MEM_malloc'd), and free the buffer */
245
 
                newStr= BLI_dynstr_get_cstring(ds);
 
272
                newStr = BLI_dynstr_get_cstring(ds);
246
273
                BLI_dynstr_free(ds);
247
274
                
248
275
                return newStr;
257
284
 
258
285
int BLI_strcaseeq(const char *a, const char *b) 
259
286
{
260
 
        return (BLI_strcasecmp(a, b)==0);
 
287
        return (BLI_strcasecmp(a, b) == 0);
261
288
}
262
289
 
263
290
/* strcasestr not available in MSVC */
267
294
        register size_t len;
268
295
        
269
296
        if ((c = *find++) != 0) {
270
 
                c= tolower(c);
 
297
                c = tolower(c);
271
298
                len = strlen(find);
272
299
                do {
273
300
                        do {
274
301
                                if ((sc = *s++) == 0)
275
302
                                        return (NULL);
276
 
                                sc= tolower(sc);
 
303
                                sc = tolower(sc);
277
304
                        } while (sc != c);
278
305
                } while (BLI_strncasecmp(s, find, len) != 0);
279
306
                s--;
284
311
 
285
312
int BLI_strcasecmp(const char *s1, const char *s2)
286
313
{
287
 
        int i;
288
 
 
289
 
        for (i=0; ; i++) {
290
 
                char c1 = tolower(s1[i]);
291
 
                char c2 = tolower(s2[i]);
292
 
 
293
 
                if (c1<c2) {
 
314
        register int i;
 
315
        register char c1, c2;
 
316
 
 
317
        for (i = 0;; i++) {
 
318
                c1 = tolower(s1[i]);
 
319
                c2 = tolower(s2[i]);
 
320
 
 
321
                if (c1 < c2) {
294
322
                        return -1;
295
323
                }
296
 
                else if (c1>c2) {
 
324
                else if (c1 > c2) {
297
325
                        return 1;
298
326
                }
299
 
                else if (c1==0) {
 
327
                else if (c1 == 0) {
300
328
                        break;
301
329
                }
302
330
        }
306
334
 
307
335
int BLI_strncasecmp(const char *s1, const char *s2, size_t len)
308
336
{
309
 
        int i;
310
 
 
311
 
        for (i=0; i<len; i++) {
312
 
                char c1 = tolower(s1[i]);
313
 
                char c2 = tolower(s2[i]);
314
 
 
315
 
                if (c1<c2) {
 
337
        register size_t i;
 
338
        register char c1, c2;
 
339
 
 
340
        for (i = 0; i < len; i++) {
 
341
                c1 = tolower(s1[i]);
 
342
                c2 = tolower(s2[i]);
 
343
 
 
344
                if (c1 < c2) {
316
345
                        return -1;
317
346
                }
318
 
                else if (c1>c2) {
 
347
                else if (c1 > c2) {
319
348
                        return 1;
320
349
                }
321
 
                else if (c1==0) {
 
350
                else if (c1 == 0) {
322
351
                        break;
323
352
                }
324
353
        }
329
358
/* natural string compare, keeping numbers in order */
330
359
int BLI_natstrcmp(const char *s1, const char *s2)
331
360
{
332
 
        int d1= 0, d2= 0;
 
361
        register int d1 = 0, d2 = 0;
 
362
        register char c1, c2;
333
363
 
334
364
        /* if both chars are numeric, to a strtol().
335
365
         * then increase string deltas as long they are 
336
366
         * numeric, else do a tolower and char compare */
337
367
 
338
368
        while (1) {
339
 
                char c1 = tolower(s1[d1]);
340
 
                char c2 = tolower(s2[d2]);
 
369
                c1 = tolower(s1[d1]);
 
370
                c2 = tolower(s2[d2]);
341
371
                
342
 
                if ( isdigit(c1) && isdigit(c2) ) {
 
372
                if (isdigit(c1) && isdigit(c2) ) {
343
373
                        int val1, val2;
344
374
                        
345
 
                        val1= (int)strtol(s1+d1, (char **)NULL, 10);
346
 
                        val2= (int)strtol(s2+d2, (char **)NULL, 10);
 
375
                        val1 = (int)strtol(s1 + d1, (char **)NULL, 10);
 
376
                        val2 = (int)strtol(s2 + d2, (char **)NULL, 10);
347
377
                        
348
 
                        if (val1<val2) {
 
378
                        if (val1 < val2) {
349
379
                                return -1;
350
380
                        }
351
 
                        else if (val1>val2) {
 
381
                        else if (val1 > val2) {
352
382
                                return 1;
353
383
                        }
354
384
                        d1++;
355
 
                        while ( isdigit(s1[d1]) )
 
385
                        while (isdigit(s1[d1]) )
356
386
                                d1++;
357
387
                        d2++;
358
 
                        while ( isdigit(s2[d2]) )
 
388
                        while (isdigit(s2[d2]) )
359
389
                                d2++;
360
390
                        
361
391
                        c1 = tolower(s1[d1]);
362
392
                        c2 = tolower(s2[d2]);
363
393
                }
364
394
        
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) {
 
395
                /* first check for '.' so "foo.bar" comes before "foo 1.bar" */
 
396
                if (c1 == '.' && c2 != '.')
 
397
                        return -1;
 
398
                if (c1 != '.' && c2 == '.')
 
399
                        return 1;
 
400
                else if (c1 < c2) {
 
401
                        return -1;
 
402
                }
 
403
                else if (c1 > c2) {
 
404
                        return 1;
 
405
                }
 
406
                else if (c1 == 0) {
377
407
                        break;
378
408
                }
379
409
                d1++;
385
415
void BLI_timestr(double _time, char *str)
386
416
{
387
417
        /* format 00:00:00.00 (hr:min:sec) string has to be 12 long */
388
 
        int  hr= ( (int)  _time) / (60*60);
389
 
        int min= (((int)  _time) / 60 ) % 60;
390
 
        int sec= ( (int) (_time)) % 60;
391
 
        int hun= ( (int) (_time   * 100.0)) % 100;
 
418
        int  hr = ( (int)  _time) / (60 * 60);
 
419
        int min = (((int)  _time) / 60 ) % 60;
 
420
        int sec = ( (int) (_time)) % 60;
 
421
        int hun = ( (int) (_time   * 100.0)) % 100;
392
422
        
393
423
        if (hr) {
394
 
                sprintf(str, "%.2d:%.2d:%.2d.%.2d",hr,min,sec,hun);
 
424
                sprintf(str, "%.2d:%.2d:%.2d.%.2d", hr, min, sec, hun);
395
425
        }
396
426
        else {
397
 
                sprintf(str, "%.2d:%.2d.%.2d",min,sec,hun);
 
427
                sprintf(str, "%.2d:%.2d.%.2d", min, sec, hun);
398
428
        }
399
429
        
400
 
        str[11]=0;
 
430
        str[11] = 0;
401
431
}
402
432
 
403
433
/* determine the length of a fixed-size string */
404
 
size_t BLI_strnlen(const char *str, size_t maxlen)
 
434
size_t BLI_strnlen(const char *str, const size_t maxlen)
405
435
{
406
436
        const char *end = memchr(str, '\0', maxlen);
407
437
        return end ? (size_t) (end - str) : maxlen;
408
438
}
409
439
 
410
 
void BLI_ascii_strtolower(char *str, int len)
 
440
void BLI_ascii_strtolower(char *str, const size_t len)
411
441
{
412
 
        int i;
 
442
        size_t i;
413
443
 
414
 
        for (i=0; i<len; i++)
 
444
        for (i = 0; i < len; i++)
415
445
                if (str[i] >= 'A' && str[i] <= 'Z')
416
446
                        str[i] += 'a' - 'A';
417
447
}
418
448
 
419
 
void BLI_ascii_strtoupper(char *str, int len)
 
449
void BLI_ascii_strtoupper(char *str, const size_t len)
420
450
{
421
 
        int i;
 
451
        size_t i;
422
452
 
423
 
        for (i=0; i<len; i++)
 
453
        for (i = 0; i < len; i++)
424
454
                if (str[i] >= 'a' && str[i] <= 'z')
425
455
                        str[i] -= 'a' - 'A';
426
456
}
427