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