PostGIS  2.5.0beta2dev-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  return (SAOffset) fwrite( p, (size_t) size, (size_t) nmemb,
119  (FILE *) file );
120 }
121 
122 /************************************************************************/
123 /* SADFSeek() */
124 /************************************************************************/
125 
126 SAOffset SADFSeek( SAFile file, SAOffset offset, int whence )
127 
128 {
129 #ifdef HAVE_FSEEKO
130  return (SAOffset) fseeko( (FILE *) file, (off_t) offset, whence );
131 #else
132  return (SAOffset) fseek( (FILE *) file, (long) offset, whence );
133 #endif
134 }
135 
136 /************************************************************************/
137 /* SADFTell() */
138 /************************************************************************/
139 
141 
142 {
143 #ifdef HAVE_FSEEKO
144  return (SAOffset) ftello( (FILE *) file );
145 #else
146  return (SAOffset) ftell( (FILE *) file );
147 #endif
148 }
149 
150 /************************************************************************/
151 /* SADFFlush() */
152 /************************************************************************/
153 
154 int SADFFlush( SAFile file )
155 
156 {
157  return fflush( (FILE *) file );
158 }
159 
160 /************************************************************************/
161 /* SADFClose() */
162 /************************************************************************/
163 
164 int SADFClose( SAFile file )
165 
166 {
167  return fclose( (FILE *) file );
168 }
169 
170 /************************************************************************/
171 /* SADFClose() */
172 /************************************************************************/
173 
174 int SADRemove( const char *filename )
175 
176 {
177  return remove( filename );
178 }
179 
180 /************************************************************************/
181 /* SADError() */
182 /************************************************************************/
183 
184 void SADError( const char *message )
185 
186 {
187  fprintf( stderr, "%s\n", message );
188 }
189 
190 /************************************************************************/
191 /* SASetupDefaultHooks() */
192 /************************************************************************/
193 
194 void SASetupDefaultHooks( SAHooks *psHooks )
195 
196 {
197  psHooks->FOpen = SADFOpen;
198  psHooks->FRead = SADFRead;
199  psHooks->FWrite = SADFWrite;
200  psHooks->FSeek = SADFSeek;
201  psHooks->FTell = SADFTell;
202  psHooks->FFlush = SADFFlush;
203  psHooks->FClose = SADFClose;
204  psHooks->Remove = SADRemove;
205 
206  psHooks->Error = SADError;
207  psHooks->Atof = atof;
208 }
209 
210 
211 
212 
213 #ifdef SHPAPI_WINDOWS
214 
215 /************************************************************************/
216 /* Utf8ToWideChar */
217 /************************************************************************/
218 
219 const wchar_t* Utf8ToWideChar( const char *pszFilename )
220 {
221  int nMulti, nWide;
222  wchar_t *pwszFileName;
223 
224  nMulti = strlen(pszFilename) + 1;
225  nWide = MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, 0, 0);
226  if( nWide == 0 )
227  {
228  return NULL;
229  }
230  pwszFileName = (wchar_t*) malloc(nWide * sizeof(wchar_t));
231  if ( pwszFileName == NULL )
232  {
233  return NULL;
234  }
235  if( MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, pwszFileName, nWide ) == 0 )
236  {
237  free( pwszFileName );
238  return NULL;
239  }
240  return pwszFileName;
241 }
242 
243 /************************************************************************/
244 /* SAUtf8WFOpen */
245 /************************************************************************/
246 
247 SAFile SAUtf8WFOpen( const char *pszFilename, const char *pszAccess )
248 {
249  SAFile file = NULL;
250  const wchar_t *pwszFileName, *pwszAccess;
251  pwszFileName = Utf8ToWideChar( pszFilename );
252  pwszAccess = Utf8ToWideChar( pszAccess );
253  if( pwszFileName != NULL && pwszFileName != NULL)
254  {
255  file = (SAFile) _wfopen( pwszFileName, pwszAccess );
256  }
257  free ((wchar_t*) pwszFileName);
258  free ((wchar_t*) pwszAccess);
259  return file;
260 }
261 
262 /************************************************************************/
263 /* SAUtf8WRemove() */
264 /************************************************************************/
265 
266 int SAUtf8WRemove( const char *pszFilename )
267 {
268  const wchar_t *pwszFileName = Utf8ToWideChar( pszFilename );
269  int rc = -1;
270  if( pwszFileName != NULL )
271  {
272  rc = _wremove( pwszFileName );
273  }
274  free ((wchar_t*) pwszFileName);
275  return rc;
276 }
277 
278 #endif
279 
280 #ifdef SHPAPI_UTF8_HOOKS
281 
282 /************************************************************************/
283 /* SASetupUtf8Hooks() */
284 /************************************************************************/
285 
286 void SASetupUtf8Hooks( SAHooks *psHooks )
287 {
288 #ifdef SHPAPI_WINDOWS
289  psHooks->FOpen = SAUtf8WFOpen;
290  psHooks->Remove = SAUtf8WRemove;
291 #else
292 # error "no implementations of UTF-8 hooks available for this platform"
293 #endif
294  psHooks->FRead = SADFRead;
295  psHooks->FWrite = SADFWrite;
296  psHooks->FSeek = SADFSeek;
297  psHooks->FTell = SADFTell;
298  psHooks->FFlush = SADFFlush;
299  psHooks->FClose = SADFClose;
300 
301  psHooks->Error = SADError;
302  psHooks->Atof = atof;
303 }
304 
305 #endif
SAFile SADFOpen(const char *pszFilename, const char *pszAccess)
Definition: safileio.c:94
SAOffset SADFTell(SAFile file)
Definition: safileio.c:140
SAOffset(* FTell)(SAFile file)
Definition: shapefil.h:259
int(* FFlush)(SAFile file)
Definition: shapefil.h:260
SAOffset SADFSeek(SAFile file, SAOffset offset, int whence)
Definition: safileio.c:126
unsigned long SAOffset
Definition: shapefil.h:250
int(* FClose)(SAFile file)
Definition: shapefil.h:261
void SADError(const char *message)
Definition: safileio.c:184
#define SHP_CVSID(string)
Definition: shapefil.h:223
SAOffset SADFWrite(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: safileio.c:115
int SADFFlush(SAFile file)
Definition: safileio.c:154
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:257
int SADFClose(SAFile file)
Definition: safileio.c:164
int(* Remove)(const char *filename)
Definition: shapefil.h:262
int SADRemove(const char *filename)
Definition: safileio.c:174
double(* Atof)(const char *str)
Definition: shapefil.h:265
SAOffset(* FSeek)(SAFile file, SAOffset offset, int whence)
Definition: shapefil.h:258
void free(void *)
void * malloc(YYSIZE_T)
int * SAFile
Definition: shapefil.h:242
SAFile(* FOpen)(const char *filename, const char *access)
Definition: shapefil.h:255
void(* Error)(const char *message)
Definition: shapefil.h:264
SAOffset(* FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:256
SAOffset SADFRead(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: safileio.c:104
void SASetupDefaultHooks(SAHooks *psHooks)
Definition: safileio.c:194