Mercurial > hg > dmlib
annotate src/dmstring.c @ 2079:9b6027d51f76
Move dm_strdup_fext() and dm_basefilename() to gfxconv, as they are only used there.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 10 Dec 2018 19:24:45 +0200 |
parents | eeddaf411083 |
children | 63dd0a611586 |
rev | line source |
---|---|
0 | 1 #include "dmlib.h" |
2 #include <stdarg.h> | |
3 | |
813 | 4 |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
5 /* 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
|
6 */ |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
7 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
|
8 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
9 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
|
10 assert(haystack != NULL); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
11 assert(needle != NULL); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
12 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
13 if (haystack == needle) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
14 return 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
15 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
16 while (*s1 && *s2) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
17 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
18 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
|
19 if (k != 0) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
20 return k; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
21 s1++; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
22 s2++; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
23 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
24 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
25 return 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
26 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
27 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
28 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
29 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
|
30 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
31 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
|
32 assert(haystack != NULL); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
33 assert(needle != NULL); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
34 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
35 if (haystack == needle) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
36 return 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
37 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
38 while (n > 0 && *s1 && *s2) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
39 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
40 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
|
41 if (k != 0) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
42 return k; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
43 s1++; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
44 s2++; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
45 n--; |
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 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
48 return 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
49 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
50 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
51 |
817 | 52 /* Check if end of the given string str matches needle |
53 * case-insensitively, return pointer to start of the match, | |
54 * if found, NULL otherwise. | |
55 */ | |
816 | 56 char *dm_strrcasecmp(char *str, const char *needle) |
57 { | |
58 if (str == NULL || needle == NULL) | |
59 return NULL; | |
60 | |
61 const size_t | |
62 slen = strlen(str), | |
63 nlen = strlen(needle); | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
836
diff
changeset
|
64 |
816 | 65 if (slen < nlen) |
66 return NULL; | |
67 | |
2076 | 68 if (dm_strcasecmp(str + slen - nlen, needle) == 0) |
69 return str + slen - nlen; | |
816 | 70 else |
71 return NULL; | |
72 } | |
73 | |
74 | |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
75 /* Implementation of strdup() with a NULL check |
0 | 76 */ |
77 char *dm_strdup(const char *s) | |
78 { | |
79 char *res; | |
80 if (s == NULL) | |
81 return NULL; | |
82 | |
83 if ((res = dmMalloc(strlen(s) + 1)) == NULL) | |
84 return NULL; | |
85 | |
86 strcpy(res, s); | |
87 return res; | |
88 } | |
89 | |
90 | |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
91 /* Implementation of strndup() with NULL check |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
92 */ |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
93 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
|
94 { |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
95 char *res; |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
96 if (str == NULL) |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
97 return NULL; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
98 |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
99 size_t len = strlen(str); |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
100 if (len > n) |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
101 len = n; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
102 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
103 if ((res = dmMalloc(len + 1)) == NULL) |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
104 return NULL; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
105 |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
106 memcpy(res, str, len); |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
107 res[len] = 0; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
108 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
109 return res; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
110 } |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
111 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
112 |
2027
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
113 /* 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
|
114 * 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
|
115 * NULL is returned. |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
116 */ |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
117 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
|
118 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
119 char *res; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
120 size_t start, end; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
121 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
122 if (len == 0) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
123 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
124 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
125 // 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
|
126 if (flags & DM_TRIM_START) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
127 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
|
128 else |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
129 start = 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
130 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
131 // 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
|
132 if (flags & DM_TRIM_END) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
133 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
|
134 else |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
135 end = len; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
136 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
137 // Allocate memory for result |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
138 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
|
139 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
140 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
141 len = end - start + 1; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
142 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
|
143 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
144 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
145 memcpy(res, src + start, len); |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
146 res[len] = 0; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
147 return res; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
148 } |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
149 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
150 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
151 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
|
152 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
153 if (src == NULL) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
154 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
155 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
156 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
|
157 } |
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 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
160 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
|
161 { |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
162 size_t len; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
163 if (src == NULL || n == 0) |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
164 return NULL; |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
165 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
166 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
|
167 |
750a7e125546
Add in several string helper functions from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
1884
diff
changeset
|
168 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
|
169 } |
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 |
0 | 172 /* Simulate a sprintf() that allocates memory |
173 */ | |
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
|
174 char *dm_strdup_vprintf_len(const char *fmt, va_list args, int *len) |
0 | 175 { |
176 int size = 64; | |
836 | 177 char *buf, *tmp; |
0 | 178 |
179 if ((buf = dmMalloc(size)) == NULL) | |
180 return NULL; | |
181 | |
182 while (1) | |
183 { | |
184 va_list ap; | |
185 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
|
186 *len = vsnprintf(buf, size, fmt, ap); |
0 | 187 va_end(ap); |
188 | |
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
|
189 if (*len > -1 && *len < size) |
0 | 190 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
|
191 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
|
192 size = *len + 1; |
0 | 193 else |
194 size *= 2; | |
195 | |
836 | 196 if ((tmp = dmRealloc(buf, size)) == NULL) |
197 { | |
198 dmFree(buf); | |
0 | 199 return NULL; |
836 | 200 } |
201 else | |
202 buf = tmp; | |
0 | 203 } |
204 } | |
205 | |
206 | |
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
|
207 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
|
208 { |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
209 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
|
210 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
|
211 } |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
212 |
47fe47f01fea
Implement dm_strdup_vprintf_len(const char *fmt, va_list args, int *len), which
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
213 |
0 | 214 char *dm_strdup_printf(const char *fmt, ...) |
215 { | |
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
|
216 int len; |
0 | 217 char *res; |
218 va_list ap; | |
219 | |
220 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
|
221 res = dm_strdup_vprintf_len(fmt, ap, &len); |
0 | 222 va_end(ap); |
223 | |
224 return res; | |
225 } |