-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcpl_conv.cpp
2136 lines (1811 loc) · 69.7 KB
/
cpl_conv.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_conv.cpp,v 1.52 2006/03/04 00:46:43 mloskot Exp $
*
* Project: CPL - Common Portability Library
* Purpose: Convenience functions.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1998, Frank Warmerdam
*
* 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.
******************************************************************************
*
* $Log: cpl_conv.cpp,v $
* Revision 1.52 2006/03/04 00:46:43 mloskot
* [WCE] CPLLocaleC class excluded from compilation for Windows CE
*
* Revision 1.51 2006/03/03 19:40:53 fwarmerdam
* added CPLLocaleC class
*
* Revision 1.50 2006/02/19 21:54:34 mloskot
* [WINCE] Changes related to Windows CE port of CPL. Most changes are #ifdef wrappers.
*
* Revision 1.49 2006/02/09 05:54:15 fwarmerdam
* use ll in format for long long on mac, not L
*
* Revision 1.48 2005/11/24 19:22:54 fwarmerdam
* Added silly-value checks in CPLMalloc/Realloc.
*
* Revision 1.47 2005/10/17 23:33:51 fwarmerdam
* CPLFGets() - be very wary about reads ending in the middle of CRLF.
* Was causing problems with world.mif/world.mid reading.
*
* Revision 1.46 2005/09/11 21:08:59 fwarmerdam
* properly manage TLS ReadLine buffer, added CPLReadLineL()
*
* Revision 1.45 2005/07/11 16:41:31 fwarmerdam
* Protect config list and shared file list with mutex.
*
* Revision 1.44 2005/05/23 03:57:57 fwarmerdam
* make make static buffers threadlocal
*
* Revision 1.43 2005/04/04 15:23:30 fwarmerdam
* some functions now CPL_STDCALL
*
* Revision 1.42 2004/11/17 22:57:21 fwarmerdam
* added CPLScanPointer() and CPLPrintPointer()
*
* Revision 1.41 2004/08/16 20:24:07 warmerda
* added CPLUnlinkTree
*
* Revision 1.40 2004/07/31 04:51:36 warmerda
* added shared file open support
*
* Revision 1.39 2004/06/01 20:40:02 warmerda
* expanded tabs
*
* Revision 1.38 2004/03/28 16:22:02 warmerda
* const correctness changes in scan functions
*
* Revision 1.37 2004/03/24 09:01:17 dron
* Added CPLPrintUIntBig().
*
* Revision 1.36 2004/02/25 09:02:25 dron
* Fixed bug in CPLPackedDMSToDec().
*
* Revision 1.35 2004/02/21 10:08:54 dron
* Fixes in various string handling functions.
*
* Revision 1.34 2004/02/07 14:03:30 dron
* CPLDecToPackedDMS() added.
*
* Revision 1.33 2004/02/01 08:37:55 dron
* Added CPLPackedDMSToDec().
*
* Revision 1.32 2003/12/28 17:24:43 warmerda
* added CPLFreeConfig
*
* Revision 1.31 2003/10/17 07:06:06 dron
* Added locale selection option to CPLScanDouble() and CPLPrintDOuble().
*
* Revision 1.30 2003/09/28 14:14:16 dron
* Added CPLScanString().
*
* Revision 1.29 2003/09/12 20:49:24 warmerda
* reimplement CPLFGets() to avoid textmode problems
*
* Revision 1.28 2003/09/08 12:54:42 dron
* Fixed warnings.
*
* Revision 1.27 2003/09/08 11:09:53 dron
* Added CPLPrintDouble() and CPLPrintTime().
*
* Revision 1.26 2003/09/07 14:38:43 dron
* Added CPLPrintString(), CPLPrintStringFill(), CPLPrintInt32(), CPLPrintUIntBig().
*
* Revision 1.25 2003/09/03 13:07:26 warmerda
* Cleaned up CPLScanLong() a bit to avoid warnings, and
* unnecessary conversion to/from double.
*
* Revision 1.24 2003/08/31 14:48:05 dron
* Added CPLScanLong() and CPLScanDouble().
*
* Revision 1.23 2003/08/25 20:01:58 dron
* Added CPLFGets() helper function.
*
* Revision 1.22 2003/05/08 21:51:14 warmerda
* added CPL{G,S}etConfigOption() usage
*
* Revision 1.21 2003/03/05 16:46:54 warmerda
* Cast strchr() result for Sun (patch from Graeme).
*
* Revision 1.20 2003/03/02 04:44:38 warmerda
* added CPLStringToComplex
*
* Revision 1.19 2003/02/14 22:12:07 warmerda
* expand tabs
*
* Revision 1.18 2002/12/18 20:22:53 warmerda
* fiddle with roundoff issues in DecToDMS
*
* Revision 1.17 2002/12/10 19:46:04 warmerda
* modified CPLReadLine() to seek back if it overreads past a CR or LF
*
* Revision 1.16 2002/12/09 18:52:51 warmerda
* added DMS conversion
*
* Revision 1.15 2002/03/05 14:26:57 warmerda
* expanded tabs
*
* Revision 1.14 2001/12/12 17:06:57 warmerda
* added CPLStat
*
* Revision 1.13 2001/07/18 04:00:49 warmerda
* added CPL_CVSID
*
* Revision 1.12 2001/03/09 03:19:24 danmo
* Set pszRLBuffer=NULL after freeing it to avoid reallocating an invalid ptr
*
* Revision 1.11 2001/03/05 03:37:19 warmerda
* Improve support for recovering CPLReadLine() working buffer.
*
* Revision 1.10 2001/01/19 21:16:41 warmerda
* expanded tabs
*
* Revision 1.9 2000/04/17 15:56:11 warmerda
* make configuration tests always happen
*
* Revision 1.8 2000/04/05 21:02:47 warmerda
* Added CPLVerifyConfiguration()
*
* Revision 1.7 1999/08/27 12:55:39 danmo
* Support 0 bytes allocations in CPLRealloc()
*
* Revision 1.6 1999/06/25 04:38:03 warmerda
* Fixed CPLReadLine() to work for long lines.
*
* Revision 1.5 1999/05/20 02:54:37 warmerda
* Added API documentation
*
* Revision 1.4 1999/01/02 20:29:53 warmerda
* Allow zero length allocations
*
* Revision 1.3 1998/12/15 19:01:07 warmerda
* Added CPLReadLine().
*
* Revision 1.2 1998/12/03 18:30:04 warmerda
* Use CPLError() instead of GPSError().
*
* Revision 1.1 1998/12/02 19:33:23 warmerda
* New
*
*/
#include "cpl_conv.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "cpl_multiproc.h"
CPL_CVSID("$Id: cpl_conv.cpp,v 1.52 2006/03/04 00:46:43 mloskot Exp $");
#if defined(WIN32CE)
# include "cpl_wince.h"
# include <wce_errno.h>
#endif
static void *hConfigMutex = NULL;
static volatile char **papszConfigOptions = NULL;
static void *hSharedFileMutex = NULL;
static volatile int nSharedFileCount = 0;
static volatile CPLSharedFileInfo *pasSharedFileList = NULL;
/************************************************************************/
/* CPLCalloc() */
/************************************************************************/
/**
* Safe version of calloc().
*
* This function is like the C library calloc(), but raises a CE_Fatal
* error with CPLError() if it fails to allocate the desired memory. It
* should be used for small memory allocations that are unlikely to fail
* and for which the application is unwilling to test for out of memory
* conditions. It uses VSICalloc() to get the memory, so any hooking of
* VSICalloc() will apply to CPLCalloc() as well. CPLFree() or VSIFree()
* can be used free memory allocated by CPLCalloc().
*
* @param nCount number of objects to allocate.
* @param nSize size (in bytes) of object to allocate.
* @return pointer to newly allocated memory, only NULL if nSize * nCount is
* NULL.
*/
void *CPLCalloc( size_t nCount, size_t nSize )
{
void *pReturn;
if( nSize * nCount == 0 )
return NULL;
pReturn = VSICalloc( nCount, nSize );
if( pReturn == NULL )
{
CPLError( CE_Fatal, CPLE_OutOfMemory,
"CPLCalloc(): Out of memory allocating %d bytes.\n",
nSize * nCount );
}
return pReturn;
}
/************************************************************************/
/* CPLMalloc() */
/************************************************************************/
/**
* Safe version of malloc().
*
* This function is like the C library malloc(), but raises a CE_Fatal
* error with CPLError() if it fails to allocate the desired memory. It
* should be used for small memory allocations that are unlikely to fail
* and for which the application is unwilling to test for out of memory
* conditions. It uses VSIMalloc() to get the memory, so any hooking of
* VSIMalloc() will apply to CPLMalloc() as well. CPLFree() or VSIFree()
* can be used free memory allocated by CPLMalloc().
*
* @param nSize size (in bytes) of memory block to allocate.
* @return pointer to newly allocated memory, only NULL if nSize is zero.
*/
void *CPLMalloc( size_t nSize )
{
void *pReturn;
CPLVerifyConfiguration();
if( nSize == 0 )
return NULL;
if( nSize < 0 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"CPLMalloc(%d): Silly size requested.\n",
nSize );
return NULL;
}
pReturn = VSIMalloc( nSize );
if( pReturn == NULL )
{
CPLError( CE_Fatal, CPLE_OutOfMemory,
"CPLMalloc(): Out of memory allocating %d bytes.\n",
nSize );
}
return pReturn;
}
/************************************************************************/
/* CPLRealloc() */
/************************************************************************/
/**
* Safe version of realloc().
*
* This function is like the C library realloc(), but raises a CE_Fatal
* error with CPLError() if it fails to allocate the desired memory. It
* should be used for small memory allocations that are unlikely to fail
* and for which the application is unwilling to test for out of memory
* conditions. It uses VSIRealloc() to get the memory, so any hooking of
* VSIRealloc() will apply to CPLRealloc() as well. CPLFree() or VSIFree()
* can be used free memory allocated by CPLRealloc().
*
* It is also safe to pass NULL in as the existing memory block for
* CPLRealloc(), in which case it uses VSIMalloc() to allocate a new block.
*
* @param pData existing memory block which should be copied to the new block.
* @param nNewSize new size (in bytes) of memory block to allocate.
* @return pointer to allocated memory, only NULL if nNewSize is zero.
*/
void * CPLRealloc( void * pData, size_t nNewSize )
{
void *pReturn;
if ( nNewSize == 0 )
{
VSIFree(pData);
return NULL;
}
if( nNewSize < 0 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"CPLRealloc(%d): Silly size requested.\n",
nNewSize );
return NULL;
}
if( pData == NULL )
pReturn = VSIMalloc( nNewSize );
else
pReturn = VSIRealloc( pData, nNewSize );
if( pReturn == NULL )
{
CPLError( CE_Fatal, CPLE_OutOfMemory,
"CPLRealloc(): Out of memory allocating %d bytes.\n",
nNewSize );
}
return pReturn;
}
/************************************************************************/
/* CPLStrdup() */
/************************************************************************/
/**
* Safe version of strdup() function.
*
* This function is similar to the C library strdup() function, but if
* the memory allocation fails it will issue a CE_Fatal error with
* CPLError() instead of returning NULL. It uses VSIStrdup(), so any
* hooking of that function will apply to CPLStrdup() as well. Memory
* allocated with CPLStrdup() can be freed with CPLFree() or VSIFree().
*
* It is also safe to pass a NULL string into CPLStrdup(). CPLStrdup()
* will allocate and return a zero length string (as opposed to a NULL
* string).
*
* @param pszString input string to be duplicated. May be NULL.
* @return pointer to a newly allocated copy of the string. Free with
* CPLFree() or VSIFree().
*/
char *CPLStrdup( const char * pszString )
{
char *pszReturn;
if( pszString == NULL )
pszString = "";
pszReturn = VSIStrdup( pszString );
if( pszReturn == NULL )
{
CPLError( CE_Fatal, CPLE_OutOfMemory,
"CPLStrdup(): Out of memory allocating %d bytes.\n",
strlen(pszString) );
}
return( pszReturn );
}
/************************************************************************/
/* CPLFGets() */
/* */
/* Note: CR = \r = ASCII 13 */
/* LF = \n = ASCII 10 */
/************************************************************************/
/**
* Reads in at most one less than nBufferSize characters from the fp
* stream and stores them into the buffer pointed to by pszBuffer.
* Reading stops after an EOF or a newline. If a newline is read, it
* is _not_ stored into the buffer. A '\0' is stored after the last
* character in the buffer. All three types of newline terminators
* recognized by the CPLFGets(): single '\r' and '\n' and '\r\n'
* combination.
*
* @param pszBuffer pointer to the targeting character buffer.
* @param nBufferSize maximum size of the string to read (not including
* termonating '\0').
* @param fp file pointer to read from.
* @return pointer to the pszBuffer containing a string read
* from the file or NULL if the error or end of file was encountered.
*/
char *CPLFGets( char *pszBuffer, int nBufferSize, FILE * fp )
{
int nActuallyRead, nOriginalOffset;
if ( nBufferSize == 0 || pszBuffer == NULL || fp == NULL )
return NULL;
/* -------------------------------------------------------------------- */
/* Let the OS level call read what it things is one line. This */
/* will include the newline. On windows, if the file happens */
/* to be in text mode, the CRLF will have been converted to */
/* just the newline (LF). If it is in binary mode it may well */
/* have both. */
/* -------------------------------------------------------------------- */
nOriginalOffset = VSIFTell( fp );
if( VSIFGets( pszBuffer, nBufferSize, fp ) == NULL )
return NULL;
nActuallyRead = strlen(pszBuffer);
if ( nActuallyRead == 0 )
return NULL;
/* -------------------------------------------------------------------- */
/* If we found \r and out buffer is full, it is possible there */
/* is also a pending \n. Check for it. */
/* -------------------------------------------------------------------- */
if( nBufferSize == nActuallyRead+1
&& pszBuffer[nActuallyRead-1] == 13 )
{
int chCheck;
chCheck = fgetc( fp );
if( chCheck != 10 )
{
// unget the character.
VSIFSeek( fp, nOriginalOffset+nActuallyRead, SEEK_SET );
}
}
/* -------------------------------------------------------------------- */
/* Trim off \n, \r or \r\n if it appears at the end. We don't */
/* need to do any "seeking" since we want the newline eaten. */
/* -------------------------------------------------------------------- */
if( nActuallyRead > 1
&& pszBuffer[nActuallyRead-1] == 10
&& pszBuffer[nActuallyRead-2] == 13 )
{
pszBuffer[nActuallyRead-2] = '\0';
}
else if( pszBuffer[nActuallyRead-1] == 10
|| pszBuffer[nActuallyRead-1] == 13 )
{
pszBuffer[nActuallyRead-1] = '\0';
}
/* -------------------------------------------------------------------- */
/* Search within the string for a \r (MacOS convention */
/* apparently), and if we find it we need to trim the string, */
/* and seek back. */
/* -------------------------------------------------------------------- */
char *pszExtraNewline = strchr( pszBuffer, 13 );
if( pszExtraNewline != NULL )
{
int chCheck;
nActuallyRead = pszExtraNewline - pszBuffer + 1;
*pszExtraNewline = '\0';
VSIFSeek( fp, nOriginalOffset + nActuallyRead - 1, SEEK_SET );
/*
* This hackery is necessary to try and find our correct
* spot on win32 systems with text mode line translation going
* on. Sometimes the fseek back overshoots, but it doesn't
* "realize it" till a character has been read. Try to read till
* we get to the right spot and get our CR.
*/
chCheck = fgetc( fp );
while( (chCheck != 13 && chCheck != EOF)
|| VSIFTell(fp) < nOriginalOffset + nActuallyRead )
{
static volatile int bWarned = FALSE;
if( !bWarned )
{
bWarned = TRUE;
CPLDebug( "CPL", "CPLFGets() correcting for DOS text mode translation seek problem." );
}
chCheck = fgetc( fp );
}
}
return pszBuffer;
}
/************************************************************************/
/* CPLReadLineBuffer() */
/* */
/* Fetch readline buffer, and ensure it is the desired size, */
/* reallocating if needed. Manages TLS (thread local storage) */
/* issues for the buffer. */
/************************************************************************/
static char *CPLReadLineBuffer( int nRequiredSize )
{
/* -------------------------------------------------------------------- */
/* A required size of -1 means the buffer should be freed. */
/* -------------------------------------------------------------------- */
if( nRequiredSize == -1 )
{
if( CPLGetTLS( CTLS_RLBUFFERINFO ) != NULL )
{
CPLFree( CPLGetTLS( CTLS_RLBUFFERINFO ) );
CPLSetTLS( CTLS_RLBUFFERINFO, NULL, FALSE );
}
return NULL;
}
/* -------------------------------------------------------------------- */
/* If the buffer doesn't exist yet, create it. */
/* -------------------------------------------------------------------- */
GUInt32 *pnAlloc = (GUInt32 *) CPLGetTLS( CTLS_RLBUFFERINFO );
if( pnAlloc == NULL )
{
pnAlloc = (GUInt32 *) CPLMalloc(200);
*pnAlloc = 196;
CPLSetTLS( CTLS_RLBUFFERINFO, pnAlloc, TRUE );
}
/* -------------------------------------------------------------------- */
/* If it is too small, grow it bigger. */
/* -------------------------------------------------------------------- */
if( (int) *pnAlloc < nRequiredSize+1 )
{
int nNewSize = nRequiredSize + 4 + 500;
pnAlloc = (GUInt32 *) CPLRealloc(pnAlloc,nNewSize);
if( pnAlloc == NULL )
{
CPLSetTLS( CTLS_RLBUFFERINFO, NULL, FALSE );
return NULL;
}
*pnAlloc = nNewSize - 4;
CPLSetTLS( CTLS_RLBUFFERINFO, pnAlloc, TRUE );
}
return (char *) (pnAlloc+1);
}
/************************************************************************/
/* CPLReadLine() */
/************************************************************************/
/**
* Simplified line reading from text file.
*
* Read a line of text from the given file handle, taking care
* to capture CR and/or LF and strip off ... equivelent of
* DKReadLine(). Pointer to an internal buffer is returned.
* The application shouldn't free it, or depend on it's value
* past the next call to CPLReadLine().
*
* Note that CPLReadLine() uses VSIFGets(), so any hooking of VSI file
* services should apply to CPLReadLine() as well.
*
* CPLReadLine() maintains an internal buffer, which will appear as a
* single block memory leak in some circumstances. CPLReadLine() may
* be called with a NULL FILE * at any time to free this working buffer.
*
* @param fp file pointer opened with VSIFOpen().
*
* @return pointer to an internal buffer containing a line of text read
* from the file or NULL if the end of file was encountered.
*/
const char *CPLReadLine( FILE * fp )
{
char *pszRLBuffer = CPLReadLineBuffer(1);
int nReadSoFar = 0;
/* -------------------------------------------------------------------- */
/* Cleanup case. */
/* -------------------------------------------------------------------- */
if( fp == NULL )
{
CPLReadLineBuffer( -1 );
return NULL;
}
/* -------------------------------------------------------------------- */
/* Loop reading chunks of the line till we get to the end of */
/* the line. */
/* -------------------------------------------------------------------- */
int nBytesReadThisTime;
do {
/* -------------------------------------------------------------------- */
/* Grow the working buffer if we have it nearly full. Fail out */
/* of read line if we can't reallocate it big enough (for */
/* instance for a _very large_ file with no newlines). */
/* -------------------------------------------------------------------- */
pszRLBuffer = CPLReadLineBuffer( nReadSoFar + 129 );
if( pszRLBuffer == NULL )
return NULL;
/* -------------------------------------------------------------------- */
/* Do the actual read. */
/* -------------------------------------------------------------------- */
if( CPLFGets( pszRLBuffer+nReadSoFar, 128, fp ) == NULL )
return NULL;
nBytesReadThisTime = strlen(pszRLBuffer+nReadSoFar);
nReadSoFar += nBytesReadThisTime;
} while( nBytesReadThisTime >= 127
&& pszRLBuffer[nReadSoFar-1] != 13
&& pszRLBuffer[nReadSoFar-1] != 10 );
return( pszRLBuffer );
}
/************************************************************************/
/* CPLReadLineL() */
/************************************************************************/
/**
* Simplified line reading from text file.
*
* Similar to CPLReadLine(), but reading from a large file API handle.
*
* @param fp file pointer opened with VSIFOpenL().
*
* @return pointer to an internal buffer containing a line of text read
* from the file or NULL if the end of file was encountered.
*/
const char *CPLReadLineL( FILE * fp )
{
/* -------------------------------------------------------------------- */
/* Cleanup case. */
/* -------------------------------------------------------------------- */
if( fp == NULL )
{
CPLReadLineBuffer( -1 );
return NULL;
}
/* -------------------------------------------------------------------- */
/* Loop reading chunks of the line till we get to the end of */
/* the line. */
/* -------------------------------------------------------------------- */
char *pszRLBuffer;
const int nChunkSize = 40;
char szChunk[nChunkSize];
int nChunkBytesRead = 0;
int nBufLength = 0;
int nChunkBytesConsumed = 0;
while( TRUE )
{
/* -------------------------------------------------------------------- */
/* Read a chunk from the input file. */
/* -------------------------------------------------------------------- */
pszRLBuffer = CPLReadLineBuffer( nBufLength + nChunkSize + 1 );
if( nChunkBytesRead == nChunkBytesConsumed + 1 )
{
// case where one character is left over from last read.
szChunk[0] = szChunk[nChunkBytesConsumed];
nChunkBytesRead = VSIFReadL( szChunk+1, 1, nChunkSize-1, fp ) + 1;
if( nChunkBytesRead == 1 )
break;
}
else
{
// fresh read.
nChunkBytesRead = VSIFReadL( szChunk, 1, nChunkSize, fp );
if( nChunkBytesRead == 0 )
break;
}
/* -------------------------------------------------------------------- */
/* copy over characters watching for end-of-line. */
/* -------------------------------------------------------------------- */
int bBreak = FALSE;
nChunkBytesConsumed = 0;
while( nChunkBytesConsumed < nChunkBytesRead-1 && !bBreak )
{
if( (szChunk[nChunkBytesConsumed] == 13
&& szChunk[nChunkBytesConsumed+1] == 10)
|| (szChunk[nChunkBytesConsumed] == 10
&& szChunk[nChunkBytesConsumed+1] == 13) )
{
nChunkBytesConsumed += 2;
bBreak = TRUE;
}
else if( szChunk[nChunkBytesConsumed] == 10
|| szChunk[nChunkBytesConsumed] == 13 )
{
nChunkBytesConsumed += 1;
bBreak = TRUE;
}
else
pszRLBuffer[nBufLength++] = szChunk[nChunkBytesConsumed++];
}
if( bBreak )
break;
/* -------------------------------------------------------------------- */
/* If there is a remaining character and it is not a newline */
/* consume it. If it is a newline, but we are clearly at the */
/* end of the file then consume it. */
/* -------------------------------------------------------------------- */
if( nChunkBytesConsumed == nChunkBytesRead-1
&& nChunkBytesRead < nChunkSize )
{
if( szChunk[nChunkBytesConsumed] == 10
|| szChunk[nChunkBytesConsumed] == 13 )
{
nChunkBytesConsumed++;
break;
}
pszRLBuffer[nBufLength++] = szChunk[nChunkBytesConsumed++];
break;
}
}
/* -------------------------------------------------------------------- */
/* If we have left over bytes after breaking out, seek back to */
/* ensure they remain to be read next time. */
/* -------------------------------------------------------------------- */
if( nChunkBytesConsumed < nChunkBytesRead )
{
int nBytesToPush = nChunkBytesRead - nChunkBytesConsumed;
VSIFSeekL( fp, VSIFTellL( fp ) - nBytesToPush, SEEK_SET );
}
pszRLBuffer[nBufLength] = '\0';
return( pszRLBuffer );
}
/************************************************************************/
/* CPLScanString() */
/************************************************************************/
/**
* Scan up to a maximum number of characters from a given string,
* allocate a buffer for a new string and fill it with scanned characters.
*
* @param pszString String containing characters to be scanned. It may be
* terminated with a null character.
*
* @param nMaxLength The maximum number of character to read. Less
* characters will be read if a null character is encountered.
*
* @param bTrimSpaces If TRUE, trim ending spaces from the input string.
* Character considered as empty using isspace(3) function.
*
* @param bNormalize If TRUE, replace ':' symbol with the '_'. It is needed if
* resulting string will be used in CPL dictionaries.
*
* @return Pointer to the resulting string buffer. Caller responsible to free
* this buffer with CPLFree().
*/
char *CPLScanString( const char *pszString, int nMaxLength,
int bTrimSpaces, int bNormalize )
{
char *pszBuffer;
if ( !pszString )
return NULL;
if ( !nMaxLength )
return CPLStrdup( "" );
pszBuffer = (char *)CPLMalloc( nMaxLength + 1 );
if ( !pszBuffer )
return NULL;
strncpy( pszBuffer, pszString, nMaxLength );
pszBuffer[nMaxLength] = '\0';
if ( bTrimSpaces )
{
size_t i = strlen( pszBuffer );
while ( i-- > 0 && isspace(pszBuffer[i]) )
pszBuffer[i] = '\0';
}
if ( bNormalize )
{
size_t i = strlen( pszBuffer );
while ( i-- > 0 )
{
if ( pszBuffer[i] == ':' )
pszBuffer[i] = '_';
}
}
return pszBuffer;
}
/************************************************************************/
/* CPLScanLong() */
/************************************************************************/
/**
* Scan up to a maximum number of characters from a string and convert
* the result to a long.
*
* @param pszString String containing characters to be scanned. It may be
* terminated with a null character.
*
* @param nMaxLength The maximum number of character to consider as part
* of the number. Less characters will be considered if a null character
* is encountered.
*
* @return Long value, converted from its ASCII form.
*/
long CPLScanLong( const char *pszString, int nMaxLength )
{
long iValue;
char *pszValue = (char *)CPLMalloc( nMaxLength + 1);
/* -------------------------------------------------------------------- */
/* Compute string into local buffer, and terminate it. */
/* -------------------------------------------------------------------- */
strncpy( pszValue, pszString, nMaxLength );
pszValue[nMaxLength] = '\0';
/* -------------------------------------------------------------------- */
/* Use atol() to fetch out the result */
/* -------------------------------------------------------------------- */
iValue = atol( pszValue );
CPLFree( pszValue );
return iValue;
}
/************************************************************************/
/* CPLScanUIntBig() */
/************************************************************************/
/**
* Extract big integer from string.
*
* Scan up to a maximum number of characters from a string and convert
* the result to a GUIntBig.
*
* @param pszString String containing characters to be scanned. It may be
* terminated with a null character.
*
* @param nMaxLength The maximum number of character to consider as part
* of the number. Less characters will be considered if a null character
* is encountered.
*
* @return GUIntBig value, converted from its ASCII form.
*/
GUIntBig CPLScanUIntBig( const char *pszString, int nMaxLength )
{
GUIntBig iValue;
char *pszValue = (char *)CPLMalloc( nMaxLength + 1);
/* -------------------------------------------------------------------- */
/* Compute string into local buffer, and terminate it. */
/* -------------------------------------------------------------------- */
strncpy( pszValue, pszString, nMaxLength );
pszValue[nMaxLength] = '\0';
/* -------------------------------------------------------------------- */
/* Fetch out the result */
/* -------------------------------------------------------------------- */
#if defined(WIN32) && defined(_MSC_VER)
iValue = _atoi64( pszValue );
# elif HAVE_ATOLL
iValue = atoll( pszValue );
#else
iValue = atol( pszValue );
#endif
CPLFree( pszValue );
return iValue;
}
/************************************************************************/
/* CPLScanPointer() */
/************************************************************************/
/**
* Extract pointer from string.
*
* Scan up to a maximum number of characters from a string and convert
* the result to a GUIntBig.
*
* @param pszString String containing characters to be scanned. It may be
* terminated with a null character.
*
* @param nMaxLength The maximum number of character to consider as part
* of the number. Less characters will be considered if a null character
* is encountered.
*
* @return pointer value, converted from its ASCII form.
*/
void *CPLScanPointer( const char *pszString, int nMaxLength )
{
void *pResult;
char szTemp[128];
/* -------------------------------------------------------------------- */
/* Compute string into local buffer, and terminate it. */
/* -------------------------------------------------------------------- */
if( nMaxLength > (int) sizeof(szTemp)-1 )
nMaxLength = sizeof(szTemp)-1;
strncpy( szTemp, pszString, nMaxLength );
szTemp[nMaxLength] = '\0';
/* -------------------------------------------------------------------- */
/* On MSVC we have to scanf pointer values without the 0x */
/* prefix. */
/* -------------------------------------------------------------------- */
if( EQUALN(szTemp,"0x",2) )
{
pResult = NULL;
#if defined(WIN32) && defined(_MSC_VER)
sscanf( szTemp+2, "%p", &pResult );
#else
sscanf( szTemp, "%p", &pResult );
#endif
}
else
{
pResult = (void *) CPLScanUIntBig( szTemp, nMaxLength );
}
return pResult;
}
/************************************************************************/
/* CPLScanDouble() */
/************************************************************************/
/**
* Scan up to a maximum number of characters from a string and convert
* the result to a double.
*
* @param pszString String containing characters to be scanned. It may be
* terminated with a null character.
*
* @param nMaxLength The maximum number of character to consider as part
* of the number. Less characters will be considered if a null character
* is encountered.
*
* @param pszLocale Pointer to a character string containing locale name
* ("C", "POSIX", "us_US", "ru_RU.KOI8-R" etc.). If NULL, we will not
* manipulate with locale settings and current process locale will be used for
* printing. Wee need this setting because in different locales decimal
* delimiter represented with the different characters. With the pszLocale
* option we can control what exact locale will be used for scanning a numeric
* value from the string (in most cases it should be C/POSIX).
*