summaryrefslogtreecommitdiff
path: root/support/dktools/dk4strw.ctr
blob: 8e0b560ab55fdac37feb690f10eda2851847e413 (plain)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
license		=	bsd



%%	header

/**	@file
	Additional string functions for
	wchar_t strings.

	Although some of the functions in this module duplicate
	functionality from the C runtime library on Windows, you should use
	the functions from this module.
	With DK4DK4_WIN_AVOID_CRT or DK4_WIN_DENY_CRT defined to non-zero
	these functions call Windows API functions like
	lstrcmpW() ... instead of CRT functions.
*/

#ifndef DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif

#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif

#ifndef DK4ERROR_H_INCLUDED
#include "dk4error.h"
#endif

#ifndef STDLIB_H_INCLUDED
#include <stdlib.h>
#define STDLIB_H_INCLUDED 1
#endif

#if DK4_HAVE_WCHAR_H
#ifndef WCHAR_H_INCLUDED
#include <wchar.h>
#define	WCHAR_H_INCLUDED 1
#endif
#endif


#ifdef __cplusplus
extern "C" {
#endif

/**	Copy string, check destination buffer size.
	
	CRT on Windows: Optional.
	@param	dst	Destination buffer address.
	@param	sz	Destination buffer size.
	@param	src	Source string.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dst or src is NULL or sz is 0,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if the dst buffer is too small.
*/
int
dk4strw_cpy_s(wchar_t *dst, size_t sz, const wchar_t *src, dk4_er_t *erp);

/**	Concatenate strings, check destination buffer size.
	
	CRT on Windows: Optional.
	@param	dst	Destination buffer address.
	@param	sz	Destination buffer size.
	@param	src	Source string.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dst or src is NULL or sz is 0,
	- DK4_E_MATH_OVERFLOW<br>
	  if a mathematical overflow occured in size calculation,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if the concatenated string doest not fit into the buffer.
*/
int
dk4strw_cat_s(wchar_t *dst, size_t sz, const wchar_t *src, dk4_er_t *erp);

/**	Copy characters within a string from right to left.
	This copy operation is intended for moving characters within
	one string from right to left, making the string shorter
	as is. This function does not check buffer size or report
	any error.
	For normal string copy operations, use the dk4strw_cpy_s()
	function instead.
	
	CRT on Windows: Not used.
	@param	dst	Destination buffer address.
	@param	src	Source string.
*/
void
dk4strw_cpy_to_left(wchar_t *dst, const wchar_t *src);

/**	Find string length.
	
	CRT on Windows: Optional, disabling CRT reduces functionality
	to strings with length fitting to int.
	@param	src	Source string.
	@return	String length.
*/
size_t
dk4strw_len(const wchar_t *src);

/**	Compare two strings.
	
	CRT on Windows: Optional.
	@param	s1	Left string, may be NULL.
	@param	s2	Right string, may be NULL.
	@return	1 if s1>s2, 0 if s1==s2, -1 if s1<s2.
*/
int
dk4strw_cmp(const wchar_t *s1, const wchar_t *s2);

/**	Compare two strings.

	CRT on Windows: Optional.
	@param	s1	Left string, may be NULL.
	@param	s2	Right string, may be NULL.
	@param	n	Maximum number of characters to compare.
	@return	1 if s1>s2, 0 if s1==s2, -1 if s1<s2.
*/
int
dk4strw_ncmp(const wchar_t *s1, const wchar_t *s2, size_t n);

/**	Compare two strings, case-insensitive comparison.
	
	CRT on Windows: Optional, disabling CRT probably degrades performance.
	@param	s1	Left string, may be NULL.
	@param	s2	Right string, may be NULL.
	@return	1 if s1>s2, 0 if s1==s2, -1 if s1<s2.
*/
int
dk4strw_casecmp(const wchar_t *s1, const wchar_t *s2);

/**	Compare two file path names.
	
	CRT on Windows: Optional.
	@param	s1	Left string, may be NULL.
	@param	s2	Right string, may be NULL.
	@return	1 if s1>s2, 0 if s1==s2, -1 if s1<s2.
*/
int
dk4strw_pathcmp(const wchar_t *s1, const wchar_t *s2);

/**	Find first occurance of character c in string s.
	
	CRT on Windows: Optional, disabling CRT degrades performance.
	@param	s	String to search for c.
	@param	c	Character to find.
	@return	Pointer to first occurance if found, NULL otherwise.
*/
wchar_t *
dk4strw_chr(const wchar_t *s, wchar_t c);

/**	Find last occurance of character c in string s.
	
	CRT on Windows: Optional, disabling CRT degrades performance.
	@param	s	String to search for c.
	@param	c	Character to find.
	@return	Pointer to last occurance if found, NULL otherwise.
*/
wchar_t *
dk4strw_rchr(const wchar_t *s, wchar_t c);

/**	Convert character to upper-case character.
	
	CRT on windows: Optional, disabling CRT degrades performance.
	@param	c	Character to convert.
	@return	Conversion result.
*/
wchar_t
dk4strw_toupper(wchar_t c);

/**	Convert character to lower-case character.
	
	CRT on windows: Optional, disabling CRT degrades performance.
	@param	c	Character to convert.
	@return	Conversion result.
*/
wchar_t
dk4strw_tolower(wchar_t c);

/**	Retrieve first token from *stringp.
	
	CRT on Windows: Optional, disabling CRT degrades performance.
	@param	stringp	Adress of string pointer.
	@param	delim	String containing the delimiter set,
			my be NULL to use the default delimiter
			set (space, tabulator, carriage return and newline).
	@return	Pointer to first token on success, NULL if no (further)
	token found.
*/
wchar_t *
dk4strw_sep(wchar_t **stringp, const wchar_t *delim);

/**	Find first token in string.
	
	CRT on Windows: Optional, disabling CRT degrades performance.
	@param	src	String to search for tokens.
	@param	delim	String containing the delimiter characters,
			may be NULL to use the default delimiter set.
	@return	Pointer to first token if found, NULL otherwise.
*/
wchar_t *
dk4strw_start(const wchar_t *src, const wchar_t *delim);

/**	Cut string after first token, find start of second token in string.
	
	CRT on Windows: Optional, disabling CRT degrades performance.
	@param	src	String to search for tokens,
			normally the result of dk4strw_start() or a
			previous dk4strw_next() call.
	@param	delim	String containing the delimiter characters,
			may be NULL to use the default delimiter set.
	@return	Pointer to second token if found, NULL otherwise.
*/
wchar_t *
dk4strw_next(wchar_t *src, const wchar_t *delim);

/**	Split a string into tokens.
	
	CRT on Windows: Optional, disabling CRT degrades performance.
	@param	dpp	Pointer to string pointer array.
	@param	szdpp	Size of destination string pointer array.
	@param	src	Source string to process.
	@param	delim	String containing delimiter characters,
			may be NULL to use the default delimiter set.
	@param	erp	Error report, may be NULL.
	@return	Number of tokens found on success, 0 if no token
	found or on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  for invalid function arguments,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  with dt.mem.nelem set to the number of tokens in string if the dpp
	  array is too short.
*/
size_t
dk4strw_tokenize(
  wchar_t **dpp, size_t szdpp, wchar_t *src, const wchar_t *delim, dk4_er_t *erp
);

/**	Normalize a string (remove leading and trailing delimiters,
	replace delimiter sequences by just one delimiter).
	
	CRT on Windows: Optional, disabling CRT degrades performance.
	@param	src	Buffer containing the string to normalize.
	@param	delim	String containing delimiter characters,
			may be NULL to use the default delimiter set.
*/
void
dk4strw_normalize(wchar_t *src, const wchar_t *delim);

/**	Find index of a string in an array of strings.
	
	CRT on Windows: Optional.
	@param	arr	Array of strings.
	@param	str	String to find in array.
	@param	cs	Flag: Case-sensitive search (0=no, other=yes).
	@return	Non-negative index value on success, -1 on error.
*/
int
dk4strw_array_index(const wchar_t * const *arr, const wchar_t *str, int cs);

/**	Find index of a string in an array of strings.
	
	CRT on windows: Optional, disabling CRT degrades performance
	for case-insensitive comparisons.
	@param	arr	Array of strings allowing abbreviation.
	@param	spec	Special character indicating start of optional text.
	@param	str	String to find in array.
	@param	cs	Flag: Case-sensitive search (0=no, other=yes).
	@return	Non-negative index value on success, -1 on error.
*/
int
dk4strw_abbr_index(
  const wchar_t * const	*arr,
  wchar_t		 spec,
  const wchar_t		*str,
  int			 cs
);

/**	Check whether a text matches a pattern, the text may be abbreviated.
	
	CRT on windows: Optional, disabling CRT degrades performance
	for case-insensitive comparisons.
 	@param	str	Text to check.
 	@param	pattern	Pattern for comparison.
 	@param	spec	Special character marking the abbreviation in the
	pattern.
	@param	cs	Flag: Case sensitive (1) or not (0).
 	@return	1 for a match, 0 otherwise.
*/
int
dk4strw_is_abbr(const wchar_t *str,const wchar_t *pattern,wchar_t spec,int cs);

/**	Check whether a string represents a boolean value.
	
	CRT on windows: Optional, disabling CRT degrades performance.
	@param	str	String to check.
	@return	1 if str represents a boolean value, 0 otherwise.
*/
int
dk4strw_is_bool(const wchar_t *str);

/**	Check whether a string represents the boolean value TRUE.
	
	CRT on windows: Optional, disabling CRT degrades performance.
	@param	str	String to check.
	@return	1 if str represents the boolean value TRUE, 0 otherwise.
*/
int
dk4strw_is_on(const wchar_t *str);

#if (defined(_WIN32) && DK4_USE_WINDOWS_LOCAL_ALLOC) \
    || (DK4_HAVE_MALLOC && DK4_HAVE_FREE)

/**	Duplicate string in dynamically allocated memory.
	
	CRT on Windows: Optional, disabling CRT reduces functionality
	to strings with length fitting to int and overall size in
	bytes - including finalizer - fitting into a DWORD.
	@param	src	String to duplicate.
	@param	erp	Error report, may be NULL.
	@return	Pointer to string in new memory on succes, NULL on error.
	Use dk4mem_free() to release the memory after usage.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if src is a NULL pointer,
	- DK4_E_MATH_OVERFLOW<br>
	  on mathematical overflow in size calculation,
	- DK4_E_MEMORY<br>
	  if no memory is available.
*/
wchar_t *
dk4strw_dup(const wchar_t *src, dk4_er_t *erp);

#endif
/* if (defined(_WIN32) ... */


/**	Remove trailing white spaces.
	
	CRT on Windows: Optional, disabling CRT degrades performance.
	@param	str	String to remove white spaces from.
	@param	whsp	White spaces set.
*/
void
dk4strw_rtwh(wchar_t *str, const wchar_t *whsp);

/**	Remove trailing newline from line.
	@param	lptr	Line pointer.
*/
void
dk4strw_delnl(wchar_t *lptr);

/**	Skip the first text words in a line, return pointer to start
	of next word.
	@param	line	Line to process.
	@param	skip	Number of words to skip.
	@return	Pointer to next word on success, NULL on error.
*/
const wchar_t *
dk4strw_skip(const wchar_t *line, size_t skip);

/**	Sanitize string (overwrite using 0).
	@param	str	Address of string to sanitize.
	@return	1 on success, 0 on error.
*/
int
dk4strw_sanitize(wchar_t *str);

/**	Sanitize string buffer (overwrite using 0 bytes).
	@param	str	Address of string  buffer.
	@param	sz	Size of string buffer (number of wchar_t).
	@return	1 on success, 0 on error.
*/
int
dk4strw_buffer_sanitize(wchar_t *str, size_t sz);

/**	Sanitize dynamically allocated memory and release it.
	@param	str	Address of string to sanitize and release.
	@return	1 on success, 0 on error.
*/
int
dk4strw_free_sanitized(wchar_t *str);

#ifdef __cplusplus
}
#endif


/**	Sanitize string and release it, set pointer to NULL.
	@param	p	Pointer to string to release.
*/
#define	dk4strw_release_sanitized(p) \
do { if (NULL != p) { (void)dk4strw_free_sanitized(p); } p = NULL; } while (0)

%%	module

#include "dk4conf.h"

#if DK4_ON_WINDOWS && (DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT)
#ifndef WINDOWS_H_INCLUDED
#include <windows.h>
#define	WINDOWS_H_INCLUDED 1
#endif
#endif


#include "dk4strw.h"
#include "dk4mem.h"
#include "dk4numco.h"

#if DK4_HAVE_WCTYPE_H
#ifndef WCTYPE_H_INCLUDED
#include <wctype.h>
#define	WCTYPE_H_INCLUDED 1
#endif
#endif

#ifndef	DK4MEMRS_H_INCLUDED
#include "dk4memrs.h"
#endif



$!trace-include



/**	Constant strings used in the module.
*/
static const wchar_t * const	dk4strw_modkw[] = {
$!string-table	prefix=L
#
#   0	Default delimiters.
#
 \t\r\n
$!end
};



/**	First index of a keyword indicating true in the
	dk4str8_boolkw array below.
*/
#define	DK4STRW_FIRST_TRUE	5

/**	Keywords for boolean values.
*/
static const wchar_t * const	dk4strw_boolkw[] = {
$!string-table	prefix=L
#
#  0...4	false
#
-
0
n$o
of$f
f$alse
#
#  5...		true
#
+
1
y$es
on
t$rue
ok
$!end
};



/**	Number of significant digits when printing a double.
*/
static const int	dk4strw_dbl_digits	=	DK4_DBL_DIGITS;



void
dk4strw_cpy_to_left(wchar_t *dst, const wchar_t *src)
{
  register		wchar_t	*dptr;
  register const	wchar_t	*sptr;
  if ((NULL != dst) && (NULL != src)) {
    dptr = dst; sptr = src;
    while(L'\0' != *sptr) { *(dptr++) = *(sptr++); }
    *dptr = L'\0';
  }
}



size_t
dk4strw_len(const wchar_t *src)
{
  if (NULL == src) { return 0; }
#if DK4_ON_WINDOWS
  /* +++ Windows */
#if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
  return ((size_t)lstrlenW(src));
#else
  return (wcslen(src));
#endif
  /* --- Windows */
#else
  /* +++ Windows */
#if DK4_HAVE_WCSLEN
  return (wcslen(src));
#else
  size_t back = 0;
  while(L'\0' != *src) { src++; back++; }
  return back;
#endif
  /* --- Windows */
#endif
}



wchar_t *
dk4strw_chr(const wchar_t *s, wchar_t c)
{
  if (NULL == s) { return NULL; }
#if DK4_ON_WINDOWS
  /* +++ Windows */
#if DK4_WIN_DENY_CRT
  {
    wchar_t *back = NULL;
    while ((L'\0' != *s) && (NULL == back)) {
      if (c == *s) {
        back = (wchar_t *)s;
      }
      s++;
    }
    return back;
  }
#else
  return (wcschr(s, c));
#endif
  /* --- Windows */
#else
  /* +++ non-Windows */
#if DK4_HAVE_WCSCHR
  return (wcschr(s, c));
#else
  {
    wchar_t *back = NULL;
    while ((L'\0' != *s) && (NULL == back)) {
      if (c == *s) {
        back = (wchar_t *)s;
      }
      s++;
    }
    return back;
  }
#endif
  /* --- non-Windows */
#endif
}



wchar_t *
dk4strw_rchr(const wchar_t *s, wchar_t c)
{
  if (NULL == s) { return NULL; }
#if DK4_ON_WINDOWS
  /* +++ Windows */
#if DK4_WIN_DENY_CRT
  {
    wchar_t *back = NULL;
    while (L'\0' != *s) {
      if (c == *s) {
        back = (wchar_t *)s;
      }
      s++;
    }
    return back;
  }
#else
  return (wcsrchr(s, c));
#endif
  /* --- Windows */
#else
  /* +++ non-Windows */
#if DK4_HAVE_WCSRCHR
  return (wcsrchr(s, c));
#else
  {
    wchar_t *back = NULL;
    while (L'\0' != *s) {
      if (c == *s) {
        back = (wchar_t *)s;
      }
      s++;
    }
    return back;
  }
#endif
  /* --- non-Windows */
#endif
}



wchar_t
dk4strw_toupper(wchar_t c)
{
#if DK4_ON_WINDOWS
  /* +++ Windows */
#if DK4_WIN_DENY_CRT
  if ((L'a' <= c) && (L'z' >= c)) {
    return (L'A' + (c - L'a'));
  } else {
    return c;
  }
#else
  return (towupper(c));
#endif
  /* --- Windows */
#else
  /* +++ non-Windows */
  return (towupper(c));
  /* --- non-Windows */
#endif
}



wchar_t
dk4strw_tolower(wchar_t c)
{
#if DK4_ON_WINDOWS
  /* +++ Windows */
#if DK4_WIN_DENY_CRT
  if ((L'A' <= c) && (L'Z' >= c)) {
    return (L'a' + (c - L'A'));
  } else {
    return c;
  }
#else
  return (towlower(c));
#endif
  /* --- Windows */
#else
  /* +++ non-Windows */
  return (towlower(c));
  /* --- non-Windows */
#endif
}



int
dk4strw_cpy_s(wchar_t *dst, size_t sz, const wchar_t *src, dk4_er_t *erp)
{
  int		back	= 0;
  size_t	sl;
  if ((NULL != dst) && (0 < sz)) {
    *dst = L'\0';
    if (NULL != src) {
      sl = dk4strw_len(src);
      if (sz > sl) {
#if DK4_ON_WINDOWS
	/* +++ Windows */
#if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
	(void)lstrcpyW(dst, (wchar_t *)src);
#else
#pragma warning (disable:4996)
	(void)wcscpy(dst, src);
#pragma warning (default:4996)
#endif
	/* --- Windows */
#else
	/* +++ non-Windows */
#if DK4_HAVE_WCSCPY
	(void)wcscpy(dst, src);
#else
        size_t		nbytes;
        dk4_er_t	er;
        dk4error_init(&er);
	if ((SIZE_MAX - 1) < sl) {
	  dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
	} else {
	  if ((SIZE_MAX / sizeof(wchar_t)) < (sl + 1)) {
	    dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
	  } else {
	    nbytes = sizeof(wchar_t) * (sl + 1);
	  }
	}
        if (DK4_E_NONE == er.ec) {
          DK4_MEMCPY(dst, src, nbytes);
        } else {
          dk4strw_cpy_to_left(dst, src);
        }
#endif
	/* --- non-Windows */
#endif
        back = 1;
      } else {
        dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
      }
    } else {
      dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}



int
dk4strw_cat_s(wchar_t *dst, size_t sz, const wchar_t *src, dk4_er_t *erp)
{
  size_t	 la;	/* Size of string in destination buffer */
  size_t	 lb;	/* Source string size */
  int		 back	= 0;
  if ((NULL != dst) && (0 < sz) && (NULL != src)) {
    la = dk4strw_len(dst);
    lb = dk4strw_len(src);
    if (sz > (la + lb)) {
#if DK4_ON_WINDOWS
      /* +++ Windows */
#if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
      (void)lstrcatW(dst, src);
#else
#pragma warning (disable:4996)
      (void)wcscat(dst, src);
#pragma warning (default:4996)
#endif
      /* --- Windows */
#else
      /* +++ non-Windows */
#if DK4_HAVE_WCSCAT
      (void)wcscat(dst, src);
#else
      size_t	nb;
      dk4_er_t	er;
      dk4error_init(&er);
      if ((SIZE_MAX - 1) < lb) {
        dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
      } else {
        if ((SIZE_MAX / sizeof(wchar_t)) < (lb + 1)) {
	  dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
	} else {
	  nb = sizeof(wchar_t) * (lb + 1);
	}
      }
      if (DK4_E_NONE == er.ec) {
	DK4_MEMCPY(&(dst[la]), src, nb);
      } else {
        dk4strw_cpy_to_left(&(dst[la]), src);
      }
#endif
      /* --- non-Windows */
#endif
      back = 1;
    } else {
      dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}




int
dk4strw_cmp(const wchar_t *s1, const wchar_t *s2)
{
  int		back = 0;
  if (NULL != s1) {
    if (NULL != s2) {
#if DK4_ON_WINDOWS
      /* +++ Windows */
#if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
      back = lstrcmpW(s1, s2);
      if (0 > back) back = -1;
      if (0 < back) back =  1;
#else
      back = wcscmp(s1, s2);
      if (0 > back) back = -1;
      if (0 < back) back =  1;
#endif
      /* --- Windows */
#else
      /* +++ non-Windows */
#if DK4_HAVE_WCSCMP
      back = wcscmp(s1, s2);
      if (0 > back) back = -1;
      if (0 < back) back =  1;
#else
#if DK4_HAVE__WCSCMP
      back = _wcscmp(s1, s2);
      if (0 > back) back = -1;
      if (0 < back) back =  1;
#else
      do {
        if (*s1 > *s2) {
	  back = 1;
	} else {
	  if (*s1 < *s2) {
	    back = -1;
	  } else {
	    if (L'\0' != *s1) {
	      s1++; s2++;
	    }
	  }
	}
      } while((0 == back) && (L'\0' != *s1));
#endif
#endif
      /* --- non-Windows */
#endif
    } else {
      back = 1;
    }
  } else {
    if (NULL != s2) {
      back = -1;
    }
  }
  return back;
}



int
dk4strw_ncmp(const wchar_t *s1, const wchar_t *s2, size_t n)
{
  int		 back	= 0;
  if (NULL != s1) {
    if (NULL != s2) {
#if (DK4_ON_WINDOWS && (DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT) ) \
|| (!(DK4_HAVE_WCSNCMP)) || (!(DK4_HAVE__WCSNCMP))
      while ((0 < n--) && (0 == back)) {
        if (L'\0' == *s1) {
	  if (L'\0' == *s2) {
	    n = 0;
	  } else {
	    back = -1;
	  }
	} else {
	  if (L'\0' == *s2) {
	    back = 1;
	  } else {
	    if (*s1 < *s2) {
	      back = -1;
	    } else {
	      if (*s1 > *s2) {
	        back = 1;
	      } else {
	        s1++; s2++;
	      }
	    }
	  }
	}
      }
#else
#if DK4_HAVE_WCSNCMP
      back = wcsncmp(s1, s2, n);
#else
      back = _wcsncmp(s1, s2, n);
#endif
      if (-1 > back) back = -1;
      if ( 1 < back) back =  1;
#endif
    } else {
      back = 1;
    }
  } else {
    if (NULL != s2) {
      back = -1;
    }
  }
  return back;
}



int
dk4strw_casecmp(const wchar_t *s1, const wchar_t *s2)
{
  int		back = 0;
  if (NULL != s1) {
    if (NULL != s2) {
#if DK4_ON_WINDOWS
      /* +++ Windows */
#if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
      back = lstrcmpiW(s1, s2);
      if (0 > back) back = -1;
      if (0 < back) back =  1;
#else
      back =_wcsicmp(s1, s2);
      if (0 > back) back = -1;
      if (0 < back) back =  1;
#endif
      /* --- Windows */
#else
      /* +++ non-Windows */
#if DK4_HAVE_WCSCASECMP
      back = wcscasecmp(s1, s2);
      if (0 > back) back = -1;
      if (0 < back) back =  1;
#else
#if DK4_HAVE__WCSCASECMP
      back = _wcscasecmp(s1, s2);
      if (0 > back) back = -1;
      if (0 < back) back =  1;
#else
#if DK4_HAVE__WCSICMP
      back = _wcsicmp(s1, s2);
      if (0 > back) back = -1;
      if (0 < back) back =  1;
#else
#if DK4_HAVE_WCSICMP
      back = wcsicmp(s1, s2);
      if (0 > back) back = -1;
      if (0 < back) back =  1;
#else
      wchar_t c1;
      wchar_t c2;
      c1 = dk4strw_toupper(*s1);
      c2 = dk4strw_toupper(*s2);
      do {
        if (c1 > c2) {
	  back = 1;
	} else {
	  if (c1 < c2) {
	    back = -1;
	  } else {
	    if (L'\0' != *s1) {
	      s1++; s2++;
	    }
	  }
	}
      } while((0 == back) && (L'\0' != *s1));
#endif
#endif
#endif
#endif
      /* --- non-Windows */
#endif
    } else {
      back = 1;
    }
  } else {
    if (NULL != s2) {
      back = -1;
    }
  }
  return back;
}



#if (defined(_WIN32) && DK4_USE_WINDOWS_LOCAL_ALLOC) \
    || (DK4_HAVE_MALLOC && DK4_HAVE_FREE)

wchar_t *
dk4strw_dup(const wchar_t *src, dk4_er_t *erp)
{
  wchar_t		*back =	NULL;
  size_t	 	 lgt;
  if (NULL != src) {
    lgt = dk4strw_len(src);
    if (SIZE_MAX > lgt) {
      back = dk4mem_new(wchar_t,(lgt+1),erp);
      if (NULL != back) {
        (void)dk4strw_cpy_s(back, (lgt+1), src, erp);
      }
    } else {
      dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}

#endif
/* if (defined(_WIN32) ... */



#if !DK4_HAVE_WCSCHR
/**	Find first occurance of character in string.
	@param	src	String to search for character.
	@param	c	Character to find.
	@return	Pointer to first occurance on success, NULL on error.
*/
static
const wchar_t *
dk4strw_i_wcschr(const wchar_t *src, wchar_t c)
{
  const wchar_t	*back	=	NULL;
  while ((L'\0' != *src) && (NULL == back)) {
    if (*src == c) {
      back = src;
    } else {
      src++;
    }
  }
  return back;
}
#endif



wchar_t *
dk4strw_start(const wchar_t *src, const wchar_t *delim)
{
  wchar_t *back = NULL;
  if (NULL != src) {
    if (NULL == delim) { delim = dk4strw_modkw[0]; }
    while((L'\0' != *src) && (NULL == back)) {
      if (NULL != dk4strw_chr(delim, *src)) {
        src++;
      } else {
        back = (wchar_t *)src;
      }
    }
  }
  return back;
}



/**	Internal function for next token search.
	
	CRT on Windows: Optional, disabling CRT degrades performance.
	@param	src	String containing tokens, must be set to
			the start of a token.
	@param	delim	Delimiter set.
	@return	Pointer to next token on success, NULL on error.
*/
static
wchar_t *
dk4strw_i_next(wchar_t *src, const wchar_t *delim)
{
  wchar_t *back	=	NULL;
  if (NULL != src) {
    if (NULL == delim) { delim = dk4strw_modkw[0]; }
    while ((L'\0' != *src) && (NULL == back)) {
      if (NULL != dk4strw_chr(delim, *src)) {
        back = src;
      } else {
        src++;
      }
    }
    if (NULL != back) {
      *(back++) = L'\0';
      back = dk4strw_start(back, delim);
    }
  }
  return back;
}



wchar_t *
dk4strw_next(wchar_t *src, const wchar_t *delim)
{
  wchar_t	*back	=	NULL;
  if (NULL != src) {
    back = dk4strw_start(src, delim);
    if (NULL != back) {
      back = dk4strw_i_next(back, delim);
    }
  }
  return back;
}



wchar_t *
dk4strw_sep(wchar_t **stringp, const wchar_t *delim)
{
  wchar_t	*back	=	NULL;
  $? "+ dk4strw_sep \"%ls\" \"%ls\"", TR_LSTR(*stringp), TR_LSTR(delim)
  if (NULL != stringp) {
    if (NULL != *stringp) {
      if (NULL == delim) { delim = dk4strw_modkw[0]; }
      back = dk4strw_start(*stringp, delim);
      if (NULL != back) {
        *stringp = dk4strw_next(back, delim);
      } else {
        *stringp = NULL;
      }
    }
  } $? "- dk4strw_sep \"%ls\"", TR_LSTR(back)
  return back;
}



size_t
dk4strw_tokenize(
  wchar_t **dpp, size_t szdpp, wchar_t *src, const wchar_t *delim, dk4_er_t *erp
)
{
  wchar_t		*context;
  wchar_t		*token;
  size_t	 back		=	0;
  if ((NULL != dpp) && (0 < szdpp) && (NULL != src)) {
    if (NULL == delim) { delim = dk4strw_modkw[0]; }
    context = src;
    while (NULL != (token = dk4strw_sep(&context, delim))) {
      if (back < szdpp) {
        dpp[back] = token;
      }
      back++;
    }
    if (back >= szdpp) {	/* Too many tokens found */
      dk4error_set_elsize_nelem(
        erp, DK4_E_BUFFER_TOO_SMALL, sizeof(DK4_PCHAR), back
      );
      back = 0;
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}



void
dk4strw_normalize(wchar_t *src, const wchar_t *delim)
{

  wchar_t	*p1;	/* Pointer to traverse text. */
  wchar_t	*p2;	/* Start of next token. */
  int		 cc;	/* Flag: Can continue. */

  if (NULL != src) {
    if (NULL == delim) { delim = dk4strw_modkw[0]; }
    p1 = src;
    cc = 0;
    if (L'\0' != *p1) {
      if (NULL != dk4strw_chr(delim, *p1)) {
        p2 = dk4strw_start(p1, delim);
	if (NULL != p2) {
	  dk4strw_cpy_to_left(p1, p2);
	  cc = 1;
	} else {
	  *p1 = L'\0';
	}
      } else {
        cc = 1;
      }
    }
    while (0 != cc) {
      if (L'\0' == *p1) {
        cc = 0;
      } else {
	if (NULL != dk4strw_chr(delim, *p1)) {
	  *p1 = *delim;
	  if (L'\0' == p1[1]) {
	    *p1 = L'\0';
	    cc = 0;
	  } else {
	    if (NULL != dk4strw_chr(delim, p1[1])) {
	      p2 = dk4strw_start(p1, delim);
	      if (NULL != p2) {
	        dk4strw_cpy_to_left(++p1, p2);
	      } else {
	        *p1 = L'\0';
		cc = 0;
	      }
	    } else {
	      p1++;
	    }
	  }
	} else {
	  p1++;
	}
      }
    }
  }
}



int
dk4strw_array_index(const wchar_t * const *arr, const wchar_t *str, int cs)
{
  int		back	=	-1;
  int		cand	=	 0;
  while ((NULL != *arr) && (-1 == back)) {
    if (0 != cs) {
      if (0 == dk4strw_cmp(*arr, str)) { back = cand; }
    } else {
      if (0 == dk4strw_casecmp(*arr, str)) { back = cand; }
    }
    arr++; cand++;
  }
  return back;
}




int
dk4strw_is_abbr(const wchar_t *str,const wchar_t *pattern,wchar_t spec,int cs)
{
  const wchar_t	*lptr	=	NULL;	/* Pointer to traverse string */
  const	wchar_t	*pptr	=	NULL;	/* Pointer to traverse pattern */
  int		 back	=	0;	/* Function result */
  int		 fspec	=	0;	/* Flag: spec was found */
  int		 cc	=	1;	/* Flag: Can continue */
  wchar_t	 cl;
  wchar_t	 cp;
  $? "+ dk4strw_is_abbr str=\"%ls\" pattern=\"%ls\"", TR_LSTR(str), TR_LSTR(pattern)
  if ((NULL != str) && (NULL != pattern)) {
    lptr = str; pptr = pattern;
    while (0 != cc) {
      if ((0 == fspec) && (*pptr == spec)) {
        fspec = 1;
	pptr++;
      } else {
        if (L'\0' != *lptr) {
	  cl = *lptr; cp = *pptr;
	  if (0 == cs) {
	    cl = dk4strw_toupper(cl);
	    cp = dk4strw_toupper(cp);
	  }
	  if (cl == cp) {
	    lptr++;
	    pptr++;
	  } else {
	    cc = 0;
	  }
	} else {
	  cc = 0;
	  if ((L'\0' == *pptr) || (0 != fspec)) { back = 1; }
	}
      }
    }
  }
  $? "- dk4strw_is_abbr %d", back
  return back;
}



int
dk4strw_abbr_index(
  const wchar_t * const	*arr,
  wchar_t		 spec,
  const wchar_t		*str,
  int			 cs
)
{
  int		 back	=	-1;
  int		 cand	=	 0;	/* Traverse array */
  $? "+ dk4strw_abbr_index"
  if ((NULL != arr) && (NULL != str)) {
    while ((NULL != *arr) && (-1 == back)) {
      if (0 != dk4strw_is_abbr(str, *arr, spec, cs)) {
        back = cand;
      }
      arr++; cand++;
    }
  }
  $? "- dk4strw_abbr_index %d", back
  return back;
}



int
dk4strw_is_bool(const wchar_t *str)
{
  int		 back	=	0;
  if (NULL != str) {
    if (-1 < dk4strw_abbr_index(dk4strw_boolkw, L'$', str, 0)) {
      back = 1;
    }
  }
  return back;
}



int
dk4strw_is_on(const wchar_t *str)
{
  int		 back	=	0;
  $? "+ dk4strw_is_on"
  if (NULL != str) {
    back = dk4strw_abbr_index(dk4strw_boolkw, L'$', str, 0);
    if (-1 < back) {
      if (DK4STRW_FIRST_TRUE <= back) {
        back = 1;	$? ". true"
      } else {
        back = 0;	$? ". false"
      }
    } else {
      back = 0;		$? ". not found"
    }
  }
  $? "- dk4strw_is_on %d", back
  return back;
}



int
dk4strw_pathcmp(const wchar_t *s1, const wchar_t *s2)
{
#if DK4_HAVE_CASE_INSENSITIVE_PATHNAMES
  return (dk4strw_casecmp(s1, s2));
#else
  return (dk4strw_cmp(s1, s2));
#endif
}



void
dk4strw_rtwh(wchar_t *str, const wchar_t *whsp)
{
  wchar_t *ptr = NULL;		/* Pointer to first whitespace */

  if (NULL == whsp) { whsp = dk4strw_modkw[0]; }
  if (NULL != str) {
    while (L'\0' != *str) {
      if (NULL != dk4strw_chr(whsp, *str)) {
        if (NULL == ptr) { ptr = str; }
      } else {
        ptr = NULL;
      }
      str++;
    }
    if (NULL != ptr) { *ptr = L'\0'; }
  }
}



void
dk4strw_delnl(wchar_t *lptr)
{
  if (NULL != lptr) {
    while (L'\0' != *lptr) {
      if (L'\n' == *lptr) {
        *lptr = L'\0';
      } else {
        if (L'\r' == *lptr) {
	  if (L'\n' == lptr[1]) {
	    *lptr = L'\0';
	  } else {
	    if (L'\0' == lptr[1]) {
	      *lptr = L'\0';
	    } else {
	      lptr++;
	    }
	  }
	} else {
	  lptr++;
	}
      }
    }
  }
}



const wchar_t *
dk4strw_skip(const wchar_t *line, size_t skip)
{
  const wchar_t	*back	= NULL;
  size_t	 words	= 0;	/* Number of words already found */
  int		 lwt	= 0;	/* Flag: Last char was text */

  if (NULL != line) {
    if (0 < skip) {
      while ((NULL == back) && (L'\0' != *line)) {
        if (L' ' == *line) {
	  lwt = 0;
	} else {
	  if (L'\t' == *line) {
	    lwt = 0;
	  } else {
	    if (L'\n' == *line) {
	      lwt = 0;
	    } else {
	      if (L'\r' == *line) {
	        lwt = 0;
	      } else {
	        if (0 == lwt) {
		  if (++words > skip) {
		    back = line;
		  }
		}
	        lwt = 1;
	      }
	    }
	  }
	}
        line++;
      }
    } else {
      back = dk4strw_start(line, NULL);
    }
  }
  return back;
}



int
dk4strw_buffer_sanitize(wchar_t *str, size_t sz)
{
  size_t	 i;
  int		 back	= 0;

  if (NULL != str) {
    if (0 < sz) {
      if ((SIZE_MAX / sizeof(wchar_t)) >= sz) {
        back = dk4mem_reset_secure(str, (sz * sizeof(wchar_t)), NULL);
      } else {
        for (i = 0; i < sz; i++) { str[i] = L'\0'; }
	back = 1;
	for (i = 0; i < sz; i++) { if (L'\0' != str[i]) { back = 0; } }
      }
    } else {
      back = 1;
    }
  }
  return back;
}



int
dk4strw_sanitize(wchar_t *str)
{
  int		 back	= 0;
  if (NULL != str) {
    back = dk4strw_buffer_sanitize(str, dk4strw_len(str));
  }
  return back;
}



int
dk4strw_free_sanitized(wchar_t *str)
{
  int		 back = 0;
  if (NULL != str) {
    back = dk4strw_sanitize(str);
    dk4mem_free(str);
  }
  return back;
}