3 * various string, file, list operations.
6 * $Id: util.c 17433 2008-11-12 21:16:53Z blendix $
8 * ***** BEGIN GPL LICENSE BLOCK *****
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
25 * All rights reserved.
27 * The Original Code is: all of this file.
29 * Contributor(s): none yet.
31 * ***** END GPL LICENSE BLOCK *****
41 #include "MEM_guardedalloc.h"
43 #include "BLI_string.h"
45 char *BLI_strdupn(const char *str, int len) {
46 char *n= MEM_mallocN(len+1, "strdup");
52 char *BLI_strdup(const char *str) {
53 return BLI_strdupn(str, strlen(str));
56 char *BLI_strncpy(char *dst, const char *src, int maxncpy) {
57 int srclen= strlen(src);
58 int cpylen= (srclen>(maxncpy-1))?(maxncpy-1):srclen;
60 memcpy(dst, src, cpylen);
66 int BLI_snprintf(char *buffer, size_t count, const char *format, ...)
71 va_start(arg, format);
72 n = vsnprintf(buffer, count, format, arg);
74 if (n != -1 && n < count) {
77 buffer[count-1] = '\0';
84 int BLI_streq(char *a, char *b) {
85 return (strcmp(a, b)==0);
87 int BLI_strcaseeq(char *a, char *b) {
88 return (BLI_strcasecmp(a, b)==0);
91 /* strcasestr not available in MSVC */
92 char *BLI_strcasestr(const char *s, const char *find)
97 if ((c = *find++) != 0) {
102 if ((sc = *s++) == 0)
106 } while (BLI_strncasecmp(s, find, len) != 0);
113 int BLI_strcasecmp(const char *s1, const char *s2) {
117 char c1 = tolower(s1[i]);
118 char c2 = tolower(s2[i]);
132 int BLI_strncasecmp(const char *s1, const char *s2, int n) {
135 for (i=0; i<n; i++) {
136 char c1 = tolower(s1[i]);
137 char c2 = tolower(s2[i]);
151 void BLI_timestr(double _time, char *str)
153 /* format 00:00:00.00 (hr:min:sec) string has to be 12 long */
154 int hr= ( (int) _time) / (60*60);
155 int min= (((int) _time) / 60 ) % 60;
156 int sec= ( (int) (_time)) % 60;
157 int hun= ( (int) (_time * 100.0)) % 100;
160 sprintf(str, "%.2d:%.2d:%.2d.%.2d",hr,min,sec,hun);
162 sprintf(str, "%.2d:%.2d.%.2d",min,sec,hun);