safileio.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /******************************************************************************
  2. * $Id: safileio.c,v 1.4 2008/01/16 20:05:14 bram Exp $
  3. *
  4. * Project: Shapelib
  5. * Purpose: Default implementation of file io based on stdio.
  6. * Author: Frank Warmerdam, warmerdam@pobox.com
  7. *
  8. ******************************************************************************
  9. * Copyright (c) 2007, Frank Warmerdam
  10. *
  11. * This software is available under the following "MIT Style" license,
  12. * or at the option of the licensee under the LGPL (see LICENSE.LGPL). This
  13. * option is discussed in more detail in shapelib.html.
  14. *
  15. * --
  16. *
  17. * Permission is hereby granted, free of charge, to any person obtaining a
  18. * copy of this software and associated documentation files (the "Software"),
  19. * to deal in the Software without restriction, including without limitation
  20. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  21. * and/or sell copies of the Software, and to permit persons to whom the
  22. * Software is furnished to do so, subject to the following conditions:
  23. *
  24. * The above copyright notice and this permission notice shall be included
  25. * in all copies or substantial portions of the Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  28. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  32. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  33. * DEALINGS IN THE SOFTWARE.
  34. ******************************************************************************
  35. *
  36. * $Log: safileio.c,v $
  37. * Revision 1.4 2008/01/16 20:05:14 bram
  38. * Add file hooks that accept UTF-8 encoded filenames on some platforms. Use SASetupUtf8Hooks
  39. * tosetup the hooks and check SHPAPI_UTF8_HOOKS for its availability. Currently, this
  40. * is only available on the Windows platform that decodes the UTF-8 filenames to wide
  41. * character strings and feeds them to _wfopen and _wremove.
  42. *
  43. * Revision 1.3 2007/12/18 18:28:11 bram
  44. * - create hook for client specific atof (bugzilla ticket 1615)
  45. * - check for NULL handle before closing cpCPG file, and close after reading.
  46. *
  47. * Revision 1.2 2007/12/15 20:25:30 bram
  48. * dbfopen.c now reads the Code Page information from the DBF file, and exports
  49. * this information as a string through the DBFGetCodePage function. This is
  50. * either the number from the LDID header field ("LDID/<number>") or as the
  51. * content of an accompanying .CPG file. When creating a DBF file, the code can
  52. * be set using DBFCreateEx.
  53. *
  54. * Revision 1.1 2007/12/06 06:56:41 fwarmerdam
  55. * new
  56. *
  57. */
  58. #include "shapefil.h"
  59. #include <math.h>
  60. #include <limits.h>
  61. #include <assert.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. #include <stdio.h>
  65. SHP_CVSID("$Id: safileio.c,v 1.4 2008/01/16 20:05:14 bram Exp $");
  66. #ifdef SHPAPI_UTF8_HOOKS
  67. # ifdef SHPAPI_WINDOWS
  68. # define WIN32_LEAN_AND_MEAN
  69. # define NOMINMAX
  70. # include <windows.h>
  71. # pragma comment(lib, "kernel32.lib")
  72. # endif
  73. #endif
  74. /************************************************************************/
  75. /* SADFOpen() */
  76. /************************************************************************/
  77. SAFile SADFOpen( const char *pszFilename, const char *pszAccess )
  78. {
  79. return (SAFile) fopen( pszFilename, pszAccess );
  80. }
  81. /************************************************************************/
  82. /* SADFRead() */
  83. /************************************************************************/
  84. SAOffset SADFRead( void *p, SAOffset size, SAOffset nmemb, SAFile file )
  85. {
  86. return (SAOffset) fread( p, (size_t) size, (size_t) nmemb,
  87. (FILE *) file );
  88. }
  89. /************************************************************************/
  90. /* SADFWrite() */
  91. /************************************************************************/
  92. SAOffset SADFWrite( void *p, SAOffset size, SAOffset nmemb, SAFile file )
  93. {
  94. return (SAOffset) fwrite( p, (size_t) size, (size_t) nmemb,
  95. (FILE *) file );
  96. }
  97. /************************************************************************/
  98. /* SADFSeek() */
  99. /************************************************************************/
  100. SAOffset SADFSeek( SAFile file, SAOffset offset, int whence )
  101. {
  102. return (SAOffset) fseek( (FILE *) file, (long) offset, whence );
  103. }
  104. /************************************************************************/
  105. /* SADFTell() */
  106. /************************************************************************/
  107. SAOffset SADFTell( SAFile file )
  108. {
  109. return (SAOffset) ftell( (FILE *) file );
  110. }
  111. /************************************************************************/
  112. /* SADFFlush() */
  113. /************************************************************************/
  114. int SADFFlush( SAFile file )
  115. {
  116. return fflush( (FILE *) file );
  117. }
  118. /************************************************************************/
  119. /* SADFClose() */
  120. /************************************************************************/
  121. int SADFClose( SAFile file )
  122. {
  123. return fclose( (FILE *) file );
  124. }
  125. /************************************************************************/
  126. /* SADFClose() */
  127. /************************************************************************/
  128. int SADRemove( const char *filename )
  129. {
  130. return remove( filename );
  131. }
  132. /************************************************************************/
  133. /* SADError() */
  134. /************************************************************************/
  135. void SADError( const char *message )
  136. {
  137. fprintf( stderr, "%s\n", message );
  138. }
  139. /************************************************************************/
  140. /* SASetupDefaultHooks() */
  141. /************************************************************************/
  142. void SASetupDefaultHooks( SAHooks *psHooks )
  143. {
  144. psHooks->FOpen = SADFOpen;
  145. psHooks->FRead = SADFRead;
  146. psHooks->FWrite = SADFWrite;
  147. psHooks->FSeek = SADFSeek;
  148. psHooks->FTell = SADFTell;
  149. psHooks->FFlush = SADFFlush;
  150. psHooks->FClose = SADFClose;
  151. psHooks->Error = SADError;
  152. }
  153. #ifdef SHPAPI_WINDOWS
  154. /************************************************************************/
  155. /* Utf8ToWideChar */
  156. /************************************************************************/
  157. const wchar_t* Utf8ToWideChar( const char *pszFilename )
  158. {
  159. int nMulti, nWide;
  160. wchar_t *pwszFileName;
  161. nMulti = strlen(pszFilename) + 1;
  162. nWide = MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, 0, 0);
  163. if( nWide == 0 )
  164. {
  165. return NULL;
  166. }
  167. pwszFileName = (wchar_t*) malloc(nWide * sizeof(wchar_t));
  168. if ( pwszFileName == NULL )
  169. {
  170. return NULL;
  171. }
  172. if( MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, pwszFileName, nWide ) == 0 )
  173. {
  174. free( pwszFileName );
  175. return NULL;
  176. }
  177. return pwszFileName;
  178. }
  179. /************************************************************************/
  180. /* SAUtf8WFOpen */
  181. /************************************************************************/
  182. SAFile SAUtf8WFOpen( const char *pszFilename, const char *pszAccess )
  183. {
  184. SAFile file = NULL;
  185. const wchar_t *pwszFileName, *pwszAccess;
  186. pwszFileName = Utf8ToWideChar( pszFilename );
  187. pwszAccess = Utf8ToWideChar( pszAccess );
  188. if( pwszFileName != NULL && pwszFileName != NULL)
  189. {
  190. file = (SAFile) _wfopen( pwszFileName, pwszAccess );
  191. }
  192. free ((wchar_t*) pwszFileName);
  193. free ((wchar_t*) pwszAccess);
  194. return file;
  195. }
  196. /************************************************************************/
  197. /* SAUtf8WRemove() */
  198. /************************************************************************/
  199. int SAUtf8WRemove( const char *pszFilename )
  200. {
  201. const wchar_t *pwszFileName = Utf8ToWideChar( pszFilename );
  202. int rc = -1;
  203. if( pwszFileName != NULL )
  204. {
  205. rc = _wremove( pwszFileName );
  206. }
  207. free ((wchar_t*) pwszFileName);
  208. return rc;
  209. }
  210. #endif
  211. #ifdef SHPAPI_UTF8_HOOKS
  212. /************************************************************************/
  213. /* SASetupUtf8Hooks() */
  214. /************************************************************************/
  215. void SASetupUtf8Hooks( SAHooks *psHooks )
  216. {
  217. #ifdef SHPAPI_WINDOWS
  218. psHooks->FOpen = SAUtf8WFOpen;
  219. psHooks->Remove = SAUtf8WRemove;
  220. #else
  221. # error "no implementations of UTF-8 hooks available for this platform"
  222. #endif
  223. psHooks->FRead = SADFRead;
  224. psHooks->FWrite = SADFWrite;
  225. psHooks->FSeek = SADFSeek;
  226. psHooks->FTell = SADFTell;
  227. psHooks->FFlush = SADFFlush;
  228. psHooks->FClose = SADFClose;
  229. psHooks->Error = SADError;
  230. psHooks->Atof = atof;
  231. }
  232. #endif