Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[modules/hydro.git] / src / shapelib / safileio.c
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
93 SAOffset SADFRead( void *p, SAOffset size, SAOffset nmemb, SAFile file )
94
95 {
96     return (SAOffset) fread( p, (size_t) size, (size_t) nmemb, 
97                              (FILE *) file );
98 }
99
100 /************************************************************************/
101 /*                             SADFWrite()                              */
102 /************************************************************************/
103
104 SAOffset SADFWrite( void *p, SAOffset size, SAOffset nmemb, SAFile file )
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
125 SAOffset SADFTell( SAFile file )
126
127 {
128     return (SAOffset) ftell( (FILE *) file );
129 }
130
131 /************************************************************************/
132 /*                             SADFFlush()                              */
133 /************************************************************************/
134
135 int SADFFlush( SAFile file )
136
137 {
138     return fflush( (FILE *) file );
139 }
140
141 /************************************************************************/
142 /*                             SADFClose()                              */
143 /************************************************************************/
144
145 int SADFClose( SAFile file )
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     psHooks->Remove  = SADRemove;
186
187     psHooks->Error   = SADError;
188     psHooks->Atof    = atof;
189 }
190
191
192
193
194 #ifdef SHPAPI_WINDOWS
195
196 /************************************************************************/
197 /*                          Utf8ToWideChar                              */
198 /************************************************************************/
199
200 const wchar_t* Utf8ToWideChar( const char *pszFilename )
201 {
202     int nMulti, nWide;
203     wchar_t *pwszFileName;
204     
205     nMulti = strlen(pszFilename) + 1;
206     nWide = MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, 0, 0);
207     if( nWide == 0 )
208     {
209         return NULL;
210     }
211     pwszFileName = (wchar_t*) malloc(nWide * sizeof(wchar_t));
212     if ( pwszFileName == NULL )
213     {
214         return NULL;
215     }
216     if( MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, pwszFileName, nWide ) == 0 )
217     {
218         free( pwszFileName );
219         return NULL;
220     }
221     return pwszFileName;
222 }
223
224 /************************************************************************/
225 /*                           SAUtf8WFOpen                               */
226 /************************************************************************/
227
228 SAFile SAUtf8WFOpen( const char *pszFilename, const char *pszAccess )
229 {
230     SAFile file = NULL;
231     const wchar_t *pwszFileName, *pwszAccess;
232     pwszFileName = Utf8ToWideChar( pszFilename );
233     pwszAccess = Utf8ToWideChar( pszAccess );
234     if( pwszFileName != NULL && pwszFileName != NULL)
235     {
236         file = (SAFile) _wfopen( pwszFileName, pwszAccess );
237     }
238     free ((wchar_t*) pwszFileName);
239     free ((wchar_t*) pwszAccess);
240     return file;
241 }
242
243 /************************************************************************/
244 /*                             SAUtf8WRemove()                          */
245 /************************************************************************/
246
247 int SAUtf8WRemove( const char *pszFilename )
248 {
249     const wchar_t *pwszFileName = Utf8ToWideChar( pszFilename );
250     int rc = -1; 
251     if( pwszFileName != NULL )
252     {
253         rc = _wremove( pwszFileName );
254     }
255     free ((wchar_t*) pwszFileName);
256     return rc;
257 }
258
259 #endif
260
261 #ifdef SHPAPI_UTF8_HOOKS
262
263 /************************************************************************/
264 /*                          SASetupUtf8Hooks()                          */
265 /************************************************************************/
266
267 void SASetupUtf8Hooks( SAHooks *psHooks )
268 {
269 #ifdef SHPAPI_WINDOWS    
270     psHooks->FOpen   = SAUtf8WFOpen;
271     psHooks->Remove  = SAUtf8WRemove;
272 #else
273 #   error "no implementations of UTF-8 hooks available for this platform"
274 #endif
275     psHooks->FRead   = SADFRead;
276     psHooks->FWrite  = SADFWrite;
277     psHooks->FSeek   = SADFSeek;
278     psHooks->FTell   = SADFTell;
279     psHooks->FFlush  = SADFFlush;
280     psHooks->FClose  = SADFClose;
281
282     psHooks->Error   = SADError;
283     psHooks->Atof    = atof;
284 }
285
286 #endif