1 /******************************************************************************
2 * $Id: safileio.c,v 1.4 2008-01-16 20:05:14 bram Exp $
5 * Purpose: Default implementation of file io based on stdio.
6 * Author: Frank Warmerdam, warmerdam@pobox.com
8 ******************************************************************************
9 * Copyright (c) 2007, Frank Warmerdam
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.
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:
24 * The above copyright notice and this permission notice shall be included
25 * in all copies or substantial portions of the Software.
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 ******************************************************************************
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.
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.
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.
54 * Revision 1.1 2007/12/06 06:56:41 fwarmerdam
68 SHP_CVSID("$Id: safileio.c,v 1.4 2008-01-16 20:05:14 bram Exp $");
70 #ifdef SHPAPI_UTF8_HOOKS
71 # ifdef SHPAPI_WINDOWS
72 # define WIN32_LEAN_AND_MEAN
75 # pragma comment(lib, "kernel32.lib")
79 /************************************************************************/
81 /************************************************************************/
83 SAFile SADFOpen( const char *pszFilename, const char *pszAccess )
86 return (SAFile) fopen( pszFilename, pszAccess );
89 /************************************************************************/
91 /************************************************************************/
93 SAOffset SADFRead( void *p, SAOffset size, SAOffset nmemb, SAFile file )
96 return (SAOffset) fread( p, (size_t) size, (size_t) nmemb,
100 /************************************************************************/
102 /************************************************************************/
104 SAOffset SADFWrite( void *p, SAOffset size, SAOffset nmemb, SAFile file )
107 return (SAOffset) fwrite( p, (size_t) size, (size_t) nmemb,
111 /************************************************************************/
113 /************************************************************************/
115 SAOffset SADFSeek( SAFile file, SAOffset offset, int whence )
118 return (SAOffset) fseek( (FILE *) file, (long) offset, whence );
121 /************************************************************************/
123 /************************************************************************/
125 SAOffset SADFTell( SAFile file )
128 return (SAOffset) ftell( (FILE *) file );
131 /************************************************************************/
133 /************************************************************************/
135 int SADFFlush( SAFile file )
138 return fflush( (FILE *) file );
141 /************************************************************************/
143 /************************************************************************/
145 int SADFClose( SAFile file )
148 return fclose( (FILE *) file );
151 /************************************************************************/
153 /************************************************************************/
155 int SADRemove( const char *filename )
158 return remove( filename );
161 /************************************************************************/
163 /************************************************************************/
165 void SADError( const char *message )
168 fprintf( stderr, "%s\n", message );
171 /************************************************************************/
172 /* SASetupDefaultHooks() */
173 /************************************************************************/
175 void SASetupDefaultHooks( SAHooks *psHooks )
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 psHooks->Remove = SADRemove;
187 psHooks->Error = SADError;
188 psHooks->Atof = atof;
194 #ifdef SHPAPI_WINDOWS
196 /************************************************************************/
198 /************************************************************************/
200 const wchar_t* Utf8ToWideChar( const char *pszFilename )
203 wchar_t *pwszFileName;
205 nMulti = strlen(pszFilename) + 1;
206 nWide = MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, 0, 0);
211 pwszFileName = (wchar_t*) malloc(nWide * sizeof(wchar_t));
212 if ( pwszFileName == NULL )
216 if( MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, pwszFileName, nWide ) == 0 )
218 free( pwszFileName );
224 /************************************************************************/
226 /************************************************************************/
228 SAFile SAUtf8WFOpen( const char *pszFilename, const char *pszAccess )
231 const wchar_t *pwszFileName, *pwszAccess;
232 pwszFileName = Utf8ToWideChar( pszFilename );
233 pwszAccess = Utf8ToWideChar( pszAccess );
234 if( pwszFileName != NULL && pwszFileName != NULL)
236 file = (SAFile) _wfopen( pwszFileName, pwszAccess );
238 free ((wchar_t*) pwszFileName);
239 free ((wchar_t*) pwszAccess);
243 /************************************************************************/
244 /* SAUtf8WRemove() */
245 /************************************************************************/
247 int SAUtf8WRemove( const char *pszFilename )
249 const wchar_t *pwszFileName = Utf8ToWideChar( pszFilename );
251 if( pwszFileName != NULL )
253 rc = _wremove( pwszFileName );
255 free ((wchar_t*) pwszFileName);
261 #ifdef SHPAPI_UTF8_HOOKS
263 /************************************************************************/
264 /* SASetupUtf8Hooks() */
265 /************************************************************************/
267 void SASetupUtf8Hooks( SAHooks *psHooks )
269 #ifdef SHPAPI_WINDOWS
270 psHooks->FOpen = SAUtf8WFOpen;
271 psHooks->Remove = SAUtf8WRemove;
273 # error "no implementations of UTF-8 hooks available for this platform"
275 psHooks->FRead = SADFRead;
276 psHooks->FWrite = SADFWrite;
277 psHooks->FSeek = SADFSeek;
278 psHooks->FTell = SADFTell;
279 psHooks->FFlush = SADFFlush;
280 psHooks->FClose = SADFClose;
282 psHooks->Error = SADError;
283 psHooks->Atof = atof;