Mercurial > hg > dmlib
annotate src/dmstring.c @ 2027:750a7e125546
Add in several string helper functions from th-libs.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 27 Nov 2018 11:31:10 +0200 |
parents | 47fe47f01fea |
children | eeddaf411083 |
rev | line source |
---|---|
0 | 1 #include "dmlib.h" |
2 #include <stdarg.h> | |
3 | |
813 | 4 |
5 /* Returns the filename and path without the last filename extension, | |
6 * E.g. everything before the last '.', if any. | |
7 */ | |
8 char *dm_basefilename(const char *filename) | |
9 { | |
10 char *tmp, *fext; | |
11 | |
12 if (filename == NULL || | |
13 (tmp = dm_strdup(filename)) == NULL) | |
14 return NULL; | |
15 | |
16 if ((fext = strrchr(tmp, '.')) != NULL) | |
17 { | |
18 char *fpath = strrchr(tmp, DM_DIR_SEPARATOR); | |
19 if (fpath == NULL || (fpath != NULL && fext > fpath)) | |
20 *fext = 0; | |
21 } | |
22 | |
23 return tmp; | |
24 } | |
25 | |
26 | |
27 /* Replace filename extension based on format pattern. | |
28 * Usage: res = dm_strdup_fext(orig_filename, "foo_%s.cmp"); | |
29 */ | |
30 char *dm_strdup_fext(const char *filename, const char *fmt) | |
31 { | |
32 char *result, *tmp; | |
33 | |
34 if ((tmp = dm_basefilename(filename)) == NULL) | |
35 return NULL; | |
36 | |
37 result = dm_strdup_printf(fmt, tmp); | |
38 | |
39 dmFree(tmp); | |
40 | |
41 return result; | |
42 } | |
43 | |
44 | |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
45 /* Compare two strings ignoring case [strcasecmp, strncasecmp] |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
46 */ |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
47 int dm_strcasecmp(const char *haystack, const char *needle) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
48 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
49 const char *s1 = haystack, *s2 = needle; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
50 assert(haystack != NULL); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
51 assert(needle != NULL); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
52 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
53 if (haystack == needle) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
54 return 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
55 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
56 while (*s1 && *s2) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
57 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
58 int k = tolower(*s1) - tolower(*s2); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
59 if (k != 0) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
60 return k; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
61 s1++; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
62 s2++; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
63 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
64 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
65 return 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
66 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
67 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
68 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
69 int dm_strncasecmp(const char *haystack, const char *needle, size_t n) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
70 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
71 const char *s1 = haystack, *s2 = needle; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
72 assert(haystack != NULL); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
73 assert(needle != NULL); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
74 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
75 if (haystack == needle) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
76 return 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
77 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
78 while (n > 0 && *s1 && *s2) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
79 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
80 int k = tolower(*s1) - tolower(*s2); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
81 if (k != 0) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
82 return k; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
83 s1++; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
84 s2++; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
85 n--; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
86 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
87 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
88 return 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
89 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
90 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
91 |
817 | 92 /* Check if end of the given string str matches needle |
93 * case-insensitively, return pointer to start of the match, | |
94 * if found, NULL otherwise. | |
95 */ | |
816 | 96 char *dm_strrcasecmp(char *str, const char *needle) |
97 { | |
98 if (str == NULL || needle == NULL) | |
99 return NULL; | |
100 | |
101 const size_t | |
102 slen = strlen(str), | |
103 nlen = strlen(needle); | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
836
diff
changeset
|
104 |
816 | 105 if (slen < nlen) |
106 return NULL; | |
107 | |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
108 if (dm_strcasecmp(str - nlen - 1, needle) == 0) |
816 | 109 return str - nlen - 1; |
110 else | |
111 return NULL; | |
112 } | |
113 | |
114 | |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
115 /* Implementation of strdup() with a NULL check |
0 | 116 */ |
117 char *dm_strdup(const char *s) | |
118 { | |
119 char *res; | |
120 if (s == NULL) | |
121 return NULL; | |
122 | |
123 if ((res = dmMalloc(strlen(s) + 1)) == NULL) | |
124 return NULL; | |
125 | |
126 strcpy(res, s); | |
127 return res; | |
128 } | |
129 | |
130 | |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
131 /* Implementation of strndup() with NULL check |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
132 */ |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
133 char *dm_strndup(const char *str, const size_t n) |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
134 { |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
135 char *res; |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
136 if (str == NULL) |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
137 return NULL; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
138 |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
139 size_t len = strlen(str); |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
140 if (len > n) |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
141 len = n; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
142 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
143 if ((res = dmMalloc(len + 1)) == NULL) |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
144 return NULL; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
145 |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
146 memcpy(res, str, len); |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
147 res[len] = 0; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
148 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
149 return res; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
150 } |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
151 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
152 |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
153 /* Like strdup, but trims whitespace from the string according to specified flags. |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
154 * See DM_TRIM_* in dmlib.h. If the resulting string would be empty (length 0), |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
155 * NULL is returned. |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
156 */ |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
157 static char * dm_strdup_trim_do(const char *src, size_t len, const int flags) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
158 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
159 char *res; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
160 size_t start, end; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
161 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
162 if (len == 0) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
163 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
164 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
165 // Trim start: find first non-whitespace character |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
166 if (flags & DM_TRIM_START) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
167 for (start = 0; start < len && isspace(src[start]); start++); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
168 else |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
169 start = 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
170 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
171 // Trim end: find last non-whitespace character |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
172 if (flags & DM_TRIM_END) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
173 for (end = len - 1; end > start && isspace(src[end]); end--); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
174 else |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
175 end = len; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
176 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
177 // Allocate memory for result |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
178 if (src[end] == 0 || isspace(src[end])) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
179 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
180 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
181 len = end - start + 1; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
182 if ((res = dmMalloc(len + 1)) == NULL) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
183 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
184 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
185 memcpy(res, src + start, len); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
186 res[len] = 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
187 return res; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
188 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
189 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
190 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
191 char *dm_strdup_trim(const char *src, const int flags) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
192 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
193 if (src == NULL) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
194 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
195 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
196 return dm_strdup_trim_do(src, strlen(src), flags); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
197 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
198 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
199 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
200 char *dm_strndup_trim(const char *src, const size_t n, const int flags) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
201 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
202 size_t len; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
203 if (src == NULL || n == 0) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
204 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
205 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
206 for (len = 0; len < n && src[len]; len++); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
207 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
208 return dm_strdup_trim_do(src, len, flags); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
209 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
210 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
211 |
0 | 212 /* Simulate a sprintf() that allocates memory |
213 */ | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
214 char *dm_strdup_vprintf_len(const char *fmt, va_list args, int *len) |
0 | 215 { |
216 int size = 64; | |
836 | 217 char *buf, *tmp; |
0 | 218 |
219 if ((buf = dmMalloc(size)) == NULL) | |
220 return NULL; | |
221 | |
222 while (1) | |
223 { | |
224 va_list ap; | |
225 va_copy(ap, args); | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
226 *len = vsnprintf(buf, size, fmt, ap); |
0 | 227 va_end(ap); |
228 | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
229 if (*len > -1 && *len < size) |
0 | 230 return buf; |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
231 if (*len > -1) |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
232 size = *len + 1; |
0 | 233 else |
234 size *= 2; | |
235 | |
836 | 236 if ((tmp = dmRealloc(buf, size)) == NULL) |
237 { | |
238 dmFree(buf); | |
0 | 239 return NULL; |
836 | 240 } |
241 else | |
242 buf = tmp; | |
0 | 243 } |
244 } | |
245 | |
246 | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
247 char *dm_strdup_vprintf(const char *fmt, va_list args) |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
248 { |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
249 int len; |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
250 return dm_strdup_vprintf_len(fmt, args, &len); |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
251 } |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
252 |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
253 |
0 | 254 char *dm_strdup_printf(const char *fmt, ...) |
255 { | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
256 int len; |
0 | 257 char *res; |
258 va_list ap; | |
259 | |
260 va_start(ap, fmt); | |
1884
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
261 res = dm_strdup_vprintf_len(fmt, ap, &len); |
0 | 262 va_end(ap); |
263 | |
264 return res; | |
265 } |