Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: CPL - Common Portability Library
4 : * Purpose: Convenience functions declarations.
5 : * This is intended to remain light weight.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1998, Frank Warmerdam
10 : * Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com>
11 : *
12 : * SPDX-License-Identifier: MIT
13 : ****************************************************************************/
14 :
15 : #ifndef CPL_CONV_H_INCLUDED
16 : #define CPL_CONV_H_INCLUDED
17 :
18 : #include "cpl_port.h"
19 : #include "cpl_vsi.h"
20 : #include "cpl_error.h"
21 :
22 : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
23 : #include <charconv>
24 : #include <cstdint>
25 : #include <limits>
26 : #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
27 : #include <optional>
28 : #define HAVE_STRING_VIEW
29 : #include <string_view>
30 : #endif
31 : #endif
32 :
33 : /**
34 : * \file cpl_conv.h
35 : *
36 : * Various convenience functions for CPL.
37 : *
38 : */
39 :
40 : /* -------------------------------------------------------------------- */
41 : /* Runtime check of various configuration items. */
42 : /* -------------------------------------------------------------------- */
43 : CPL_C_START
44 :
45 : /*! @cond Doxygen_Suppress */
46 : void CPL_DLL CPLVerifyConfiguration(void);
47 : /*! @endcond */
48 :
49 : bool CPL_DLL CPLIsDebugEnabled(void);
50 :
51 : /** Special value to indicate setting the null value to a configuration option,
52 : * even if there is an environment variable defining it.
53 : *
54 : * @since 3.13
55 : */
56 : #define CPL_NULL_VALUE "__CPL_NULL_VALUE__"
57 :
58 : const char CPL_DLL *CPL_STDCALL CPLGetConfigOption(const char *, const char *)
59 : CPL_WARN_UNUSED_RESULT;
60 : const char CPL_DLL *CPL_STDCALL CPLGetThreadLocalConfigOption(
61 : const char *, const char *) CPL_WARN_UNUSED_RESULT;
62 : const char CPL_DLL *CPL_STDCALL
63 : CPLGetGlobalConfigOption(const char *, const char *) CPL_WARN_UNUSED_RESULT;
64 : void CPL_DLL CPL_STDCALL CPLSetConfigOption(const char *, const char *);
65 : void CPL_DLL CPL_STDCALL CPLSetThreadLocalConfigOption(const char *pszKey,
66 : const char *pszValue);
67 : void CPL_DLL CPLDeclareKnownConfigOption(const char *pszKey,
68 : const char *pszDefinition);
69 : char CPL_DLL **CPLGetKnownConfigOptions(void);
70 :
71 : /** Callback for CPLSubscribeToSetConfigOption() */
72 : typedef void (*CPLSetConfigOptionSubscriber)(const char *pszKey,
73 : const char *pszValue,
74 : bool bThreadLocal,
75 : void *pUserData);
76 : int CPL_DLL CPLSubscribeToSetConfigOption(
77 : CPLSetConfigOptionSubscriber pfnCallback, void *pUserData);
78 : void CPL_DLL CPLUnsubscribeToSetConfigOption(int nSubscriberId);
79 :
80 : /*! @cond Doxygen_Suppress */
81 : void CPL_DLL CPL_STDCALL CPLFreeConfig(void);
82 : /*! @endcond */
83 : char CPL_DLL **CPLGetConfigOptions(void);
84 : void CPL_DLL CPLSetConfigOptions(const char *const *papszConfigOptions);
85 : char CPL_DLL **CPLGetThreadLocalConfigOptions(void);
86 : void CPL_DLL
87 : CPLSetThreadLocalConfigOptions(const char *const *papszConfigOptions);
88 : void CPL_DLL CPLLoadConfigOptionsFromFile(const char *pszFilename,
89 : int bOverrideEnvVars);
90 : void CPL_DLL CPLLoadConfigOptionsFromPredefinedFiles(void);
91 :
92 : /* -------------------------------------------------------------------- */
93 : /* Safe malloc() API. Thin cover over VSI functions with fatal */
94 : /* error reporting if memory allocation fails. */
95 : /* -------------------------------------------------------------------- */
96 : void CPL_DLL *CPLMalloc(size_t) CPL_WARN_UNUSED_RESULT;
97 : void CPL_DLL *CPLCalloc(size_t, size_t) CPL_WARN_UNUSED_RESULT;
98 : void CPL_DLL *CPLRealloc(void *, size_t) CPL_WARN_UNUSED_RESULT;
99 : char CPL_DLL *
100 : CPLStrdup(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
101 : char CPL_DLL *CPLStrlwr(char *);
102 :
103 : /** Alias of VSIFree() */
104 : #define CPLFree VSIFree
105 :
106 : /* -------------------------------------------------------------------- */
107 : /* Read a line from a text file, and strip of CR/LF. */
108 : /* -------------------------------------------------------------------- */
109 : char CPL_DLL *CPLFGets(char *, int, FILE *);
110 : const char CPL_DLL *CPLReadLine(FILE *);
111 : const char CPL_DLL *CPLReadLineL(VSILFILE *);
112 : const char CPL_DLL *CPLReadLine2L(VSILFILE *, int, CSLConstList);
113 : const char CPL_DLL *CPLReadLine3L(VSILFILE *, int, int *, CSLConstList);
114 :
115 : /* -------------------------------------------------------------------- */
116 : /* Convert ASCII string to floating point number */
117 : /* (THESE FUNCTIONS ARE NOT LOCALE AWARE!). */
118 : /* -------------------------------------------------------------------- */
119 : double CPL_DLL CPLAtof(const char *);
120 : double CPL_DLL CPLAtofDelim(const char *, char);
121 : double CPL_DLL CPLStrtod(const char *, char **);
122 : double CPL_DLL CPLStrtodM(const char *, char **);
123 : double CPL_DLL CPLStrtodDelim(const char *, char **, char);
124 : float CPL_DLL CPLStrtof(const char *, char **);
125 : float CPL_DLL CPLStrtofDelim(const char *, char **, char);
126 :
127 : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
128 : extern "C++"
129 : {
130 : namespace cpl
131 : {
132 : #if defined(DOXYGEN_SKIP) || defined(HAVE_STRING_VIEW)
133 : double CPL_DLL strtod_delim(std::string_view, char **, char);
134 : #endif
135 : } // namespace cpl
136 : }
137 : #endif
138 :
139 : /* -------------------------------------------------------------------- */
140 : /* Convert number to string. This function is locale agnostic */
141 : /* (i.e. it will support "," or "." regardless of current locale) */
142 : /* -------------------------------------------------------------------- */
143 : double CPL_DLL CPLAtofM(const char *);
144 :
145 : /* -------------------------------------------------------------------- */
146 : /* Read a numeric value from an ASCII character string. */
147 : /* -------------------------------------------------------------------- */
148 : char CPL_DLL *CPLScanString(const char *, int, int, int);
149 : double CPL_DLL CPLScanDouble(const char *, int);
150 : long CPL_DLL CPLScanLong(const char *, int);
151 : unsigned long CPL_DLL CPLScanULong(const char *, int);
152 : GUIntBig CPL_DLL CPLScanUIntBig(const char *, int);
153 : GIntBig CPL_DLL CPLAtoGIntBig(const char *pszString);
154 : GIntBig CPL_DLL CPLAtoGIntBigEx(const char *pszString, int bWarn,
155 : int *pbOverflow);
156 : void CPL_DLL *CPLScanPointer(const char *, int);
157 :
158 : /* -------------------------------------------------------------------- */
159 : /* Print a value to an ASCII character string. */
160 : /* -------------------------------------------------------------------- */
161 : int CPL_DLL CPLPrintString(char *, const char *, int);
162 : int CPL_DLL CPLPrintStringFill(char *, const char *, int);
163 : int CPL_DLL CPLPrintInt32(char *, GInt32, int);
164 : int CPL_DLL CPLPrintUIntBig(char *, GUIntBig, int);
165 : int CPL_DLL CPLPrintDouble(char *, const char *, double, const char *);
166 : int CPL_DLL CPLPrintTime(char *, int, const char *, const struct tm *,
167 : const char *);
168 : int CPL_DLL CPLPrintPointer(char *, void *, int);
169 :
170 : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
171 : extern "C++"
172 : {
173 : std::string CPL_DLL CPLFormatReadableFileSize(uint64_t nSizeInBytes);
174 : std::string CPL_DLL CPLFormatReadableFileSize(double dfSizeInBytes);
175 : }
176 : #endif
177 :
178 : /* -------------------------------------------------------------------- */
179 : /* Fetch a function from DLL / so. */
180 : /* -------------------------------------------------------------------- */
181 :
182 : void CPL_DLL *CPLGetSymbol(const char *, const char *);
183 :
184 : /* -------------------------------------------------------------------- */
185 : /* Fetch executable path. */
186 : /* -------------------------------------------------------------------- */
187 : int CPL_DLL CPLGetExecPath(char *pszPathBuf, int nMaxLength);
188 :
189 : /* -------------------------------------------------------------------- */
190 : /* Filename handling functions. */
191 : /* -------------------------------------------------------------------- */
192 :
193 : #if defined(DOXYGEN_SKIP) || !defined(__cplusplus) || \
194 : !defined(GDAL_COMPILATION) || \
195 : (defined(__cplusplus) && defined(ALLOW_DEPRECATED_CPL_PATH_FUNCTIONS))
196 : const char CPL_DLL *
197 : CPLGetPath(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
198 : const char CPL_DLL *
199 : CPLGetDirname(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
200 : const char CPL_DLL *
201 : CPLGetBasename(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
202 : const char CPL_DLL *
203 : CPLGetExtension(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
204 : const char CPL_DLL *CPLFormFilename(
205 : const char *pszPath, const char *pszBasename,
206 : const char *pszExtension) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
207 : const char CPL_DLL *CPLFormCIFilename(
208 : const char *pszPath, const char *pszBasename,
209 : const char *pszExtension) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
210 : const char CPL_DLL *CPLResetExtension(const char *, const char *)
211 : CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
212 : const char CPL_DLL *CPLProjectRelativeFilename(const char *pszProjectDir,
213 : const char *pszSecondaryFilename)
214 : CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
215 : const char CPL_DLL *
216 : CPLCleanTrailingSlash(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
217 : const char CPL_DLL *CPLGenerateTempFilename(const char *pszStem)
218 : CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
219 : const char CPL_DLL *CPLExpandTilde(const char *pszFilename)
220 : CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
221 : const char CPL_DLL *
222 : CPLLaunderForFilename(const char *pszName,
223 : const char *pszOutputPath) CPL_WARN_UNUSED_RESULT;
224 : #endif
225 :
226 : char CPL_DLL *CPLGetCurrentDir(void);
227 : const char CPL_DLL *
228 : CPLGetFilename(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
229 : int CPL_DLL CPLIsFilenameRelative(const char *pszFilename);
230 : const char CPL_DLL *CPLExtractRelativePath(const char *, const char *, int *)
231 : CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
232 : char CPL_DLL **
233 : CPLCorrespondingPaths(const char *pszOldFilename, const char *pszNewFilename,
234 : CSLConstList papszFileList) CPL_WARN_UNUSED_RESULT;
235 : int CPL_DLL CPLCheckForFile(char *pszFilename, CSLConstList papszSiblingList);
236 :
237 : const char CPL_DLL *CPLGetHomeDir(void) CPL_WARN_UNUSED_RESULT;
238 :
239 : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
240 :
241 : extern "C++"
242 : {
243 : std::string CPL_DLL CPLGetPathSafe(const char *) CPL_WARN_UNUSED_RESULT;
244 : std::string CPL_DLL CPLGetDirnameSafe(const char *) CPL_WARN_UNUSED_RESULT;
245 : std::string CPL_DLL CPLGetBasenameSafe(const char *) CPL_WARN_UNUSED_RESULT;
246 : std::string CPL_DLL CPLGetExtensionSafe(const char *)
247 : CPL_WARN_UNUSED_RESULT;
248 : std::string CPL_DLL CPLFormFilenameSafe(
249 : const char *pszPath, const char *pszBasename,
250 : const char *pszExtension = nullptr) CPL_WARN_UNUSED_RESULT;
251 : std::string CPL_DLL CPLFormCIFilenameSafe(
252 : const char *pszPath, const char *pszBasename,
253 : const char *pszExtension = nullptr) CPL_WARN_UNUSED_RESULT;
254 : std::string CPL_DLL CPLResetExtensionSafe(const char *, const char *)
255 : CPL_WARN_UNUSED_RESULT;
256 : std::string CPL_DLL CPLProjectRelativeFilenameSafe(
257 : const char *pszProjectDir,
258 : const char *pszSecondaryFilename) CPL_WARN_UNUSED_RESULT;
259 : std::string CPL_DLL CPLCleanTrailingSlashSafe(const char *pszPath)
260 : CPL_WARN_UNUSED_RESULT;
261 : std::string CPL_DLL CPLGenerateTempFilenameSafe(const char *pszStem)
262 : CPL_WARN_UNUSED_RESULT;
263 : std::string CPL_DLL CPLExpandTildeSafe(const char *pszFilename)
264 : CPL_WARN_UNUSED_RESULT;
265 : std::string CPL_DLL CPLLaunderForFilenameSafe(
266 : const char *pszName, const char *pszOutputPath) CPL_WARN_UNUSED_RESULT;
267 : std::string CPL_DLL CPLLaunderForFilenameSafe(
268 : const std::string &osName, char chReplacementChar = '_',
269 : const char *pszExtraReservedCharacters = nullptr)
270 : CPL_WARN_UNUSED_RESULT;
271 :
272 : #ifdef HAVE_STRING_VIEW
273 : std::string
274 : CPL_DLL CPLLexicallyNormalize(std::string_view svPath, char sep1,
275 : char sep2 = 0) CPL_WARN_UNUSED_RESULT;
276 : #endif
277 : }
278 :
279 : #endif // defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
280 :
281 : bool CPL_DLL CPLHasPathTraversal(const char *pszFilename);
282 : bool CPL_DLL CPLHasUnbalancedPathTraversal(const char *pszFilename);
283 :
284 : /* -------------------------------------------------------------------- */
285 : /* Find File Function */
286 : /* -------------------------------------------------------------------- */
287 :
288 : /** Callback for CPLPushFileFinder */
289 : typedef char const *(*CPLFileFinder)(const char *, const char *);
290 :
291 : const char CPL_DLL *CPLFindFile(const char *pszClass, const char *pszBasename);
292 : const char CPL_DLL *CPLDefaultFindFile(const char *pszClass,
293 : const char *pszBasename);
294 : void CPL_DLL CPLPushFileFinder(CPLFileFinder pfnFinder);
295 : CPLFileFinder CPL_DLL CPLPopFileFinder(void);
296 : void CPL_DLL CPLPushFinderLocation(const char *);
297 : void CPL_DLL CPLPopFinderLocation(void);
298 : void CPL_DLL CPLFinderClean(void);
299 :
300 : /* -------------------------------------------------------------------- */
301 : /* Safe version of stat() that works properly on stuff like "C:". */
302 : /* -------------------------------------------------------------------- */
303 : int CPL_DLL CPLStat(const char *, VSIStatBuf *) CPL_WARN_UNUSED_RESULT;
304 :
305 : /* -------------------------------------------------------------------- */
306 : /* Reference counted file handle manager. Makes sharing file */
307 : /* handles more practical. */
308 : /* -------------------------------------------------------------------- */
309 :
310 : /** Information on a shared file */
311 : typedef struct
312 : {
313 : FILE *fp; /**< File pointer */
314 : int nRefCount; /**< Reference counter */
315 : int bLarge; /**< Whether fp must be interpreted as VSIFILE* */
316 : char *pszFilename; /**< Filename */
317 : char *pszAccess; /**< Access mode */
318 : } CPLSharedFileInfo;
319 :
320 : FILE CPL_DLL *CPLOpenShared(const char *, const char *, int);
321 : void CPL_DLL CPLCloseShared(FILE *);
322 : CPLSharedFileInfo CPL_DLL *CPLGetSharedList(int *);
323 : void CPL_DLL CPLDumpSharedList(FILE *);
324 : /*! @cond Doxygen_Suppress */
325 : void CPL_DLL CPLCleanupSharedFileMutex(void);
326 : /*! @endcond */
327 :
328 : /* -------------------------------------------------------------------- */
329 : /* DMS to Dec to DMS conversion. */
330 : /* -------------------------------------------------------------------- */
331 : double CPL_DLL CPLDMSToDec(const char *is);
332 : const char CPL_DLL *CPLDecToDMS(double dfAngle, const char *pszAxis,
333 : int nPrecision);
334 : double CPL_DLL CPLPackedDMSToDec(double);
335 : double CPL_DLL CPLDecToPackedDMS(double dfDec);
336 :
337 : CPLErr CPL_DLL CPLStringToComplex(const char *pszString, double *pdfReal,
338 : double *pdfImag);
339 :
340 : /* -------------------------------------------------------------------- */
341 : /* Misc other functions. */
342 : /* -------------------------------------------------------------------- */
343 : int CPL_DLL CPLUnlinkTree(const char *);
344 : int CPL_DLL CPLCopyFile(const char *pszNewPath, const char *pszOldPath);
345 : int CPL_DLL CPLCopyTree(const char *pszNewPath, const char *pszOldPath);
346 : int CPL_DLL CPLMoveFile(const char *pszNewPath, const char *pszOldPath);
347 : int CPL_DLL CPLSymlink(const char *pszOldPath, const char *pszNewPath,
348 : CSLConstList papszOptions);
349 : int CPL_DLL CPLGetRemainingFileDescriptorCount(void);
350 :
351 : /* -------------------------------------------------------------------- */
352 : /* Lock related functions. */
353 : /* -------------------------------------------------------------------- */
354 :
355 : /** Return code of CPLLockFileEx(). */
356 : typedef enum
357 : {
358 : CLFS_OK, /**< CPLLockFileEx() succeeded. */
359 : CLFS_CANNOT_CREATE_LOCK, /**< Lock file creation failed. */
360 : CLFS_LOCK_BUSY, /**< Lock already taken (and still alive). */
361 : CLFS_API_MISUSE, /**< API misuse. */
362 : CLFS_THREAD_CREATION_FAILED, /**< Thread creation failed. */
363 : } CPLLockFileStatus;
364 :
365 : /** Handle type returned by CPLLockFileEx(). */
366 : typedef struct CPLLockFileStruct *CPLLockFileHandle;
367 :
368 : CPLLockFileStatus CPL_DLL CPLLockFileEx(const char *pszLockFileName,
369 : CPLLockFileHandle *phLockFileHandle,
370 : CSLConstList papszOptions);
371 :
372 : void CPL_DLL CPLUnlockFileEx(CPLLockFileHandle hLockFileHandle);
373 :
374 : /* -------------------------------------------------------------------- */
375 : /* ZIP Creation. */
376 : /* -------------------------------------------------------------------- */
377 :
378 : /*! @cond Doxygen_Suppress */
379 : #define CPL_ZIP_API_OFFERED
380 : /*! @endcond */
381 : void CPL_DLL *CPLCreateZip(const char *pszZipFilename, char **papszOptions);
382 : CPLErr CPL_DLL CPLCreateFileInZip(void *hZip, const char *pszFilename,
383 : char **papszOptions);
384 : CPLErr CPL_DLL CPLWriteFileInZip(void *hZip, const void *pBuffer,
385 : int nBufferSize);
386 : CPLErr CPL_DLL CPLCloseFileInZip(void *hZip);
387 : CPLErr CPL_DLL CPLAddFileInZip(void *hZip, const char *pszArchiveFilename,
388 : const char *pszInputFilename, VSILFILE *fpInput,
389 : CSLConstList papszOptions,
390 : GDALProgressFunc pProgressFunc,
391 : void *pProgressData);
392 : CPLErr CPL_DLL CPLCloseZip(void *hZip);
393 :
394 : /* -------------------------------------------------------------------- */
395 : /* ZLib compression */
396 : /* -------------------------------------------------------------------- */
397 :
398 : void CPL_DLL *CPLZLibDeflate(const void *ptr, size_t nBytes, int nLevel,
399 : void *outptr, size_t nOutAvailableBytes,
400 : size_t *pnOutBytes);
401 : void CPL_DLL *CPLZLibInflate(const void *ptr, size_t nBytes, void *outptr,
402 : size_t nOutAvailableBytes, size_t *pnOutBytes);
403 : void CPL_DLL *CPLZLibInflateEx(const void *ptr, size_t nBytes, void *outptr,
404 : size_t nOutAvailableBytes,
405 : bool bAllowResizeOutptr, size_t *pnOutBytes);
406 :
407 : /* -------------------------------------------------------------------- */
408 : /* XML validation. */
409 : /* -------------------------------------------------------------------- */
410 : int CPL_DLL CPLValidateXML(const char *pszXMLFilename,
411 : const char *pszXSDFilename,
412 : CSLConstList papszOptions);
413 :
414 : /* -------------------------------------------------------------------- */
415 : /* Locale handling. Prevents parallel executions of setlocale(). */
416 : /* -------------------------------------------------------------------- */
417 : char *CPLsetlocale(int category, const char *locale);
418 : /*! @cond Doxygen_Suppress */
419 : void CPLCleanupSetlocaleMutex(void);
420 : /*! @endcond */
421 :
422 : /*!
423 : CPLIsPowerOfTwo()
424 : @param i - tested number
425 : @return TRUE if i is power of two otherwise return FALSE
426 : */
427 : int CPL_DLL CPLIsPowerOfTwo(unsigned int i);
428 :
429 : /* -------------------------------------------------------------------- */
430 : /* Terminal related */
431 : /* -------------------------------------------------------------------- */
432 :
433 : bool CPL_DLL CPLIsInteractive(FILE *f);
434 :
435 : CPL_C_END
436 :
437 : /* -------------------------------------------------------------------- */
438 : /* C++ object for temporarily forcing a LC_NUMERIC locale to "C". */
439 : /* -------------------------------------------------------------------- */
440 :
441 : //! @cond Doxygen_Suppress
442 : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
443 :
444 : extern "C++"
445 : {
446 : class CPL_DLL CPLLocaleC
447 : {
448 : CPL_DISALLOW_COPY_ASSIGN(CPLLocaleC)
449 : public:
450 : CPLLocaleC();
451 : ~CPLLocaleC();
452 :
453 : private:
454 : char *pszOldLocale;
455 : };
456 :
457 : // Does the same as CPLLocaleC except that, when available, it tries to
458 : // only affect the current thread. But code that would be dependent of
459 : // setlocale(LC_NUMERIC, NULL) returning "C", such as current proj.4
460 : // versions, will not work depending on the actual implementation
461 : class CPLThreadLocaleCPrivate;
462 :
463 : class CPL_DLL CPLThreadLocaleC
464 : {
465 : CPL_DISALLOW_COPY_ASSIGN(CPLThreadLocaleC)
466 :
467 : public:
468 : CPLThreadLocaleC();
469 : ~CPLThreadLocaleC();
470 :
471 : private:
472 : CPLThreadLocaleCPrivate *m_private;
473 : };
474 : }
475 :
476 : #endif /* def __cplusplus */
477 : //! @endcond
478 :
479 : /* -------------------------------------------------------------------- */
480 : /* C++ object for temporarily forcing a config option */
481 : /* -------------------------------------------------------------------- */
482 :
483 : //! @cond Doxygen_Suppress
484 : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
485 :
486 : extern "C++"
487 : {
488 : class CPL_DLL CPLConfigOptionSetter
489 : {
490 : CPL_DISALLOW_COPY_ASSIGN(CPLConfigOptionSetter)
491 : public:
492 : CPLConfigOptionSetter(const char *pszKey, const char *pszValue,
493 : bool bSetOnlyIfUndefined);
494 : ~CPLConfigOptionSetter();
495 :
496 : private:
497 : char *m_pszKey;
498 : char *m_pszOldValue;
499 : bool m_bRestoreOldValue;
500 : };
501 : }
502 :
503 : #endif /* def __cplusplus */
504 : //! @endcond
505 :
506 : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
507 :
508 : extern "C++"
509 : {
510 :
511 : #ifndef DOXYGEN_SKIP
512 : #include <type_traits> // for std::is_base_of
513 : #endif
514 :
515 : namespace cpl
516 : {
517 : /** Use cpl::down_cast<Derived*>(pointer_to_base) as equivalent of
518 : * static_cast<Derived*>(pointer_to_base) with safe checking in debug
519 : * mode.
520 : *
521 : * Only works if no virtual inheritance is involved.
522 : *
523 : * @param f pointer to a base class
524 : * @return pointer to a derived class
525 : */
526 7212244 : template <typename To, typename From> inline To down_cast(From *f)
527 : {
528 : static_assert(
529 : (std::is_base_of<From,
530 : typename std::remove_pointer<To>::type>::value),
531 : "target type not derived from source type");
532 7212244 : CPLAssert(f == nullptr || dynamic_cast<To>(f) != nullptr);
533 7212244 : return static_cast<To>(f);
534 : }
535 :
536 : /** Computes ceil(a/b) where a and b are integers */
537 247873 : template <class T, class U> inline T div_round_up(T a, U b)
538 : {
539 247873 : return a / b + (((a % b) == 0) ? 0 : 1);
540 : }
541 :
542 : #ifdef HAVE_STRING_VIEW
543 : template <typename T>
544 : std::optional<T> CPL_DLL strict_parse(std::string_view str);
545 : #endif
546 :
547 : } // namespace cpl
548 : } // extern "C++"
549 :
550 : #ifdef HAVE_STRING_VIEW
551 : #undef HAVE_STRING_VIEW
552 : #endif
553 :
554 : #endif /* def __cplusplus */
555 :
556 : #endif /* ndef CPL_CONV_H_INCLUDED */
|