PostGIS  2.1.10dev-r@@SVN_REVISION@@
safileio.c
Go to the documentation of this file.
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 
59 #include "shapefil.h"
60 
61 #include <math.h>
62 #include <limits.h>
63 #include <assert.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <stdio.h>
67 
68 SHP_CVSID("$Id: safileio.c,v 1.4 2008-01-16 20:05:14 bram Exp $")
69 
70 #ifdef SHPAPI_UTF8_HOOKS
71 # ifdef SHPAPI_WINDOWS
72 # define WIN32_LEAN_AND_MEAN
73 # define NOMINMAX
74 # include <windows.h>
75 # pragma comment(lib, "kernel32.lib")
76 # endif
77 #endif
78 
79 /* Local prototypes */
80 SAFile SADFOpen( const char *pszFilename, const char *pszAccess );
81 SAOffset SADFRead( void *p, SAOffset size, SAOffset nmemb, SAFile file );
82 SAOffset SADFWrite( void *p, SAOffset size, SAOffset nmemb, SAFile file );
83 SAOffset SADFSeek( SAFile file, SAOffset offset, int whence );
84 SAOffset SADFTell( SAFile file );
85 int SADFFlush( SAFile file );
86 int SADFClose( SAFile file );
87 int SADRemove( const char *filename );
88 void SADError( const char *message );
89 
90 
91 /************************************************************************/
92 /* SADFOpen() */
93 /************************************************************************/
94 
95 SAFile SADFOpen( const char *pszFilename, const char *pszAccess )
96 
97 {
98  return (SAFile) fopen( pszFilename, pszAccess );
99 }
100 
101 /************************************************************************/
102 /* SADFRead() */
103 /************************************************************************/
104 
105 SAOffset SADFRead( void *p, SAOffset size, SAOffset nmemb, SAFile file )
106 
107 {
108  return (SAOffset) fread( p, (size_t) size, (size_t) nmemb,
109  (FILE *) file );
110 }
111 
112 /************************************************************************/
113 /* SADFWrite() */
114 /************************************************************************/
115 
116 SAOffset SADFWrite( void *p, SAOffset size, SAOffset nmemb, SAFile file )
117 
118 {
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
SAFile SADFOpen(const char *pszFilename, const char *pszAccess)
Definition: safileio.c:95
SAOffset SADFTell(SAFile file)
Definition: safileio.c:141
SAOffset(* FTell)(SAFile file)
Definition: shapefil.h:260
int(* FFlush)(SAFile file)
Definition: shapefil.h:261
SAOffset SADFSeek(SAFile file, SAOffset offset, int whence)
Definition: safileio.c:127
unsigned long SAOffset
Definition: shapefil.h:251
int(* FClose)(SAFile file)
Definition: shapefil.h:262
void SADError(const char *message)
Definition: safileio.c:185
#define SHP_CVSID(string)
Definition: shapefil.h:224
SAOffset SADFWrite(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: safileio.c:116
int SADFFlush(SAFile file)
Definition: safileio.c:155
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:258
int SADFClose(SAFile file)
Definition: safileio.c:165
int(* Remove)(const char *filename)
Definition: shapefil.h:263
int SADRemove(const char *filename)
Definition: safileio.c:175
double(* Atof)(const char *str)
Definition: shapefil.h:266
SAOffset(* FSeek)(SAFile file, SAOffset offset, int whence)
Definition: shapefil.h:259
int * SAFile
Definition: shapefil.h:243
SAFile(* FOpen)(const char *filename, const char *access)
Definition: shapefil.h:256
void(* Error)(const char *message)
Definition: shapefil.h:265
SAOffset(* FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:257
SAOffset SADFRead(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: safileio.c:105
void SASetupDefaultHooks(SAHooks *psHooks)
Definition: safileio.c:195