forked from katiya-cw/iso8211lib-1.4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpl_string.cpp
1772 lines (1547 loc) · 54.4 KB
/
cpl_string.cpp
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
/**********************************************************************
* $Id: cpl_string.cpp,v 1.56 2006/06/30 14:58:22 dron Exp $
*
* Name: cpl_string.cpp
* Project: CPL - Common Portability Library
* Purpose: String and Stringlist manipulation functions.
* Author: Daniel Morissette, [email protected]
*
**********************************************************************
* Copyright (c) 1998, Daniel Morissette
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**********************************************************************
*
* Independent Security Audit 2003/04/04 Andrey Kiselev:
* Completed audit of this module. All functions may be used without buffer
* overflows and stack corruptions with any kind of input data strings with
* except of CPLSPrintf() and CSLAppendPrintf() (see note below).
*
* Security Audit 2003/03/28 warmerda:
* Completed security audit. I believe that this module may be safely used
* to parse tokenize arbitrary input strings, assemble arbitrary sets of
* names values into string lists, unescape and escape text even if provided
* by a potentially hostile source.
*
* CPLSPrintf() and CSLAppendPrintf() may not be safely invoked on
* arbitrary length inputs since it has a fixed size output buffer on system
* without vsnprintf().
*
* $Log: cpl_string.cpp,v $
* Revision 1.56 2006/06/30 14:58:22 dron
* Avoid warnings on win/64.
*
* Revision 1.55 2006/04/12 15:10:40 fwarmerdam
* argument to InsertString should be const
*
* Revision 1.54 2006/02/19 21:54:34 mloskot
* [WINCE] Changes related to Windows CE port of CPL. Most changes are #ifdef wrappers.
*
* Revision 1.53 2005/12/19 20:17:21 fwarmerdam
* enable pszValue NULL to delete entry in CSLSetNameValue
*
* Revision 1.52 2005/12/06 07:33:12 fwarmerdam
* fixed support for tokenizing string with trailing delimeter, bug 945
*
* Revision 1.51 2005/10/13 01:20:16 fwarmerdam
* added CSLMerge()
*
* Revision 1.50 2005/09/30 19:11:40 fwarmerdam
* fixed hextobinary conversion, bug 935
*
* Revision 1.49 2005/09/14 19:21:17 fwarmerdam
* binary pointer is const in binarytohex
*
* Revision 1.48 2005/09/13 15:07:23 dron
* Initialize counters in CPLHexToBinary().
*
* Revision 1.47 2005/09/11 21:09:27 fwarmerdam
* use large file API in CSLLoad
*
* Revision 1.46 2005/09/05 20:19:08 fwarmerdam
* fixed binarytohex function
*
* Revision 1.45 2005/08/31 03:31:15 fwarmerdam
* added binarytohex/hextobinary
*
* Revision 1.44 2005/08/04 19:41:33 fwarmerdam
* support setting null value in CSLSetNameValue
*
* Revision 1.43 2005/05/23 03:59:44 fwarmerdam
* make cplsprintf buffer threadlocal
*
* Revision 1.42 2005/04/04 15:23:31 fwarmerdam
* some functions now CPL_STDCALL
*
* Revision 1.41 2004/09/17 21:26:28 fwarmerdam
* Yikes ... CPLEscapeString() was badly broken for BackslashEscapable.
*
* Revision 1.40 2004/08/16 20:23:46 warmerda
* added .csv escaping
*
* Revision 1.39 2004/07/12 21:50:38 warmerda
* Added SQL escaping style
*
* Revision 1.38 2004/04/23 22:23:32 warmerda
* Fixed key memory leak in seldom used CSLSetNameValueSeperator().
*
* Revision 1.37 2003/12/02 15:56:47 warmerda
* avoid use of CSLAddString() in tokenize, manage list ourselves
*
* Revision 1.36 2003/08/29 17:32:27 warmerda
* Open file in binary mode for CSLLoad() since CPLReadline() works much
* better then.
*
* Revision 1.35 2003/07/17 10:15:40 dron
* CSLTestBoolean() added.
*
* Revision 1.34 2003/05/28 19:22:38 warmerda
* fixed docs
*
* Revision 1.33 2003/05/21 04:20:30 warmerda
* avoid warnings
*
* Revision 1.32 2003/04/04 14:57:38 dron
* _vsnprintf() hack moved to the cpl_config.h.vc.
*
* Revision 1.31 2003/04/04 14:16:07 dron
* Use _vsnprintf() in Windows environment.
*
* Revision 1.30 2003/03/28 05:29:53 warmerda
* Fixed buffer overflow risk in escaping code (for XML method). Avoid
* use of CPLSPrintf() for name/value list assembly to avoid risk with long
* key names or values. Use vsnprintf() in CPLSPrintf() on platforms where it
* is available. Security audit complete.
*
* Revision 1.29 2003/03/27 21:32:08 warmerda
* Fixed bug with escaped spaces.
*
* Revision 1.28 2003/03/11 21:33:02 warmerda
* added URL encode/decode support, untested
*
* Revision 1.27 2003/01/30 19:15:55 warmerda
* added some docs
*
* Revision 1.26 2003/01/14 14:31:16 warmerda
* Added "OFF" as a negative response to CSLFetchBoolean().
*
* Revision 1.25 2002/10/07 19:35:38 dron
* Fixed description for CSLFetchBoolean()
*
* Revision 1.24 2002/07/12 22:37:05 warmerda
* added CSLFetchBoolean
*
* Revision 1.23 2002/07/09 20:25:25 warmerda
* expand tabs
*
* Revision 1.22 2002/05/28 18:53:43 warmerda
* added XML escaping support
*
* Revision 1.21 2002/04/26 14:55:26 warmerda
* Added CPLEscapeString() and CPLUnescapeString() (unescape untested)
*
* Revision 1.20 2002/03/05 14:26:57 warmerda
* expanded tabs
*
* Revision 1.19 2002/01/16 03:59:27 warmerda
* added CPLTokenizeString2
*
* Revision 1.18 2001/12/11 22:40:26 warmerda
* cleanup CPLReadLine buffer in CSLLoad()
*
* Revision 1.17 2001/11/07 14:31:16 warmerda
* doc fix
*
* Revision 1.16 2001/07/18 04:00:49 warmerda
* added CPL_CVSID
*
* Revision 1.15 2001/01/19 21:16:41 warmerda
* expanded tabs
*
* Revision 1.14 2000/10/06 15:19:03 warmerda
* added CPLSetNameValueSeparator
*
* Revision 1.13 2000/08/22 17:47:50 warmerda
* Fixed declaration of gnCPLSPrintfBuffer.
*
* Revision 1.12 2000/08/18 21:20:54 svillene
* *** empty log message ***
*
* Revision 1.11 2000/03/30 05:38:48 warmerda
* added CPLParseNameValue
*
* Revision 1.10 1999/06/26 14:05:10 warmerda
* Added CSLFindString().
*
* Revision 1.9 1999/04/28 02:33:02 danmo
* CSLInsertStrings(): make sure papszStrList is NULL-terminated properly
*
* Revision 1.8 1999/03/12 21:19:49 danmo
* Fixed TokenizeStringComplex() vs strings ending with empty token,
* and fixed a problem with CSLAdd/SetNameValue() vs empty string list.
*
* Revision 1.7 1999/03/09 21:29:57 warmerda
* Added backslash escaping within string constants for tokenize function.
*
* Revision 1.6 1999/02/25 04:40:46 danmo
* Modif. CSLLoad() to use CPLReadLine() (better handling of newlines)
*
* Revision 1.5 1999/02/17 01:41:58 warmerda
* Added CSLGetField
*
* Revision 1.4 1998/12/15 19:01:40 warmerda
* *** empty log message ***
*
* Revision 1.3 1998/12/05 23:04:21 warmerda
* Use EQUALN() instead of strincmp() which doesn't exist on Linux.
*
* Revision 1.2 1998/12/04 21:40:42 danmo
* Added more Name=Value manipulation fuctions
*
* Revision 1.1 1998/12/03 18:26:02 warmerda
* New
*
**********************************************************************/
#include "cpl_string.h"
#include "cpl_vsi.h"
#if defined(WIN32CE)
# include <wce_errno.h>
# include <wce_string.h>
#endif
CPL_CVSID("$Id: cpl_string.cpp,v 1.56 2006/06/30 14:58:22 dron Exp $");
/*=====================================================================
StringList manipulation functions.
=====================================================================*/
/**********************************************************************
* CSLAddString()
*
* Append a string to a StringList and return a pointer to the modified
* StringList.
* If the input StringList is NULL, then a new StringList is created.
**********************************************************************/
char **CSLAddString(char **papszStrList, const char *pszNewString)
{
int nItems=0;
if (pszNewString == NULL)
return papszStrList; /* Nothing to do!*/
/* Allocate room for the new string */
if (papszStrList == NULL)
papszStrList = (char**) CPLCalloc(2,sizeof(char*));
else
{
nItems = CSLCount(papszStrList);
papszStrList = (char**)CPLRealloc(papszStrList,
(nItems+2)*sizeof(char*));
}
/* Copy the string in the list */
papszStrList[nItems] = CPLStrdup(pszNewString);
papszStrList[nItems+1] = NULL;
return papszStrList;
}
/**********************************************************************
* CSLCount()
*
* Return the number of lines in a Stringlist.
**********************************************************************/
int CSLCount(char **papszStrList)
{
int nItems=0;
if (papszStrList)
{
while(*papszStrList != NULL)
{
nItems++;
papszStrList++;
}
}
return nItems;
}
/************************************************************************/
/* CSLGetField() */
/* */
/* Fetches the indicated field, being careful not to crash if */
/* the field doesn't exist within this string list. The */
/* returned pointer should not be freed, and doesn't */
/* necessarily last long. */
/************************************************************************/
const char * CSLGetField( char ** papszStrList, int iField )
{
int i;
if( papszStrList == NULL || iField < 0 )
return( "" );
for( i = 0; i < iField+1; i++ )
{
if( papszStrList[i] == NULL )
return "";
}
return( papszStrList[iField] );
}
/**********************************************************************
* CSLDestroy()
*
* Free all memory used by a StringList.
**********************************************************************/
void CPL_STDCALL CSLDestroy(char **papszStrList)
{
char **papszPtr;
if (papszStrList)
{
papszPtr = papszStrList;
while(*papszPtr != NULL)
{
CPLFree(*papszPtr);
papszPtr++;
}
CPLFree(papszStrList);
}
}
/**********************************************************************
* CSLDuplicate()
*
* Allocate and return a copy of a StringList.
**********************************************************************/
char **CSLDuplicate(char **papszStrList)
{
char **papszNewList, **papszSrc, **papszDst;
int nLines;
nLines = CSLCount(papszStrList);
if (nLines == 0)
return NULL;
papszNewList = (char **)CPLMalloc((nLines+1)*sizeof(char*));
papszSrc = papszStrList;
papszDst = papszNewList;
while(*papszSrc != NULL)
{
*papszDst = CPLStrdup(*papszSrc);
papszSrc++;
papszDst++;
}
*papszDst = NULL;
return papszNewList;
}
/************************************************************************/
/* CSLMerge */
/************************************************************************/
/**
* \brief Merge two lists.
*
* The two lists are merged, ensuring that if any keys appear in both
* that the value from the second (papszOverride) list take precidence.
*
* @param papszOrig the original list, being modified.
* @param papszOverride the list of items being merged in. This list
* is unaltered and remains owned by the caller.
*
* @return updated list.
*/
char **CSLMerge( char **papszOrig, char **papszOverride )
{
int i;
if( papszOrig == NULL && papszOverride != NULL )
return CSLDuplicate( papszOverride );
if( papszOverride == NULL )
return papszOrig;
for( i = 0; papszOverride[i] != NULL; i++ )
{
char *pszKey = NULL;
const char *pszValue = CPLParseNameValue( papszOverride[i], &pszKey );
papszOrig = CSLSetNameValue( papszOrig, pszKey, pszValue );
CPLFree( pszKey );
}
return papszOrig;
}
/**********************************************************************
* CSLLoad()
*
* Load a test file into a stringlist.
*
* Lines are limited in length by the size of the CPLReadLine() buffer.
**********************************************************************/
char **CSLLoad(const char *pszFname)
{
FILE *fp;
const char *pszLine;
char **papszStrList=NULL;
fp = VSIFOpenL(pszFname, "rb");
if (fp)
{
while(!VSIFEofL(fp))
{
if ( (pszLine = CPLReadLineL(fp)) != NULL )
{
papszStrList = CSLAddString(papszStrList, pszLine);
}
}
VSIFCloseL(fp);
CPLReadLineL( NULL );
}
else
{
/* Unable to open file */
CPLError(CE_Failure, CPLE_OpenFailed,
"CSLLoad(%s): %s", pszFname, strerror(errno));
}
return papszStrList;
}
/**********************************************************************
* CSLSave()
*
* Write a stringlist to a text file.
*
* Returns the number of lines written, or 0 if the file could not
* be written.
**********************************************************************/
int CSLSave(char **papszStrList, const char *pszFname)
{
FILE *fp;
int nLines = 0;
if (papszStrList)
{
if ((fp = VSIFOpen(pszFname, "wt")) != NULL)
{
while(*papszStrList != NULL)
{
if (VSIFPuts(*papszStrList, fp) == EOF ||
VSIFPutc('\n', fp) == EOF)
{
CPLError(CE_Failure, CPLE_FileIO,
"CSLSave(%s): %s", pszFname,
strerror(errno));
break; /* A Problem happened... abort */
}
nLines++;
papszStrList++;
}
VSIFClose(fp);
}
else
{
/* Unable to open file */
CPLError(CE_Failure, CPLE_OpenFailed,
"CSLSave(%s): %s", pszFname, strerror(errno));
}
}
return nLines;
}
/**********************************************************************
* CSLPrint()
*
* Print a StringList to fpOut. If fpOut==NULL, then output is sent
* to stdout.
*
* Returns the number of lines printed.
**********************************************************************/
int CSLPrint(char **papszStrList, FILE *fpOut)
{
int nLines=0;
if (fpOut == NULL)
fpOut = stdout;
if (papszStrList)
{
while(*papszStrList != NULL)
{
VSIFPrintf(fpOut, "%s\n", *papszStrList);
nLines++;
papszStrList++;
}
}
return nLines;
}
/**********************************************************************
* CSLInsertStrings()
*
* Copies the contents of a StringList inside another StringList
* before the specified line.
*
* nInsertAtLineNo is a 0-based line index before which the new strings
* should be inserted. If this value is -1 or is larger than the actual
* number of strings in the list then the strings are added at the end
* of the source StringList.
*
* Returns the modified StringList.
**********************************************************************/
char **CSLInsertStrings(char **papszStrList, int nInsertAtLineNo,
char **papszNewLines)
{
int i, nSrcLines, nDstLines, nToInsert;
char **ppszSrc, **ppszDst;
if (papszNewLines == NULL ||
( nToInsert = CSLCount(papszNewLines) ) == 0)
return papszStrList; /* Nothing to do!*/
nSrcLines = CSLCount(papszStrList);
nDstLines = nSrcLines + nToInsert;
/* Allocate room for the new strings */
papszStrList = (char**)CPLRealloc(papszStrList,
(nDstLines+1)*sizeof(char*));
/* Make sure the array is NULL-terminated... it may not be if
* papszStrList was NULL before Realloc()
*/
papszStrList[nSrcLines] = NULL;
/* Make some room in the original list at the specified location
* Note that we also have to move the NULL pointer at the end of
* the source StringList.
*/
if (nInsertAtLineNo == -1 || nInsertAtLineNo > nSrcLines)
nInsertAtLineNo = nSrcLines;
ppszSrc = papszStrList + nSrcLines;
ppszDst = papszStrList + nDstLines;
for (i=nSrcLines; i>=nInsertAtLineNo; i--)
{
*ppszDst = *ppszSrc;
ppszDst--;
ppszSrc--;
}
/* Copy the strings to the list */
ppszSrc = papszNewLines;
ppszDst = papszStrList + nInsertAtLineNo;
for (; *ppszSrc != NULL; ppszSrc++, ppszDst++)
{
*ppszDst = CPLStrdup(*ppszSrc);
}
return papszStrList;
}
/**********************************************************************
* CSLInsertString()
*
* Insert a string at a given line number inside a StringList
*
* nInsertAtLineNo is a 0-based line index before which the new string
* should be inserted. If this value is -1 or is larger than the actual
* number of strings in the list then the string is added at the end
* of the source StringList.
*
* Returns the modified StringList.
**********************************************************************/
char **CSLInsertString(char **papszStrList, int nInsertAtLineNo,
const char *pszNewLine)
{
char *apszList[2];
/* Create a temporary StringList and call CSLInsertStrings()
*/
apszList[0] = (char *) pszNewLine;
apszList[1] = NULL;
return CSLInsertStrings(papszStrList, nInsertAtLineNo, apszList);
}
/**********************************************************************
* CSLRemoveStrings()
*
* Remove strings inside a StringList
*
* nFirstLineToDelete is the 0-based line index of the first line to
* remove. If this value is -1 or is larger than the actual
* number of strings in list then the nNumToRemove last strings are
* removed.
*
* If ppapszRetStrings != NULL then the deleted strings won't be
* free'd, they will be stored in a new StringList and the pointer to
* this new list will be returned in *ppapszRetStrings.
*
* Returns the modified StringList.
**********************************************************************/
char **CSLRemoveStrings(char **papszStrList, int nFirstLineToDelete,
int nNumToRemove, char ***ppapszRetStrings)
{
int i, nSrcLines, nDstLines;
char **ppszSrc, **ppszDst;
nSrcLines = CSLCount(papszStrList);
nDstLines = nSrcLines - nNumToRemove;
if (nNumToRemove < 1 || nSrcLines == 0)
return papszStrList; /* Nothing to do!*/
/* If operation will result in an empty StringList then don't waste
* time here!
*/
if (nDstLines < 1)
{
CSLDestroy(papszStrList);
return NULL;
}
/* Remove lines from the source StringList...
* Either free() each line or store them to a new StringList depending on
* the caller's choice.
*/
ppszDst = papszStrList + nFirstLineToDelete;
if (ppapszRetStrings == NULL)
{
/* free() all the strings that will be removed.
*/
for (i=0; i < nNumToRemove; i++)
{
CPLFree(*ppszDst);
*ppszDst = NULL;
}
}
else
{
/* Store the strings to remove in a new StringList
*/
*ppapszRetStrings = (char **)CPLCalloc(nNumToRemove+1, sizeof(char*));
for (i=0; i < nNumToRemove; i++)
{
(*ppapszRetStrings)[i] = *ppszDst;
*ppszDst = NULL;
ppszDst++;
}
}
/* Shift down all the lines that follow the lines to remove.
*/
if (nFirstLineToDelete == -1 || nFirstLineToDelete > nSrcLines)
nFirstLineToDelete = nDstLines;
ppszSrc = papszStrList + nFirstLineToDelete + nNumToRemove;
ppszDst = papszStrList + nFirstLineToDelete;
for ( ; *ppszSrc != NULL; ppszSrc++, ppszDst++)
{
*ppszDst = *ppszSrc;
}
/* Move the NULL pointer at the end of the StringList */
*ppszDst = *ppszSrc;
/* At this point, we could realloc() papszStrList to a smaller size, but
* since this array will likely grow again in further operations on the
* StringList we'll leave it as it is.
*/
return papszStrList;
}
/************************************************************************/
/* CSLFindString() */
/* */
/* Find a string within a string list. The string must match */
/* the full length, but the comparison is case insensitive. */
/* Return -1 on failure. */
/************************************************************************/
int CSLFindString( char ** papszList, const char * pszTarget )
{
int i;
if( papszList == NULL )
return -1;
for( i = 0; papszList[i] != NULL; i++ )
{
if( EQUAL(papszList[i],pszTarget) )
return i;
}
return -1;
}
/**********************************************************************
* CSLTokenizeString()
*
* Tokenizes a string and returns a StringList with one string for
* each token.
**********************************************************************/
char **CSLTokenizeString( const char *pszString )
{
return CSLTokenizeString2( pszString, " ", CSLT_HONOURSTRINGS );
}
/************************************************************************/
/* CSLTokenizeStringComplex() */
/* */
/* Obsolete tokenizing api. */
/************************************************************************/
char ** CSLTokenizeStringComplex( const char * pszString,
const char * pszDelimiters,
int bHonourStrings, int bAllowEmptyTokens )
{
int nFlags = 0;
if( bHonourStrings )
nFlags |= CSLT_HONOURSTRINGS;
if( bAllowEmptyTokens )
nFlags |= CSLT_ALLOWEMPTYTOKENS;
return CSLTokenizeString2( pszString, pszDelimiters, nFlags );
}
/************************************************************************/
/* CSLTokenizeString2() */
/* */
/* The ultimate tokenizer? */
/************************************************************************/
char ** CSLTokenizeString2( const char * pszString,
const char * pszDelimiters,
int nCSLTFlags )
{
char **papszRetList = NULL;
int nRetMax = 0, nRetLen = 0;
char *pszToken;
int nTokenMax, nTokenLen;
int bHonourStrings = (nCSLTFlags & CSLT_HONOURSTRINGS);
int bAllowEmptyTokens = (nCSLTFlags & CSLT_ALLOWEMPTYTOKENS);
pszToken = (char *) CPLCalloc(10,1);
nTokenMax = 10;
while( pszString != NULL && *pszString != '\0' )
{
int bInString = FALSE;
nTokenLen = 0;
/* Try to find the next delimeter, marking end of token */
for( ; *pszString != '\0'; pszString++ )
{
/* End if this is a delimeter skip it and break. */
if( !bInString && strchr(pszDelimiters, *pszString) != NULL )
{
pszString++;
break;
}
/* If this is a quote, and we are honouring constant
strings, then process the constant strings, with out delim
but don't copy over the quotes */
if( bHonourStrings && *pszString == '"' )
{
if( nCSLTFlags & CSLT_PRESERVEQUOTES )
{
pszToken[nTokenLen] = *pszString;
nTokenLen++;
}
if( bInString )
{
bInString = FALSE;
continue;
}
else
{
bInString = TRUE;
continue;
}
}
/* Within string constants we allow for escaped quotes, but
in processing them we will unescape the quotes */
if( bInString && pszString[0] == '\\' && pszString[1] == '"' )
{
if( nCSLTFlags & CSLT_PRESERVEESCAPES )
{
pszToken[nTokenLen] = *pszString;
nTokenLen++;
}
pszString++;
}
/* Within string constants a \\ sequence reduces to \ */
else if( bInString
&& pszString[0] == '\\' && pszString[1] == '\\' )
{
if( nCSLTFlags & CSLT_PRESERVEESCAPES )
{
pszToken[nTokenLen] = *pszString;
nTokenLen++;
}
pszString++;
}
if( nTokenLen >= nTokenMax-3 )
{
nTokenMax = nTokenMax * 2 + 10;
pszToken = (char *) CPLRealloc( pszToken, nTokenMax );
}
pszToken[nTokenLen] = *pszString;
nTokenLen++;
}
pszToken[nTokenLen] = '\0';
/*
* Add the token.
*/
if( pszToken[0] != '\0' || bAllowEmptyTokens )
{
if( nRetLen >= nRetMax - 1 )
{
nRetMax = nRetMax * 2 + 10;
papszRetList = (char **)
CPLRealloc(papszRetList, sizeof(char*) * nRetMax );
}
papszRetList[nRetLen++] = CPLStrdup( pszToken );
papszRetList[nRetLen] = NULL;
}
}
/*
* If the last token was empty, then we need to capture
* it now, as the loop would skip it.
*/
if( *pszString == '\0' && bAllowEmptyTokens && nRetLen > 0
&& strchr(pszDelimiters,*(pszString-1)) != NULL )
{
if( nRetLen >= nRetMax - 1 )
{
nRetMax = nRetMax * 2 + 10;
papszRetList = (char **)
CPLRealloc(papszRetList, sizeof(char*) * nRetMax );
}
papszRetList[nRetLen++] = CPLStrdup("");
papszRetList[nRetLen] = NULL;
}
if( papszRetList == NULL )
papszRetList = (char **) CPLCalloc(sizeof(char *),1);
CPLFree( pszToken );
return papszRetList;
}
/**********************************************************************
* CPLSPrintf()
*
* My own version of CPLSPrintf() that works with 10 static buffer.
*
* It returns a ref. to a static buffer that should not be freed and
* is valid only until the next call to CPLSPrintf().
*
* NOTE: This function should move to cpl_conv.cpp.
**********************************************************************/
/* For now, assume that a 8000 chars buffer will be enough.
*/
#define CPLSPrintf_BUF_SIZE 8000
#define CPLSPrintf_BUF_Count 10
static CPL_THREADLOCAL char gszCPLSPrintfBuffer[CPLSPrintf_BUF_Count][CPLSPrintf_BUF_SIZE];
static CPL_THREADLOCAL int gnCPLSPrintfBuffer = 0;
const char *CPLSPrintf(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
#if defined(HAVE_VSNPRINTF)
vsnprintf(gszCPLSPrintfBuffer[gnCPLSPrintfBuffer], CPLSPrintf_BUF_SIZE-1,
fmt, args);
#else
vsprintf(gszCPLSPrintfBuffer[gnCPLSPrintfBuffer], fmt, args);
#endif
va_end(args);
int nCurrent = gnCPLSPrintfBuffer;
if (++gnCPLSPrintfBuffer == CPLSPrintf_BUF_Count)
gnCPLSPrintfBuffer = 0;
return gszCPLSPrintfBuffer[nCurrent];
}
/**********************************************************************
* CSLAppendPrintf()
*
* Use CPLSPrintf() to append a new line at the end of a StringList.
*
* Returns the modified StringList.
**********************************************************************/
char **CSLAppendPrintf(char **papszStrList, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
#if defined(HAVE_VSNPRINTF)
vsnprintf(gszCPLSPrintfBuffer[gnCPLSPrintfBuffer], CPLSPrintf_BUF_SIZE-1,
fmt, args);
#else
vsprintf(gszCPLSPrintfBuffer[gnCPLSPrintfBuffer], fmt, args);
#endif
va_end(args);
int nCurrent = gnCPLSPrintfBuffer;
if (++gnCPLSPrintfBuffer == CPLSPrintf_BUF_Count)
gnCPLSPrintfBuffer = 0;
return CSLAddString(papszStrList, gszCPLSPrintfBuffer[nCurrent]);
}
/************************************************************************/
/* CSLTestBoolean() */
/************************************************************************/
/**
* Test what boolean value contained in the string.
*
* If pszValue is "NO", "FALSE", "OFF" or "0" will be returned FALSE.
* Otherwise, TRUE will be returned.
*
* @param pszValue the string should be tested.
*
* @return TRUE or FALSE.
*/
int CSLTestBoolean( const char *pszValue )
{
if( EQUAL(pszValue,"NO")
|| EQUAL(pszValue,"FALSE")
|| EQUAL(pszValue,"OFF")
|| EQUAL(pszValue,"0") )
return FALSE;
else
return TRUE;
}
/**********************************************************************
* CSLFetchBoolean()
*
* Check for boolean key value.
*
* In a StringList of "Name=Value" pairs, look to see if there is a key
* with the given name, and if it can be interpreted as being TRUE. If