/src/gdal/port/cpl_conv.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: CPL - Common Portability Library |
4 | | * Purpose: Convenience functions. |
5 | | * Author: Frank Warmerdam, warmerdam@pobox.com |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 1998, Frank Warmerdam |
9 | | * Copyright (c) 2007-2014, Even Rouault <even dot rouault at spatialys.com> |
10 | | * |
11 | | * SPDX-License-Identifier: MIT |
12 | | ****************************************************************************/ |
13 | | |
14 | | #include "cpl_config.h" |
15 | | |
16 | | #ifdef HAVE_USELOCALE |
17 | | // For uselocale, define _XOPEN_SOURCE = 700 |
18 | | // and OpenBSD with libcxx 19.1.7 requires 800 for vasprintf |
19 | | // (cf https://github.com/OSGeo/gdal/issues/12619) |
20 | | // (not sure if the following is still up to date...) but on Solaris, we don't |
21 | | // have uselocale and we cannot have std=c++11 with _XOPEN_SOURCE != 600 |
22 | | #if defined(__sun__) && __cplusplus >= 201103L |
23 | | #if _XOPEN_SOURCE != 600 |
24 | | #ifdef _XOPEN_SOURCE |
25 | | #undef _XOPEN_SOURCE |
26 | | #endif |
27 | | #define _XOPEN_SOURCE 600 |
28 | | #endif |
29 | | #else |
30 | | #ifdef _XOPEN_SOURCE |
31 | | #undef _XOPEN_SOURCE |
32 | | #endif |
33 | | #define _XOPEN_SOURCE 800 |
34 | | #endif |
35 | | #endif |
36 | | |
37 | | // For atoll (at least for NetBSD) |
38 | | #ifndef _ISOC99_SOURCE |
39 | | #define _ISOC99_SOURCE |
40 | | #endif |
41 | | |
42 | | #ifdef MSVC_USE_VLD |
43 | | #include <vld.h> |
44 | | #endif |
45 | | |
46 | | #include "cpl_conv.h" |
47 | | |
48 | | #include <algorithm> |
49 | | #include <atomic> |
50 | | #include <cctype> |
51 | | #include <cerrno> |
52 | | #include <climits> |
53 | | #include <clocale> |
54 | | #include <cmath> |
55 | | #include <cstdlib> |
56 | | #include <cstring> |
57 | | #include <ctime> |
58 | | #include <mutex> |
59 | | #include <set> |
60 | | |
61 | | #if HAVE_UNISTD_H |
62 | | #include <unistd.h> |
63 | | #endif |
64 | | #if HAVE_XLOCALE_H |
65 | | #include <xlocale.h> // for LC_NUMERIC_MASK on MacOS |
66 | | #endif |
67 | | |
68 | | #include <sys/types.h> // open |
69 | | #include <sys/stat.h> // open |
70 | | #include <fcntl.h> // open |
71 | | |
72 | | #ifdef _WIN32 |
73 | | #include <io.h> // _isatty, _wopen |
74 | | #else |
75 | | #include <unistd.h> // isatty |
76 | | #endif |
77 | | |
78 | | #include <string> |
79 | | |
80 | | #if __cplusplus >= 202002L |
81 | | #include <bit> // For std::endian |
82 | | #endif |
83 | | |
84 | | #include "cpl_config.h" |
85 | | #include "cpl_multiproc.h" |
86 | | #include "cpl_string.h" |
87 | | #include "cpl_vsi.h" |
88 | | #include "cpl_vsil_curl_priv.h" |
89 | | #include "cpl_known_config_options.h" |
90 | | |
91 | | #ifdef DEBUG |
92 | | #define OGRAPISPY_ENABLED |
93 | | #endif |
94 | | #ifdef OGRAPISPY_ENABLED |
95 | | // Keep in sync with ograpispy.cpp |
96 | | void OGRAPISPYCPLSetConfigOption(const char *, const char *); |
97 | | void OGRAPISPYCPLSetThreadLocalConfigOption(const char *, const char *); |
98 | | #endif |
99 | | |
100 | | // Uncomment to get list of options that have been fetched and set. |
101 | | // #define DEBUG_CONFIG_OPTIONS |
102 | | |
103 | | static CPLMutex *hConfigMutex = nullptr; |
104 | | static volatile char **g_papszConfigOptions = nullptr; |
105 | | static bool gbIgnoreEnvVariables = |
106 | | false; // if true, only take into account configuration options set through |
107 | | // configuration file or |
108 | | // CPLSetConfigOption()/CPLSetThreadLocalConfigOption() |
109 | | |
110 | | static std::vector<std::pair<CPLSetConfigOptionSubscriber, void *>> |
111 | | gSetConfigOptionSubscribers{}; |
112 | | |
113 | | // Used by CPLOpenShared() and friends. |
114 | | static CPLMutex *hSharedFileMutex = nullptr; |
115 | | static int nSharedFileCount = 0; |
116 | | static CPLSharedFileInfo *pasSharedFileList = nullptr; |
117 | | |
118 | | // Used by CPLsetlocale(). |
119 | | static CPLMutex *hSetLocaleMutex = nullptr; |
120 | | |
121 | | // Note: ideally this should be added in CPLSharedFileInfo* |
122 | | // but CPLSharedFileInfo is exposed in the API, hence that trick |
123 | | // to hide this detail. |
124 | | typedef struct |
125 | | { |
126 | | GIntBig nPID; // pid of opening thread. |
127 | | } CPLSharedFileInfoExtra; |
128 | | |
129 | | static volatile CPLSharedFileInfoExtra *pasSharedFileListExtra = nullptr; |
130 | | |
131 | | /************************************************************************/ |
132 | | /* CPLCalloc() */ |
133 | | /************************************************************************/ |
134 | | |
135 | | /** |
136 | | * Safe version of calloc(). |
137 | | * |
138 | | * This function is like the C library calloc(), but raises a CE_Fatal |
139 | | * error with CPLError() if it fails to allocate the desired memory. It |
140 | | * should be used for small memory allocations that are unlikely to fail |
141 | | * and for which the application is unwilling to test for out of memory |
142 | | * conditions. It uses VSICalloc() to get the memory, so any hooking of |
143 | | * VSICalloc() will apply to CPLCalloc() as well. CPLFree() or VSIFree() |
144 | | * can be used free memory allocated by CPLCalloc(). |
145 | | * |
146 | | * @param nCount number of objects to allocate. |
147 | | * @param nSize size (in bytes) of object to allocate. |
148 | | * @return pointer to newly allocated memory, only NULL if nSize * nCount is |
149 | | * NULL. |
150 | | */ |
151 | | |
152 | | void *CPLCalloc(size_t nCount, size_t nSize) |
153 | | |
154 | 16.5k | { |
155 | 16.5k | if (nSize * nCount == 0) |
156 | 0 | return nullptr; |
157 | | |
158 | 16.5k | void *pReturn = CPLMalloc(nCount * nSize); |
159 | 16.5k | memset(pReturn, 0, nCount * nSize); |
160 | 16.5k | return pReturn; |
161 | 16.5k | } |
162 | | |
163 | | /************************************************************************/ |
164 | | /* CPLMalloc() */ |
165 | | /************************************************************************/ |
166 | | |
167 | | /** |
168 | | * Safe version of malloc(). |
169 | | * |
170 | | * This function is like the C library malloc(), but raises a CE_Fatal |
171 | | * error with CPLError() if it fails to allocate the desired memory. It |
172 | | * should be used for small memory allocations that are unlikely to fail |
173 | | * and for which the application is unwilling to test for out of memory |
174 | | * conditions. It uses VSIMalloc() to get the memory, so any hooking of |
175 | | * VSIMalloc() will apply to CPLMalloc() as well. CPLFree() or VSIFree() |
176 | | * can be used free memory allocated by CPLMalloc(). |
177 | | * |
178 | | * @param nSize size (in bytes) of memory block to allocate. |
179 | | * @return pointer to newly allocated memory, only NULL if nSize is zero. |
180 | | */ |
181 | | |
182 | | void *CPLMalloc(size_t nSize) |
183 | | |
184 | 119k | { |
185 | 119k | if (nSize == 0) |
186 | 16.7k | return nullptr; |
187 | | |
188 | 102k | if ((nSize >> (8 * sizeof(nSize) - 1)) != 0) |
189 | 0 | { |
190 | | // coverity[dead_error_begin] |
191 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
192 | 0 | "CPLMalloc(%ld): Silly size requested.", |
193 | 0 | static_cast<long>(nSize)); |
194 | 0 | return nullptr; |
195 | 0 | } |
196 | | |
197 | 102k | void *pReturn = VSIMalloc(nSize); |
198 | 102k | if (pReturn == nullptr) |
199 | 0 | { |
200 | 0 | if (nSize < 2000) |
201 | 0 | { |
202 | 0 | CPLEmergencyError("CPLMalloc(): Out of memory allocating a small " |
203 | 0 | "number of bytes."); |
204 | 0 | } |
205 | | |
206 | 0 | CPLError(CE_Fatal, CPLE_OutOfMemory, |
207 | 0 | "CPLMalloc(): Out of memory allocating %ld bytes.", |
208 | 0 | static_cast<long>(nSize)); |
209 | 0 | } |
210 | | |
211 | 102k | return pReturn; |
212 | 102k | } |
213 | | |
214 | | /************************************************************************/ |
215 | | /* CPLRealloc() */ |
216 | | /************************************************************************/ |
217 | | |
218 | | /** |
219 | | * Safe version of realloc(). |
220 | | * |
221 | | * This function is like the C library realloc(), but raises a CE_Fatal |
222 | | * error with CPLError() if it fails to allocate the desired memory. It |
223 | | * should be used for small memory allocations that are unlikely to fail |
224 | | * and for which the application is unwilling to test for out of memory |
225 | | * conditions. It uses VSIRealloc() to get the memory, so any hooking of |
226 | | * VSIRealloc() will apply to CPLRealloc() as well. CPLFree() or VSIFree() |
227 | | * can be used free memory allocated by CPLRealloc(). |
228 | | * |
229 | | * It is also safe to pass NULL in as the existing memory block for |
230 | | * CPLRealloc(), in which case it uses VSIMalloc() to allocate a new block. |
231 | | * |
232 | | * @param pData existing memory block which should be copied to the new block. |
233 | | * @param nNewSize new size (in bytes) of memory block to allocate. |
234 | | * @return pointer to allocated memory, only NULL if nNewSize is zero. |
235 | | */ |
236 | | |
237 | | void *CPLRealloc(void *pData, size_t nNewSize) |
238 | | |
239 | 5.42k | { |
240 | 5.42k | if (nNewSize == 0) |
241 | 0 | { |
242 | 0 | VSIFree(pData); |
243 | 0 | return nullptr; |
244 | 0 | } |
245 | | |
246 | 5.42k | if ((nNewSize >> (8 * sizeof(nNewSize) - 1)) != 0) |
247 | 0 | { |
248 | | // coverity[dead_error_begin] |
249 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
250 | 0 | "CPLRealloc(%ld): Silly size requested.", |
251 | 0 | static_cast<long>(nNewSize)); |
252 | 0 | return nullptr; |
253 | 0 | } |
254 | | |
255 | 5.42k | void *pReturn = nullptr; |
256 | | |
257 | 5.42k | if (pData == nullptr) |
258 | 4.16k | pReturn = VSIMalloc(nNewSize); |
259 | 1.25k | else |
260 | 1.25k | pReturn = VSIRealloc(pData, nNewSize); |
261 | | |
262 | 5.42k | if (pReturn == nullptr) |
263 | 0 | { |
264 | 0 | if (nNewSize < 2000) |
265 | 0 | { |
266 | 0 | char szSmallMsg[80] = {}; |
267 | |
|
268 | 0 | snprintf(szSmallMsg, sizeof(szSmallMsg), |
269 | 0 | "CPLRealloc(): Out of memory allocating %ld bytes.", |
270 | 0 | static_cast<long>(nNewSize)); |
271 | 0 | CPLEmergencyError(szSmallMsg); |
272 | 0 | } |
273 | 0 | else |
274 | 0 | { |
275 | 0 | CPLError(CE_Fatal, CPLE_OutOfMemory, |
276 | 0 | "CPLRealloc(): Out of memory allocating %ld bytes.", |
277 | 0 | static_cast<long>(nNewSize)); |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | 5.42k | return pReturn; |
282 | 5.42k | } |
283 | | |
284 | | /************************************************************************/ |
285 | | /* CPLStrdup() */ |
286 | | /************************************************************************/ |
287 | | |
288 | | /** |
289 | | * Safe version of strdup() function. |
290 | | * |
291 | | * This function is similar to the C library strdup() function, but if |
292 | | * the memory allocation fails it will issue a CE_Fatal error with |
293 | | * CPLError() instead of returning NULL. Memory |
294 | | * allocated with CPLStrdup() can be freed with CPLFree() or VSIFree(). |
295 | | * |
296 | | * It is also safe to pass a NULL string into CPLStrdup(). CPLStrdup() |
297 | | * will allocate and return a zero length string (as opposed to a NULL |
298 | | * string). |
299 | | * |
300 | | * @param pszString input string to be duplicated. May be NULL. |
301 | | * @return pointer to a newly allocated copy of the string. Free with |
302 | | * CPLFree() or VSIFree(). |
303 | | */ |
304 | | |
305 | | char *CPLStrdup(const char *pszString) |
306 | | |
307 | 13.8k | { |
308 | 13.8k | if (pszString == nullptr) |
309 | 0 | pszString = ""; |
310 | | |
311 | 13.8k | const size_t nLen = strlen(pszString); |
312 | 13.8k | char *pszReturn = static_cast<char *>(CPLMalloc(nLen + 1)); |
313 | 13.8k | memcpy(pszReturn, pszString, nLen + 1); |
314 | 13.8k | return (pszReturn); |
315 | 13.8k | } |
316 | | |
317 | | /************************************************************************/ |
318 | | /* CPLStrlwr() */ |
319 | | /************************************************************************/ |
320 | | |
321 | | /** |
322 | | * Convert each characters of the string to lower case. |
323 | | * |
324 | | * For example, "ABcdE" will be converted to "abcde". |
325 | | * Starting with GDAL 3.9, this function is no longer locale dependent. |
326 | | * |
327 | | * @param pszString input string to be converted. |
328 | | * @return pointer to the same string, pszString. |
329 | | */ |
330 | | |
331 | | char *CPLStrlwr(char *pszString) |
332 | | |
333 | 0 | { |
334 | 0 | if (pszString == nullptr) |
335 | 0 | return nullptr; |
336 | | |
337 | 0 | char *pszTemp = pszString; |
338 | |
|
339 | 0 | while (*pszTemp) |
340 | 0 | { |
341 | 0 | *pszTemp = |
342 | 0 | static_cast<char>(CPLTolower(static_cast<unsigned char>(*pszTemp))); |
343 | 0 | pszTemp++; |
344 | 0 | } |
345 | |
|
346 | 0 | return pszString; |
347 | 0 | } |
348 | | |
349 | | /************************************************************************/ |
350 | | /* CPLFGets() */ |
351 | | /* */ |
352 | | /* Note: LF = \n = ASCII 10 */ |
353 | | /* CR = \r = ASCII 13 */ |
354 | | /************************************************************************/ |
355 | | |
356 | | // ASCII characters. |
357 | | constexpr char knLF = 10; |
358 | | constexpr char knCR = 13; |
359 | | |
360 | | /** |
361 | | * Reads in at most one less than nBufferSize characters from the fp |
362 | | * stream and stores them into the buffer pointed to by pszBuffer. |
363 | | * Reading stops after an EOF or a newline. If a newline is read, it |
364 | | * is _not_ stored into the buffer. A '\\0' is stored after the last |
365 | | * character in the buffer. All three types of newline terminators |
366 | | * recognized by the CPLFGets(): single '\\r' and '\\n' and '\\r\\n' |
367 | | * combination. |
368 | | * |
369 | | * @param pszBuffer pointer to the targeting character buffer. |
370 | | * @param nBufferSize maximum size of the string to read (not including |
371 | | * terminating '\\0'). |
372 | | * @param fp file pointer to read from. |
373 | | * @return pointer to the pszBuffer containing a string read |
374 | | * from the file or NULL if the error or end of file was encountered. |
375 | | */ |
376 | | |
377 | | char *CPLFGets(char *pszBuffer, int nBufferSize, FILE *fp) |
378 | | |
379 | 0 | { |
380 | 0 | if (nBufferSize == 0 || pszBuffer == nullptr || fp == nullptr) |
381 | 0 | return nullptr; |
382 | | |
383 | | /* -------------------------------------------------------------------- */ |
384 | | /* Let the OS level call read what it things is one line. This */ |
385 | | /* will include the newline. On windows, if the file happens */ |
386 | | /* to be in text mode, the CRLF will have been converted to */ |
387 | | /* just the newline (LF). If it is in binary mode it may well */ |
388 | | /* have both. */ |
389 | | /* -------------------------------------------------------------------- */ |
390 | 0 | const long nOriginalOffset = VSIFTell(fp); |
391 | 0 | if (VSIFGets(pszBuffer, nBufferSize, fp) == nullptr) |
392 | 0 | return nullptr; |
393 | | |
394 | 0 | int nActuallyRead = static_cast<int>(strlen(pszBuffer)); |
395 | 0 | if (nActuallyRead == 0) |
396 | 0 | return nullptr; |
397 | | |
398 | | /* -------------------------------------------------------------------- */ |
399 | | /* If we found \r and out buffer is full, it is possible there */ |
400 | | /* is also a pending \n. Check for it. */ |
401 | | /* -------------------------------------------------------------------- */ |
402 | 0 | if (nBufferSize == nActuallyRead + 1 && |
403 | 0 | pszBuffer[nActuallyRead - 1] == knCR) |
404 | 0 | { |
405 | 0 | const int chCheck = fgetc(fp); |
406 | 0 | if (chCheck != knLF) |
407 | 0 | { |
408 | | // unget the character. |
409 | 0 | if (VSIFSeek(fp, nOriginalOffset + nActuallyRead, SEEK_SET) == -1) |
410 | 0 | { |
411 | 0 | CPLError(CE_Failure, CPLE_FileIO, |
412 | 0 | "Unable to unget a character"); |
413 | 0 | } |
414 | 0 | } |
415 | 0 | } |
416 | | |
417 | | /* -------------------------------------------------------------------- */ |
418 | | /* Trim off \n, \r or \r\n if it appears at the end. We don't */ |
419 | | /* need to do any "seeking" since we want the newline eaten. */ |
420 | | /* -------------------------------------------------------------------- */ |
421 | 0 | if (nActuallyRead > 1 && pszBuffer[nActuallyRead - 1] == knLF && |
422 | 0 | pszBuffer[nActuallyRead - 2] == knCR) |
423 | 0 | { |
424 | 0 | pszBuffer[nActuallyRead - 2] = '\0'; |
425 | 0 | } |
426 | 0 | else if (pszBuffer[nActuallyRead - 1] == knLF || |
427 | 0 | pszBuffer[nActuallyRead - 1] == knCR) |
428 | 0 | { |
429 | 0 | pszBuffer[nActuallyRead - 1] = '\0'; |
430 | 0 | } |
431 | | |
432 | | /* -------------------------------------------------------------------- */ |
433 | | /* Search within the string for a \r (MacOS convention */ |
434 | | /* apparently), and if we find it we need to trim the string, */ |
435 | | /* and seek back. */ |
436 | | /* -------------------------------------------------------------------- */ |
437 | 0 | char *pszExtraNewline = strchr(pszBuffer, knCR); |
438 | |
|
439 | 0 | if (pszExtraNewline != nullptr) |
440 | 0 | { |
441 | 0 | nActuallyRead = static_cast<int>(pszExtraNewline - pszBuffer + 1); |
442 | |
|
443 | 0 | *pszExtraNewline = '\0'; |
444 | 0 | if (VSIFSeek(fp, nOriginalOffset + nActuallyRead - 1, SEEK_SET) != 0) |
445 | 0 | return nullptr; |
446 | | |
447 | | // This hackery is necessary to try and find our correct |
448 | | // spot on win32 systems with text mode line translation going |
449 | | // on. Sometimes the fseek back overshoots, but it doesn't |
450 | | // "realize it" till a character has been read. Try to read till |
451 | | // we get to the right spot and get our CR. |
452 | 0 | int chCheck = fgetc(fp); |
453 | 0 | while ((chCheck != knCR && chCheck != EOF) || |
454 | 0 | VSIFTell(fp) < nOriginalOffset + nActuallyRead) |
455 | 0 | { |
456 | 0 | static bool bWarned = false; |
457 | |
|
458 | 0 | if (!bWarned) |
459 | 0 | { |
460 | 0 | bWarned = true; |
461 | 0 | CPLDebug("CPL", |
462 | 0 | "CPLFGets() correcting for DOS text mode translation " |
463 | 0 | "seek problem."); |
464 | 0 | } |
465 | 0 | chCheck = fgetc(fp); |
466 | 0 | } |
467 | 0 | } |
468 | | |
469 | 0 | return pszBuffer; |
470 | 0 | } |
471 | | |
472 | | /************************************************************************/ |
473 | | /* CPLReadLineBuffer() */ |
474 | | /* */ |
475 | | /* Fetch readline buffer, and ensure it is the desired size, */ |
476 | | /* reallocating if needed. Manages TLS (thread local storage) */ |
477 | | /* issues for the buffer. */ |
478 | | /* We use a special trick to track the actual size of the buffer */ |
479 | | /* The first 4 bytes are reserved to store it as a int, hence the */ |
480 | | /* -4 / +4 hacks with the size and pointer. */ |
481 | | /************************************************************************/ |
482 | | static char *CPLReadLineBuffer(int nRequiredSize) |
483 | | |
484 | 4.68k | { |
485 | | |
486 | | /* -------------------------------------------------------------------- */ |
487 | | /* A required size of -1 means the buffer should be freed. */ |
488 | | /* -------------------------------------------------------------------- */ |
489 | 4.68k | if (nRequiredSize == -1) |
490 | 0 | { |
491 | 0 | int bMemoryError = FALSE; |
492 | 0 | void *pRet = CPLGetTLSEx(CTLS_RLBUFFERINFO, &bMemoryError); |
493 | 0 | if (pRet != nullptr) |
494 | 0 | { |
495 | 0 | CPLFree(pRet); |
496 | 0 | CPLSetTLS(CTLS_RLBUFFERINFO, nullptr, FALSE); |
497 | 0 | } |
498 | 0 | return nullptr; |
499 | 0 | } |
500 | | |
501 | | /* -------------------------------------------------------------------- */ |
502 | | /* If the buffer doesn't exist yet, create it. */ |
503 | | /* -------------------------------------------------------------------- */ |
504 | 4.68k | int bMemoryError = FALSE; |
505 | 4.68k | GUInt32 *pnAlloc = |
506 | 4.68k | static_cast<GUInt32 *>(CPLGetTLSEx(CTLS_RLBUFFERINFO, &bMemoryError)); |
507 | 4.68k | if (bMemoryError) |
508 | 0 | return nullptr; |
509 | | |
510 | 4.68k | if (pnAlloc == nullptr) |
511 | 1 | { |
512 | 1 | pnAlloc = static_cast<GUInt32 *>(VSI_MALLOC_VERBOSE(200)); |
513 | 1 | if (pnAlloc == nullptr) |
514 | 0 | return nullptr; |
515 | 1 | *pnAlloc = 196; |
516 | 1 | CPLSetTLS(CTLS_RLBUFFERINFO, pnAlloc, TRUE); |
517 | 1 | } |
518 | | |
519 | | /* -------------------------------------------------------------------- */ |
520 | | /* If it is too small, grow it bigger. */ |
521 | | /* -------------------------------------------------------------------- */ |
522 | 4.68k | if (static_cast<int>(*pnAlloc) - 1 < nRequiredSize) |
523 | 4 | { |
524 | 4 | const int nNewSize = nRequiredSize + 4 + 500; |
525 | 4 | if (nNewSize <= 0) |
526 | 0 | { |
527 | 0 | VSIFree(pnAlloc); |
528 | 0 | CPLSetTLS(CTLS_RLBUFFERINFO, nullptr, FALSE); |
529 | 0 | CPLError(CE_Failure, CPLE_OutOfMemory, |
530 | 0 | "CPLReadLineBuffer(): Trying to allocate more than " |
531 | 0 | "2 GB."); |
532 | 0 | return nullptr; |
533 | 0 | } |
534 | | |
535 | 4 | GUInt32 *pnAllocNew = |
536 | 4 | static_cast<GUInt32 *>(VSI_REALLOC_VERBOSE(pnAlloc, nNewSize)); |
537 | 4 | if (pnAllocNew == nullptr) |
538 | 0 | { |
539 | 0 | VSIFree(pnAlloc); |
540 | 0 | CPLSetTLS(CTLS_RLBUFFERINFO, nullptr, FALSE); |
541 | 0 | return nullptr; |
542 | 0 | } |
543 | 4 | pnAlloc = pnAllocNew; |
544 | | |
545 | 4 | *pnAlloc = nNewSize - 4; |
546 | 4 | CPLSetTLS(CTLS_RLBUFFERINFO, pnAlloc, TRUE); |
547 | 4 | } |
548 | | |
549 | 4.68k | return reinterpret_cast<char *>(pnAlloc + 1); |
550 | 4.68k | } |
551 | | |
552 | | /************************************************************************/ |
553 | | /* CPLReadLine() */ |
554 | | /************************************************************************/ |
555 | | |
556 | | /** |
557 | | * Simplified line reading from text file. |
558 | | * |
559 | | * Read a line of text from the given file handle, taking care |
560 | | * to capture CR and/or LF and strip off ... equivalent of |
561 | | * DKReadLine(). Pointer to an internal buffer is returned. |
562 | | * The application shouldn't free it, or depend on its value |
563 | | * past the next call to CPLReadLine(). |
564 | | * |
565 | | * Note that CPLReadLine() uses VSIFGets(), so any hooking of VSI file |
566 | | * services should apply to CPLReadLine() as well. |
567 | | * |
568 | | * CPLReadLine() maintains an internal buffer, which will appear as a |
569 | | * single block memory leak in some circumstances. CPLReadLine() may |
570 | | * be called with a NULL FILE * at any time to free this working buffer. |
571 | | * |
572 | | * @param fp file pointer opened with VSIFOpen(). |
573 | | * |
574 | | * @return pointer to an internal buffer containing a line of text read |
575 | | * from the file or NULL if the end of file was encountered. |
576 | | */ |
577 | | |
578 | | const char *CPLReadLine(FILE *fp) |
579 | | |
580 | 0 | { |
581 | | /* -------------------------------------------------------------------- */ |
582 | | /* Cleanup case. */ |
583 | | /* -------------------------------------------------------------------- */ |
584 | 0 | if (fp == nullptr) |
585 | 0 | { |
586 | 0 | CPLReadLineBuffer(-1); |
587 | 0 | return nullptr; |
588 | 0 | } |
589 | | |
590 | | /* -------------------------------------------------------------------- */ |
591 | | /* Loop reading chunks of the line till we get to the end of */ |
592 | | /* the line. */ |
593 | | /* -------------------------------------------------------------------- */ |
594 | 0 | size_t nBytesReadThisTime = 0; |
595 | 0 | char *pszRLBuffer = nullptr; |
596 | 0 | size_t nReadSoFar = 0; |
597 | |
|
598 | 0 | do |
599 | 0 | { |
600 | | /* -------------------------------------------------------------------- |
601 | | */ |
602 | | /* Grow the working buffer if we have it nearly full. Fail out */ |
603 | | /* of read line if we can't reallocate it big enough (for */ |
604 | | /* instance for a _very large_ file with no newlines). */ |
605 | | /* -------------------------------------------------------------------- |
606 | | */ |
607 | 0 | if (nReadSoFar > 100 * 1024 * 1024) |
608 | | // It is dubious that we need to read a line longer than 100 MB. |
609 | 0 | return nullptr; |
610 | 0 | pszRLBuffer = CPLReadLineBuffer(static_cast<int>(nReadSoFar) + 129); |
611 | 0 | if (pszRLBuffer == nullptr) |
612 | 0 | return nullptr; |
613 | | |
614 | | /* -------------------------------------------------------------------- |
615 | | */ |
616 | | /* Do the actual read. */ |
617 | | /* -------------------------------------------------------------------- |
618 | | */ |
619 | 0 | if (CPLFGets(pszRLBuffer + nReadSoFar, 128, fp) == nullptr && |
620 | 0 | nReadSoFar == 0) |
621 | 0 | return nullptr; |
622 | | |
623 | 0 | nBytesReadThisTime = strlen(pszRLBuffer + nReadSoFar); |
624 | 0 | nReadSoFar += nBytesReadThisTime; |
625 | 0 | } while (nBytesReadThisTime >= 127 && pszRLBuffer[nReadSoFar - 1] != knCR && |
626 | 0 | pszRLBuffer[nReadSoFar - 1] != knLF); |
627 | | |
628 | 0 | return pszRLBuffer; |
629 | 0 | } |
630 | | |
631 | | /************************************************************************/ |
632 | | /* CPLReadLineL() */ |
633 | | /************************************************************************/ |
634 | | |
635 | | /** |
636 | | * Simplified line reading from text file. |
637 | | * |
638 | | * Similar to CPLReadLine(), but reading from a large file API handle. |
639 | | * |
640 | | * @param fp file pointer opened with VSIFOpenL(). |
641 | | * |
642 | | * @return pointer to an internal buffer containing a line of text read |
643 | | * from the file or NULL if the end of file was encountered. |
644 | | */ |
645 | | |
646 | | const char *CPLReadLineL(VSILFILE *fp) |
647 | 1.31k | { |
648 | 1.31k | return CPLReadLine2L(fp, -1, nullptr); |
649 | 1.31k | } |
650 | | |
651 | | /************************************************************************/ |
652 | | /* CPLReadLine2L() */ |
653 | | /************************************************************************/ |
654 | | |
655 | | /** |
656 | | * Simplified line reading from text file. |
657 | | * |
658 | | * Similar to CPLReadLine(), but reading from a large file API handle. |
659 | | * |
660 | | * @param fp file pointer opened with VSIFOpenL(). |
661 | | * @param nMaxCars maximum number of characters allowed, or -1 for no limit. |
662 | | * @param papszOptions NULL-terminated array of options. Unused for now. |
663 | | |
664 | | * @return pointer to an internal buffer containing a line of text read |
665 | | * from the file or NULL if the end of file was encountered or the maximum |
666 | | * number of characters allowed reached. |
667 | | * |
668 | | * @since GDAL 1.7.0 |
669 | | */ |
670 | | |
671 | | const char *CPLReadLine2L(VSILFILE *fp, int nMaxCars, |
672 | | CPL_UNUSED CSLConstList papszOptions) |
673 | | |
674 | 1.31k | { |
675 | 1.31k | int nBufLength; |
676 | 1.31k | return CPLReadLine3L(fp, nMaxCars, &nBufLength, papszOptions); |
677 | 1.31k | } |
678 | | |
679 | | /************************************************************************/ |
680 | | /* CPLReadLine3L() */ |
681 | | /************************************************************************/ |
682 | | |
683 | | /** |
684 | | * Simplified line reading from text file. |
685 | | * |
686 | | * Similar to CPLReadLine(), but reading from a large file API handle. |
687 | | * |
688 | | * @param fp file pointer opened with VSIFOpenL(). |
689 | | * @param nMaxCars maximum number of characters allowed, or -1 for no limit. |
690 | | * @param papszOptions NULL-terminated array of options. Unused for now. |
691 | | * @param[out] pnBufLength size of output string (must be non-NULL) |
692 | | |
693 | | * @return pointer to an internal buffer containing a line of text read |
694 | | * from the file or NULL if the end of file was encountered or the maximum |
695 | | * number of characters allowed reached. |
696 | | * |
697 | | * @since GDAL 2.3.0 |
698 | | */ |
699 | | const char *CPLReadLine3L(VSILFILE *fp, int nMaxCars, int *pnBufLength, |
700 | | CPL_UNUSED CSLConstList papszOptions) |
701 | 1.31k | { |
702 | | /* -------------------------------------------------------------------- */ |
703 | | /* Cleanup case. */ |
704 | | /* -------------------------------------------------------------------- */ |
705 | 1.31k | if (fp == nullptr) |
706 | 0 | { |
707 | 0 | CPLReadLineBuffer(-1); |
708 | 0 | return nullptr; |
709 | 0 | } |
710 | | |
711 | | /* -------------------------------------------------------------------- */ |
712 | | /* Loop reading chunks of the line till we get to the end of */ |
713 | | /* the line. */ |
714 | | /* -------------------------------------------------------------------- */ |
715 | 1.31k | char *pszRLBuffer = nullptr; |
716 | 1.31k | const size_t nChunkSize = 40; |
717 | 1.31k | char szChunk[nChunkSize] = {}; |
718 | 1.31k | size_t nChunkBytesRead = 0; |
719 | 1.31k | size_t nChunkBytesConsumed = 0; |
720 | | |
721 | 1.31k | *pnBufLength = 0; |
722 | 1.31k | szChunk[0] = 0; |
723 | | |
724 | 4.68k | while (true) |
725 | 4.68k | { |
726 | | /* -------------------------------------------------------------------- |
727 | | */ |
728 | | /* Read a chunk from the input file. */ |
729 | | /* -------------------------------------------------------------------- |
730 | | */ |
731 | 4.68k | if (*pnBufLength > INT_MAX - static_cast<int>(nChunkSize) - 1) |
732 | 0 | { |
733 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
734 | 0 | "Too big line : more than 2 billion characters!."); |
735 | 0 | CPLReadLineBuffer(-1); |
736 | 0 | return nullptr; |
737 | 0 | } |
738 | | |
739 | 4.68k | pszRLBuffer = |
740 | 4.68k | CPLReadLineBuffer(static_cast<int>(*pnBufLength + nChunkSize + 1)); |
741 | 4.68k | if (pszRLBuffer == nullptr) |
742 | 0 | return nullptr; |
743 | | |
744 | 4.68k | if (nChunkBytesRead == nChunkBytesConsumed + 1) |
745 | 3.37k | { |
746 | | |
747 | | // case where one character is left over from last read. |
748 | 3.37k | szChunk[0] = szChunk[nChunkBytesConsumed]; |
749 | | |
750 | 3.37k | nChunkBytesConsumed = 0; |
751 | 3.37k | nChunkBytesRead = VSIFReadL(szChunk + 1, 1, nChunkSize - 1, fp) + 1; |
752 | 3.37k | } |
753 | 1.31k | else |
754 | 1.31k | { |
755 | 1.31k | nChunkBytesConsumed = 0; |
756 | | |
757 | | // fresh read. |
758 | 1.31k | nChunkBytesRead = VSIFReadL(szChunk, 1, nChunkSize, fp); |
759 | 1.31k | if (nChunkBytesRead == 0) |
760 | 171 | { |
761 | 171 | if (*pnBufLength == 0) |
762 | 171 | return nullptr; |
763 | | |
764 | 0 | break; |
765 | 171 | } |
766 | 1.31k | } |
767 | | |
768 | | /* -------------------------------------------------------------------- |
769 | | */ |
770 | | /* copy over characters watching for end-of-line. */ |
771 | | /* -------------------------------------------------------------------- |
772 | | */ |
773 | 4.51k | bool bBreak = false; |
774 | 154k | while (nChunkBytesConsumed < nChunkBytesRead - 1 && !bBreak) |
775 | 150k | { |
776 | 150k | if ((szChunk[nChunkBytesConsumed] == knCR && |
777 | 150k | szChunk[nChunkBytesConsumed + 1] == knLF) || |
778 | 150k | (szChunk[nChunkBytesConsumed] == knLF && |
779 | 150k | szChunk[nChunkBytesConsumed + 1] == knCR)) |
780 | 0 | { |
781 | 0 | nChunkBytesConsumed += 2; |
782 | 0 | bBreak = true; |
783 | 0 | } |
784 | 150k | else if (szChunk[nChunkBytesConsumed] == knLF || |
785 | 150k | szChunk[nChunkBytesConsumed] == knCR) |
786 | 1.14k | { |
787 | 1.14k | nChunkBytesConsumed += 1; |
788 | 1.14k | bBreak = true; |
789 | 1.14k | } |
790 | 149k | else |
791 | 149k | { |
792 | 149k | pszRLBuffer[(*pnBufLength)++] = szChunk[nChunkBytesConsumed++]; |
793 | 149k | if (nMaxCars >= 0 && *pnBufLength == nMaxCars) |
794 | 0 | { |
795 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
796 | 0 | "Maximum number of characters allowed reached."); |
797 | 0 | return nullptr; |
798 | 0 | } |
799 | 149k | } |
800 | 150k | } |
801 | | |
802 | 4.51k | if (bBreak) |
803 | 1.14k | break; |
804 | | |
805 | | /* -------------------------------------------------------------------- |
806 | | */ |
807 | | /* If there is a remaining character and it is not a newline */ |
808 | | /* consume it. If it is a newline, but we are clearly at the */ |
809 | | /* end of the file then consume it. */ |
810 | | /* -------------------------------------------------------------------- |
811 | | */ |
812 | 3.37k | if (nChunkBytesConsumed == nChunkBytesRead - 1 && |
813 | 3.37k | nChunkBytesRead < nChunkSize) |
814 | 1 | { |
815 | 1 | if (szChunk[nChunkBytesConsumed] == knLF || |
816 | 1 | szChunk[nChunkBytesConsumed] == knCR) |
817 | 1 | { |
818 | 1 | nChunkBytesConsumed++; |
819 | 1 | break; |
820 | 1 | } |
821 | | |
822 | 0 | pszRLBuffer[(*pnBufLength)++] = szChunk[nChunkBytesConsumed++]; |
823 | 0 | break; |
824 | 1 | } |
825 | 3.37k | } |
826 | | |
827 | | /* -------------------------------------------------------------------- */ |
828 | | /* If we have left over bytes after breaking out, seek back to */ |
829 | | /* ensure they remain to be read next time. */ |
830 | | /* -------------------------------------------------------------------- */ |
831 | 1.14k | if (nChunkBytesConsumed < nChunkBytesRead) |
832 | 1.14k | { |
833 | 1.14k | const size_t nBytesToPush = nChunkBytesRead - nChunkBytesConsumed; |
834 | | |
835 | 1.14k | if (VSIFSeekL(fp, VSIFTellL(fp) - nBytesToPush, SEEK_SET) != 0) |
836 | 0 | return nullptr; |
837 | 1.14k | } |
838 | | |
839 | 1.14k | pszRLBuffer[*pnBufLength] = '\0'; |
840 | | |
841 | 1.14k | return pszRLBuffer; |
842 | 1.14k | } |
843 | | |
844 | | /************************************************************************/ |
845 | | /* CPLScanString() */ |
846 | | /************************************************************************/ |
847 | | |
848 | | /** |
849 | | * Scan up to a maximum number of characters from a given string, |
850 | | * allocate a buffer for a new string and fill it with scanned characters. |
851 | | * |
852 | | * @param pszString String containing characters to be scanned. It may be |
853 | | * terminated with a null character. |
854 | | * |
855 | | * @param nMaxLength The maximum number of character to read. Less |
856 | | * characters will be read if a null character is encountered. |
857 | | * |
858 | | * @param bTrimSpaces If TRUE, trim ending spaces from the input string. |
859 | | * Character considered as empty using isspace(3) function. |
860 | | * |
861 | | * @param bNormalize If TRUE, replace ':' symbol with the '_'. It is needed if |
862 | | * resulting string will be used in CPL dictionaries. |
863 | | * |
864 | | * @return Pointer to the resulting string buffer. Caller responsible to free |
865 | | * this buffer with CPLFree(). |
866 | | */ |
867 | | |
868 | | char *CPLScanString(const char *pszString, int nMaxLength, int bTrimSpaces, |
869 | | int bNormalize) |
870 | 0 | { |
871 | 0 | if (!pszString) |
872 | 0 | return nullptr; |
873 | | |
874 | 0 | if (!nMaxLength) |
875 | 0 | return CPLStrdup(""); |
876 | | |
877 | 0 | char *pszBuffer = static_cast<char *>(CPLMalloc(nMaxLength + 1)); |
878 | 0 | if (!pszBuffer) |
879 | 0 | return nullptr; |
880 | | |
881 | 0 | strncpy(pszBuffer, pszString, nMaxLength); |
882 | 0 | pszBuffer[nMaxLength] = '\0'; |
883 | |
|
884 | 0 | if (bTrimSpaces) |
885 | 0 | { |
886 | 0 | size_t i = strlen(pszBuffer); |
887 | 0 | while (i > 0) |
888 | 0 | { |
889 | 0 | i--; |
890 | 0 | if (!isspace(static_cast<unsigned char>(pszBuffer[i]))) |
891 | 0 | break; |
892 | 0 | pszBuffer[i] = '\0'; |
893 | 0 | } |
894 | 0 | } |
895 | |
|
896 | 0 | if (bNormalize) |
897 | 0 | { |
898 | 0 | size_t i = strlen(pszBuffer); |
899 | 0 | while (i > 0) |
900 | 0 | { |
901 | 0 | i--; |
902 | 0 | if (pszBuffer[i] == ':') |
903 | 0 | pszBuffer[i] = '_'; |
904 | 0 | } |
905 | 0 | } |
906 | |
|
907 | 0 | return pszBuffer; |
908 | 0 | } |
909 | | |
910 | | /************************************************************************/ |
911 | | /* CPLScanLong() */ |
912 | | /************************************************************************/ |
913 | | |
914 | | /** |
915 | | * Scan up to a maximum number of characters from a string and convert |
916 | | * the result to a long. |
917 | | * |
918 | | * @param pszString String containing characters to be scanned. It may be |
919 | | * terminated with a null character. |
920 | | * |
921 | | * @param nMaxLength The maximum number of character to consider as part |
922 | | * of the number. Less characters will be considered if a null character |
923 | | * is encountered. |
924 | | * |
925 | | * @return Long value, converted from its ASCII form. |
926 | | */ |
927 | | |
928 | | long CPLScanLong(const char *pszString, int nMaxLength) |
929 | 0 | { |
930 | 0 | CPLAssert(nMaxLength >= 0); |
931 | 0 | if (pszString == nullptr) |
932 | 0 | return 0; |
933 | 0 | const size_t nLength = CPLStrnlen(pszString, nMaxLength); |
934 | 0 | const std::string osValue(pszString, nLength); |
935 | 0 | return atol(osValue.c_str()); |
936 | 0 | } |
937 | | |
938 | | /************************************************************************/ |
939 | | /* CPLScanULong() */ |
940 | | /************************************************************************/ |
941 | | |
942 | | /** |
943 | | * Scan up to a maximum number of characters from a string and convert |
944 | | * the result to a unsigned long. |
945 | | * |
946 | | * @param pszString String containing characters to be scanned. It may be |
947 | | * terminated with a null character. |
948 | | * |
949 | | * @param nMaxLength The maximum number of character to consider as part |
950 | | * of the number. Less characters will be considered if a null character |
951 | | * is encountered. |
952 | | * |
953 | | * @return Unsigned long value, converted from its ASCII form. |
954 | | */ |
955 | | |
956 | | unsigned long CPLScanULong(const char *pszString, int nMaxLength) |
957 | 0 | { |
958 | 0 | CPLAssert(nMaxLength >= 0); |
959 | 0 | if (pszString == nullptr) |
960 | 0 | return 0; |
961 | 0 | const size_t nLength = CPLStrnlen(pszString, nMaxLength); |
962 | 0 | const std::string osValue(pszString, nLength); |
963 | 0 | return strtoul(osValue.c_str(), nullptr, 10); |
964 | 0 | } |
965 | | |
966 | | /************************************************************************/ |
967 | | /* CPLScanUIntBig() */ |
968 | | /************************************************************************/ |
969 | | |
970 | | /** |
971 | | * Extract big integer from string. |
972 | | * |
973 | | * Scan up to a maximum number of characters from a string and convert |
974 | | * the result to a GUIntBig. |
975 | | * |
976 | | * @param pszString String containing characters to be scanned. It may be |
977 | | * terminated with a null character. |
978 | | * |
979 | | * @param nMaxLength The maximum number of character to consider as part |
980 | | * of the number. Less characters will be considered if a null character |
981 | | * is encountered. |
982 | | * |
983 | | * @return GUIntBig value, converted from its ASCII form. |
984 | | */ |
985 | | |
986 | | GUIntBig CPLScanUIntBig(const char *pszString, int nMaxLength) |
987 | 3.57k | { |
988 | 3.57k | CPLAssert(nMaxLength >= 0); |
989 | 3.57k | if (pszString == nullptr) |
990 | 0 | return 0; |
991 | 3.57k | const size_t nLength = CPLStrnlen(pszString, nMaxLength); |
992 | 3.57k | const std::string osValue(pszString, nLength); |
993 | | |
994 | | /* -------------------------------------------------------------------- */ |
995 | | /* Fetch out the result */ |
996 | | /* -------------------------------------------------------------------- */ |
997 | 3.57k | return strtoull(osValue.c_str(), nullptr, 10); |
998 | 3.57k | } |
999 | | |
1000 | | /************************************************************************/ |
1001 | | /* CPLAtoGIntBig() */ |
1002 | | /************************************************************************/ |
1003 | | |
1004 | | /** |
1005 | | * Convert a string to a 64 bit signed integer. |
1006 | | * |
1007 | | * @param pszString String containing 64 bit signed integer. |
1008 | | * @return 64 bit signed integer. |
1009 | | * @since GDAL 2.0 |
1010 | | */ |
1011 | | |
1012 | | GIntBig CPLAtoGIntBig(const char *pszString) |
1013 | 241 | { |
1014 | 241 | return atoll(pszString); |
1015 | 241 | } |
1016 | | |
1017 | | #if defined(__MINGW32__) || defined(__sun__) |
1018 | | |
1019 | | // mingw atoll() doesn't return ERANGE in case of overflow |
1020 | | static int CPLAtoGIntBigExHasOverflow(const char *pszString, GIntBig nVal) |
1021 | | { |
1022 | | if (strlen(pszString) <= 18) |
1023 | | return FALSE; |
1024 | | while (*pszString == ' ') |
1025 | | pszString++; |
1026 | | if (*pszString == '+') |
1027 | | pszString++; |
1028 | | char szBuffer[32] = {}; |
1029 | | /* x86_64-w64-mingw32-g++ (GCC) 4.8.2 annoyingly warns */ |
1030 | | #ifdef HAVE_GCC_DIAGNOSTIC_PUSH |
1031 | | #pragma GCC diagnostic push |
1032 | | #pragma GCC diagnostic ignored "-Wformat" |
1033 | | #endif |
1034 | | snprintf(szBuffer, sizeof(szBuffer), CPL_FRMT_GIB, nVal); |
1035 | | #ifdef HAVE_GCC_DIAGNOSTIC_PUSH |
1036 | | #pragma GCC diagnostic pop |
1037 | | #endif |
1038 | | return strcmp(szBuffer, pszString) != 0; |
1039 | | } |
1040 | | |
1041 | | #endif |
1042 | | |
1043 | | /************************************************************************/ |
1044 | | /* CPLAtoGIntBigEx() */ |
1045 | | /************************************************************************/ |
1046 | | |
1047 | | /** |
1048 | | * Convert a string to a 64 bit signed integer. |
1049 | | * |
1050 | | * @param pszString String containing 64 bit signed integer. |
1051 | | * @param bWarn Issue a warning if an overflow occurs during conversion |
1052 | | * @param pbOverflow Pointer to an integer to store if an overflow occurred, or |
1053 | | * NULL |
1054 | | * @return 64 bit signed integer. |
1055 | | * @since GDAL 2.0 |
1056 | | */ |
1057 | | |
1058 | | GIntBig CPLAtoGIntBigEx(const char *pszString, int bWarn, int *pbOverflow) |
1059 | 0 | { |
1060 | 0 | errno = 0; |
1061 | 0 | GIntBig nVal = strtoll(pszString, nullptr, 10); |
1062 | 0 | if (errno == ERANGE |
1063 | | #if defined(__MINGW32__) || defined(__sun__) |
1064 | | || CPLAtoGIntBigExHasOverflow(pszString, nVal) |
1065 | | #endif |
1066 | 0 | ) |
1067 | 0 | { |
1068 | 0 | if (pbOverflow) |
1069 | 0 | *pbOverflow = TRUE; |
1070 | 0 | if (bWarn) |
1071 | 0 | { |
1072 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1073 | 0 | "64 bit integer overflow when converting %s", pszString); |
1074 | 0 | } |
1075 | 0 | while (*pszString == ' ') |
1076 | 0 | pszString++; |
1077 | 0 | return (*pszString == '-') ? GINTBIG_MIN : GINTBIG_MAX; |
1078 | 0 | } |
1079 | 0 | else if (pbOverflow) |
1080 | 0 | { |
1081 | 0 | *pbOverflow = FALSE; |
1082 | 0 | } |
1083 | 0 | return nVal; |
1084 | 0 | } |
1085 | | |
1086 | | /************************************************************************/ |
1087 | | /* CPLScanPointer() */ |
1088 | | /************************************************************************/ |
1089 | | |
1090 | | /** |
1091 | | * Extract pointer from string. |
1092 | | * |
1093 | | * Scan up to a maximum number of characters from a string and convert |
1094 | | * the result to a pointer. |
1095 | | * |
1096 | | * @param pszString String containing characters to be scanned. It may be |
1097 | | * terminated with a null character. |
1098 | | * |
1099 | | * @param nMaxLength The maximum number of character to consider as part |
1100 | | * of the number. Less characters will be considered if a null character |
1101 | | * is encountered. |
1102 | | * |
1103 | | * @return pointer value, converted from its ASCII form. |
1104 | | */ |
1105 | | |
1106 | | void *CPLScanPointer(const char *pszString, int nMaxLength) |
1107 | 0 | { |
1108 | 0 | char szTemp[128] = {}; |
1109 | | |
1110 | | /* -------------------------------------------------------------------- */ |
1111 | | /* Compute string into local buffer, and terminate it. */ |
1112 | | /* -------------------------------------------------------------------- */ |
1113 | 0 | if (nMaxLength > static_cast<int>(sizeof(szTemp)) - 1) |
1114 | 0 | nMaxLength = sizeof(szTemp) - 1; |
1115 | |
|
1116 | 0 | strncpy(szTemp, pszString, nMaxLength); |
1117 | 0 | szTemp[nMaxLength] = '\0'; |
1118 | | |
1119 | | /* -------------------------------------------------------------------- */ |
1120 | | /* On MSVC we have to scanf pointer values without the 0x */ |
1121 | | /* prefix. */ |
1122 | | /* -------------------------------------------------------------------- */ |
1123 | 0 | if (STARTS_WITH_CI(szTemp, "0x")) |
1124 | 0 | { |
1125 | 0 | void *pResult = nullptr; |
1126 | |
|
1127 | | #if defined(__MSVCRT__) || (defined(_WIN32) && defined(_MSC_VER)) |
1128 | | // cppcheck-suppress invalidscanf |
1129 | | sscanf(szTemp + 2, "%p", &pResult); |
1130 | | #else |
1131 | | // cppcheck-suppress invalidscanf |
1132 | 0 | sscanf(szTemp, "%p", &pResult); |
1133 | | |
1134 | | // Solaris actually behaves like MSVCRT. |
1135 | 0 | if (pResult == nullptr) |
1136 | 0 | { |
1137 | | // cppcheck-suppress invalidscanf |
1138 | 0 | sscanf(szTemp + 2, "%p", &pResult); |
1139 | 0 | } |
1140 | 0 | #endif |
1141 | 0 | return pResult; |
1142 | 0 | } |
1143 | | |
1144 | 0 | #if SIZEOF_VOIDP == 8 |
1145 | 0 | return reinterpret_cast<void *>(CPLScanUIntBig(szTemp, nMaxLength)); |
1146 | | #else |
1147 | | return reinterpret_cast<void *>(CPLScanULong(szTemp, nMaxLength)); |
1148 | | #endif |
1149 | 0 | } |
1150 | | |
1151 | | /************************************************************************/ |
1152 | | /* CPLScanDouble() */ |
1153 | | /************************************************************************/ |
1154 | | |
1155 | | /** |
1156 | | * Extract double from string. |
1157 | | * |
1158 | | * Scan up to a maximum number of characters from a string and convert the |
1159 | | * result to a double. This function uses CPLAtof() to convert string to |
1160 | | * double value, so it uses a comma as a decimal delimiter. |
1161 | | * |
1162 | | * @param pszString String containing characters to be scanned. It may be |
1163 | | * terminated with a null character. |
1164 | | * |
1165 | | * @param nMaxLength The maximum number of character to consider as part |
1166 | | * of the number. Less characters will be considered if a null character |
1167 | | * is encountered. |
1168 | | * |
1169 | | * @return Double value, converted from its ASCII form. |
1170 | | */ |
1171 | | |
1172 | | double CPLScanDouble(const char *pszString, int nMaxLength) |
1173 | 0 | { |
1174 | 0 | char szValue[32] = {}; |
1175 | 0 | char *pszValue = nullptr; |
1176 | |
|
1177 | 0 | if (nMaxLength + 1 < static_cast<int>(sizeof(szValue))) |
1178 | 0 | pszValue = szValue; |
1179 | 0 | else |
1180 | 0 | pszValue = static_cast<char *>(CPLMalloc(nMaxLength + 1)); |
1181 | | |
1182 | | /* -------------------------------------------------------------------- */ |
1183 | | /* Compute string into local buffer, and terminate it. */ |
1184 | | /* -------------------------------------------------------------------- */ |
1185 | 0 | strncpy(pszValue, pszString, nMaxLength); |
1186 | 0 | pszValue[nMaxLength] = '\0'; |
1187 | | |
1188 | | /* -------------------------------------------------------------------- */ |
1189 | | /* Make a pass through converting 'D's to 'E's. */ |
1190 | | /* -------------------------------------------------------------------- */ |
1191 | 0 | for (int i = 0; i < nMaxLength; i++) |
1192 | 0 | if (pszValue[i] == 'd' || pszValue[i] == 'D') |
1193 | 0 | pszValue[i] = 'E'; |
1194 | | |
1195 | | /* -------------------------------------------------------------------- */ |
1196 | | /* The conversion itself. */ |
1197 | | /* -------------------------------------------------------------------- */ |
1198 | 0 | const double dfValue = CPLAtof(pszValue); |
1199 | |
|
1200 | 0 | if (pszValue != szValue) |
1201 | 0 | CPLFree(pszValue); |
1202 | 0 | return dfValue; |
1203 | 0 | } |
1204 | | |
1205 | | /************************************************************************/ |
1206 | | /* CPLPrintString() */ |
1207 | | /************************************************************************/ |
1208 | | |
1209 | | /** |
1210 | | * Copy the string pointed to by pszSrc, NOT including the terminating |
1211 | | * `\\0' character, to the array pointed to by pszDest. |
1212 | | * |
1213 | | * @param pszDest Pointer to the destination string buffer. Should be |
1214 | | * large enough to hold the resulting string. |
1215 | | * |
1216 | | * @param pszSrc Pointer to the source buffer. |
1217 | | * |
1218 | | * @param nMaxLen Maximum length of the resulting string. If string length |
1219 | | * is greater than nMaxLen, it will be truncated. |
1220 | | * |
1221 | | * @return Number of characters printed. |
1222 | | */ |
1223 | | |
1224 | | int CPLPrintString(char *pszDest, const char *pszSrc, int nMaxLen) |
1225 | 0 | { |
1226 | 0 | if (!pszDest) |
1227 | 0 | return 0; |
1228 | | |
1229 | 0 | if (!pszSrc) |
1230 | 0 | { |
1231 | 0 | *pszDest = '\0'; |
1232 | 0 | return 1; |
1233 | 0 | } |
1234 | | |
1235 | 0 | int nChars = 0; |
1236 | 0 | char *pszTemp = pszDest; |
1237 | |
|
1238 | 0 | while (nChars < nMaxLen && *pszSrc) |
1239 | 0 | { |
1240 | 0 | *pszTemp++ = *pszSrc++; |
1241 | 0 | nChars++; |
1242 | 0 | } |
1243 | |
|
1244 | 0 | return nChars; |
1245 | 0 | } |
1246 | | |
1247 | | /************************************************************************/ |
1248 | | /* CPLPrintStringFill() */ |
1249 | | /************************************************************************/ |
1250 | | |
1251 | | /** |
1252 | | * Copy the string pointed to by pszSrc, NOT including the terminating |
1253 | | * `\\0' character, to the array pointed to by pszDest. Remainder of the |
1254 | | * destination string will be filled with space characters. This is only |
1255 | | * difference from the PrintString(). |
1256 | | * |
1257 | | * @param pszDest Pointer to the destination string buffer. Should be |
1258 | | * large enough to hold the resulting string. |
1259 | | * |
1260 | | * @param pszSrc Pointer to the source buffer. |
1261 | | * |
1262 | | * @param nMaxLen Maximum length of the resulting string. If string length |
1263 | | * is greater than nMaxLen, it will be truncated. |
1264 | | * |
1265 | | * @return Number of characters printed. |
1266 | | */ |
1267 | | |
1268 | | int CPLPrintStringFill(char *pszDest, const char *pszSrc, int nMaxLen) |
1269 | 0 | { |
1270 | 0 | if (!pszDest) |
1271 | 0 | return 0; |
1272 | | |
1273 | 0 | if (!pszSrc) |
1274 | 0 | { |
1275 | 0 | memset(pszDest, ' ', nMaxLen); |
1276 | 0 | return nMaxLen; |
1277 | 0 | } |
1278 | | |
1279 | 0 | char *pszTemp = pszDest; |
1280 | 0 | while (nMaxLen && *pszSrc) |
1281 | 0 | { |
1282 | 0 | *pszTemp++ = *pszSrc++; |
1283 | 0 | nMaxLen--; |
1284 | 0 | } |
1285 | |
|
1286 | 0 | if (nMaxLen) |
1287 | 0 | memset(pszTemp, ' ', nMaxLen); |
1288 | |
|
1289 | 0 | return nMaxLen; |
1290 | 0 | } |
1291 | | |
1292 | | /************************************************************************/ |
1293 | | /* CPLPrintInt32() */ |
1294 | | /************************************************************************/ |
1295 | | |
1296 | | /** |
1297 | | * Print GInt32 value into specified string buffer. This string will not |
1298 | | * be NULL-terminated. |
1299 | | * |
1300 | | * @param pszBuffer Pointer to the destination string buffer. Should be |
1301 | | * large enough to hold the resulting string. Note, that the string will |
1302 | | * not be NULL-terminated, so user should do this himself, if needed. |
1303 | | * |
1304 | | * @param iValue Numerical value to print. |
1305 | | * |
1306 | | * @param nMaxLen Maximum length of the resulting string. If string length |
1307 | | * is greater than nMaxLen, it will be truncated. |
1308 | | * |
1309 | | * @return Number of characters printed. |
1310 | | */ |
1311 | | |
1312 | | int CPLPrintInt32(char *pszBuffer, GInt32 iValue, int nMaxLen) |
1313 | 0 | { |
1314 | 0 | if (!pszBuffer) |
1315 | 0 | return 0; |
1316 | | |
1317 | 0 | if (nMaxLen >= 64) |
1318 | 0 | nMaxLen = 63; |
1319 | |
|
1320 | 0 | char szTemp[64] = {}; |
1321 | |
|
1322 | | #if UINT_MAX == 65535 |
1323 | | snprintf(szTemp, sizeof(szTemp), "%*ld", nMaxLen, iValue); |
1324 | | #else |
1325 | 0 | snprintf(szTemp, sizeof(szTemp), "%*d", nMaxLen, iValue); |
1326 | 0 | #endif |
1327 | |
|
1328 | 0 | return CPLPrintString(pszBuffer, szTemp, nMaxLen); |
1329 | 0 | } |
1330 | | |
1331 | | /************************************************************************/ |
1332 | | /* CPLPrintUIntBig() */ |
1333 | | /************************************************************************/ |
1334 | | |
1335 | | /** |
1336 | | * Print GUIntBig value into specified string buffer. This string will not |
1337 | | * be NULL-terminated. |
1338 | | * |
1339 | | * @param pszBuffer Pointer to the destination string buffer. Should be |
1340 | | * large enough to hold the resulting string. Note, that the string will |
1341 | | * not be NULL-terminated, so user should do this himself, if needed. |
1342 | | * |
1343 | | * @param iValue Numerical value to print. |
1344 | | * |
1345 | | * @param nMaxLen Maximum length of the resulting string. If string length |
1346 | | * is greater than nMaxLen, it will be truncated. |
1347 | | * |
1348 | | * @return Number of characters printed. |
1349 | | */ |
1350 | | |
1351 | | int CPLPrintUIntBig(char *pszBuffer, GUIntBig iValue, int nMaxLen) |
1352 | 0 | { |
1353 | 0 | if (!pszBuffer) |
1354 | 0 | return 0; |
1355 | | |
1356 | 0 | if (nMaxLen >= 64) |
1357 | 0 | nMaxLen = 63; |
1358 | |
|
1359 | 0 | char szTemp[64] = {}; |
1360 | |
|
1361 | | #if defined(__MSVCRT__) || (defined(_WIN32) && defined(_MSC_VER)) |
1362 | | /* x86_64-w64-mingw32-g++ (GCC) 4.8.2 annoyingly warns */ |
1363 | | #ifdef HAVE_GCC_DIAGNOSTIC_PUSH |
1364 | | #pragma GCC diagnostic push |
1365 | | #pragma GCC diagnostic ignored "-Wformat" |
1366 | | #pragma GCC diagnostic ignored "-Wformat-extra-args" |
1367 | | #endif |
1368 | | snprintf(szTemp, sizeof(szTemp), "%*I64u", nMaxLen, iValue); |
1369 | | #ifdef HAVE_GCC_DIAGNOSTIC_PUSH |
1370 | | #pragma GCC diagnostic pop |
1371 | | #endif |
1372 | | #else |
1373 | 0 | snprintf(szTemp, sizeof(szTemp), "%*llu", nMaxLen, iValue); |
1374 | 0 | #endif |
1375 | |
|
1376 | 0 | return CPLPrintString(pszBuffer, szTemp, nMaxLen); |
1377 | 0 | } |
1378 | | |
1379 | | /************************************************************************/ |
1380 | | /* CPLPrintPointer() */ |
1381 | | /************************************************************************/ |
1382 | | |
1383 | | /** |
1384 | | * Print pointer value into specified string buffer. This string will not |
1385 | | * be NULL-terminated. |
1386 | | * |
1387 | | * @param pszBuffer Pointer to the destination string buffer. Should be |
1388 | | * large enough to hold the resulting string. Note, that the string will |
1389 | | * not be NULL-terminated, so user should do this himself, if needed. |
1390 | | * |
1391 | | * @param pValue Pointer to ASCII encode. |
1392 | | * |
1393 | | * @param nMaxLen Maximum length of the resulting string. If string length |
1394 | | * is greater than nMaxLen, it will be truncated. |
1395 | | * |
1396 | | * @return Number of characters printed. |
1397 | | */ |
1398 | | |
1399 | | int CPLPrintPointer(char *pszBuffer, void *pValue, int nMaxLen) |
1400 | 0 | { |
1401 | 0 | if (!pszBuffer) |
1402 | 0 | return 0; |
1403 | | |
1404 | 0 | if (nMaxLen >= 64) |
1405 | 0 | nMaxLen = 63; |
1406 | |
|
1407 | 0 | char szTemp[64] = {}; |
1408 | |
|
1409 | 0 | snprintf(szTemp, sizeof(szTemp), "%p", pValue); |
1410 | | |
1411 | | // On windows, and possibly some other platforms the sprintf("%p") |
1412 | | // does not prefix things with 0x so it is hard to know later if the |
1413 | | // value is hex encoded. Fix this up here. |
1414 | |
|
1415 | 0 | if (!STARTS_WITH_CI(szTemp, "0x")) |
1416 | 0 | snprintf(szTemp, sizeof(szTemp), "0x%p", pValue); |
1417 | |
|
1418 | 0 | return CPLPrintString(pszBuffer, szTemp, nMaxLen); |
1419 | 0 | } |
1420 | | |
1421 | | /************************************************************************/ |
1422 | | /* CPLPrintDouble() */ |
1423 | | /************************************************************************/ |
1424 | | |
1425 | | /** |
1426 | | * Print double value into specified string buffer. Exponential character |
1427 | | * flag 'E' (or 'e') will be replaced with 'D', as in Fortran. Resulting |
1428 | | * string will not to be NULL-terminated. |
1429 | | * |
1430 | | * @param pszBuffer Pointer to the destination string buffer. Should be |
1431 | | * large enough to hold the resulting string. Note, that the string will |
1432 | | * not be NULL-terminated, so user should do this himself, if needed. |
1433 | | * |
1434 | | * @param pszFormat Format specifier (for example, "%16.9E"). |
1435 | | * |
1436 | | * @param dfValue Numerical value to print. |
1437 | | * |
1438 | | * @param pszLocale Unused. |
1439 | | * |
1440 | | * @return Number of characters printed. |
1441 | | */ |
1442 | | |
1443 | | int CPLPrintDouble(char *pszBuffer, const char *pszFormat, double dfValue, |
1444 | | CPL_UNUSED const char *pszLocale) |
1445 | 0 | { |
1446 | 0 | if (!pszBuffer) |
1447 | 0 | return 0; |
1448 | | |
1449 | 0 | const int knDoubleBufferSize = 64; |
1450 | 0 | char szTemp[knDoubleBufferSize] = {}; |
1451 | |
|
1452 | 0 | CPLsnprintf(szTemp, knDoubleBufferSize, pszFormat, dfValue); |
1453 | 0 | szTemp[knDoubleBufferSize - 1] = '\0'; |
1454 | |
|
1455 | 0 | for (int i = 0; szTemp[i] != '\0'; i++) |
1456 | 0 | { |
1457 | 0 | if (szTemp[i] == 'E' || szTemp[i] == 'e') |
1458 | 0 | szTemp[i] = 'D'; |
1459 | 0 | } |
1460 | |
|
1461 | 0 | return CPLPrintString(pszBuffer, szTemp, 64); |
1462 | 0 | } |
1463 | | |
1464 | | /************************************************************************/ |
1465 | | /* CPLPrintTime() */ |
1466 | | /************************************************************************/ |
1467 | | |
1468 | | /** |
1469 | | * Print specified time value accordingly to the format options and |
1470 | | * specified locale name. This function does following: |
1471 | | * |
1472 | | * - if locale parameter is not NULL, the current locale setting will be |
1473 | | * stored and replaced with the specified one; |
1474 | | * - format time value with the strftime(3) function; |
1475 | | * - restore back current locale, if was saved. |
1476 | | * |
1477 | | * @param pszBuffer Pointer to the destination string buffer. Should be |
1478 | | * large enough to hold the resulting string. Note, that the string will |
1479 | | * not be NULL-terminated, so user should do this himself, if needed. |
1480 | | * |
1481 | | * @param nMaxLen Maximum length of the resulting string. If string length is |
1482 | | * greater than nMaxLen, it will be truncated. |
1483 | | * |
1484 | | * @param pszFormat Controls the output format. Options are the same as |
1485 | | * for strftime(3) function. |
1486 | | * |
1487 | | * @param poBrokenTime Pointer to the broken-down time structure. May be |
1488 | | * requested with the VSIGMTime() and VSILocalTime() functions. |
1489 | | * |
1490 | | * @param pszLocale Pointer to a character string containing locale name |
1491 | | * ("C", "POSIX", "us_US", "ru_RU.KOI8-R" etc.). If NULL we will not |
1492 | | * manipulate with locale settings and current process locale will be used for |
1493 | | * printing. Be aware that it may be unsuitable to use current locale for |
1494 | | * printing time, because all names will be printed in your native language, |
1495 | | * as well as time format settings also may be adjusted differently from the |
1496 | | * C/POSIX defaults. To solve these problems this option was introduced. |
1497 | | * |
1498 | | * @return Number of characters printed. |
1499 | | */ |
1500 | | |
1501 | | int CPLPrintTime(char *pszBuffer, int nMaxLen, const char *pszFormat, |
1502 | | const struct tm *poBrokenTime, const char *pszLocale) |
1503 | 0 | { |
1504 | 0 | char *pszTemp = |
1505 | 0 | static_cast<char *>(CPLMalloc((nMaxLen + 1) * sizeof(char))); |
1506 | |
|
1507 | 0 | if (pszLocale && EQUAL(pszLocale, "C") && |
1508 | 0 | strcmp(pszFormat, "%a, %d %b %Y %H:%M:%S GMT") == 0) |
1509 | 0 | { |
1510 | | // Particular case when formatting RFC822 datetime, to avoid locale |
1511 | | // change |
1512 | 0 | static const char *const aszMonthStr[] = {"Jan", "Feb", "Mar", "Apr", |
1513 | 0 | "May", "Jun", "Jul", "Aug", |
1514 | 0 | "Sep", "Oct", "Nov", "Dec"}; |
1515 | 0 | static const char *const aszDayOfWeek[] = {"Sun", "Mon", "Tue", "Wed", |
1516 | 0 | "Thu", "Fri", "Sat"}; |
1517 | 0 | snprintf(pszTemp, nMaxLen + 1, "%s, %02d %s %04d %02d:%02d:%02d GMT", |
1518 | 0 | aszDayOfWeek[std::max(0, std::min(6, poBrokenTime->tm_wday))], |
1519 | 0 | poBrokenTime->tm_mday, |
1520 | 0 | aszMonthStr[std::max(0, std::min(11, poBrokenTime->tm_mon))], |
1521 | 0 | poBrokenTime->tm_year + 1900, poBrokenTime->tm_hour, |
1522 | 0 | poBrokenTime->tm_min, poBrokenTime->tm_sec); |
1523 | 0 | } |
1524 | 0 | else |
1525 | 0 | { |
1526 | | #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE) |
1527 | | char *pszCurLocale = NULL; |
1528 | | |
1529 | | if (pszLocale || EQUAL(pszLocale, "")) |
1530 | | { |
1531 | | // Save the current locale. |
1532 | | pszCurLocale = CPLsetlocale(LC_ALL, NULL); |
1533 | | // Set locale to the specified value. |
1534 | | CPLsetlocale(LC_ALL, pszLocale); |
1535 | | } |
1536 | | #else |
1537 | 0 | (void)pszLocale; |
1538 | 0 | #endif |
1539 | |
|
1540 | 0 | if (!strftime(pszTemp, nMaxLen + 1, pszFormat, poBrokenTime)) |
1541 | 0 | memset(pszTemp, 0, nMaxLen + 1); |
1542 | |
|
1543 | | #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE) |
1544 | | // Restore stored locale back. |
1545 | | if (pszCurLocale) |
1546 | | CPLsetlocale(LC_ALL, pszCurLocale); |
1547 | | #endif |
1548 | 0 | } |
1549 | |
|
1550 | 0 | const int nChars = CPLPrintString(pszBuffer, pszTemp, nMaxLen); |
1551 | |
|
1552 | 0 | CPLFree(pszTemp); |
1553 | |
|
1554 | 0 | return nChars; |
1555 | 0 | } |
1556 | | |
1557 | | /************************************************************************/ |
1558 | | /* CPLVerifyConfiguration() */ |
1559 | | /************************************************************************/ |
1560 | | |
1561 | | void CPLVerifyConfiguration() |
1562 | | |
1563 | 0 | { |
1564 | | /* -------------------------------------------------------------------- */ |
1565 | | /* Verify data types. */ |
1566 | | /* -------------------------------------------------------------------- */ |
1567 | 0 | static_assert(sizeof(short) == 2); // We unfortunately rely on this |
1568 | 0 | static_assert(sizeof(int) == 4); // We unfortunately rely on this |
1569 | 0 | static_assert(sizeof(float) == 4); // We unfortunately rely on this |
1570 | 0 | static_assert(sizeof(double) == 8); // We unfortunately rely on this |
1571 | 0 | static_assert(sizeof(GInt64) == 8); |
1572 | 0 | static_assert(sizeof(GInt32) == 4); |
1573 | 0 | static_assert(sizeof(GInt16) == 2); |
1574 | 0 | static_assert(sizeof(GByte) == 1); |
1575 | | |
1576 | | /* -------------------------------------------------------------------- */ |
1577 | | /* Verify byte order */ |
1578 | | /* -------------------------------------------------------------------- */ |
1579 | 0 | #ifdef CPL_LSB |
1580 | | #if __cplusplus >= 202002L |
1581 | | static_assert(std::endian::native == std::endian::little); |
1582 | | #elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) |
1583 | | static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__); |
1584 | 0 | #endif |
1585 | | #elif defined(CPL_MSB) |
1586 | | #if __cplusplus >= 202002L |
1587 | | static_assert(std::endian::native == std::endian::big); |
1588 | | #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) |
1589 | | static_assert(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__); |
1590 | | #endif |
1591 | | #else |
1592 | | #error "CPL_LSB or CPL_MSB must be defined" |
1593 | | #endif |
1594 | 0 | } |
1595 | | |
1596 | | #ifdef DEBUG_CONFIG_OPTIONS |
1597 | | |
1598 | | static CPLMutex *hRegisterConfigurationOptionMutex = nullptr; |
1599 | | static std::set<CPLString> *paoGetKeys = nullptr; |
1600 | | static std::set<CPLString> *paoSetKeys = nullptr; |
1601 | | |
1602 | | /************************************************************************/ |
1603 | | /* CPLShowAccessedOptions() */ |
1604 | | /************************************************************************/ |
1605 | | |
1606 | | static void CPLShowAccessedOptions() |
1607 | | { |
1608 | | std::set<CPLString>::iterator aoIter; |
1609 | | |
1610 | | printf("Configuration options accessed in reading : "); /*ok*/ |
1611 | | aoIter = paoGetKeys->begin(); |
1612 | | while (aoIter != paoGetKeys->end()) |
1613 | | { |
1614 | | printf("%s, ", (*aoIter).c_str()); /*ok*/ |
1615 | | ++aoIter; |
1616 | | } |
1617 | | printf("\n"); /*ok*/ |
1618 | | |
1619 | | printf("Configuration options accessed in writing : "); /*ok*/ |
1620 | | aoIter = paoSetKeys->begin(); |
1621 | | while (aoIter != paoSetKeys->end()) |
1622 | | { |
1623 | | printf("%s, ", (*aoIter).c_str()); /*ok*/ |
1624 | | ++aoIter; |
1625 | | } |
1626 | | printf("\n"); /*ok*/ |
1627 | | |
1628 | | delete paoGetKeys; |
1629 | | delete paoSetKeys; |
1630 | | paoGetKeys = nullptr; |
1631 | | paoSetKeys = nullptr; |
1632 | | } |
1633 | | |
1634 | | /************************************************************************/ |
1635 | | /* CPLAccessConfigOption() */ |
1636 | | /************************************************************************/ |
1637 | | |
1638 | | static void CPLAccessConfigOption(const char *pszKey, bool bGet) |
1639 | | { |
1640 | | CPLMutexHolderD(&hRegisterConfigurationOptionMutex); |
1641 | | if (paoGetKeys == nullptr) |
1642 | | { |
1643 | | paoGetKeys = new std::set<CPLString>; |
1644 | | paoSetKeys = new std::set<CPLString>; |
1645 | | atexit(CPLShowAccessedOptions); |
1646 | | } |
1647 | | if (bGet) |
1648 | | paoGetKeys->insert(pszKey); |
1649 | | else |
1650 | | paoSetKeys->insert(pszKey); |
1651 | | } |
1652 | | #endif |
1653 | | |
1654 | | /************************************************************************/ |
1655 | | /* CPLGetConfigOption() */ |
1656 | | /************************************************************************/ |
1657 | | |
1658 | | /** |
1659 | | * Get the value of a configuration option. |
1660 | | * |
1661 | | * The value is the value of a (key, value) option set with |
1662 | | * CPLSetConfigOption(), or CPLSetThreadLocalConfigOption() of the same |
1663 | | * thread. If the given option was no defined with |
1664 | | * CPLSetConfigOption(), it tries to find it in environment variables. |
1665 | | * |
1666 | | * Note: the string returned by CPLGetConfigOption() might be short-lived, and |
1667 | | * in particular it will become invalid after a call to CPLSetConfigOption() |
1668 | | * with the same key. |
1669 | | * |
1670 | | * To override temporary a potentially existing option with a new value, you |
1671 | | * can use the following snippet : |
1672 | | * \code{.cpp} |
1673 | | * // backup old value |
1674 | | * const char* pszOldValTmp = CPLGetConfigOption(pszKey, NULL); |
1675 | | * char* pszOldVal = pszOldValTmp ? CPLStrdup(pszOldValTmp) : NULL; |
1676 | | * // override with new value |
1677 | | * CPLSetConfigOption(pszKey, pszNewVal); |
1678 | | * // do something useful |
1679 | | * // restore old value |
1680 | | * CPLSetConfigOption(pszKey, pszOldVal); |
1681 | | * CPLFree(pszOldVal); |
1682 | | * \endcode |
1683 | | * |
1684 | | * @param pszKey the key of the option to retrieve |
1685 | | * @param pszDefault a default value if the key does not match existing defined |
1686 | | * options (may be NULL) |
1687 | | * @return the value associated to the key, or the default value if not found |
1688 | | * |
1689 | | * @see CPLSetConfigOption(), https://gdal.org/user/configoptions.html |
1690 | | */ |
1691 | | const char *CPL_STDCALL CPLGetConfigOption(const char *pszKey, |
1692 | | const char *pszDefault) |
1693 | | |
1694 | 154k | { |
1695 | 154k | const char *pszResult = CPLGetThreadLocalConfigOption(pszKey, nullptr); |
1696 | | |
1697 | 154k | if (pszResult == nullptr) |
1698 | 154k | { |
1699 | 154k | pszResult = CPLGetGlobalConfigOption(pszKey, nullptr); |
1700 | 154k | } |
1701 | | |
1702 | 154k | if (gbIgnoreEnvVariables) |
1703 | 0 | { |
1704 | 0 | const char *pszEnvVar = getenv(pszKey); |
1705 | 0 | if (pszEnvVar != nullptr) |
1706 | 0 | { |
1707 | 0 | CPLDebug("CPL", |
1708 | 0 | "Ignoring environment variable %s=%s because of " |
1709 | 0 | "ignore-env-vars=yes setting in configuration file", |
1710 | 0 | pszKey, pszEnvVar); |
1711 | 0 | } |
1712 | 0 | } |
1713 | 154k | else if (pszResult == nullptr) |
1714 | 99.3k | { |
1715 | 99.3k | pszResult = getenv(pszKey); |
1716 | 99.3k | } |
1717 | | |
1718 | 154k | if (pszResult == nullptr) |
1719 | 99.3k | return pszDefault; |
1720 | | |
1721 | 55.3k | return pszResult; |
1722 | 154k | } |
1723 | | |
1724 | | /************************************************************************/ |
1725 | | /* CPLGetConfigOptions() */ |
1726 | | /************************************************************************/ |
1727 | | |
1728 | | /** |
1729 | | * Return the list of configuration options as KEY=VALUE pairs. |
1730 | | * |
1731 | | * The list is the one set through the CPLSetConfigOption() API. |
1732 | | * |
1733 | | * Options that through environment variables or with |
1734 | | * CPLSetThreadLocalConfigOption() will *not* be listed. |
1735 | | * |
1736 | | * @return a copy of the list, to be freed with CSLDestroy(). |
1737 | | * @since GDAL 2.2 |
1738 | | */ |
1739 | | char **CPLGetConfigOptions(void) |
1740 | 0 | { |
1741 | 0 | CPLMutexHolderD(&hConfigMutex); |
1742 | 0 | return CSLDuplicate(const_cast<char **>(g_papszConfigOptions)); |
1743 | 0 | } |
1744 | | |
1745 | | /************************************************************************/ |
1746 | | /* CPLSetConfigOptions() */ |
1747 | | /************************************************************************/ |
1748 | | |
1749 | | /** |
1750 | | * Replace the full list of configuration options with the passed list of |
1751 | | * KEY=VALUE pairs. |
1752 | | * |
1753 | | * This has the same effect of clearing the existing list, and setting |
1754 | | * individually each pair with the CPLSetConfigOption() API. |
1755 | | * |
1756 | | * This does not affect options set through environment variables or with |
1757 | | * CPLSetThreadLocalConfigOption(). |
1758 | | * |
1759 | | * The passed list is copied by the function. |
1760 | | * |
1761 | | * @param papszConfigOptions the new list (or NULL). |
1762 | | * |
1763 | | * @since GDAL 2.2 |
1764 | | */ |
1765 | | void CPLSetConfigOptions(const char *const *papszConfigOptions) |
1766 | 0 | { |
1767 | 0 | CPLMutexHolderD(&hConfigMutex); |
1768 | 0 | CSLDestroy(const_cast<char **>(g_papszConfigOptions)); |
1769 | 0 | g_papszConfigOptions = const_cast<volatile char **>( |
1770 | 0 | CSLDuplicate(const_cast<char **>(papszConfigOptions))); |
1771 | 0 | } |
1772 | | |
1773 | | /************************************************************************/ |
1774 | | /* CPLGetThreadLocalConfigOption() */ |
1775 | | /************************************************************************/ |
1776 | | |
1777 | | /** Same as CPLGetConfigOption() but only with options set with |
1778 | | * CPLSetThreadLocalConfigOption() */ |
1779 | | const char *CPL_STDCALL CPLGetThreadLocalConfigOption(const char *pszKey, |
1780 | | const char *pszDefault) |
1781 | | |
1782 | 154k | { |
1783 | | #ifdef DEBUG_CONFIG_OPTIONS |
1784 | | CPLAccessConfigOption(pszKey, TRUE); |
1785 | | #endif |
1786 | | |
1787 | 154k | const char *pszResult = nullptr; |
1788 | | |
1789 | 154k | int bMemoryError = FALSE; |
1790 | 154k | char **papszTLConfigOptions = reinterpret_cast<char **>( |
1791 | 154k | CPLGetTLSEx(CTLS_CONFIGOPTIONS, &bMemoryError)); |
1792 | 154k | if (papszTLConfigOptions != nullptr) |
1793 | 0 | pszResult = CSLFetchNameValue(papszTLConfigOptions, pszKey); |
1794 | | |
1795 | 154k | if (pszResult == nullptr) |
1796 | 154k | return pszDefault; |
1797 | | |
1798 | 0 | return pszResult; |
1799 | 154k | } |
1800 | | |
1801 | | /************************************************************************/ |
1802 | | /* CPLGetGlobalConfigOption() */ |
1803 | | /************************************************************************/ |
1804 | | |
1805 | | /** Same as CPLGetConfigOption() but excludes environment variables and |
1806 | | * options set with CPLSetThreadLocalConfigOption(). |
1807 | | * This function should generally not be used by applications, which should |
1808 | | * use CPLGetConfigOption() instead. |
1809 | | * @since 3.8 */ |
1810 | | const char *CPL_STDCALL CPLGetGlobalConfigOption(const char *pszKey, |
1811 | | const char *pszDefault) |
1812 | 154k | { |
1813 | | #ifdef DEBUG_CONFIG_OPTIONS |
1814 | | CPLAccessConfigOption(pszKey, TRUE); |
1815 | | #endif |
1816 | | |
1817 | 154k | CPLMutexHolderD(&hConfigMutex); |
1818 | | |
1819 | 154k | const char *pszResult = |
1820 | 154k | CSLFetchNameValue(const_cast<char **>(g_papszConfigOptions), pszKey); |
1821 | | |
1822 | 154k | if (pszResult == nullptr) |
1823 | 99.3k | return pszDefault; |
1824 | | |
1825 | 55.3k | return pszResult; |
1826 | 154k | } |
1827 | | |
1828 | | /************************************************************************/ |
1829 | | /* CPLSubscribeToSetConfigOption() */ |
1830 | | /************************************************************************/ |
1831 | | |
1832 | | /** |
1833 | | * Install a callback that will be notified of calls to CPLSetConfigOption()/ |
1834 | | * CPLSetThreadLocalConfigOption() |
1835 | | * |
1836 | | * @param pfnCallback Callback. Must not be NULL |
1837 | | * @param pUserData Callback user data. May be NULL. |
1838 | | * @return subscriber ID that can be used with CPLUnsubscribeToSetConfigOption() |
1839 | | * @since GDAL 3.7 |
1840 | | */ |
1841 | | |
1842 | | int CPLSubscribeToSetConfigOption(CPLSetConfigOptionSubscriber pfnCallback, |
1843 | | void *pUserData) |
1844 | 2 | { |
1845 | 2 | CPLMutexHolderD(&hConfigMutex); |
1846 | 2 | for (int nId = 0; |
1847 | 2 | nId < static_cast<int>(gSetConfigOptionSubscribers.size()); ++nId) |
1848 | 0 | { |
1849 | 0 | if (!gSetConfigOptionSubscribers[nId].first) |
1850 | 0 | { |
1851 | 0 | gSetConfigOptionSubscribers[nId].first = pfnCallback; |
1852 | 0 | gSetConfigOptionSubscribers[nId].second = pUserData; |
1853 | 0 | return nId; |
1854 | 0 | } |
1855 | 0 | } |
1856 | 2 | int nId = static_cast<int>(gSetConfigOptionSubscribers.size()); |
1857 | 2 | gSetConfigOptionSubscribers.push_back( |
1858 | 2 | std::pair<CPLSetConfigOptionSubscriber, void *>(pfnCallback, |
1859 | 2 | pUserData)); |
1860 | 2 | return nId; |
1861 | 2 | } |
1862 | | |
1863 | | /************************************************************************/ |
1864 | | /* CPLUnsubscribeToSetConfigOption() */ |
1865 | | /************************************************************************/ |
1866 | | |
1867 | | /** |
1868 | | * Remove a subscriber installed with CPLSubscribeToSetConfigOption() |
1869 | | * |
1870 | | * @param nId Subscriber id returned by CPLSubscribeToSetConfigOption() |
1871 | | * @since GDAL 3.7 |
1872 | | */ |
1873 | | |
1874 | | void CPLUnsubscribeToSetConfigOption(int nId) |
1875 | 0 | { |
1876 | 0 | CPLMutexHolderD(&hConfigMutex); |
1877 | 0 | if (nId == static_cast<int>(gSetConfigOptionSubscribers.size()) - 1) |
1878 | 0 | { |
1879 | 0 | gSetConfigOptionSubscribers.resize(gSetConfigOptionSubscribers.size() - |
1880 | 0 | 1); |
1881 | 0 | } |
1882 | 0 | else if (nId >= 0 && |
1883 | 0 | nId < static_cast<int>(gSetConfigOptionSubscribers.size())) |
1884 | 0 | { |
1885 | 0 | gSetConfigOptionSubscribers[nId].first = nullptr; |
1886 | 0 | } |
1887 | 0 | } |
1888 | | |
1889 | | /************************************************************************/ |
1890 | | /* NotifyOtherComponentsConfigOptionChanged() */ |
1891 | | /************************************************************************/ |
1892 | | |
1893 | | static void NotifyOtherComponentsConfigOptionChanged(const char *pszKey, |
1894 | | const char *pszValue, |
1895 | | bool bThreadLocal) |
1896 | 15.2k | { |
1897 | | // When changing authentication parameters of virtual file systems, |
1898 | | // partially invalidate cached state about file availability. |
1899 | 15.2k | if (STARTS_WITH_CI(pszKey, "AWS_") || STARTS_WITH_CI(pszKey, "GS_") || |
1900 | 15.2k | STARTS_WITH_CI(pszKey, "GOOGLE_") || |
1901 | 15.2k | STARTS_WITH_CI(pszKey, "GDAL_HTTP_HEADER_FILE") || |
1902 | 15.2k | STARTS_WITH_CI(pszKey, "AZURE_") || |
1903 | 15.2k | (STARTS_WITH_CI(pszKey, "SWIFT_") && !EQUAL(pszKey, "SWIFT_MAX_KEYS"))) |
1904 | 768 | { |
1905 | 768 | VSICurlAuthParametersChanged(); |
1906 | 768 | } |
1907 | | |
1908 | 15.2k | if (!gSetConfigOptionSubscribers.empty()) |
1909 | 14.7k | { |
1910 | 14.7k | for (const auto &iter : gSetConfigOptionSubscribers) |
1911 | 14.7k | { |
1912 | 14.7k | if (iter.first) |
1913 | 14.7k | iter.first(pszKey, pszValue, bThreadLocal, iter.second); |
1914 | 14.7k | } |
1915 | 14.7k | } |
1916 | 15.2k | } |
1917 | | |
1918 | | /************************************************************************/ |
1919 | | /* CPLIsDebugEnabled() */ |
1920 | | /************************************************************************/ |
1921 | | |
1922 | | static int gnDebug = -1; |
1923 | | |
1924 | | /** Returns whether CPL_DEBUG is enabled. |
1925 | | * |
1926 | | * @since 3.11 |
1927 | | */ |
1928 | | bool CPLIsDebugEnabled() |
1929 | 14.8k | { |
1930 | 14.8k | if (gnDebug < 0) |
1931 | 2 | { |
1932 | | // Check that apszKnownConfigOptions is correctly sorted with |
1933 | | // STRCASECMP() criterion. |
1934 | 2.12k | for (size_t i = 1; i < CPL_ARRAYSIZE(apszKnownConfigOptions); ++i) |
1935 | 2.12k | { |
1936 | 2.12k | if (STRCASECMP(apszKnownConfigOptions[i - 1], |
1937 | 2.12k | apszKnownConfigOptions[i]) >= 0) |
1938 | 0 | { |
1939 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1940 | 0 | "ERROR: apszKnownConfigOptions[] isn't correctly " |
1941 | 0 | "sorted: %s >= %s", |
1942 | 0 | apszKnownConfigOptions[i - 1], |
1943 | 0 | apszKnownConfigOptions[i]); |
1944 | 0 | } |
1945 | 2.12k | } |
1946 | 2 | gnDebug = CPLTestBool(CPLGetConfigOption("CPL_DEBUG", "OFF")); |
1947 | 2 | } |
1948 | | |
1949 | 14.8k | return gnDebug != 0; |
1950 | 14.8k | } |
1951 | | |
1952 | | /************************************************************************/ |
1953 | | /* CPLDeclareKnownConfigOption() */ |
1954 | | /************************************************************************/ |
1955 | | |
1956 | | static std::mutex goMutexDeclaredKnownConfigOptions; |
1957 | | static std::set<CPLString> goSetKnownConfigOptions; |
1958 | | |
1959 | | /** Declare that the specified configuration option is known. |
1960 | | * |
1961 | | * This is useful to avoid a warning to be emitted on unknown configuration |
1962 | | * options when CPL_DEBUG is enabled. |
1963 | | * |
1964 | | * @param pszKey Name of the configuration option to declare. |
1965 | | * @param pszDefinition Unused for now. Must be set to nullptr. |
1966 | | * @since 3.11 |
1967 | | */ |
1968 | | void CPLDeclareKnownConfigOption(const char *pszKey, |
1969 | | [[maybe_unused]] const char *pszDefinition) |
1970 | 0 | { |
1971 | 0 | std::lock_guard oLock(goMutexDeclaredKnownConfigOptions); |
1972 | 0 | goSetKnownConfigOptions.insert(CPLString(pszKey).toupper()); |
1973 | 0 | } |
1974 | | |
1975 | | /************************************************************************/ |
1976 | | /* CPLGetKnownConfigOptions() */ |
1977 | | /************************************************************************/ |
1978 | | |
1979 | | /** Return the list of known configuration options. |
1980 | | * |
1981 | | * Must be freed with CSLDestroy(). |
1982 | | * @since 3.11 |
1983 | | */ |
1984 | | char **CPLGetKnownConfigOptions() |
1985 | 0 | { |
1986 | 0 | std::lock_guard oLock(goMutexDeclaredKnownConfigOptions); |
1987 | 0 | CPLStringList aosList; |
1988 | 0 | for (const char *pszKey : apszKnownConfigOptions) |
1989 | 0 | aosList.AddString(pszKey); |
1990 | 0 | for (const auto &osKey : goSetKnownConfigOptions) |
1991 | 0 | aosList.AddString(osKey); |
1992 | 0 | return aosList.StealList(); |
1993 | 0 | } |
1994 | | |
1995 | | /************************************************************************/ |
1996 | | /* CPLSetConfigOptionDetectUnknownConfigOption() */ |
1997 | | /************************************************************************/ |
1998 | | |
1999 | | static void CPLSetConfigOptionDetectUnknownConfigOption(const char *pszKey, |
2000 | | const char *pszValue) |
2001 | 15.2k | { |
2002 | 15.2k | if (EQUAL(pszKey, "CPL_DEBUG")) |
2003 | 411 | { |
2004 | 411 | gnDebug = pszValue ? CPLTestBool(pszValue) : false; |
2005 | 411 | } |
2006 | 14.8k | else if (CPLIsDebugEnabled()) |
2007 | 13.9k | { |
2008 | 13.9k | if (!std::binary_search(std::begin(apszKnownConfigOptions), |
2009 | 13.9k | std::end(apszKnownConfigOptions), pszKey, |
2010 | 13.9k | [](const char *a, const char *b) |
2011 | 155k | { return STRCASECMP(a, b) < 0; })) |
2012 | 13.1k | { |
2013 | 13.1k | bool bFound; |
2014 | 13.1k | { |
2015 | 13.1k | std::lock_guard oLock(goMutexDeclaredKnownConfigOptions); |
2016 | 13.1k | bFound = cpl::contains(goSetKnownConfigOptions, |
2017 | 13.1k | CPLString(pszKey).toupper()); |
2018 | 13.1k | } |
2019 | 13.1k | if (!bFound) |
2020 | 13.1k | { |
2021 | 13.1k | const char *pszOldValue = CPLGetConfigOption(pszKey, nullptr); |
2022 | 13.1k | if (!((!pszValue && !pszOldValue) || |
2023 | 13.1k | (pszValue && pszOldValue && |
2024 | 13.1k | EQUAL(pszValue, pszOldValue)))) |
2025 | 7.48k | { |
2026 | 7.48k | CPLError(CE_Warning, CPLE_AppDefined, |
2027 | 7.48k | "Unknown configuration option '%s'.", pszKey); |
2028 | 7.48k | } |
2029 | 13.1k | } |
2030 | 13.1k | } |
2031 | 13.9k | } |
2032 | 15.2k | } |
2033 | | |
2034 | | /************************************************************************/ |
2035 | | /* CPLSetConfigOption() */ |
2036 | | /************************************************************************/ |
2037 | | |
2038 | | /** |
2039 | | * Set a configuration option for GDAL/OGR use. |
2040 | | * |
2041 | | * Those options are defined as a (key, value) couple. The value corresponding |
2042 | | * to a key can be got later with the CPLGetConfigOption() method. |
2043 | | * |
2044 | | * This mechanism is similar to environment variables, but options set with |
2045 | | * CPLSetConfigOption() overrides, for CPLGetConfigOption() point of view, |
2046 | | * values defined in the environment. |
2047 | | * |
2048 | | * If CPLSetConfigOption() is called several times with the same key, the |
2049 | | * value provided during the last call will be used. |
2050 | | * |
2051 | | * Options can also be passed on the command line of most GDAL utilities |
2052 | | * with '\--config KEY VALUE' (or '\--config KEY=VALUE' since GDAL 3.10). |
2053 | | * For example, ogrinfo \--config CPL_DEBUG ON ~/data/test/point.shp |
2054 | | * |
2055 | | * This function can also be used to clear a setting by passing NULL as the |
2056 | | * value (note: passing NULL will not unset an existing environment variable; |
2057 | | * it will just unset a value previously set by CPLSetConfigOption()). |
2058 | | * |
2059 | | * Starting with GDAL 3.11, if CPL_DEBUG is enabled prior to this call, and |
2060 | | * CPLSetConfigOption() is called with a key that is neither a known |
2061 | | * configuration option of GDAL itself, or one that has been declared with |
2062 | | * CPLDeclareKnownConfigOption(), a warning will be emitted. |
2063 | | * |
2064 | | * @param pszKey the key of the option |
2065 | | * @param pszValue the value of the option, or NULL to clear a setting. |
2066 | | * |
2067 | | * @see https://gdal.org/user/configoptions.html |
2068 | | */ |
2069 | | void CPL_STDCALL CPLSetConfigOption(const char *pszKey, const char *pszValue) |
2070 | | |
2071 | 15.2k | { |
2072 | | #ifdef DEBUG_CONFIG_OPTIONS |
2073 | | CPLAccessConfigOption(pszKey, FALSE); |
2074 | | #endif |
2075 | 15.2k | CPLMutexHolderD(&hConfigMutex); |
2076 | | |
2077 | 15.2k | #ifdef OGRAPISPY_ENABLED |
2078 | 15.2k | OGRAPISPYCPLSetConfigOption(pszKey, pszValue); |
2079 | 15.2k | #endif |
2080 | | |
2081 | 15.2k | CPLSetConfigOptionDetectUnknownConfigOption(pszKey, pszValue); |
2082 | | |
2083 | 15.2k | g_papszConfigOptions = const_cast<volatile char **>(CSLSetNameValue( |
2084 | 15.2k | const_cast<char **>(g_papszConfigOptions), pszKey, pszValue)); |
2085 | | |
2086 | 15.2k | NotifyOtherComponentsConfigOptionChanged(pszKey, pszValue, |
2087 | 15.2k | /*bTheadLocal=*/false); |
2088 | 15.2k | } |
2089 | | |
2090 | | /************************************************************************/ |
2091 | | /* CPLSetThreadLocalTLSFreeFunc() */ |
2092 | | /************************************************************************/ |
2093 | | |
2094 | | /* non-stdcall wrapper function for CSLDestroy() (#5590) */ |
2095 | | static void CPLSetThreadLocalTLSFreeFunc(void *pData) |
2096 | 0 | { |
2097 | 0 | CSLDestroy(reinterpret_cast<char **>(pData)); |
2098 | 0 | } |
2099 | | |
2100 | | /************************************************************************/ |
2101 | | /* CPLSetThreadLocalConfigOption() */ |
2102 | | /************************************************************************/ |
2103 | | |
2104 | | /** |
2105 | | * Set a configuration option for GDAL/OGR use. |
2106 | | * |
2107 | | * Those options are defined as a (key, value) couple. The value corresponding |
2108 | | * to a key can be got later with the CPLGetConfigOption() method. |
2109 | | * |
2110 | | * This function sets the configuration option that only applies in the |
2111 | | * current thread, as opposed to CPLSetConfigOption() which sets an option |
2112 | | * that applies on all threads. CPLSetThreadLocalConfigOption() will override |
2113 | | * the effect of CPLSetConfigOption) for the current thread. |
2114 | | * |
2115 | | * This function can also be used to clear a setting by passing NULL as the |
2116 | | * value (note: passing NULL will not unset an existing environment variable or |
2117 | | * a value set through CPLSetConfigOption(); |
2118 | | * it will just unset a value previously set by |
2119 | | * CPLSetThreadLocalConfigOption()). |
2120 | | * |
2121 | | * @param pszKey the key of the option |
2122 | | * @param pszValue the value of the option, or NULL to clear a setting. |
2123 | | */ |
2124 | | |
2125 | | void CPL_STDCALL CPLSetThreadLocalConfigOption(const char *pszKey, |
2126 | | const char *pszValue) |
2127 | | |
2128 | 0 | { |
2129 | | #ifdef DEBUG_CONFIG_OPTIONS |
2130 | | CPLAccessConfigOption(pszKey, FALSE); |
2131 | | #endif |
2132 | |
|
2133 | 0 | #ifdef OGRAPISPY_ENABLED |
2134 | 0 | OGRAPISPYCPLSetThreadLocalConfigOption(pszKey, pszValue); |
2135 | 0 | #endif |
2136 | |
|
2137 | 0 | int bMemoryError = FALSE; |
2138 | 0 | char **papszTLConfigOptions = reinterpret_cast<char **>( |
2139 | 0 | CPLGetTLSEx(CTLS_CONFIGOPTIONS, &bMemoryError)); |
2140 | 0 | if (bMemoryError) |
2141 | 0 | return; |
2142 | | |
2143 | 0 | CPLSetConfigOptionDetectUnknownConfigOption(pszKey, pszValue); |
2144 | |
|
2145 | 0 | papszTLConfigOptions = |
2146 | 0 | CSLSetNameValue(papszTLConfigOptions, pszKey, pszValue); |
2147 | |
|
2148 | 0 | CPLSetTLSWithFreeFunc(CTLS_CONFIGOPTIONS, papszTLConfigOptions, |
2149 | 0 | CPLSetThreadLocalTLSFreeFunc); |
2150 | |
|
2151 | 0 | NotifyOtherComponentsConfigOptionChanged(pszKey, pszValue, |
2152 | 0 | /*bTheadLocal=*/true); |
2153 | 0 | } |
2154 | | |
2155 | | /************************************************************************/ |
2156 | | /* CPLGetThreadLocalConfigOptions() */ |
2157 | | /************************************************************************/ |
2158 | | |
2159 | | /** |
2160 | | * Return the list of thread local configuration options as KEY=VALUE pairs. |
2161 | | * |
2162 | | * Options that through environment variables or with |
2163 | | * CPLSetConfigOption() will *not* be listed. |
2164 | | * |
2165 | | * @return a copy of the list, to be freed with CSLDestroy(). |
2166 | | * @since GDAL 2.2 |
2167 | | */ |
2168 | | char **CPLGetThreadLocalConfigOptions(void) |
2169 | 0 | { |
2170 | 0 | int bMemoryError = FALSE; |
2171 | 0 | char **papszTLConfigOptions = reinterpret_cast<char **>( |
2172 | 0 | CPLGetTLSEx(CTLS_CONFIGOPTIONS, &bMemoryError)); |
2173 | 0 | if (bMemoryError) |
2174 | 0 | return nullptr; |
2175 | 0 | return CSLDuplicate(papszTLConfigOptions); |
2176 | 0 | } |
2177 | | |
2178 | | /************************************************************************/ |
2179 | | /* CPLSetThreadLocalConfigOptions() */ |
2180 | | /************************************************************************/ |
2181 | | |
2182 | | /** |
2183 | | * Replace the full list of thread local configuration options with the |
2184 | | * passed list of KEY=VALUE pairs. |
2185 | | * |
2186 | | * This has the same effect of clearing the existing list, and setting |
2187 | | * individually each pair with the CPLSetThreadLocalConfigOption() API. |
2188 | | * |
2189 | | * This does not affect options set through environment variables or with |
2190 | | * CPLSetConfigOption(). |
2191 | | * |
2192 | | * The passed list is copied by the function. |
2193 | | * |
2194 | | * @param papszConfigOptions the new list (or NULL). |
2195 | | * |
2196 | | * @since GDAL 2.2 |
2197 | | */ |
2198 | | void CPLSetThreadLocalConfigOptions(const char *const *papszConfigOptions) |
2199 | 0 | { |
2200 | 0 | int bMemoryError = FALSE; |
2201 | 0 | char **papszTLConfigOptions = reinterpret_cast<char **>( |
2202 | 0 | CPLGetTLSEx(CTLS_CONFIGOPTIONS, &bMemoryError)); |
2203 | 0 | if (bMemoryError) |
2204 | 0 | return; |
2205 | 0 | CSLDestroy(papszTLConfigOptions); |
2206 | 0 | papszTLConfigOptions = |
2207 | 0 | CSLDuplicate(const_cast<char **>(papszConfigOptions)); |
2208 | 0 | CPLSetTLSWithFreeFunc(CTLS_CONFIGOPTIONS, papszTLConfigOptions, |
2209 | 0 | CPLSetThreadLocalTLSFreeFunc); |
2210 | 0 | } |
2211 | | |
2212 | | /************************************************************************/ |
2213 | | /* CPLFreeConfig() */ |
2214 | | /************************************************************************/ |
2215 | | |
2216 | | void CPL_STDCALL CPLFreeConfig() |
2217 | | |
2218 | 0 | { |
2219 | 0 | { |
2220 | 0 | CPLMutexHolderD(&hConfigMutex); |
2221 | |
|
2222 | 0 | CSLDestroy(const_cast<char **>(g_papszConfigOptions)); |
2223 | 0 | g_papszConfigOptions = nullptr; |
2224 | |
|
2225 | 0 | int bMemoryError = FALSE; |
2226 | 0 | char **papszTLConfigOptions = reinterpret_cast<char **>( |
2227 | 0 | CPLGetTLSEx(CTLS_CONFIGOPTIONS, &bMemoryError)); |
2228 | 0 | if (papszTLConfigOptions != nullptr) |
2229 | 0 | { |
2230 | 0 | CSLDestroy(papszTLConfigOptions); |
2231 | 0 | CPLSetTLS(CTLS_CONFIGOPTIONS, nullptr, FALSE); |
2232 | 0 | } |
2233 | 0 | } |
2234 | 0 | CPLDestroyMutex(hConfigMutex); |
2235 | 0 | hConfigMutex = nullptr; |
2236 | 0 | } |
2237 | | |
2238 | | /************************************************************************/ |
2239 | | /* CPLLoadConfigOptionsFromFile() */ |
2240 | | /************************************************************************/ |
2241 | | |
2242 | | /** Load configuration from a given configuration file. |
2243 | | |
2244 | | A configuration file is a text file in a .ini style format, that lists |
2245 | | configuration options and their values. |
2246 | | Lines starting with # are comment lines. |
2247 | | |
2248 | | Example: |
2249 | | \verbatim |
2250 | | [configoptions] |
2251 | | # set BAR as the value of configuration option FOO |
2252 | | FOO=BAR |
2253 | | \endverbatim |
2254 | | |
2255 | | Starting with GDAL 3.5, a configuration file can also contain credentials |
2256 | | (or more generally options related to a virtual file system) for a given path |
2257 | | prefix, that can also be set with VSISetPathSpecificOption(). Credentials should |
2258 | | be put under a [credentials] section, and for each path prefix, under a relative |
2259 | | subsection whose name starts with "[." (e.g. "[.some_arbitrary_name]"), and |
2260 | | whose first key is "path". |
2261 | | |
2262 | | Example: |
2263 | | \verbatim |
2264 | | [credentials] |
2265 | | |
2266 | | [.private_bucket] |
2267 | | path=/vsis3/my_private_bucket |
2268 | | AWS_SECRET_ACCESS_KEY=... |
2269 | | AWS_ACCESS_KEY_ID=... |
2270 | | |
2271 | | [.sentinel_s2_l1c] |
2272 | | path=/vsis3/sentinel-s2-l1c |
2273 | | AWS_REQUEST_PAYER=requester |
2274 | | \endverbatim |
2275 | | |
2276 | | Starting with GDAL 3.6, a leading [directives] section might be added with |
2277 | | a "ignore-env-vars=yes" setting to indicate that, starting with that point, |
2278 | | all environment variables should be ignored, and only configuration options |
2279 | | defined in the [configoptions] sections or through the CPLSetConfigOption() / |
2280 | | CPLSetThreadLocalConfigOption() functions should be taken into account. |
2281 | | |
2282 | | This function is typically called by CPLLoadConfigOptionsFromPredefinedFiles() |
2283 | | |
2284 | | @param pszFilename File where to load configuration from. |
2285 | | @param bOverrideEnvVars Whether configuration options from the configuration |
2286 | | file should override environment variables. |
2287 | | @since GDAL 3.3 |
2288 | | */ |
2289 | | void CPLLoadConfigOptionsFromFile(const char *pszFilename, int bOverrideEnvVars) |
2290 | 0 | { |
2291 | 0 | VSILFILE *fp = VSIFOpenL(pszFilename, "rb"); |
2292 | 0 | if (fp == nullptr) |
2293 | 0 | return; |
2294 | 0 | CPLDebug("CPL", "Loading configuration from %s", pszFilename); |
2295 | 0 | const char *pszLine; |
2296 | 0 | enum class Section |
2297 | 0 | { |
2298 | 0 | NONE, |
2299 | 0 | GENERAL, |
2300 | 0 | CONFIG_OPTIONS, |
2301 | 0 | CREDENTIALS, |
2302 | 0 | }; |
2303 | 0 | Section eCurrentSection = Section::NONE; |
2304 | 0 | bool bInSubsection = false; |
2305 | 0 | std::string osPath; |
2306 | 0 | int nSectionCounter = 0; |
2307 | |
|
2308 | 0 | const auto IsSpaceOnly = [](const char *pszStr) |
2309 | 0 | { |
2310 | 0 | for (; *pszStr; ++pszStr) |
2311 | 0 | { |
2312 | 0 | if (!isspace(static_cast<unsigned char>(*pszStr))) |
2313 | 0 | return false; |
2314 | 0 | } |
2315 | 0 | return true; |
2316 | 0 | }; |
2317 | |
|
2318 | 0 | while ((pszLine = CPLReadLine2L(fp, -1, nullptr)) != nullptr) |
2319 | 0 | { |
2320 | 0 | if (IsSpaceOnly(pszLine)) |
2321 | 0 | { |
2322 | | // Blank line |
2323 | 0 | } |
2324 | 0 | else if (pszLine[0] == '#') |
2325 | 0 | { |
2326 | | // Comment line |
2327 | 0 | } |
2328 | 0 | else if (strcmp(pszLine, "[configoptions]") == 0) |
2329 | 0 | { |
2330 | 0 | nSectionCounter++; |
2331 | 0 | eCurrentSection = Section::CONFIG_OPTIONS; |
2332 | 0 | } |
2333 | 0 | else if (strcmp(pszLine, "[credentials]") == 0) |
2334 | 0 | { |
2335 | 0 | nSectionCounter++; |
2336 | 0 | eCurrentSection = Section::CREDENTIALS; |
2337 | 0 | bInSubsection = false; |
2338 | 0 | osPath.clear(); |
2339 | 0 | } |
2340 | 0 | else if (strcmp(pszLine, "[directives]") == 0) |
2341 | 0 | { |
2342 | 0 | nSectionCounter++; |
2343 | 0 | if (nSectionCounter != 1) |
2344 | 0 | { |
2345 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
2346 | 0 | "The [directives] section should be the first one in " |
2347 | 0 | "the file, otherwise some its settings might not be " |
2348 | 0 | "used correctly."); |
2349 | 0 | } |
2350 | 0 | eCurrentSection = Section::GENERAL; |
2351 | 0 | } |
2352 | 0 | else if (eCurrentSection == Section::GENERAL) |
2353 | 0 | { |
2354 | 0 | char *pszKey = nullptr; |
2355 | 0 | const char *pszValue = CPLParseNameValue(pszLine, &pszKey); |
2356 | 0 | if (pszKey && pszValue) |
2357 | 0 | { |
2358 | 0 | if (strcmp(pszKey, "ignore-env-vars") == 0) |
2359 | 0 | { |
2360 | 0 | gbIgnoreEnvVariables = CPLTestBool(pszValue); |
2361 | 0 | } |
2362 | 0 | else |
2363 | 0 | { |
2364 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
2365 | 0 | "Ignoring %s line in [directives] section", |
2366 | 0 | pszLine); |
2367 | 0 | } |
2368 | 0 | } |
2369 | 0 | CPLFree(pszKey); |
2370 | 0 | } |
2371 | 0 | else if (eCurrentSection == Section::CREDENTIALS) |
2372 | 0 | { |
2373 | 0 | if (strncmp(pszLine, "[.", 2) == 0) |
2374 | 0 | { |
2375 | 0 | bInSubsection = true; |
2376 | 0 | osPath.clear(); |
2377 | 0 | } |
2378 | 0 | else if (bInSubsection) |
2379 | 0 | { |
2380 | 0 | char *pszKey = nullptr; |
2381 | 0 | const char *pszValue = CPLParseNameValue(pszLine, &pszKey); |
2382 | 0 | if (pszKey && pszValue) |
2383 | 0 | { |
2384 | 0 | if (strcmp(pszKey, "path") == 0) |
2385 | 0 | { |
2386 | 0 | if (!osPath.empty()) |
2387 | 0 | { |
2388 | 0 | CPLError( |
2389 | 0 | CE_Warning, CPLE_AppDefined, |
2390 | 0 | "Duplicated 'path' key in the same subsection. " |
2391 | 0 | "Ignoring %s=%s", |
2392 | 0 | pszKey, pszValue); |
2393 | 0 | } |
2394 | 0 | else |
2395 | 0 | { |
2396 | 0 | osPath = pszValue; |
2397 | 0 | } |
2398 | 0 | } |
2399 | 0 | else if (osPath.empty()) |
2400 | 0 | { |
2401 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
2402 | 0 | "First entry in a credentials subsection " |
2403 | 0 | "should be 'path'."); |
2404 | 0 | } |
2405 | 0 | else |
2406 | 0 | { |
2407 | 0 | VSISetPathSpecificOption(osPath.c_str(), pszKey, |
2408 | 0 | pszValue); |
2409 | 0 | } |
2410 | 0 | } |
2411 | 0 | CPLFree(pszKey); |
2412 | 0 | } |
2413 | 0 | else if (pszLine[0] == '[') |
2414 | 0 | { |
2415 | 0 | eCurrentSection = Section::NONE; |
2416 | 0 | } |
2417 | 0 | else |
2418 | 0 | { |
2419 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
2420 | 0 | "Ignoring content in [credential] section that is not " |
2421 | 0 | "in a [.xxxxx] subsection"); |
2422 | 0 | } |
2423 | 0 | } |
2424 | 0 | else if (pszLine[0] == '[') |
2425 | 0 | { |
2426 | 0 | eCurrentSection = Section::NONE; |
2427 | 0 | } |
2428 | 0 | else if (eCurrentSection == Section::CONFIG_OPTIONS) |
2429 | 0 | { |
2430 | 0 | char *pszKey = nullptr; |
2431 | 0 | const char *pszValue = CPLParseNameValue(pszLine, &pszKey); |
2432 | 0 | if (pszKey && pszValue) |
2433 | 0 | { |
2434 | 0 | if (bOverrideEnvVars || gbIgnoreEnvVariables || |
2435 | 0 | getenv(pszKey) == nullptr) |
2436 | 0 | { |
2437 | 0 | CPLDebugOnly("CPL", "Setting configuration option %s=%s", |
2438 | 0 | pszKey, pszValue); |
2439 | 0 | CPLSetConfigOption(pszKey, pszValue); |
2440 | 0 | } |
2441 | 0 | else |
2442 | 0 | { |
2443 | 0 | CPLDebug("CPL", |
2444 | 0 | "Ignoring configuration option %s=%s from " |
2445 | 0 | "configuration file as it is already set " |
2446 | 0 | "as an environment variable", |
2447 | 0 | pszKey, pszValue); |
2448 | 0 | } |
2449 | 0 | } |
2450 | 0 | CPLFree(pszKey); |
2451 | 0 | } |
2452 | 0 | } |
2453 | 0 | VSIFCloseL(fp); |
2454 | 0 | } |
2455 | | |
2456 | | /************************************************************************/ |
2457 | | /* CPLLoadConfigOptionsFromPredefinedFiles() */ |
2458 | | /************************************************************************/ |
2459 | | |
2460 | | /** Load configuration from a set of predefined files. |
2461 | | * |
2462 | | * If the environment variable (or configuration option) GDAL_CONFIG_FILE is |
2463 | | * set, then CPLLoadConfigOptionsFromFile() will be called with the value of |
2464 | | * this configuration option as the file location. |
2465 | | * |
2466 | | * Otherwise, for Unix builds, CPLLoadConfigOptionsFromFile() will be called |
2467 | | * with ${sysconfdir}/gdal/gdalrc first where ${sysconfdir} evaluates |
2468 | | * to ${prefix}/etc, unless the \--sysconfdir switch of configure has been |
2469 | | * invoked. |
2470 | | * |
2471 | | * Then CPLLoadConfigOptionsFromFile() will be called with ${HOME}/.gdal/gdalrc |
2472 | | * on Unix builds (potentially overriding what was loaded with the sysconfdir) |
2473 | | * or ${USERPROFILE}/.gdal/gdalrc on Windows builds. |
2474 | | * |
2475 | | * CPLLoadConfigOptionsFromFile() will be called with bOverrideEnvVars = false, |
2476 | | * that is the value of environment variables previously set will be used |
2477 | | * instead of the value set in the configuration files (unless the configuration |
2478 | | * file contains a leading [directives] section with a "ignore-env-vars=yes" |
2479 | | * setting). |
2480 | | * |
2481 | | * This function is automatically called by GDALDriverManager() constructor |
2482 | | * |
2483 | | * @since GDAL 3.3 |
2484 | | */ |
2485 | | void CPLLoadConfigOptionsFromPredefinedFiles() |
2486 | 0 | { |
2487 | 0 | const char *pszFile = CPLGetConfigOption("GDAL_CONFIG_FILE", nullptr); |
2488 | 0 | if (pszFile != nullptr) |
2489 | 0 | { |
2490 | 0 | CPLLoadConfigOptionsFromFile(pszFile, false); |
2491 | 0 | } |
2492 | 0 | else |
2493 | 0 | { |
2494 | 0 | #ifdef SYSCONFDIR |
2495 | 0 | CPLLoadConfigOptionsFromFile( |
2496 | 0 | CPLFormFilenameSafe( |
2497 | 0 | CPLFormFilenameSafe(SYSCONFDIR, "gdal", nullptr).c_str(), |
2498 | 0 | "gdalrc", nullptr) |
2499 | 0 | .c_str(), |
2500 | 0 | false); |
2501 | 0 | #endif |
2502 | |
|
2503 | | #ifdef _WIN32 |
2504 | | const char *pszHome = CPLGetConfigOption("USERPROFILE", nullptr); |
2505 | | #else |
2506 | 0 | const char *pszHome = CPLGetConfigOption("HOME", nullptr); |
2507 | 0 | #endif |
2508 | 0 | if (pszHome != nullptr) |
2509 | 0 | { |
2510 | 0 | CPLLoadConfigOptionsFromFile( |
2511 | 0 | CPLFormFilenameSafe( |
2512 | 0 | CPLFormFilenameSafe(pszHome, ".gdal", nullptr).c_str(), |
2513 | 0 | "gdalrc", nullptr) |
2514 | 0 | .c_str(), |
2515 | 0 | false); |
2516 | 0 | } |
2517 | 0 | } |
2518 | 0 | } |
2519 | | |
2520 | | /************************************************************************/ |
2521 | | /* CPLStat() */ |
2522 | | /************************************************************************/ |
2523 | | |
2524 | | /** Same as VSIStat() except it works on "C:" as if it were "C:\". */ |
2525 | | |
2526 | | int CPLStat(const char *pszPath, VSIStatBuf *psStatBuf) |
2527 | | |
2528 | 0 | { |
2529 | 0 | if (strlen(pszPath) == 2 && pszPath[1] == ':') |
2530 | 0 | { |
2531 | 0 | char szAltPath[4] = {pszPath[0], pszPath[1], '\\', '\0'}; |
2532 | 0 | return VSIStat(szAltPath, psStatBuf); |
2533 | 0 | } |
2534 | | |
2535 | 0 | return VSIStat(pszPath, psStatBuf); |
2536 | 0 | } |
2537 | | |
2538 | | /************************************************************************/ |
2539 | | /* proj_strtod() */ |
2540 | | /************************************************************************/ |
2541 | | static double proj_strtod(char *nptr, char **endptr) |
2542 | | |
2543 | 0 | { |
2544 | 0 | char c = '\0'; |
2545 | 0 | char *cp = nptr; |
2546 | | |
2547 | | // Scan for characters which cause problems with VC++ strtod(). |
2548 | 0 | while ((c = *cp) != '\0') |
2549 | 0 | { |
2550 | 0 | if (c == 'd' || c == 'D') |
2551 | 0 | { |
2552 | | // Found one, so NUL it out, call strtod(), |
2553 | | // then restore it and return. |
2554 | 0 | *cp = '\0'; |
2555 | 0 | const double result = CPLStrtod(nptr, endptr); |
2556 | 0 | *cp = c; |
2557 | 0 | return result; |
2558 | 0 | } |
2559 | 0 | ++cp; |
2560 | 0 | } |
2561 | | |
2562 | | // No offending characters, just handle normally. |
2563 | | |
2564 | 0 | return CPLStrtod(nptr, endptr); |
2565 | 0 | } |
2566 | | |
2567 | | /************************************************************************/ |
2568 | | /* CPLDMSToDec() */ |
2569 | | /************************************************************************/ |
2570 | | |
2571 | | static const char *sym = "NnEeSsWw"; |
2572 | | constexpr double vm[] = {1.0, 0.0166666666667, 0.00027777778}; |
2573 | | |
2574 | | /** CPLDMSToDec */ |
2575 | | double CPLDMSToDec(const char *is) |
2576 | | |
2577 | 0 | { |
2578 | | // Copy string into work space. |
2579 | 0 | while (isspace(static_cast<unsigned char>(*is))) |
2580 | 0 | ++is; |
2581 | |
|
2582 | 0 | const char *p = is; |
2583 | 0 | char work[64] = {}; |
2584 | 0 | char *s = work; |
2585 | 0 | int n = sizeof(work); |
2586 | 0 | for (; isgraph(*p) && --n;) |
2587 | 0 | *s++ = *p++; |
2588 | 0 | *s = '\0'; |
2589 | | // It is possible that a really odd input (like lots of leading |
2590 | | // zeros) could be truncated in copying into work. But... |
2591 | 0 | s = work; |
2592 | 0 | int sign = *s; |
2593 | |
|
2594 | 0 | if (sign == '+' || sign == '-') |
2595 | 0 | s++; |
2596 | 0 | else |
2597 | 0 | sign = '+'; |
2598 | |
|
2599 | 0 | int nl = 0; |
2600 | 0 | double v = 0.0; |
2601 | 0 | for (; nl < 3; nl = n + 1) |
2602 | 0 | { |
2603 | 0 | if (!(isdigit(static_cast<unsigned char>(*s)) || *s == '.')) |
2604 | 0 | break; |
2605 | 0 | const double tv = proj_strtod(s, &s); |
2606 | 0 | if (tv == HUGE_VAL) |
2607 | 0 | return tv; |
2608 | 0 | switch (*s) |
2609 | 0 | { |
2610 | 0 | case 'D': |
2611 | 0 | case 'd': |
2612 | 0 | n = 0; |
2613 | 0 | break; |
2614 | 0 | case '\'': |
2615 | 0 | n = 1; |
2616 | 0 | break; |
2617 | 0 | case '"': |
2618 | 0 | n = 2; |
2619 | 0 | break; |
2620 | 0 | case 'r': |
2621 | 0 | case 'R': |
2622 | 0 | if (nl) |
2623 | 0 | { |
2624 | 0 | return 0.0; |
2625 | 0 | } |
2626 | 0 | ++s; |
2627 | 0 | v = tv; |
2628 | 0 | goto skip; |
2629 | 0 | default: |
2630 | 0 | v += tv * vm[nl]; |
2631 | 0 | skip: |
2632 | 0 | n = 4; |
2633 | 0 | continue; |
2634 | 0 | } |
2635 | 0 | if (n < nl) |
2636 | 0 | { |
2637 | 0 | return 0.0; |
2638 | 0 | } |
2639 | 0 | v += tv * vm[n]; |
2640 | 0 | ++s; |
2641 | 0 | } |
2642 | | // Postfix sign. |
2643 | 0 | if (*s && ((p = strchr(sym, *s))) != nullptr) |
2644 | 0 | { |
2645 | 0 | sign = (p - sym) >= 4 ? '-' : '+'; |
2646 | 0 | ++s; |
2647 | 0 | } |
2648 | 0 | if (sign == '-') |
2649 | 0 | v = -v; |
2650 | |
|
2651 | 0 | return v; |
2652 | 0 | } |
2653 | | |
2654 | | /************************************************************************/ |
2655 | | /* CPLDecToDMS() */ |
2656 | | /************************************************************************/ |
2657 | | |
2658 | | /** Translate a decimal degrees value to a DMS string with hemisphere. */ |
2659 | | |
2660 | | const char *CPLDecToDMS(double dfAngle, const char *pszAxis, int nPrecision) |
2661 | | |
2662 | 0 | { |
2663 | 0 | VALIDATE_POINTER1(pszAxis, "CPLDecToDMS", ""); |
2664 | | |
2665 | 0 | if (std::isnan(dfAngle)) |
2666 | 0 | return "Invalid angle"; |
2667 | | |
2668 | 0 | const double dfEpsilon = (0.5 / 3600.0) * pow(0.1, nPrecision); |
2669 | 0 | const double dfABSAngle = std::abs(dfAngle) + dfEpsilon; |
2670 | 0 | if (dfABSAngle > 361.0) |
2671 | 0 | { |
2672 | 0 | return "Invalid angle"; |
2673 | 0 | } |
2674 | | |
2675 | 0 | const int nDegrees = static_cast<int>(dfABSAngle); |
2676 | 0 | const int nMinutes = static_cast<int>((dfABSAngle - nDegrees) * 60); |
2677 | 0 | double dfSeconds = dfABSAngle * 3600 - nDegrees * 3600 - nMinutes * 60; |
2678 | |
|
2679 | 0 | if (dfSeconds > dfEpsilon * 3600.0) |
2680 | 0 | dfSeconds -= dfEpsilon * 3600.0; |
2681 | |
|
2682 | 0 | const char *pszHemisphere = nullptr; |
2683 | 0 | if (EQUAL(pszAxis, "Long") && dfAngle < 0.0) |
2684 | 0 | pszHemisphere = "W"; |
2685 | 0 | else if (EQUAL(pszAxis, "Long")) |
2686 | 0 | pszHemisphere = "E"; |
2687 | 0 | else if (dfAngle < 0.0) |
2688 | 0 | pszHemisphere = "S"; |
2689 | 0 | else |
2690 | 0 | pszHemisphere = "N"; |
2691 | |
|
2692 | 0 | char szFormat[30] = {}; |
2693 | 0 | CPLsnprintf(szFormat, sizeof(szFormat), "%%3dd%%2d\'%%%d.%df\"%s", |
2694 | 0 | nPrecision + 3, nPrecision, pszHemisphere); |
2695 | |
|
2696 | 0 | static CPL_THREADLOCAL char szBuffer[50] = {}; |
2697 | 0 | CPLsnprintf(szBuffer, sizeof(szBuffer), szFormat, nDegrees, nMinutes, |
2698 | 0 | dfSeconds); |
2699 | |
|
2700 | 0 | return szBuffer; |
2701 | 0 | } |
2702 | | |
2703 | | /************************************************************************/ |
2704 | | /* CPLPackedDMSToDec() */ |
2705 | | /************************************************************************/ |
2706 | | |
2707 | | /** |
2708 | | * Convert a packed DMS value (DDDMMMSSS.SS) into decimal degrees. |
2709 | | * |
2710 | | * This function converts a packed DMS angle to seconds. The standard |
2711 | | * packed DMS format is: |
2712 | | * |
2713 | | * degrees * 1000000 + minutes * 1000 + seconds |
2714 | | * |
2715 | | * Example: angle = 120025045.25 yields |
2716 | | * deg = 120 |
2717 | | * min = 25 |
2718 | | * sec = 45.25 |
2719 | | * |
2720 | | * The algorithm used for the conversion is as follows: |
2721 | | * |
2722 | | * 1. The absolute value of the angle is used. |
2723 | | * |
2724 | | * 2. The degrees are separated out: |
2725 | | * deg = angle/1000000 (fractional portion truncated) |
2726 | | * |
2727 | | * 3. The minutes are separated out: |
2728 | | * min = (angle - deg * 1000000) / 1000 (fractional portion truncated) |
2729 | | * |
2730 | | * 4. The seconds are then computed: |
2731 | | * sec = angle - deg * 1000000 - min * 1000 |
2732 | | * |
2733 | | * 5. The total angle in seconds is computed: |
2734 | | * sec = deg * 3600.0 + min * 60.0 + sec |
2735 | | * |
2736 | | * 6. The sign of sec is set to that of the input angle. |
2737 | | * |
2738 | | * Packed DMS values used by the USGS GCTP package and probably by other |
2739 | | * software. |
2740 | | * |
2741 | | * NOTE: This code does not validate input value. If you give the wrong |
2742 | | * value, you will get the wrong result. |
2743 | | * |
2744 | | * @param dfPacked Angle in packed DMS format. |
2745 | | * |
2746 | | * @return Angle in decimal degrees. |
2747 | | * |
2748 | | */ |
2749 | | |
2750 | | double CPLPackedDMSToDec(double dfPacked) |
2751 | 0 | { |
2752 | 0 | const double dfSign = dfPacked < 0.0 ? -1 : 1; |
2753 | |
|
2754 | 0 | double dfSeconds = std::abs(dfPacked); |
2755 | 0 | double dfDegrees = floor(dfSeconds / 1000000.0); |
2756 | 0 | dfSeconds -= dfDegrees * 1000000.0; |
2757 | 0 | const double dfMinutes = floor(dfSeconds / 1000.0); |
2758 | 0 | dfSeconds -= dfMinutes * 1000.0; |
2759 | 0 | dfSeconds = dfSign * (dfDegrees * 3600.0 + dfMinutes * 60.0 + dfSeconds); |
2760 | 0 | dfDegrees = dfSeconds / 3600.0; |
2761 | |
|
2762 | 0 | return dfDegrees; |
2763 | 0 | } |
2764 | | |
2765 | | /************************************************************************/ |
2766 | | /* CPLDecToPackedDMS() */ |
2767 | | /************************************************************************/ |
2768 | | /** |
2769 | | * Convert decimal degrees into packed DMS value (DDDMMMSSS.SS). |
2770 | | * |
2771 | | * This function converts a value, specified in decimal degrees into |
2772 | | * packed DMS angle. The standard packed DMS format is: |
2773 | | * |
2774 | | * degrees * 1000000 + minutes * 1000 + seconds |
2775 | | * |
2776 | | * See also CPLPackedDMSToDec(). |
2777 | | * |
2778 | | * @param dfDec Angle in decimal degrees. |
2779 | | * |
2780 | | * @return Angle in packed DMS format. |
2781 | | * |
2782 | | */ |
2783 | | |
2784 | | double CPLDecToPackedDMS(double dfDec) |
2785 | 0 | { |
2786 | 0 | const double dfSign = dfDec < 0.0 ? -1 : 1; |
2787 | |
|
2788 | 0 | dfDec = std::abs(dfDec); |
2789 | 0 | const double dfDegrees = floor(dfDec); |
2790 | 0 | const double dfMinutes = floor((dfDec - dfDegrees) * 60.0); |
2791 | 0 | const double dfSeconds = (dfDec - dfDegrees) * 3600.0 - dfMinutes * 60.0; |
2792 | |
|
2793 | 0 | return dfSign * (dfDegrees * 1000000.0 + dfMinutes * 1000.0 + dfSeconds); |
2794 | 0 | } |
2795 | | |
2796 | | /************************************************************************/ |
2797 | | /* CPLStringToComplex() */ |
2798 | | /************************************************************************/ |
2799 | | |
2800 | | /** Fetch the real and imaginary part of a serialized complex number */ |
2801 | | CPLErr CPL_DLL CPLStringToComplex(const char *pszString, double *pdfReal, |
2802 | | double *pdfImag) |
2803 | | |
2804 | 0 | { |
2805 | 0 | while (*pszString == ' ') |
2806 | 0 | pszString++; |
2807 | |
|
2808 | 0 | char *end; |
2809 | 0 | *pdfReal = CPLStrtod(pszString, &end); |
2810 | |
|
2811 | 0 | int iPlus = -1; |
2812 | 0 | int iImagEnd = -1; |
2813 | |
|
2814 | 0 | if (pszString == end) |
2815 | 0 | { |
2816 | 0 | goto error; |
2817 | 0 | } |
2818 | | |
2819 | 0 | *pdfImag = 0.0; |
2820 | |
|
2821 | 0 | for (int i = static_cast<int>(end - pszString); |
2822 | 0 | i < 100 && pszString[i] != '\0' && pszString[i] != ' '; i++) |
2823 | 0 | { |
2824 | 0 | if (pszString[i] == '+') |
2825 | 0 | { |
2826 | 0 | if (iPlus != -1) |
2827 | 0 | goto error; |
2828 | 0 | iPlus = i; |
2829 | 0 | } |
2830 | 0 | if (pszString[i] == '-') |
2831 | 0 | { |
2832 | 0 | if (iPlus != -1) |
2833 | 0 | goto error; |
2834 | 0 | iPlus = i; |
2835 | 0 | } |
2836 | 0 | if (pszString[i] == 'i') |
2837 | 0 | { |
2838 | 0 | if (iPlus == -1) |
2839 | 0 | goto error; |
2840 | 0 | iImagEnd = i; |
2841 | 0 | } |
2842 | 0 | } |
2843 | | |
2844 | | // If we have a "+" or "-" we must also have an "i" |
2845 | 0 | if ((iPlus == -1) != (iImagEnd == -1)) |
2846 | 0 | { |
2847 | 0 | goto error; |
2848 | 0 | } |
2849 | | |
2850 | | // Parse imaginary component, if any |
2851 | 0 | if (iPlus > -1) |
2852 | 0 | { |
2853 | 0 | *pdfImag = CPLStrtod(pszString + iPlus, &end); |
2854 | 0 | } |
2855 | | |
2856 | | // Check everything remaining is whitespace |
2857 | 0 | for (; *end != '\0'; end++) |
2858 | 0 | { |
2859 | 0 | if (!isspace(*end) && end - pszString != iImagEnd) |
2860 | 0 | { |
2861 | 0 | goto error; |
2862 | 0 | } |
2863 | 0 | } |
2864 | | |
2865 | 0 | return CE_None; |
2866 | | |
2867 | 0 | error: |
2868 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Failed to parse number: %s", |
2869 | 0 | pszString); |
2870 | 0 | return CE_Failure; |
2871 | 0 | } |
2872 | | |
2873 | | /************************************************************************/ |
2874 | | /* CPLOpenShared() */ |
2875 | | /************************************************************************/ |
2876 | | |
2877 | | /** |
2878 | | * Open a shared file handle. |
2879 | | * |
2880 | | * Some operating systems have limits on the number of file handles that can |
2881 | | * be open at one time. This function attempts to maintain a registry of |
2882 | | * already open file handles, and reuse existing ones if the same file |
2883 | | * is requested by another part of the application. |
2884 | | * |
2885 | | * Note that access is only shared for access types "r", "rb", "r+" and |
2886 | | * "rb+". All others will just result in direct VSIOpen() calls. Keep in |
2887 | | * mind that a file is only reused if the file name is exactly the same. |
2888 | | * Different names referring to the same file will result in different |
2889 | | * handles. |
2890 | | * |
2891 | | * The VSIFOpen() or VSIFOpenL() function is used to actually open the file, |
2892 | | * when an existing file handle can't be shared. |
2893 | | * |
2894 | | * @param pszFilename the name of the file to open. |
2895 | | * @param pszAccess the normal fopen()/VSIFOpen() style access string. |
2896 | | * @param bLargeIn If TRUE VSIFOpenL() (for large files) will be used instead of |
2897 | | * VSIFOpen(). |
2898 | | * |
2899 | | * @return a file handle or NULL if opening fails. |
2900 | | */ |
2901 | | |
2902 | | FILE *CPLOpenShared(const char *pszFilename, const char *pszAccess, |
2903 | | int bLargeIn) |
2904 | | |
2905 | 0 | { |
2906 | 0 | const bool bLarge = CPL_TO_BOOL(bLargeIn); |
2907 | 0 | CPLMutexHolderD(&hSharedFileMutex); |
2908 | 0 | const GIntBig nPID = CPLGetPID(); |
2909 | | |
2910 | | /* -------------------------------------------------------------------- */ |
2911 | | /* Is there an existing file we can use? */ |
2912 | | /* -------------------------------------------------------------------- */ |
2913 | 0 | const bool bReuse = EQUAL(pszAccess, "rb") || EQUAL(pszAccess, "rb+"); |
2914 | |
|
2915 | 0 | for (int i = 0; bReuse && i < nSharedFileCount; i++) |
2916 | 0 | { |
2917 | 0 | if (strcmp(pasSharedFileList[i].pszFilename, pszFilename) == 0 && |
2918 | 0 | !bLarge == !pasSharedFileList[i].bLarge && |
2919 | 0 | EQUAL(pasSharedFileList[i].pszAccess, pszAccess) && |
2920 | 0 | nPID == pasSharedFileListExtra[i].nPID) |
2921 | 0 | { |
2922 | 0 | pasSharedFileList[i].nRefCount++; |
2923 | 0 | return pasSharedFileList[i].fp; |
2924 | 0 | } |
2925 | 0 | } |
2926 | | |
2927 | | /* -------------------------------------------------------------------- */ |
2928 | | /* Open the file. */ |
2929 | | /* -------------------------------------------------------------------- */ |
2930 | 0 | FILE *fp = bLarge |
2931 | 0 | ? reinterpret_cast<FILE *>(VSIFOpenL(pszFilename, pszAccess)) |
2932 | 0 | : VSIFOpen(pszFilename, pszAccess); |
2933 | |
|
2934 | 0 | if (fp == nullptr) |
2935 | 0 | return nullptr; |
2936 | | |
2937 | | /* -------------------------------------------------------------------- */ |
2938 | | /* Add an entry to the list. */ |
2939 | | /* -------------------------------------------------------------------- */ |
2940 | 0 | nSharedFileCount++; |
2941 | |
|
2942 | 0 | pasSharedFileList = static_cast<CPLSharedFileInfo *>( |
2943 | 0 | CPLRealloc(const_cast<CPLSharedFileInfo *>(pasSharedFileList), |
2944 | 0 | sizeof(CPLSharedFileInfo) * nSharedFileCount)); |
2945 | 0 | pasSharedFileListExtra = static_cast<CPLSharedFileInfoExtra *>( |
2946 | 0 | CPLRealloc(const_cast<CPLSharedFileInfoExtra *>(pasSharedFileListExtra), |
2947 | 0 | sizeof(CPLSharedFileInfoExtra) * nSharedFileCount)); |
2948 | |
|
2949 | 0 | pasSharedFileList[nSharedFileCount - 1].fp = fp; |
2950 | 0 | pasSharedFileList[nSharedFileCount - 1].nRefCount = 1; |
2951 | 0 | pasSharedFileList[nSharedFileCount - 1].bLarge = bLarge; |
2952 | 0 | pasSharedFileList[nSharedFileCount - 1].pszFilename = |
2953 | 0 | CPLStrdup(pszFilename); |
2954 | 0 | pasSharedFileList[nSharedFileCount - 1].pszAccess = CPLStrdup(pszAccess); |
2955 | 0 | pasSharedFileListExtra[nSharedFileCount - 1].nPID = nPID; |
2956 | |
|
2957 | 0 | return fp; |
2958 | 0 | } |
2959 | | |
2960 | | /************************************************************************/ |
2961 | | /* CPLCloseShared() */ |
2962 | | /************************************************************************/ |
2963 | | |
2964 | | /** |
2965 | | * Close shared file. |
2966 | | * |
2967 | | * Dereferences the indicated file handle, and closes it if the reference |
2968 | | * count has dropped to zero. A CPLError() is issued if the file is not |
2969 | | * in the shared file list. |
2970 | | * |
2971 | | * @param fp file handle from CPLOpenShared() to deaccess. |
2972 | | */ |
2973 | | |
2974 | | void CPLCloseShared(FILE *fp) |
2975 | | |
2976 | 0 | { |
2977 | 0 | CPLMutexHolderD(&hSharedFileMutex); |
2978 | | |
2979 | | /* -------------------------------------------------------------------- */ |
2980 | | /* Search for matching information. */ |
2981 | | /* -------------------------------------------------------------------- */ |
2982 | 0 | int i = 0; |
2983 | 0 | for (; i < nSharedFileCount && fp != pasSharedFileList[i].fp; i++) |
2984 | 0 | { |
2985 | 0 | } |
2986 | |
|
2987 | 0 | if (i == nSharedFileCount) |
2988 | 0 | { |
2989 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
2990 | 0 | "Unable to find file handle %p in CPLCloseShared().", fp); |
2991 | 0 | return; |
2992 | 0 | } |
2993 | | |
2994 | | /* -------------------------------------------------------------------- */ |
2995 | | /* Dereference and return if there are still some references. */ |
2996 | | /* -------------------------------------------------------------------- */ |
2997 | 0 | if (--pasSharedFileList[i].nRefCount > 0) |
2998 | 0 | return; |
2999 | | |
3000 | | /* -------------------------------------------------------------------- */ |
3001 | | /* Close the file, and remove the information. */ |
3002 | | /* -------------------------------------------------------------------- */ |
3003 | 0 | if (pasSharedFileList[i].bLarge) |
3004 | 0 | { |
3005 | 0 | if (VSIFCloseL(reinterpret_cast<VSILFILE *>(pasSharedFileList[i].fp)) != |
3006 | 0 | 0) |
3007 | 0 | { |
3008 | 0 | CPLError(CE_Failure, CPLE_FileIO, "Error while closing %s", |
3009 | 0 | pasSharedFileList[i].pszFilename); |
3010 | 0 | } |
3011 | 0 | } |
3012 | 0 | else |
3013 | 0 | { |
3014 | 0 | VSIFClose(pasSharedFileList[i].fp); |
3015 | 0 | } |
3016 | |
|
3017 | 0 | CPLFree(pasSharedFileList[i].pszFilename); |
3018 | 0 | CPLFree(pasSharedFileList[i].pszAccess); |
3019 | |
|
3020 | 0 | nSharedFileCount--; |
3021 | 0 | memmove( |
3022 | 0 | const_cast<CPLSharedFileInfo *>(pasSharedFileList + i), |
3023 | 0 | const_cast<CPLSharedFileInfo *>(pasSharedFileList + nSharedFileCount), |
3024 | 0 | sizeof(CPLSharedFileInfo)); |
3025 | 0 | memmove(const_cast<CPLSharedFileInfoExtra *>(pasSharedFileListExtra + i), |
3026 | 0 | const_cast<CPLSharedFileInfoExtra *>(pasSharedFileListExtra + |
3027 | 0 | nSharedFileCount), |
3028 | 0 | sizeof(CPLSharedFileInfoExtra)); |
3029 | |
|
3030 | 0 | if (nSharedFileCount == 0) |
3031 | 0 | { |
3032 | 0 | CPLFree(const_cast<CPLSharedFileInfo *>(pasSharedFileList)); |
3033 | 0 | pasSharedFileList = nullptr; |
3034 | 0 | CPLFree(const_cast<CPLSharedFileInfoExtra *>(pasSharedFileListExtra)); |
3035 | 0 | pasSharedFileListExtra = nullptr; |
3036 | 0 | } |
3037 | 0 | } |
3038 | | |
3039 | | /************************************************************************/ |
3040 | | /* CPLCleanupSharedFileMutex() */ |
3041 | | /************************************************************************/ |
3042 | | |
3043 | | void CPLCleanupSharedFileMutex() |
3044 | 0 | { |
3045 | 0 | if (hSharedFileMutex != nullptr) |
3046 | 0 | { |
3047 | 0 | CPLDestroyMutex(hSharedFileMutex); |
3048 | 0 | hSharedFileMutex = nullptr; |
3049 | 0 | } |
3050 | 0 | } |
3051 | | |
3052 | | /************************************************************************/ |
3053 | | /* CPLGetSharedList() */ |
3054 | | /************************************************************************/ |
3055 | | |
3056 | | /** |
3057 | | * Fetch list of open shared files. |
3058 | | * |
3059 | | * @param pnCount place to put the count of entries. |
3060 | | * |
3061 | | * @return the pointer to the first in the array of shared file info |
3062 | | * structures. |
3063 | | */ |
3064 | | |
3065 | | CPLSharedFileInfo *CPLGetSharedList(int *pnCount) |
3066 | | |
3067 | 0 | { |
3068 | 0 | if (pnCount != nullptr) |
3069 | 0 | *pnCount = nSharedFileCount; |
3070 | |
|
3071 | 0 | return const_cast<CPLSharedFileInfo *>(pasSharedFileList); |
3072 | 0 | } |
3073 | | |
3074 | | /************************************************************************/ |
3075 | | /* CPLDumpSharedList() */ |
3076 | | /************************************************************************/ |
3077 | | |
3078 | | /** |
3079 | | * Report open shared files. |
3080 | | * |
3081 | | * Dumps all open shared files to the indicated file handle. If the |
3082 | | * file handle is NULL information is sent via the CPLDebug() call. |
3083 | | * |
3084 | | * @param fp File handle to write to. |
3085 | | */ |
3086 | | |
3087 | | void CPLDumpSharedList(FILE *fp) |
3088 | | |
3089 | 0 | { |
3090 | 0 | if (nSharedFileCount > 0) |
3091 | 0 | { |
3092 | 0 | if (fp == nullptr) |
3093 | 0 | CPLDebug("CPL", "%d Shared files open.", nSharedFileCount); |
3094 | 0 | else |
3095 | 0 | fprintf(fp, "%d Shared files open.", nSharedFileCount); |
3096 | 0 | } |
3097 | |
|
3098 | 0 | for (int i = 0; i < nSharedFileCount; i++) |
3099 | 0 | { |
3100 | 0 | if (fp == nullptr) |
3101 | 0 | CPLDebug("CPL", "%2d %d %4s %s", pasSharedFileList[i].nRefCount, |
3102 | 0 | pasSharedFileList[i].bLarge, |
3103 | 0 | pasSharedFileList[i].pszAccess, |
3104 | 0 | pasSharedFileList[i].pszFilename); |
3105 | 0 | else |
3106 | 0 | fprintf(fp, "%2d %d %4s %s", pasSharedFileList[i].nRefCount, |
3107 | 0 | pasSharedFileList[i].bLarge, pasSharedFileList[i].pszAccess, |
3108 | 0 | pasSharedFileList[i].pszFilename); |
3109 | 0 | } |
3110 | 0 | } |
3111 | | |
3112 | | /************************************************************************/ |
3113 | | /* CPLUnlinkTree() */ |
3114 | | /************************************************************************/ |
3115 | | |
3116 | | /** Recursively unlink a directory. |
3117 | | * |
3118 | | * @return 0 on successful completion, -1 if function fails. |
3119 | | */ |
3120 | | |
3121 | | int CPLUnlinkTree(const char *pszPath) |
3122 | | |
3123 | 0 | { |
3124 | | /* -------------------------------------------------------------------- */ |
3125 | | /* First, ensure there is such a file. */ |
3126 | | /* -------------------------------------------------------------------- */ |
3127 | 0 | VSIStatBufL sStatBuf; |
3128 | |
|
3129 | 0 | if (VSIStatL(pszPath, &sStatBuf) != 0) |
3130 | 0 | { |
3131 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
3132 | 0 | "It seems no file system object called '%s' exists.", pszPath); |
3133 | |
|
3134 | 0 | return -1; |
3135 | 0 | } |
3136 | | |
3137 | | /* -------------------------------------------------------------------- */ |
3138 | | /* If it is a simple file, just delete it. */ |
3139 | | /* -------------------------------------------------------------------- */ |
3140 | 0 | if (VSI_ISREG(sStatBuf.st_mode)) |
3141 | 0 | { |
3142 | 0 | if (VSIUnlink(pszPath) != 0) |
3143 | 0 | { |
3144 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Failed to unlink %s.", |
3145 | 0 | pszPath); |
3146 | |
|
3147 | 0 | return -1; |
3148 | 0 | } |
3149 | | |
3150 | 0 | return 0; |
3151 | 0 | } |
3152 | | |
3153 | | /* -------------------------------------------------------------------- */ |
3154 | | /* If it is a directory recurse then unlink the directory. */ |
3155 | | /* -------------------------------------------------------------------- */ |
3156 | 0 | else if (VSI_ISDIR(sStatBuf.st_mode)) |
3157 | 0 | { |
3158 | 0 | char **papszItems = VSIReadDir(pszPath); |
3159 | |
|
3160 | 0 | for (int i = 0; papszItems != nullptr && papszItems[i] != nullptr; i++) |
3161 | 0 | { |
3162 | 0 | if (papszItems[i][0] == '\0' || EQUAL(papszItems[i], ".") || |
3163 | 0 | EQUAL(papszItems[i], "..")) |
3164 | 0 | continue; |
3165 | | |
3166 | 0 | const std::string osSubPath = |
3167 | 0 | CPLFormFilenameSafe(pszPath, papszItems[i], nullptr); |
3168 | |
|
3169 | 0 | const int nErr = CPLUnlinkTree(osSubPath.c_str()); |
3170 | |
|
3171 | 0 | if (nErr != 0) |
3172 | 0 | { |
3173 | 0 | CSLDestroy(papszItems); |
3174 | 0 | return nErr; |
3175 | 0 | } |
3176 | 0 | } |
3177 | | |
3178 | 0 | CSLDestroy(papszItems); |
3179 | |
|
3180 | 0 | if (VSIRmdir(pszPath) != 0) |
3181 | 0 | { |
3182 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Failed to unlink %s.", |
3183 | 0 | pszPath); |
3184 | |
|
3185 | 0 | return -1; |
3186 | 0 | } |
3187 | | |
3188 | 0 | return 0; |
3189 | 0 | } |
3190 | | |
3191 | | /* -------------------------------------------------------------------- */ |
3192 | | /* otherwise report an error. */ |
3193 | | /* -------------------------------------------------------------------- */ |
3194 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
3195 | 0 | "Failed to unlink %s.\nUnrecognised filesystem object.", pszPath); |
3196 | 0 | return 1000; |
3197 | 0 | } |
3198 | | |
3199 | | /************************************************************************/ |
3200 | | /* CPLCopyFile() */ |
3201 | | /************************************************************************/ |
3202 | | |
3203 | | /** Copy a file */ |
3204 | | int CPLCopyFile(const char *pszNewPath, const char *pszOldPath) |
3205 | | |
3206 | 0 | { |
3207 | 0 | return VSICopyFile(pszOldPath, pszNewPath, nullptr, |
3208 | 0 | static_cast<vsi_l_offset>(-1), nullptr, nullptr, |
3209 | 0 | nullptr); |
3210 | 0 | } |
3211 | | |
3212 | | /************************************************************************/ |
3213 | | /* CPLCopyTree() */ |
3214 | | /************************************************************************/ |
3215 | | |
3216 | | /** Recursively copy a tree */ |
3217 | | int CPLCopyTree(const char *pszNewPath, const char *pszOldPath) |
3218 | | |
3219 | 0 | { |
3220 | 0 | VSIStatBufL sStatBuf; |
3221 | 0 | if (VSIStatL(pszNewPath, &sStatBuf) == 0) |
3222 | 0 | { |
3223 | 0 | CPLError( |
3224 | 0 | CE_Failure, CPLE_AppDefined, |
3225 | 0 | "It seems that a file system object called '%s' already exists.", |
3226 | 0 | pszNewPath); |
3227 | |
|
3228 | 0 | return -1; |
3229 | 0 | } |
3230 | | |
3231 | 0 | if (VSIStatL(pszOldPath, &sStatBuf) != 0) |
3232 | 0 | { |
3233 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
3234 | 0 | "It seems no file system object called '%s' exists.", |
3235 | 0 | pszOldPath); |
3236 | |
|
3237 | 0 | return -1; |
3238 | 0 | } |
3239 | | |
3240 | 0 | if (VSI_ISDIR(sStatBuf.st_mode)) |
3241 | 0 | { |
3242 | 0 | if (VSIMkdir(pszNewPath, 0755) != 0) |
3243 | 0 | { |
3244 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
3245 | 0 | "Cannot create directory '%s'.", pszNewPath); |
3246 | |
|
3247 | 0 | return -1; |
3248 | 0 | } |
3249 | | |
3250 | 0 | char **papszItems = VSIReadDir(pszOldPath); |
3251 | |
|
3252 | 0 | for (int i = 0; papszItems != nullptr && papszItems[i] != nullptr; i++) |
3253 | 0 | { |
3254 | 0 | if (EQUAL(papszItems[i], ".") || EQUAL(papszItems[i], "..")) |
3255 | 0 | continue; |
3256 | | |
3257 | 0 | const std::string osNewSubPath = |
3258 | 0 | CPLFormFilenameSafe(pszNewPath, papszItems[i], nullptr); |
3259 | 0 | const std::string osOldSubPath = |
3260 | 0 | CPLFormFilenameSafe(pszOldPath, papszItems[i], nullptr); |
3261 | |
|
3262 | 0 | const int nErr = |
3263 | 0 | CPLCopyTree(osNewSubPath.c_str(), osOldSubPath.c_str()); |
3264 | |
|
3265 | 0 | if (nErr != 0) |
3266 | 0 | { |
3267 | 0 | CSLDestroy(papszItems); |
3268 | 0 | return nErr; |
3269 | 0 | } |
3270 | 0 | } |
3271 | 0 | CSLDestroy(papszItems); |
3272 | |
|
3273 | 0 | return 0; |
3274 | 0 | } |
3275 | 0 | else if (VSI_ISREG(sStatBuf.st_mode)) |
3276 | 0 | { |
3277 | 0 | return CPLCopyFile(pszNewPath, pszOldPath); |
3278 | 0 | } |
3279 | 0 | else |
3280 | 0 | { |
3281 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
3282 | 0 | "Unrecognized filesystem object : '%s'.", pszOldPath); |
3283 | 0 | return -1; |
3284 | 0 | } |
3285 | 0 | } |
3286 | | |
3287 | | /************************************************************************/ |
3288 | | /* CPLMoveFile() */ |
3289 | | /************************************************************************/ |
3290 | | |
3291 | | /** Move a file */ |
3292 | | int CPLMoveFile(const char *pszNewPath, const char *pszOldPath) |
3293 | | |
3294 | 0 | { |
3295 | 0 | if (VSIRename(pszOldPath, pszNewPath) == 0) |
3296 | 0 | return 0; |
3297 | | |
3298 | 0 | const int nRet = CPLCopyFile(pszNewPath, pszOldPath); |
3299 | |
|
3300 | 0 | if (nRet == 0) |
3301 | 0 | VSIUnlink(pszOldPath); |
3302 | 0 | return nRet; |
3303 | 0 | } |
3304 | | |
3305 | | /************************************************************************/ |
3306 | | /* CPLSymlink() */ |
3307 | | /************************************************************************/ |
3308 | | |
3309 | | /** Create a symbolic link */ |
3310 | | #ifdef _WIN32 |
3311 | | int CPLSymlink(const char *, const char *, CSLConstList) |
3312 | | { |
3313 | | return -1; |
3314 | | } |
3315 | | #else |
3316 | | int CPLSymlink(const char *pszOldPath, const char *pszNewPath, |
3317 | | CSLConstList /* papszOptions */) |
3318 | 0 | { |
3319 | 0 | return symlink(pszOldPath, pszNewPath); |
3320 | 0 | } |
3321 | | #endif |
3322 | | |
3323 | | /************************************************************************/ |
3324 | | /* ==================================================================== */ |
3325 | | /* CPLLocaleC */ |
3326 | | /* ==================================================================== */ |
3327 | | /************************************************************************/ |
3328 | | |
3329 | | //! @cond Doxygen_Suppress |
3330 | | /************************************************************************/ |
3331 | | /* CPLLocaleC() */ |
3332 | | /************************************************************************/ |
3333 | | |
3334 | 0 | CPLLocaleC::CPLLocaleC() : pszOldLocale(nullptr) |
3335 | 0 | { |
3336 | 0 | if (CPLTestBool(CPLGetConfigOption("GDAL_DISABLE_CPLLOCALEC", "NO"))) |
3337 | 0 | return; |
3338 | | |
3339 | 0 | pszOldLocale = CPLStrdup(CPLsetlocale(LC_NUMERIC, nullptr)); |
3340 | 0 | if (EQUAL(pszOldLocale, "C") || EQUAL(pszOldLocale, "POSIX") || |
3341 | 0 | CPLsetlocale(LC_NUMERIC, "C") == nullptr) |
3342 | 0 | { |
3343 | 0 | CPLFree(pszOldLocale); |
3344 | 0 | pszOldLocale = nullptr; |
3345 | 0 | } |
3346 | 0 | } |
3347 | | |
3348 | | /************************************************************************/ |
3349 | | /* ~CPLLocaleC() */ |
3350 | | /************************************************************************/ |
3351 | | |
3352 | | CPLLocaleC::~CPLLocaleC() |
3353 | | |
3354 | 0 | { |
3355 | 0 | if (pszOldLocale == nullptr) |
3356 | 0 | return; |
3357 | | |
3358 | 0 | CPLsetlocale(LC_NUMERIC, pszOldLocale); |
3359 | 0 | CPLFree(pszOldLocale); |
3360 | 0 | } |
3361 | | |
3362 | | /************************************************************************/ |
3363 | | /* CPLThreadLocaleCPrivate */ |
3364 | | /************************************************************************/ |
3365 | | |
3366 | | #ifdef HAVE_USELOCALE |
3367 | | |
3368 | | class CPLThreadLocaleCPrivate |
3369 | | { |
3370 | | locale_t nNewLocale; |
3371 | | locale_t nOldLocale; |
3372 | | |
3373 | | CPL_DISALLOW_COPY_ASSIGN(CPLThreadLocaleCPrivate) |
3374 | | |
3375 | | public: |
3376 | | CPLThreadLocaleCPrivate(); |
3377 | | ~CPLThreadLocaleCPrivate(); |
3378 | | }; |
3379 | | |
3380 | | CPLThreadLocaleCPrivate::CPLThreadLocaleCPrivate() |
3381 | 0 | : nNewLocale(newlocale(LC_NUMERIC_MASK, "C", nullptr)), |
3382 | 0 | nOldLocale(uselocale(nNewLocale)) |
3383 | 0 | { |
3384 | 0 | } |
3385 | | |
3386 | | CPLThreadLocaleCPrivate::~CPLThreadLocaleCPrivate() |
3387 | 0 | { |
3388 | 0 | uselocale(nOldLocale); |
3389 | 0 | freelocale(nNewLocale); |
3390 | 0 | } |
3391 | | |
3392 | | #elif defined(_MSC_VER) |
3393 | | |
3394 | | class CPLThreadLocaleCPrivate |
3395 | | { |
3396 | | int nOldValConfigThreadLocale; |
3397 | | char *pszOldLocale; |
3398 | | |
3399 | | CPL_DISALLOW_COPY_ASSIGN(CPLThreadLocaleCPrivate) |
3400 | | |
3401 | | public: |
3402 | | CPLThreadLocaleCPrivate(); |
3403 | | ~CPLThreadLocaleCPrivate(); |
3404 | | }; |
3405 | | |
3406 | | CPLThreadLocaleCPrivate::CPLThreadLocaleCPrivate() |
3407 | | { |
3408 | | nOldValConfigThreadLocale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); |
3409 | | pszOldLocale = setlocale(LC_NUMERIC, "C"); |
3410 | | if (pszOldLocale) |
3411 | | pszOldLocale = CPLStrdup(pszOldLocale); |
3412 | | } |
3413 | | |
3414 | | CPLThreadLocaleCPrivate::~CPLThreadLocaleCPrivate() |
3415 | | { |
3416 | | if (pszOldLocale != nullptr) |
3417 | | { |
3418 | | setlocale(LC_NUMERIC, pszOldLocale); |
3419 | | CPLFree(pszOldLocale); |
3420 | | } |
3421 | | _configthreadlocale(nOldValConfigThreadLocale); |
3422 | | } |
3423 | | |
3424 | | #else |
3425 | | |
3426 | | class CPLThreadLocaleCPrivate |
3427 | | { |
3428 | | char *pszOldLocale; |
3429 | | |
3430 | | CPL_DISALLOW_COPY_ASSIGN(CPLThreadLocaleCPrivate) |
3431 | | |
3432 | | public: |
3433 | | CPLThreadLocaleCPrivate(); |
3434 | | ~CPLThreadLocaleCPrivate(); |
3435 | | }; |
3436 | | |
3437 | | CPLThreadLocaleCPrivate::CPLThreadLocaleCPrivate() |
3438 | | : pszOldLocale(CPLStrdup(CPLsetlocale(LC_NUMERIC, nullptr))) |
3439 | | { |
3440 | | if (EQUAL(pszOldLocale, "C") || EQUAL(pszOldLocale, "POSIX") || |
3441 | | CPLsetlocale(LC_NUMERIC, "C") == nullptr) |
3442 | | { |
3443 | | CPLFree(pszOldLocale); |
3444 | | pszOldLocale = nullptr; |
3445 | | } |
3446 | | } |
3447 | | |
3448 | | CPLThreadLocaleCPrivate::~CPLThreadLocaleCPrivate() |
3449 | | { |
3450 | | if (pszOldLocale != nullptr) |
3451 | | { |
3452 | | CPLsetlocale(LC_NUMERIC, pszOldLocale); |
3453 | | CPLFree(pszOldLocale); |
3454 | | } |
3455 | | } |
3456 | | |
3457 | | #endif |
3458 | | |
3459 | | /************************************************************************/ |
3460 | | /* CPLThreadLocaleC() */ |
3461 | | /************************************************************************/ |
3462 | | |
3463 | 0 | CPLThreadLocaleC::CPLThreadLocaleC() : m_private(new CPLThreadLocaleCPrivate) |
3464 | 0 | { |
3465 | 0 | } |
3466 | | |
3467 | | /************************************************************************/ |
3468 | | /* ~CPLThreadLocaleC() */ |
3469 | | /************************************************************************/ |
3470 | | |
3471 | | CPLThreadLocaleC::~CPLThreadLocaleC() |
3472 | | |
3473 | 0 | { |
3474 | 0 | delete m_private; |
3475 | 0 | } |
3476 | | |
3477 | | //! @endcond |
3478 | | |
3479 | | /************************************************************************/ |
3480 | | /* CPLsetlocale() */ |
3481 | | /************************************************************************/ |
3482 | | |
3483 | | /** |
3484 | | * Prevents parallel executions of setlocale(). |
3485 | | * |
3486 | | * Calling setlocale() concurrently from two or more threads is a |
3487 | | * potential data race. A mutex is used to provide a critical region so |
3488 | | * that only one thread at a time can be executing setlocale(). |
3489 | | * |
3490 | | * The return should not be freed, and copied quickly as it may be invalidated |
3491 | | * by a following next call to CPLsetlocale(). |
3492 | | * |
3493 | | * @param category See your compiler's documentation on setlocale. |
3494 | | * @param locale See your compiler's documentation on setlocale. |
3495 | | * |
3496 | | * @return See your compiler's documentation on setlocale. |
3497 | | */ |
3498 | | char *CPLsetlocale(int category, const char *locale) |
3499 | 0 | { |
3500 | 0 | CPLMutexHolder oHolder(&hSetLocaleMutex); |
3501 | 0 | char *pszRet = setlocale(category, locale); |
3502 | 0 | if (pszRet == nullptr) |
3503 | 0 | return pszRet; |
3504 | | |
3505 | | // Make it thread-locale storage. |
3506 | 0 | return const_cast<char *>(CPLSPrintf("%s", pszRet)); |
3507 | 0 | } |
3508 | | |
3509 | | /************************************************************************/ |
3510 | | /* CPLCleanupSetlocaleMutex() */ |
3511 | | /************************************************************************/ |
3512 | | |
3513 | | void CPLCleanupSetlocaleMutex(void) |
3514 | 0 | { |
3515 | 0 | if (hSetLocaleMutex != nullptr) |
3516 | 0 | CPLDestroyMutex(hSetLocaleMutex); |
3517 | 0 | hSetLocaleMutex = nullptr; |
3518 | 0 | } |
3519 | | |
3520 | | /************************************************************************/ |
3521 | | /* IsPowerOfTwo() */ |
3522 | | /************************************************************************/ |
3523 | | |
3524 | | int CPLIsPowerOfTwo(unsigned int i) |
3525 | 0 | { |
3526 | 0 | if (i == 0) |
3527 | 0 | return FALSE; |
3528 | 0 | return (i & (i - 1)) == 0 ? TRUE : FALSE; |
3529 | 0 | } |
3530 | | |
3531 | | /************************************************************************/ |
3532 | | /* CPLCheckForFile() */ |
3533 | | /************************************************************************/ |
3534 | | |
3535 | | /** |
3536 | | * Check for file existence. |
3537 | | * |
3538 | | * The function checks if a named file exists in the filesystem, hopefully |
3539 | | * in an efficient fashion if a sibling file list is available. It exists |
3540 | | * primarily to do faster file checking for functions like GDAL open methods |
3541 | | * that get a list of files from the target directory. |
3542 | | * |
3543 | | * If the sibling file list exists (is not NULL) it is assumed to be a list |
3544 | | * of files in the same directory as the target file, and it will be checked |
3545 | | * (case insensitively) for a match. If a match is found, pszFilename is |
3546 | | * updated with the correct case and TRUE is returned. |
3547 | | * |
3548 | | * If papszSiblingFiles is NULL, a VSIStatL() is used to test for the files |
3549 | | * existence, and no case insensitive testing is done. |
3550 | | * |
3551 | | * @param pszFilename name of file to check for - filename case updated in |
3552 | | * some cases. |
3553 | | * @param papszSiblingFiles a list of files in the same directory as |
3554 | | * pszFilename if available, or NULL. This list should have no path components. |
3555 | | * |
3556 | | * @return TRUE if a match is found, or FALSE if not. |
3557 | | */ |
3558 | | |
3559 | | int CPLCheckForFile(char *pszFilename, char **papszSiblingFiles) |
3560 | | |
3561 | 0 | { |
3562 | | /* -------------------------------------------------------------------- */ |
3563 | | /* Fallback case if we don't have a sibling file list. */ |
3564 | | /* -------------------------------------------------------------------- */ |
3565 | 0 | if (papszSiblingFiles == nullptr) |
3566 | 0 | { |
3567 | 0 | VSIStatBufL sStatBuf; |
3568 | |
|
3569 | 0 | return VSIStatExL(pszFilename, &sStatBuf, VSI_STAT_EXISTS_FLAG) == 0; |
3570 | 0 | } |
3571 | | |
3572 | | /* -------------------------------------------------------------------- */ |
3573 | | /* We have sibling files, compare the non-path filename portion */ |
3574 | | /* of pszFilename too all entries. */ |
3575 | | /* -------------------------------------------------------------------- */ |
3576 | 0 | const CPLString osFileOnly = CPLGetFilename(pszFilename); |
3577 | |
|
3578 | 0 | for (int i = 0; papszSiblingFiles[i] != nullptr; i++) |
3579 | 0 | { |
3580 | 0 | if (EQUAL(papszSiblingFiles[i], osFileOnly)) |
3581 | 0 | { |
3582 | 0 | strcpy(pszFilename + strlen(pszFilename) - osFileOnly.size(), |
3583 | 0 | papszSiblingFiles[i]); |
3584 | 0 | return TRUE; |
3585 | 0 | } |
3586 | 0 | } |
3587 | | |
3588 | 0 | return FALSE; |
3589 | 0 | } |
3590 | | |
3591 | | /************************************************************************/ |
3592 | | /* Stub implementation of zip services if we don't have libz. */ |
3593 | | /************************************************************************/ |
3594 | | |
3595 | | #if !defined(HAVE_LIBZ) |
3596 | | |
3597 | | void *CPLCreateZip(const char *, char **) |
3598 | | |
3599 | | { |
3600 | | CPLError(CE_Failure, CPLE_NotSupported, |
3601 | | "This GDAL/OGR build does not include zlib and zip services."); |
3602 | | return nullptr; |
3603 | | } |
3604 | | |
3605 | | CPLErr CPLCreateFileInZip(void *, const char *, char **) |
3606 | | { |
3607 | | return CE_Failure; |
3608 | | } |
3609 | | |
3610 | | CPLErr CPLWriteFileInZip(void *, const void *, int) |
3611 | | { |
3612 | | return CE_Failure; |
3613 | | } |
3614 | | |
3615 | | CPLErr CPLCloseFileInZip(void *) |
3616 | | { |
3617 | | return CE_Failure; |
3618 | | } |
3619 | | |
3620 | | CPLErr CPLCloseZip(void *) |
3621 | | { |
3622 | | return CE_Failure; |
3623 | | } |
3624 | | |
3625 | | void *CPLZLibDeflate(const void *, size_t, int, void *, size_t, |
3626 | | size_t *pnOutBytes) |
3627 | | { |
3628 | | if (pnOutBytes != nullptr) |
3629 | | *pnOutBytes = 0; |
3630 | | return nullptr; |
3631 | | } |
3632 | | |
3633 | | void *CPLZLibInflate(const void *, size_t, void *, size_t, size_t *pnOutBytes) |
3634 | | { |
3635 | | if (pnOutBytes != nullptr) |
3636 | | *pnOutBytes = 0; |
3637 | | return nullptr; |
3638 | | } |
3639 | | |
3640 | | #endif /* !defined(HAVE_LIBZ) */ |
3641 | | |
3642 | | /************************************************************************/ |
3643 | | /* ==================================================================== */ |
3644 | | /* CPLConfigOptionSetter */ |
3645 | | /* ==================================================================== */ |
3646 | | /************************************************************************/ |
3647 | | |
3648 | | //! @cond Doxygen_Suppress |
3649 | | /************************************************************************/ |
3650 | | /* CPLConfigOptionSetter() */ |
3651 | | /************************************************************************/ |
3652 | | |
3653 | | CPLConfigOptionSetter::CPLConfigOptionSetter(const char *pszKey, |
3654 | | const char *pszValue, |
3655 | | bool bSetOnlyIfUndefined) |
3656 | 0 | : m_pszKey(CPLStrdup(pszKey)), m_pszOldValue(nullptr), |
3657 | 0 | m_bRestoreOldValue(false) |
3658 | 0 | { |
3659 | 0 | const char *pszOldValue = CPLGetThreadLocalConfigOption(pszKey, nullptr); |
3660 | 0 | if ((bSetOnlyIfUndefined && |
3661 | 0 | CPLGetConfigOption(pszKey, nullptr) == nullptr) || |
3662 | 0 | !bSetOnlyIfUndefined) |
3663 | 0 | { |
3664 | 0 | m_bRestoreOldValue = true; |
3665 | 0 | if (pszOldValue) |
3666 | 0 | m_pszOldValue = CPLStrdup(pszOldValue); |
3667 | 0 | CPLSetThreadLocalConfigOption(pszKey, pszValue); |
3668 | 0 | } |
3669 | 0 | } |
3670 | | |
3671 | | /************************************************************************/ |
3672 | | /* ~CPLConfigOptionSetter() */ |
3673 | | /************************************************************************/ |
3674 | | |
3675 | | CPLConfigOptionSetter::~CPLConfigOptionSetter() |
3676 | 0 | { |
3677 | 0 | if (m_bRestoreOldValue) |
3678 | 0 | { |
3679 | 0 | CPLSetThreadLocalConfigOption(m_pszKey, m_pszOldValue); |
3680 | 0 | CPLFree(m_pszOldValue); |
3681 | 0 | } |
3682 | 0 | CPLFree(m_pszKey); |
3683 | 0 | } |
3684 | | |
3685 | | //! @endcond |
3686 | | |
3687 | | /************************************************************************/ |
3688 | | /* CPLIsInteractive() */ |
3689 | | /************************************************************************/ |
3690 | | |
3691 | | /** Returns whether the provided file refers to a terminal. |
3692 | | * |
3693 | | * This function is a wrapper of the ``isatty()`` POSIX function. |
3694 | | * |
3695 | | * @param f File to test. Typically stdin, stdout or stderr |
3696 | | * @return true if it is an open file referring to a terminal. |
3697 | | * @since GDAL 3.11 |
3698 | | */ |
3699 | | bool CPLIsInteractive(FILE *f) |
3700 | 1.95k | { |
3701 | 1.95k | #ifndef _WIN32 |
3702 | 1.95k | return isatty(static_cast<int>(fileno(f))); |
3703 | | #else |
3704 | | return _isatty(_fileno(f)); |
3705 | | #endif |
3706 | 1.95k | } |
3707 | | |
3708 | | /************************************************************************/ |
3709 | | /* CPLLockFileStruct */ |
3710 | | /************************************************************************/ |
3711 | | |
3712 | | //! @cond Doxygen_Suppress |
3713 | | struct CPLLockFileStruct |
3714 | | { |
3715 | | std::string osLockFilename{}; |
3716 | | std::atomic<bool> bStop = false; |
3717 | | CPLJoinableThread *hThread = nullptr; |
3718 | | }; |
3719 | | |
3720 | | //! @endcond |
3721 | | |
3722 | | /************************************************************************/ |
3723 | | /* CPLLockFileEx() */ |
3724 | | /************************************************************************/ |
3725 | | |
3726 | | /** Create and acquire a lock file. |
3727 | | * |
3728 | | * Only one caller can acquire the lock file at a time. The O_CREAT|O_EXCL |
3729 | | * flags of open() are used for that purpose (there might be limitations for |
3730 | | * network file systems). |
3731 | | * |
3732 | | * The lock file is continuously touched by a thread started by this function, |
3733 | | * to indicate it is still alive. If an existing lock file is found that has |
3734 | | * not been recently refreshed it will be considered stalled, and will be |
3735 | | * deleted before attempting to recreate it. |
3736 | | * |
3737 | | * This function must be paired with CPLUnlockFileEx(). |
3738 | | * |
3739 | | * Available options are: |
3740 | | * <ul> |
3741 | | * <li>WAIT_TIME=value_in_sec/inf: Maximum amount of time in second that this |
3742 | | * function can spend waiting for the lock. If not set, default to infinity. |
3743 | | * </li> |
3744 | | * <li>STALLED_DELAY=value_in_sec: Delay in second to consider that an existing |
3745 | | * lock file that has not been touched since STALLED_DELAY is stalled, and can |
3746 | | * be re-acquired. Defaults to 10 seconds. |
3747 | | * </li> |
3748 | | * <li>VERBOSE_WAIT_MESSAGE=YES/NO: Whether to emit a CE_Warning message while |
3749 | | * waiting for a busy lock. Default to NO. |
3750 | | * </li> |
3751 | | * </ul> |
3752 | | |
3753 | | * @param pszLockFileName Lock file name. The directory must already exist. |
3754 | | * Must not be NULL. |
3755 | | * @param[out] phLockFileHandle Pointer to at location where to store the lock |
3756 | | * handle that must be passed to CPLUnlockFileEx(). |
3757 | | * *phLockFileHandle will be null if the return |
3758 | | * code of that function is not CLFS_OK. |
3759 | | * @param papszOptions NULL terminated list of strings, or NULL. |
3760 | | * |
3761 | | * @return lock file status. |
3762 | | * |
3763 | | * @since 3.11 |
3764 | | */ |
3765 | | CPLLockFileStatus CPLLockFileEx(const char *pszLockFileName, |
3766 | | CPLLockFileHandle *phLockFileHandle, |
3767 | | CSLConstList papszOptions) |
3768 | 0 | { |
3769 | 0 | if (!pszLockFileName || !phLockFileHandle) |
3770 | 0 | return CLFS_API_MISUSE; |
3771 | | |
3772 | 0 | *phLockFileHandle = nullptr; |
3773 | |
|
3774 | 0 | const double dfWaitTime = |
3775 | 0 | CPLAtof(CSLFetchNameValueDef(papszOptions, "WAIT_TIME", "inf")); |
3776 | 0 | const double dfStalledDelay = |
3777 | 0 | CPLAtof(CSLFetchNameValueDef(papszOptions, "STALLED_DELAY", "10")); |
3778 | 0 | const bool bVerboseWait = |
3779 | 0 | CPLFetchBool(papszOptions, "VERBOSE_WAIT_MESSAGE", false); |
3780 | |
|
3781 | 0 | for (int i = 0; i < 2; ++i) |
3782 | 0 | { |
3783 | | #ifdef _WIN32 |
3784 | | wchar_t *pwszFilename = |
3785 | | CPLRecodeToWChar(pszLockFileName, CPL_ENC_UTF8, CPL_ENC_UCS2); |
3786 | | int fd = _wopen(pwszFilename, _O_CREAT | _O_EXCL, _S_IREAD | _S_IWRITE); |
3787 | | CPLFree(pwszFilename); |
3788 | | #else |
3789 | 0 | int fd = open(pszLockFileName, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); |
3790 | 0 | #endif |
3791 | 0 | if (fd == -1) |
3792 | 0 | { |
3793 | 0 | if (errno != EEXIST || i == 1) |
3794 | 0 | { |
3795 | 0 | return CLFS_CANNOT_CREATE_LOCK; |
3796 | 0 | } |
3797 | 0 | else |
3798 | 0 | { |
3799 | | // Wait for the .lock file to have been removed or |
3800 | | // not refreshed since dfStalledDelay seconds. |
3801 | 0 | double dfCurWaitTime = dfWaitTime; |
3802 | 0 | VSIStatBufL sStat; |
3803 | 0 | while (VSIStatL(pszLockFileName, &sStat) == 0 && |
3804 | 0 | static_cast<double>(sStat.st_mtime) + dfStalledDelay > |
3805 | 0 | static_cast<double>(time(nullptr))) |
3806 | 0 | { |
3807 | 0 | if (dfCurWaitTime <= 1e-5) |
3808 | 0 | return CLFS_LOCK_BUSY; |
3809 | | |
3810 | 0 | if (bVerboseWait) |
3811 | 0 | { |
3812 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
3813 | 0 | "Waiting for %s to be freed...", |
3814 | 0 | pszLockFileName); |
3815 | 0 | } |
3816 | 0 | else |
3817 | 0 | { |
3818 | 0 | CPLDebug("CPL", "Waiting for %s to be freed...", |
3819 | 0 | pszLockFileName); |
3820 | 0 | } |
3821 | |
|
3822 | 0 | const double dfPauseDelay = std::min(0.5, dfWaitTime); |
3823 | 0 | CPLSleep(dfPauseDelay); |
3824 | 0 | dfCurWaitTime -= dfPauseDelay; |
3825 | 0 | } |
3826 | | |
3827 | 0 | if (VSIUnlink(pszLockFileName) != 0) |
3828 | 0 | { |
3829 | 0 | return CLFS_CANNOT_CREATE_LOCK; |
3830 | 0 | } |
3831 | 0 | } |
3832 | 0 | } |
3833 | 0 | else |
3834 | 0 | { |
3835 | 0 | close(fd); |
3836 | 0 | break; |
3837 | 0 | } |
3838 | 0 | } |
3839 | | |
3840 | | // Touch regularly the lock file to show it is still alive |
3841 | 0 | struct KeepAliveLockFile |
3842 | 0 | { |
3843 | 0 | static void func(void *user_data) |
3844 | 0 | { |
3845 | 0 | CPLLockFileHandle hLockFileHandle = |
3846 | 0 | static_cast<CPLLockFileHandle>(user_data); |
3847 | 0 | while (!hLockFileHandle->bStop) |
3848 | 0 | { |
3849 | 0 | auto f = VSIVirtualHandleUniquePtr( |
3850 | 0 | VSIFOpenL(hLockFileHandle->osLockFilename.c_str(), "wb")); |
3851 | 0 | if (f) |
3852 | 0 | { |
3853 | 0 | f.reset(); |
3854 | 0 | } |
3855 | 0 | constexpr double REFRESH_DELAY = 0.5; |
3856 | 0 | CPLSleep(REFRESH_DELAY); |
3857 | 0 | } |
3858 | 0 | } |
3859 | 0 | }; |
3860 | |
|
3861 | 0 | *phLockFileHandle = new CPLLockFileStruct(); |
3862 | 0 | (*phLockFileHandle)->osLockFilename = pszLockFileName; |
3863 | |
|
3864 | 0 | (*phLockFileHandle)->hThread = |
3865 | 0 | CPLCreateJoinableThread(KeepAliveLockFile::func, *phLockFileHandle); |
3866 | 0 | if ((*phLockFileHandle)->hThread == nullptr) |
3867 | 0 | { |
3868 | 0 | VSIUnlink(pszLockFileName); |
3869 | 0 | delete *phLockFileHandle; |
3870 | 0 | *phLockFileHandle = nullptr; |
3871 | 0 | return CLFS_THREAD_CREATION_FAILED; |
3872 | 0 | } |
3873 | | |
3874 | 0 | return CLFS_OK; |
3875 | 0 | } |
3876 | | |
3877 | | /************************************************************************/ |
3878 | | /* CPLUnlockFileEx() */ |
3879 | | /************************************************************************/ |
3880 | | |
3881 | | /** Release and delete a lock file. |
3882 | | * |
3883 | | * This function must be paired with CPLLockFileEx(). |
3884 | | * |
3885 | | * @param hLockFileHandle Lock handle (value of *phLockFileHandle argument |
3886 | | * set by CPLLockFileEx()), or NULL. |
3887 | | * |
3888 | | * @since 3.11 |
3889 | | */ |
3890 | | void CPLUnlockFileEx(CPLLockFileHandle hLockFileHandle) |
3891 | 0 | { |
3892 | 0 | if (hLockFileHandle) |
3893 | 0 | { |
3894 | | // Remove .lock file |
3895 | 0 | hLockFileHandle->bStop = true; |
3896 | 0 | CPLJoinThread(hLockFileHandle->hThread); |
3897 | 0 | VSIUnlink(hLockFileHandle->osLockFilename.c_str()); |
3898 | |
|
3899 | 0 | delete hLockFileHandle; |
3900 | 0 | } |
3901 | 0 | } |
3902 | | |
3903 | | /************************************************************************/ |
3904 | | /* CPLFormatReadableFileSize() */ |
3905 | | /************************************************************************/ |
3906 | | |
3907 | | template <class T> |
3908 | | static std::string CPLFormatReadableFileSizeInternal(T nSizeInBytes) |
3909 | 0 | { |
3910 | 0 | constexpr T ONE_MEGA_BYTE = 1000 * 1000; |
3911 | 0 | constexpr T ONE_GIGA_BYTE = 1000 * ONE_MEGA_BYTE; |
3912 | 0 | constexpr T ONE_TERA_BYTE = 1000 * ONE_GIGA_BYTE; |
3913 | 0 | constexpr T ONE_PETA_BYTE = 1000 * ONE_TERA_BYTE; |
3914 | 0 | constexpr T ONE_HEXA_BYTE = 1000 * ONE_PETA_BYTE; |
3915 | |
|
3916 | 0 | if (nSizeInBytes > ONE_HEXA_BYTE) |
3917 | 0 | return CPLSPrintf("%.02f HB", static_cast<double>(nSizeInBytes) / |
3918 | 0 | static_cast<double>(ONE_HEXA_BYTE)); |
3919 | | |
3920 | 0 | if (nSizeInBytes > ONE_PETA_BYTE) |
3921 | 0 | return CPLSPrintf("%.02f PB", static_cast<double>(nSizeInBytes) / |
3922 | 0 | static_cast<double>(ONE_PETA_BYTE)); |
3923 | | |
3924 | 0 | if (nSizeInBytes > ONE_TERA_BYTE) |
3925 | 0 | return CPLSPrintf("%.02f TB", static_cast<double>(nSizeInBytes) / |
3926 | 0 | static_cast<double>(ONE_TERA_BYTE)); |
3927 | | |
3928 | 0 | if (nSizeInBytes > ONE_GIGA_BYTE) |
3929 | 0 | return CPLSPrintf("%.02f GB", static_cast<double>(nSizeInBytes) / |
3930 | 0 | static_cast<double>(ONE_GIGA_BYTE)); |
3931 | | |
3932 | 0 | if (nSizeInBytes > ONE_MEGA_BYTE) |
3933 | 0 | return CPLSPrintf("%.02f MB", static_cast<double>(nSizeInBytes) / |
3934 | 0 | static_cast<double>(ONE_MEGA_BYTE)); |
3935 | | |
3936 | 0 | return CPLSPrintf("%03d,%03d bytes", static_cast<int>(nSizeInBytes) / 1000, |
3937 | 0 | static_cast<int>(nSizeInBytes) % 1000); |
3938 | 0 | } Unexecuted instantiation: cpl_conv.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > CPLFormatReadableFileSizeInternal<unsigned long>(unsigned long) Unexecuted instantiation: cpl_conv.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > CPLFormatReadableFileSizeInternal<double>(double) |
3939 | | |
3940 | | /** Return a file size in a human readable way. |
3941 | | * |
3942 | | * e.g 1200000 -> "1.20 MB" |
3943 | | * |
3944 | | * @since 3.12 |
3945 | | */ |
3946 | | std::string CPLFormatReadableFileSize(uint64_t nSizeInBytes) |
3947 | 0 | { |
3948 | 0 | return CPLFormatReadableFileSizeInternal(nSizeInBytes); |
3949 | 0 | } |
3950 | | |
3951 | | /** Return a file size in a human readable way. |
3952 | | * |
3953 | | * e.g 1200000 -> "1.20 MB" |
3954 | | * |
3955 | | * @since 3.12 |
3956 | | */ |
3957 | | std::string CPLFormatReadableFileSize(double dfSizeInBytes) |
3958 | 0 | { |
3959 | 0 | return CPLFormatReadableFileSizeInternal(dfSizeInBytes); |
3960 | 0 | } |