1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
WDL - wdlcstring.h
Copyright (C) 2005 and later, Cockos Incorporated
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/*
C string manipulation utilities -- [v]snprintf for Win32, also snprintf_append, lstrcatn, etc
*/
#ifndef _WDL_CSTRING_H_
#define _WDL_CSTRING_H_
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
#include "wdltypes.h"
#ifdef _WDL_CSTRING_IMPL_ONLY_
#ifdef _WDL_CSTRING_IF_ONLY_
#undef _WDL_CSTRING_IF_ONLY_
#endif
#define _WDL_CSTRING_PREFIX
#else
#define _WDL_CSTRING_PREFIX static WDL_STATICFUNC_UNUSED
#endif
#if defined(_WIN32) && defined(_MSC_VER)
// provide snprintf()/vsnprintf() for win32 -- note that these have no way of knowing
// what the amount written was, code should(must) be written to not depend on this.
#ifdef snprintf
#undef snprintf
#endif
#define snprintf WDL_snprintf
#ifdef vsnprintf
#undef vsnprintf
#endif
#define vsnprintf WDL_vsnprintf
#endif // win32 snprintf/vsnprintf
// use wdlcstring.h's lstrcpyn_safe rather than the real lstrcpyn.
#ifdef _WIN32
#ifdef lstrcpyn
#undef lstrcpyn
#endif
#define lstrcpyn lstrcpyn_safe
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WDL_CSTRING_IF_ONLY_
void lstrcpyn_safe(char *o, const char *in, INT_PTR count);
void lstrcatn(char *o, const char *in, INT_PTR count);
void WDL_VARARG_WARN(printf,3,4) snprintf_append(char *o, INT_PTR count, const char *format, ...);
void vsnprintf_append(char *o, INT_PTR count, const char *format, va_list va);
const char *WDL_get_filepart(const char *str); // returns whole string if no dir chars
const char *WDL_get_fileext(const char *str); // returns ".ext" or end of string "" if no extension
char *WDL_remove_fileext(char *str); // returns pointer to "ext" if ".ext" was removed (zero-d dot), or NULL
char WDL_remove_filepart(char *str); // returns dir character that was zeroed, or 0 if new string is empty
int WDL_remove_trailing_dirchars(char *str); // returns trailing dirchar count removed, will not convert "/" into ""
size_t WDL_remove_trailing_crlf(char *str); // returns new length
#if defined(_WIN32) && defined(_MSC_VER)
void WDL_vsnprintf(char *o, size_t count, const char *format, va_list args);
void WDL_VARARG_WARN(printf,3,4) WDL_snprintf(char *o, size_t count, const char *format, ...);
#endif
int WDL_strcmp_logical(const char *s1, const char *s2, int case_sensitive);
#else
#if defined(_WIN32) && defined(_MSC_VER)
_WDL_CSTRING_PREFIX void WDL_vsnprintf(char *o, size_t count, const char *format, va_list args)
{
if (count>0)
{
int rv;
o[0]=0;
rv=_vsnprintf(o,count,format,args); // returns -1 if over, and does not null terminate, ugh
if (rv < 0 || rv>=(int)count-1) o[count-1]=0;
}
}
_WDL_CSTRING_PREFIX void WDL_VARARG_WARN(printf,3,4) WDL_snprintf(char *o, size_t count, const char *format, ...)
{
if (count>0)
{
int rv;
va_list va;
va_start(va,format);
o[0]=0;
rv=_vsnprintf(o,count,format,va); // returns -1 if over, and does not null terminate, ugh
va_end(va);
if (rv < 0 || rv>=(int)count-1) o[count-1]=0;
}
}
#endif
_WDL_CSTRING_PREFIX void lstrcpyn_safe(char *o, const char *in, INT_PTR count)
{
if (count>0)
{
while (--count>0 && *in) *o++ = *in++;
*o=0;
}
}
_WDL_CSTRING_PREFIX void lstrcatn(char *o, const char *in, INT_PTR count)
{
if (count>0)
{
while (*o) { if (--count < 1) return; o++; }
while (--count>0 && *in) *o++ = *in++;
*o=0;
}
}
_WDL_CSTRING_PREFIX const char *WDL_get_filepart(const char *str) // returns whole string if no dir chars
{
const char *p = str;
while (*p) p++;
while (p >= str && !WDL_IS_DIRCHAR(*p)) --p;
return p + 1;
}
_WDL_CSTRING_PREFIX const char *WDL_get_fileext(const char *str) // returns ".ext" or end of string "" if no extension
{
const char *p=str, *ep;
while (*p) p++;
ep = p;
while (p >= str && !WDL_IS_DIRCHAR(*p))
{
if (*p == '.') return p;
--p;
}
return ep;
}
_WDL_CSTRING_PREFIX char *WDL_remove_fileext(char *str) // returns pointer to "ext" if ".ext" was removed (zero-d dot), or NULL
{
char *p=str;
while (*p) p++;
while (p >= str && !WDL_IS_DIRCHAR(*p))
{
if (*p == '.')
{
*p = 0;
return p+1;
}
--p;
}
return NULL;
}
_WDL_CSTRING_PREFIX char WDL_remove_filepart(char *str) // returns dir character that was zeroed, or 0 if new string is empty
{
char *p=str;
while (*p) p++;
while (p >= str)
{
char c = *p;
if (WDL_IS_DIRCHAR(c))
{
*p = 0;
return c;
}
--p;
}
str[0] = 0;
return 0;
}
_WDL_CSTRING_PREFIX int WDL_remove_trailing_dirchars(char *str) // returns trailing dirchar count removed
{
int cnt = 0;
char *p=str;
while (*p) p++;
while (p > str+1 && WDL_IS_DIRCHAR(p[-1]))
{
cnt++;
p--;
}
*p = 0;
return cnt;
}
_WDL_CSTRING_PREFIX size_t WDL_remove_trailing_crlf(char *str) // returns new length
{
char *p=str;
while (*p) p++;
while (p > str && (p[-1] == '\r' || p[-1] == '\n')) p--;
*p = 0;
return p-str;
}
_WDL_CSTRING_PREFIX void WDL_VARARG_WARN(printf,3,4) snprintf_append(char *o, INT_PTR count, const char *format, ...)
{
if (count>0)
{
va_list va;
while (*o) { if (--count < 1) return; o++; }
va_start(va,format);
vsnprintf(o,count,format,va);
va_end(va);
}
}
_WDL_CSTRING_PREFIX void vsnprintf_append(char *o, INT_PTR count, const char *format, va_list va)
{
if (count>0)
{
while (*o) { if (--count < 1) return; o++; }
vsnprintf(o,count,format,va);
}
}
_WDL_CSTRING_PREFIX int WDL_strcmp_logical(const char *s1, const char *s2, int case_sensitive)
{
// also exists as WDL_LogicalSortStringKeyedArray::_cmpstr()
char lastNonZeroChar=0;
// last matching character, updated if not 0. this allows us to track whether
// we are inside of a number with the same leading digits
for (;;)
{
char c1=*s1++, c2=*s2++;
if (!c1) return c1-c2;
if (c1!=c2)
{
if (c1 >= '0' && c1 <= '9' && c2 >= '0' && c2 <= '9')
{
int lzdiff=0, cnt=0;
if (lastNonZeroChar < '1' || lastNonZeroChar > '9')
{
while (c1 == '0') { c1=*s1++; lzdiff--; }
while (c2 == '0') { c2=*s2++; lzdiff++; } // lzdiff = lz2-lz1, more leading 0s = earlier in list
}
for (;;)
{
if (c1 >= '0' && c1 <= '9')
{
if (c2 < '0' || c2 > '9') return 1;
c1=s1[cnt];
c2=s2[cnt++];
}
else
{
if (c2 >= '0' && c2 <= '9') return -1;
break;
}
}
s1--;
s2--;
while (cnt--)
{
const int d = *s1++ - *s2++;
if (d) return d;
}
if (lzdiff) return lzdiff;
}
else
{
if (!case_sensitive)
{
if (c1>='a' && c1<='z') c1+='A'-'a';
if (c2>='a' && c2<='z') c2+='A'-'a';
}
if (c1 != c2) return c1-c2;
}
}
else if (c1 != '0') lastNonZeroChar=c1;
}
}
#endif
#ifdef __cplusplus
};
#endif
#undef _WDL_CSTRING_PREFIX
#endif
|