PostGIS  3.4.0dev-r@@SVN_REVISION@@
safileio.c
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id$
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 COPYING). 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$
37  * Revision 1.6 2018-06-15 19:56:32 erouault
38  * * safileio.c: remove duplicate test. Patch by Jaroslav Fojtik.
39  * Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2744
40  *
41  * Revision 1.5 2016-12-05 12:44:05 erouault
42  * * Major overhaul of Makefile build system to use autoconf/automake.
43  *
44  * * Warning fixes in contrib/
45  *
46  * Revision 1.4 2008-01-16 20:05:14 bram
47  * Add file hooks that accept UTF-8 encoded filenames on some platforms. Use SASetupUtf8Hooks
48  * tosetup the hooks and check SHPAPI_UTF8_HOOKS for its availability. Currently, this
49  * is only available on the Windows platform that decodes the UTF-8 filenames to wide
50  * character strings and feeds them to _wfopen and _wremove.
51  *
52  * Revision 1.3 2007/12/18 18:28:11 bram
53  * - create hook for client specific atof (bugzilla ticket 1615)
54  * - check for NULL handle before closing cpCPG file, and close after reading.
55  *
56  * Revision 1.2 2007/12/15 20:25:30 bram
57  * dbfopen.c now reads the Code Page information from the DBF file, and exports
58  * this information as a string through the DBFGetCodePage function. This is
59  * either the number from the LDID header field ("LDID/<number>") or as the
60  * content of an accompanying .CPG file. When creating a DBF file, the code can
61  * be set using DBFCreateEx.
62  *
63  * Revision 1.1 2007/12/06 06:56:41 fwarmerdam
64  * new
65  *
66  */
67 
68 #include "shapefil.h"
69 
70 #include <math.h>
71 #include <limits.h>
72 #include <assert.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <stdio.h>
76 
77 SHP_CVSID("$Id$");
78 
79 #ifdef SHPAPI_UTF8_HOOKS
80 # ifdef SHPAPI_WINDOWS
81 # define WIN32_LEAN_AND_MEAN
82 # define NOMINMAX
83 # include <windows.h>
84 # pragma comment(lib, "kernel32.lib")
85 # endif
86 #endif
87 
88 /************************************************************************/
89 /* SADFOpen() */
90 /************************************************************************/
91 
92 static
93 SAFile SADFOpen( const char *pszFilename, const char *pszAccess )
94 
95 {
96  return (SAFile) fopen( pszFilename, pszAccess );
97 }
98 
99 /************************************************************************/
100 /* SADFRead() */
101 /************************************************************************/
102 
103 static
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 static
116 SAOffset SADFWrite( void *p, SAOffset size, SAOffset nmemb, SAFile file )
117 
118 {
119  if (!nmemb || !p) return 0;
120  return (SAOffset) fwrite( p, (size_t) size, (size_t) nmemb,
121  (FILE *) file );
122 }
123 
124 /************************************************************************/
125 /* SADFSeek() */
126 /************************************************************************/
127 
128 static
129 SAOffset SADFSeek( SAFile file, SAOffset offset, int whence )
130 
131 {
132  return (SAOffset) fseek( (FILE *) file, (long) offset, whence );
133 }
134 
135 /************************************************************************/
136 /* SADFTell() */
137 /************************************************************************/
138 
139 static
141 
142 {
143  return (SAOffset) ftell( (FILE *) file );
144 }
145 
146 /************************************************************************/
147 /* SADFFlush() */
148 /************************************************************************/
149 
150 static
151 int SADFFlush( SAFile file )
152 
153 {
154  return fflush( (FILE *) file );
155 }
156 
157 /************************************************************************/
158 /* SADFClose() */
159 /************************************************************************/
160 
161 static
162 int SADFClose( SAFile file )
163 
164 {
165  return fclose( (FILE *) file );
166 }
167 
168 /************************************************************************/
169 /* SADFClose() */
170 /************************************************************************/
171 
172 static
173 int SADRemove( const char *filename )
174 
175 {
176  return remove( filename );
177 }
178 
179 /************************************************************************/
180 /* SADError() */
181 /************************************************************************/
182 
183 static
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 && pwszAccess != 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
void * malloc(YYSIZE_T)
void free(void *)
static SAOffset SADFWrite(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: safileio.c:116
static SAOffset SADFSeek(SAFile file, SAOffset offset, int whence)
Definition: safileio.c:129
static SAFile SADFOpen(const char *pszFilename, const char *pszAccess)
Definition: safileio.c:93
SHP_CVSID("$Id$")
static int SADFClose(SAFile file)
Definition: safileio.c:162
static void SADError(const char *message)
Definition: safileio.c:184
static int SADRemove(const char *filename)
Definition: safileio.c:173
void SASetupDefaultHooks(SAHooks *psHooks)
Definition: safileio.c:194
static SAOffset SADFTell(SAFile file)
Definition: safileio.c:140
static int SADFFlush(SAFile file)
Definition: safileio.c:151
static SAOffset SADFRead(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: safileio.c:104
int * SAFile
Definition: shapefil.h:283
unsigned long SAOffset
Definition: shapefil.h:286
void(* Error)(const char *message)
Definition: shapefil.h:299
SAFile(* FOpen)(const char *filename, const char *access)
Definition: shapefil.h:290
SAOffset(* FTell)(SAFile file)
Definition: shapefil.h:294
int(* FFlush)(SAFile file)
Definition: shapefil.h:295
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:292
double(* Atof)(const char *str)
Definition: shapefil.h:300
int(* Remove)(const char *filename)
Definition: shapefil.h:297
int(* FClose)(SAFile file)
Definition: shapefil.h:296
SAOffset(* FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:291
SAOffset(* FSeek)(SAFile file, SAOffset offset, int whence)
Definition: shapefil.h:293