GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 /************************************************************************/
80 /* SADFOpen() */
81 /************************************************************************/
82 
83 SAFile SADFOpen( const char *pszFilename, const char *pszAccess )
84 
85 {
86  return (SAFile) fopen( pszFilename, pszAccess );
87 }
88 
89 /************************************************************************/
90 /* SADFRead() */
91 /************************************************************************/
92 
94 
95 {
96  return (SAOffset) fread( p, (size_t) size, (size_t) nmemb,
97  (FILE *) file );
98 }
99 
100 /************************************************************************/
101 /* SADFWrite() */
102 /************************************************************************/
103 
105 
106 {
107  return (SAOffset) fwrite( p, (size_t) size, (size_t) nmemb,
108  (FILE *) file );
109 }
110 
111 /************************************************************************/
112 /* SADFSeek() */
113 /************************************************************************/
114 
115 SAOffset SADFSeek( SAFile file, SAOffset offset, int whence )
116 
117 {
118  return (SAOffset) fseek( (FILE *) file, (long) offset, whence );
119 }
120 
121 /************************************************************************/
122 /* SADFTell() */
123 /************************************************************************/
124 
126 
127 {
128  return (SAOffset) ftell( (FILE *) file );
129 }
130 
131 /************************************************************************/
132 /* SADFFlush() */
133 /************************************************************************/
134 
136 
137 {
138  return fflush( (FILE *) file );
139 }
140 
141 /************************************************************************/
142 /* SADFClose() */
143 /************************************************************************/
144 
146 
147 {
148  return fclose( (FILE *) file );
149 }
150 
151 /************************************************************************/
152 /* SADFClose() */
153 /************************************************************************/
154 
155 int SADRemove( const char *filename )
156 
157 {
158  return remove( filename );
159 }
160 
161 /************************************************************************/
162 /* SADError() */
163 /************************************************************************/
164 
165 void SADError( const char *message )
166 
167 {
168  fprintf( stderr, "%s\n", message );
169 }
170 
171 /************************************************************************/
172 /* SASetupDefaultHooks() */
173 /************************************************************************/
174 
175 void SASetupDefaultHooks( SAHooks *psHooks )
176 
177 {
178  psHooks->FOpen = SADFOpen;
179  psHooks->FRead = SADFRead;
180  psHooks->FWrite = SADFWrite;
181  psHooks->FSeek = SADFSeek;
182  psHooks->FTell = SADFTell;
183  psHooks->FFlush = SADFFlush;
184  psHooks->FClose = SADFClose;
185 
186  psHooks->Error = SADError;
187 }
188 
189 
190 
191 
192 #ifdef SHPAPI_WINDOWS
193 
194 /************************************************************************/
195 /* Utf8ToWideChar */
196 /************************************************************************/
197 
198 const wchar_t* Utf8ToWideChar( const char *pszFilename )
199 {
200  int nMulti, nWide;
201  wchar_t *pwszFileName;
202 
203  nMulti = strlen(pszFilename) + 1;
204  nWide = MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, 0, 0);
205  if( nWide == 0 )
206  {
207  return NULL;
208  }
209  pwszFileName = (wchar_t*) malloc(nWide * sizeof(wchar_t));
210  if ( pwszFileName == NULL )
211  {
212  return NULL;
213  }
214  if( MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, pwszFileName, nWide ) == 0 )
215  {
216  free( pwszFileName );
217  return NULL;
218  }
219  return pwszFileName;
220 }
221 
222 /************************************************************************/
223 /* SAUtf8WFOpen */
224 /************************************************************************/
225 
226 SAFile SAUtf8WFOpen( const char *pszFilename, const char *pszAccess )
227 {
228  SAFile file = NULL;
229  const wchar_t *pwszFileName, *pwszAccess;
230  pwszFileName = Utf8ToWideChar( pszFilename );
231  pwszAccess = Utf8ToWideChar( pszAccess );
232  if( pwszFileName != NULL && pwszFileName != NULL)
233  {
234  file = (SAFile) _wfopen( pwszFileName, pwszAccess );
235  }
236  free ((wchar_t*) pwszFileName);
237  free ((wchar_t*) pwszAccess);
238  return file;
239 }
240 
241 /************************************************************************/
242 /* SAUtf8WRemove() */
243 /************************************************************************/
244 
245 int SAUtf8WRemove( const char *pszFilename )
246 {
247  const wchar_t *pwszFileName = Utf8ToWideChar( pszFilename );
248  int rc = -1;
249  if( pwszFileName != NULL )
250  {
251  rc = _wremove( pwszFileName );
252  }
253  free ((wchar_t*) pwszFileName);
254  return rc;
255 }
256 
257 #endif
258 
259 #ifdef SHPAPI_UTF8_HOOKS
260 
261 /************************************************************************/
262 /* SASetupUtf8Hooks() */
263 /************************************************************************/
264 
265 void SASetupUtf8Hooks( SAHooks *psHooks )
266 {
267 #ifdef SHPAPI_WINDOWS
268  psHooks->FOpen = SAUtf8WFOpen;
269  psHooks->Remove = SAUtf8WRemove;
270 #else
271 # error "no implementations of UTF-8 hooks available for this platform"
272 #endif
273  psHooks->FRead = SADFRead;
274  psHooks->FWrite = SADFWrite;
275  psHooks->FSeek = SADFSeek;
276  psHooks->FTell = SADFTell;
277  psHooks->FFlush = SADFFlush;
278  psHooks->FClose = SADFClose;
279 
280  psHooks->Error = SADError;
281  psHooks->Atof = atof;
282 }
283 
284 #endif
SAOffset(* FTell)(SAFile file)
Definition: shapefil.h:203
SAFile SADFOpen(const char *pszFilename, const char *pszAccess)
Definition: safileio.c:83
SAFile(* FOpen)(const char *filename, const char *path)
Definition: shapefil.h:199
SAOffset SADFWrite(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: safileio.c:104
unsigned long SAOffset
Definition: shapefil.h:195
int * SAFile
Definition: shapefil.h:192
int(* FClose)(SAFile file)
Definition: shapefil.h:205
SAOffset(* FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:200
void SADError(const char *message)
Definition: safileio.c:165
int(* FFlush)(SAFile file)
Definition: shapefil.h:204
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
int SADFClose(SAFile file)
Definition: safileio.c:145
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:201
SHP_CVSID("$Id: safileio.c,v 1.4 2008/01/16 20:05:14 bram Exp $")
void SASetupDefaultHooks(SAHooks *psHooks)
Definition: safileio.c:175
SAOffset SADFSeek(SAFile file, SAOffset offset, int whence)
Definition: safileio.c:115
void * malloc(YYSIZE_T)
SAOffset(* FSeek)(SAFile file, SAOffset offset, int whence)
Definition: shapefil.h:202
int SADRemove(const char *filename)
Definition: safileio.c:155
void(* Error)(const char *message)
Definition: shapefil.h:207
string message
Definition: gcmd.py:274
int SADFFlush(SAFile file)
Definition: safileio.c:135
return NULL
Definition: dbfopen.c:1394
fclose(fd)
void free(void *)
#define file
SAOffset SADFRead(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: safileio.c:93
SAOffset SADFTell(SAFile file)
Definition: safileio.c:125