1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/******************************************************************************
* $Id$
*
* Project: CPL - Common Portability Library
* Purpose: Convenience functions declarations.
* This is intended to remain light weight.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 1998, Frank Warmerdam
* Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#ifndef CPL_CONV_H_INCLUDED
#define CPL_CONV_H_INCLUDED
#include "cpl_port.h"
#include "cpl_vsi.h"
#include "cpl_error.h"
/**
* \file cpl_conv.h
*
* Various convenience functions for CPL.
*
*/
/* -------------------------------------------------------------------- */
/* Runtime check of various configuration items. */
/* -------------------------------------------------------------------- */
CPL_C_START
/*! @cond Doxygen_Suppress */
void CPL_DLL CPLVerifyConfiguration(void);
/*! @endcond */
const char CPL_DLL *CPL_STDCALL CPLGetConfigOption(const char *, const char *)
CPL_WARN_UNUSED_RESULT;
const char CPL_DLL *CPL_STDCALL CPLGetThreadLocalConfigOption(
const char *, const char *) CPL_WARN_UNUSED_RESULT;
void CPL_DLL CPL_STDCALL CPLSetConfigOption(const char *, const char *);
void CPL_DLL CPL_STDCALL CPLSetThreadLocalConfigOption(const char *pszKey,
const char *pszValue);
/*! @cond Doxygen_Suppress */
void CPL_DLL CPL_STDCALL CPLFreeConfig(void);
/*! @endcond */
char CPL_DLL **CPLGetConfigOptions(void);
void CPL_DLL CPLSetConfigOptions(const char *const *papszConfigOptions);
char CPL_DLL **CPLGetThreadLocalConfigOptions(void);
void CPL_DLL
CPLSetThreadLocalConfigOptions(const char *const *papszConfigOptions);
void CPL_DLL CPLLoadConfigOptionsFromFile(const char *pszFilename,
int bOverrideEnvVars);
void CPL_DLL CPLLoadConfigOptionsFromPredefinedFiles(void);
/* -------------------------------------------------------------------- */
/* Safe malloc() API. Thin cover over VSI functions with fatal */
/* error reporting if memory allocation fails. */
/* -------------------------------------------------------------------- */
void CPL_DLL *CPLMalloc(size_t) CPL_WARN_UNUSED_RESULT;
void CPL_DLL *CPLCalloc(size_t, size_t) CPL_WARN_UNUSED_RESULT;
void CPL_DLL *CPLRealloc(void *, size_t) CPL_WARN_UNUSED_RESULT;
char CPL_DLL *
CPLStrdup(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
char CPL_DLL *CPLStrlwr(char *);
/** Alias of VSIFree() */
#define CPLFree VSIFree
/* -------------------------------------------------------------------- */
/* Read a line from a text file, and strip of CR/LF. */
/* -------------------------------------------------------------------- */
char CPL_DLL *CPLFGets(char *, int, FILE *);
const char CPL_DLL *CPLReadLine(FILE *);
const char CPL_DLL *CPLReadLineL(VSILFILE *);
const char CPL_DLL *CPLReadLine2L(VSILFILE *, int, CSLConstList);
const char CPL_DLL *CPLReadLine3L(VSILFILE *, int, int *, CSLConstList);
/* -------------------------------------------------------------------- */
/* Convert ASCII string to floating point number */
/* (THESE FUNCTIONS ARE NOT LOCALE AWARE!). */
/* -------------------------------------------------------------------- */
double CPL_DLL CPLAtof(const char *);
double CPL_DLL CPLAtofDelim(const char *, char);
double CPL_DLL CPLStrtod(const char *, char **);
double CPL_DLL CPLStrtodDelim(const char *, char **, char);
float CPL_DLL CPLStrtof(const char *, char **);
float CPL_DLL CPLStrtofDelim(const char *, char **, char);
/* -------------------------------------------------------------------- */
/* Convert number to string. This function is locale agnostic */
/* (i.e. it will support "," or "." regardless of current locale) */
/* -------------------------------------------------------------------- */
double CPL_DLL CPLAtofM(const char *);
/* -------------------------------------------------------------------- */
/* Read a numeric value from an ASCII character string. */
/* -------------------------------------------------------------------- */
char CPL_DLL *CPLScanString(const char *, int, int, int);
double CPL_DLL CPLScanDouble(const char *, int);
long CPL_DLL CPLScanLong(const char *, int);
unsigned long CPL_DLL CPLScanULong(const char *, int);
GUIntBig CPL_DLL CPLScanUIntBig(const char *, int);
GIntBig CPL_DLL CPLAtoGIntBig(const char *pszString);
GIntBig CPL_DLL CPLAtoGIntBigEx(const char *pszString, int bWarn,
int *pbOverflow);
void CPL_DLL *CPLScanPointer(const char *, int);
/* -------------------------------------------------------------------- */
/* Print a value to an ASCII character string. */
/* -------------------------------------------------------------------- */
int CPL_DLL CPLPrintString(char *, const char *, int);
int CPL_DLL CPLPrintStringFill(char *, const char *, int);
int CPL_DLL CPLPrintInt32(char *, GInt32, int);
int CPL_DLL CPLPrintUIntBig(char *, GUIntBig, int);
int CPL_DLL CPLPrintDouble(char *, const char *, double, const char *);
int CPL_DLL CPLPrintTime(char *, int, const char *, const struct tm *,
const char *);
int CPL_DLL CPLPrintPointer(char *, void *, int);
/* -------------------------------------------------------------------- */
/* Fetch a function from DLL / so. */
/* -------------------------------------------------------------------- */
void CPL_DLL *CPLGetSymbol(const char *, const char *);
/* -------------------------------------------------------------------- */
/* Fetch executable path. */
/* -------------------------------------------------------------------- */
int CPL_DLL CPLGetExecPath(char *pszPathBuf, int nMaxLength);
/* -------------------------------------------------------------------- */
/* Filename handling functions. */
/* -------------------------------------------------------------------- */
const char CPL_DLL *
CPLGetPath(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *
CPLGetDirname(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *
CPLGetFilename(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *
CPLGetBasename(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *
CPLGetExtension(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
char CPL_DLL *CPLGetCurrentDir(void);
const char CPL_DLL *CPLFormFilename(
const char *pszPath, const char *pszBasename,
const char *pszExtension) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *CPLFormCIFilename(
const char *pszPath, const char *pszBasename,
const char *pszExtension) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *CPLResetExtension(const char *, const char *)
CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *CPLProjectRelativeFilename(const char *pszProjectDir,
const char *pszSecondaryFilename)
CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
int CPL_DLL CPLIsFilenameRelative(const char *pszFilename);
const char CPL_DLL *CPLExtractRelativePath(const char *, const char *, int *)
CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *
CPLCleanTrailingSlash(const char *) CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
char CPL_DLL **
CPLCorrespondingPaths(const char *pszOldFilename, const char *pszNewFilename,
char **papszFileList) CPL_WARN_UNUSED_RESULT;
int CPL_DLL CPLCheckForFile(char *pszFilename, char **papszSiblingList);
const char CPL_DLL *CPLGenerateTempFilename(const char *pszStem)
CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *CPLExpandTilde(const char *pszFilename)
CPL_WARN_UNUSED_RESULT CPL_RETURNS_NONNULL;
const char CPL_DLL *CPLGetHomeDir(void) CPL_WARN_UNUSED_RESULT;
const char CPL_DLL *
CPLLaunderForFilename(const char *pszName,
const char *pszOutputPath) CPL_WARN_UNUSED_RESULT;
/* -------------------------------------------------------------------- */
/* Find File Function */
/* -------------------------------------------------------------------- */
/** Callback for CPLPushFileFinder */
typedef char const *(*CPLFileFinder)(const char *, const char *);
const char CPL_DLL *CPLFindFile(const char *pszClass, const char *pszBasename);
const char CPL_DLL *CPLDefaultFindFile(const char *pszClass,
const char *pszBasename);
void CPL_DLL CPLPushFileFinder(CPLFileFinder pfnFinder);
CPLFileFinder CPL_DLL CPLPopFileFinder(void);
void CPL_DLL CPLPushFinderLocation(const char *);
void CPL_DLL CPLPopFinderLocation(void);
void CPL_DLL CPLFinderClean(void);
/* -------------------------------------------------------------------- */
/* Safe version of stat() that works properly on stuff like "C:". */
/* -------------------------------------------------------------------- */
int CPL_DLL CPLStat(const char *, VSIStatBuf *) CPL_WARN_UNUSED_RESULT;
/* -------------------------------------------------------------------- */
/* Reference counted file handle manager. Makes sharing file */
/* handles more practical. */
/* -------------------------------------------------------------------- */
/** Information on a shared file */
typedef struct
{
FILE *fp; /**< File pointer */
int nRefCount; /**< Reference counter */
int bLarge; /**< Whether fp must be interpreted as VSIFILE* */
char *pszFilename; /**< Filename */
char *pszAccess; /**< Access mode */
} CPLSharedFileInfo;
FILE CPL_DLL *CPLOpenShared(const char *, const char *, int);
void CPL_DLL CPLCloseShared(FILE *);
CPLSharedFileInfo CPL_DLL *CPLGetSharedList(int *);
void CPL_DLL CPLDumpSharedList(FILE *);
/*! @cond Doxygen_Suppress */
void CPL_DLL CPLCleanupSharedFileMutex(void);
/*! @endcond */
/* -------------------------------------------------------------------- */
/* DMS to Dec to DMS conversion. */
/* -------------------------------------------------------------------- */
double CPL_DLL CPLDMSToDec(const char *is);
const char CPL_DLL *CPLDecToDMS(double dfAngle, const char *pszAxis,
int nPrecision);
double CPL_DLL CPLPackedDMSToDec(double);
double CPL_DLL CPLDecToPackedDMS(double dfDec);
void CPL_DLL CPLStringToComplex(const char *pszString, double *pdfReal,
double *pdfImag);
/* -------------------------------------------------------------------- */
/* Misc other functions. */
/* -------------------------------------------------------------------- */
int CPL_DLL CPLUnlinkTree(const char *);
int CPL_DLL CPLCopyFile(const char *pszNewPath, const char *pszOldPath);
int CPL_DLL CPLCopyTree(const char *pszNewPath, const char *pszOldPath);
int CPL_DLL CPLMoveFile(const char *pszNewPath, const char *pszOldPath);
int CPL_DLL CPLSymlink(const char *pszOldPath, const char *pszNewPath,
CSLConstList papszOptions);
/* -------------------------------------------------------------------- */
/* ZIP Creation. */
/* -------------------------------------------------------------------- */
/*! @cond Doxygen_Suppress */
#define CPL_ZIP_API_OFFERED
/*! @endcond */
void CPL_DLL *CPLCreateZip(const char *pszZipFilename, char **papszOptions);
CPLErr CPL_DLL CPLCreateFileInZip(void *hZip, const char *pszFilename,
char **papszOptions);
CPLErr CPL_DLL CPLWriteFileInZip(void *hZip, const void *pBuffer,
int nBufferSize);
CPLErr CPL_DLL CPLCloseFileInZip(void *hZip);
CPLErr CPL_DLL CPLCloseZip(void *hZip);
/* -------------------------------------------------------------------- */
/* ZLib compression */
/* -------------------------------------------------------------------- */
void CPL_DLL *CPLZLibDeflate(const void *ptr, size_t nBytes, int nLevel,
void *outptr, size_t nOutAvailableBytes,
size_t *pnOutBytes);
void CPL_DLL *CPLZLibInflate(const void *ptr, size_t nBytes, void *outptr,
size_t nOutAvailableBytes, size_t *pnOutBytes);
/* -------------------------------------------------------------------- */
/* XML validation. */
/* -------------------------------------------------------------------- */
int CPL_DLL CPLValidateXML(const char *pszXMLFilename,
const char *pszXSDFilename,
CSLConstList papszOptions);
/* -------------------------------------------------------------------- */
/* Locale handling. Prevents parallel executions of setlocale(). */
/* -------------------------------------------------------------------- */
char *CPLsetlocale(int category, const char *locale);
/*! @cond Doxygen_Suppress */
void CPLCleanupSetlocaleMutex(void);
/*! @endcond */
/*!
CPLIsPowerOfTwo()
@param i - tested number
@return TRUE if i is power of two otherwise return FALSE
*/
int CPL_DLL CPLIsPowerOfTwo(unsigned int i);
CPL_C_END
/* -------------------------------------------------------------------- */
/* C++ object for temporarily forcing a LC_NUMERIC locale to "C". */
/* -------------------------------------------------------------------- */
//! @cond Doxygen_Suppress
#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
extern "C++"
{
class CPL_DLL CPLLocaleC
{
CPL_DISALLOW_COPY_ASSIGN(CPLLocaleC)
public:
CPLLocaleC();
~CPLLocaleC();
private:
char *pszOldLocale;
};
// Does the same as CPLLocaleC except that, when available, it tries to
// only affect the current thread. But code that would be dependent of
// setlocale(LC_NUMERIC, NULL) returning "C", such as current proj.4
// versions, will not work depending on the actual implementation
class CPLThreadLocaleCPrivate;
class CPL_DLL CPLThreadLocaleC
{
CPL_DISALLOW_COPY_ASSIGN(CPLThreadLocaleC)
public:
CPLThreadLocaleC();
~CPLThreadLocaleC();
private:
CPLThreadLocaleCPrivate *m_private;
};
}
#endif /* def __cplusplus */
//! @endcond
/* -------------------------------------------------------------------- */
/* C++ object for temporarily forcing a config option */
/* -------------------------------------------------------------------- */
//! @cond Doxygen_Suppress
#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
extern "C++"
{
class CPL_DLL CPLConfigOptionSetter
{
CPL_DISALLOW_COPY_ASSIGN(CPLConfigOptionSetter)
public:
CPLConfigOptionSetter(const char *pszKey, const char *pszValue,
bool bSetOnlyIfUndefined);
~CPLConfigOptionSetter();
private:
char *m_pszKey;
char *m_pszOldValue;
bool m_bRestoreOldValue;
};
}
#endif /* def __cplusplus */
//! @endcond
#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
extern "C++"
{
#ifndef DOXYGEN_SKIP
#include <type_traits> // for std::is_base_of
#endif
namespace cpl
{
/** Use cpl::down_cast<Derived*>(pointer_to_base) as equivalent of
* static_cast<Derived*>(pointer_to_base) with safe checking in debug
* mode.
*
* Only works if no virtual inheritance is involved.
*
* @param f pointer to a base class
* @return pointer to a derived class
*/
template <typename To, typename From> inline To down_cast(From *f)
{
static_assert(
(std::is_base_of<From,
typename std::remove_pointer<To>::type>::value),
"target type not derived from source type");
CPLAssert(f == nullptr || dynamic_cast<To>(f) != nullptr);
return static_cast<To>(f);
}
} // namespace cpl
} // extern "C++"
#endif /* def __cplusplus */
#if defined(__cplusplus) && defined(GDAL_COMPILATION)
extern "C++"
{
#include <memory> // for std::unique_ptr
namespace cpl
{
/** std::make_unique<> implementation borrowed from C++14 */
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&...args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
} // namespace cpl
} // extern "C++"
#endif /* def __cplusplus */
#endif /* ndef CPL_CONV_H_INCLUDED */
|