cpl_conv.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /******************************************************************************
  2. * $Id: cpl_conv.h 28601 2015-03-03 11:06:40Z 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. void CPL_DLL CPLVerifyConfiguration(void);
  47. const char CPL_DLL * CPL_STDCALL
  48. CPLGetConfigOption( const char *, const char * ) CPL_WARN_UNUSED_RESULT;
  49. void CPL_DLL CPL_STDCALL CPLSetConfigOption( const char *, const char * );
  50. void CPL_DLL CPL_STDCALL CPLSetThreadLocalConfigOption( const char *pszKey,
  51. const char *pszValue );
  52. void CPL_DLL CPL_STDCALL CPLFreeConfig(void);
  53. /* -------------------------------------------------------------------- */
  54. /* Safe malloc() API. Thin cover over VSI functions with fatal */
  55. /* error reporting if memory allocation fails. */
  56. /* -------------------------------------------------------------------- */
  57. void CPL_DLL *CPLMalloc( size_t ) CPL_WARN_UNUSED_RESULT;
  58. void CPL_DLL *CPLCalloc( size_t, size_t ) CPL_WARN_UNUSED_RESULT;
  59. void CPL_DLL *CPLRealloc( void *, size_t ) CPL_WARN_UNUSED_RESULT;
  60. char CPL_DLL *CPLStrdup( const char * ) CPL_WARN_UNUSED_RESULT;
  61. char CPL_DLL *CPLStrlwr( char *);
  62. #define CPLFree VSIFree
  63. /* -------------------------------------------------------------------- */
  64. /* Read a line from a text file, and strip of CR/LF. */
  65. /* -------------------------------------------------------------------- */
  66. char CPL_DLL *CPLFGets( char *, int, FILE *);
  67. const char CPL_DLL *CPLReadLine( FILE * );
  68. const char CPL_DLL *CPLReadLineL( VSILFILE * );
  69. const char CPL_DLL *CPLReadLine2L( VSILFILE * , int nMaxCols, char** papszOptions);
  70. /* -------------------------------------------------------------------- */
  71. /* Convert ASCII string to floationg point number */
  72. /* (THESE FUNCTIONS ARE NOT LOCALE AWARE!). */
  73. /* -------------------------------------------------------------------- */
  74. double CPL_DLL CPLAtof(const char *);
  75. double CPL_DLL CPLAtofDelim(const char *, char);
  76. double CPL_DLL CPLStrtod(const char *, char **);
  77. double CPL_DLL CPLStrtodDelim(const char *, char **, char);
  78. float CPL_DLL CPLStrtof(const char *, char **);
  79. float CPL_DLL CPLStrtofDelim(const char *, char **, char);
  80. /* -------------------------------------------------------------------- */
  81. /* Convert number to string. This function is locale agnostic */
  82. /* (ie. it will support "," or "." regardless of current locale) */
  83. /* -------------------------------------------------------------------- */
  84. double CPL_DLL CPLAtofM(const char *);
  85. /* -------------------------------------------------------------------- */
  86. /* Read a numeric value from an ASCII character string. */
  87. /* -------------------------------------------------------------------- */
  88. char CPL_DLL *CPLScanString( const char *, int, int, int );
  89. double CPL_DLL CPLScanDouble( const char *, int );
  90. long CPL_DLL CPLScanLong( const char *, int );
  91. unsigned long CPL_DLL CPLScanULong( const char *, int );
  92. GUIntBig CPL_DLL CPLScanUIntBig( const char *, int );
  93. GIntBig CPL_DLL CPLAtoGIntBig( const char* pszString );
  94. GIntBig CPL_DLL CPLAtoGIntBigEx( const char* pszString, int bWarn, int *pbOverflow );
  95. void CPL_DLL *CPLScanPointer( const char *, int );
  96. /* -------------------------------------------------------------------- */
  97. /* Print a value to an ASCII character string. */
  98. /* -------------------------------------------------------------------- */
  99. int CPL_DLL CPLPrintString( char *, const char *, int );
  100. int CPL_DLL CPLPrintStringFill( char *, const char *, int );
  101. int CPL_DLL CPLPrintInt32( char *, GInt32 , int );
  102. int CPL_DLL CPLPrintUIntBig( char *, GUIntBig , int );
  103. int CPL_DLL CPLPrintDouble( char *, const char *, double, const char * );
  104. int CPL_DLL CPLPrintTime( char *, int , const char *, const struct tm *,
  105. const char * );
  106. int CPL_DLL CPLPrintPointer( char *, void *, int );
  107. /* -------------------------------------------------------------------- */
  108. /* Fetch a function from DLL / so. */
  109. /* -------------------------------------------------------------------- */
  110. void CPL_DLL *CPLGetSymbol( const char *, const char * );
  111. /* -------------------------------------------------------------------- */
  112. /* Fetch executable path. */
  113. /* -------------------------------------------------------------------- */
  114. int CPL_DLL CPLGetExecPath( char *pszPathBuf, int nMaxLength );
  115. /* -------------------------------------------------------------------- */
  116. /* Filename handling functions. */
  117. /* -------------------------------------------------------------------- */
  118. const char CPL_DLL *CPLGetPath( const char * );
  119. const char CPL_DLL *CPLGetDirname( const char * );
  120. const char CPL_DLL *CPLGetFilename( const char * );
  121. const char CPL_DLL *CPLGetBasename( const char * );
  122. const char CPL_DLL *CPLGetExtension( const char * );
  123. char CPL_DLL *CPLGetCurrentDir(void);
  124. const char CPL_DLL *CPLFormFilename( const char *pszPath,
  125. const char *pszBasename,
  126. const char *pszExtension );
  127. const char CPL_DLL *CPLFormCIFilename( const char *pszPath,
  128. const char *pszBasename,
  129. const char *pszExtension );
  130. const char CPL_DLL *CPLResetExtension( const char *, const char * );
  131. const char CPL_DLL *CPLProjectRelativeFilename( const char *pszProjectDir,
  132. const char *pszSecondaryFilename );
  133. int CPL_DLL CPLIsFilenameRelative( const char *pszFilename );
  134. const char CPL_DLL *CPLExtractRelativePath(const char *, const char *, int *);
  135. const char CPL_DLL *CPLCleanTrailingSlash( const char * );
  136. char CPL_DLL **CPLCorrespondingPaths( const char *pszOldFilename,
  137. const char *pszNewFilename,
  138. char **papszFileList );
  139. int CPL_DLL CPLCheckForFile( char *pszFilename, char **papszSiblingList );
  140. const char CPL_DLL *CPLGenerateTempFilename( const char *pszStem );
  141. /* -------------------------------------------------------------------- */
  142. /* Find File Function */
  143. /* -------------------------------------------------------------------- */
  144. typedef const char *(*CPLFileFinder)(const char *, const char *);
  145. const char CPL_DLL *CPLFindFile(const char *pszClass,
  146. const char *pszBasename);
  147. const char CPL_DLL *CPLDefaultFindFile(const char *pszClass,
  148. const char *pszBasename);
  149. void CPL_DLL CPLPushFileFinder( CPLFileFinder pfnFinder );
  150. CPLFileFinder CPL_DLL CPLPopFileFinder(void);
  151. void CPL_DLL CPLPushFinderLocation( const char * );
  152. void CPL_DLL CPLPopFinderLocation(void);
  153. void CPL_DLL CPLFinderClean(void);
  154. /* -------------------------------------------------------------------- */
  155. /* Safe version of stat() that works properly on stuff like "C:". */
  156. /* -------------------------------------------------------------------- */
  157. int CPL_DLL CPLStat( const char *, VSIStatBuf * );
  158. /* -------------------------------------------------------------------- */
  159. /* Reference counted file handle manager. Makes sharing file */
  160. /* handles more practical. */
  161. /* -------------------------------------------------------------------- */
  162. typedef struct {
  163. FILE *fp;
  164. int nRefCount;
  165. int bLarge;
  166. char *pszFilename;
  167. char *pszAccess;
  168. } CPLSharedFileInfo;
  169. FILE CPL_DLL *CPLOpenShared( const char *, const char *, int );
  170. void CPL_DLL CPLCloseShared( FILE * );
  171. CPLSharedFileInfo CPL_DLL *CPLGetSharedList( int * );
  172. void CPL_DLL CPLDumpSharedList( FILE * );
  173. void CPL_DLL CPLCleanupSharedFileMutex( void );
  174. /* -------------------------------------------------------------------- */
  175. /* DMS to Dec to DMS conversion. */
  176. /* -------------------------------------------------------------------- */
  177. double CPL_DLL CPLDMSToDec( const char *is );
  178. const char CPL_DLL *CPLDecToDMS( double dfAngle, const char * pszAxis,
  179. int nPrecision );
  180. double CPL_DLL CPLPackedDMSToDec( double );
  181. double CPL_DLL CPLDecToPackedDMS( double dfDec );
  182. void CPL_DLL CPLStringToComplex( const char *pszString,
  183. double *pdfReal, double *pdfImag );
  184. /* -------------------------------------------------------------------- */
  185. /* Misc other functions. */
  186. /* -------------------------------------------------------------------- */
  187. int CPL_DLL CPLUnlinkTree( const char * );
  188. int CPL_DLL CPLCopyFile( const char *pszNewPath, const char *pszOldPath );
  189. int CPL_DLL CPLCopyTree( const char *pszNewPath, const char *pszOldPath );
  190. int CPL_DLL CPLMoveFile( const char *pszNewPath, const char *pszOldPath );
  191. /* -------------------------------------------------------------------- */
  192. /* ZIP Creation. */
  193. /* -------------------------------------------------------------------- */
  194. #define CPL_ZIP_API_OFFERED
  195. void CPL_DLL *CPLCreateZip( const char *pszZipFilename, char **papszOptions );
  196. CPLErr CPL_DLL CPLCreateFileInZip( void *hZip, const char *pszFilename,
  197. char **papszOptions );
  198. CPLErr CPL_DLL CPLWriteFileInZip( void *hZip, const void *pBuffer, int nBufferSize );
  199. CPLErr CPL_DLL CPLCloseFileInZip( void *hZip );
  200. CPLErr CPL_DLL CPLCloseZip( void *hZip );
  201. /* -------------------------------------------------------------------- */
  202. /* ZLib compression */
  203. /* -------------------------------------------------------------------- */
  204. void CPL_DLL *CPLZLibDeflate( const void* ptr, size_t nBytes, int nLevel,
  205. void* outptr, size_t nOutAvailableBytes,
  206. size_t* pnOutBytes );
  207. void CPL_DLL *CPLZLibInflate( const void* ptr, size_t nBytes,
  208. void* outptr, size_t nOutAvailableBytes,
  209. size_t* pnOutBytes );
  210. /* -------------------------------------------------------------------- */
  211. /* XML validation. */
  212. /* -------------------------------------------------------------------- */
  213. int CPL_DLL CPLValidateXML(const char* pszXMLFilename,
  214. const char* pszXSDFilename,
  215. char** papszOptions);
  216. /* -------------------------------------------------------------------- */
  217. /* Locale handling. Prevents parallel executions of setlocale(). */
  218. /* -------------------------------------------------------------------- */
  219. char* CPLsetlocale (int category, const char* locale);
  220. void CPLCleanupSetlocaleMutex(void);
  221. CPL_C_END
  222. /* -------------------------------------------------------------------- */
  223. /* C++ object for temporariliy forcing a LC_NUMERIC locale to "C". */
  224. /* -------------------------------------------------------------------- */
  225. #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
  226. class CPL_DLL CPLLocaleC
  227. {
  228. public:
  229. CPLLocaleC();
  230. ~CPLLocaleC();
  231. private:
  232. char *pszOldLocale;
  233. /* Make it non-copyable */
  234. CPLLocaleC(CPLLocaleC&);
  235. CPLLocaleC& operator=(CPLLocaleC&);
  236. };
  237. #endif /* def __cplusplus */
  238. #endif /* ndef CPL_CONV_H_INCLUDED */