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