gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

393 行
17KB

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