GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-9aacb948ba
safileio.c
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id: safileio.c,v 1.6 2018-06-15 19:56:32 erouault 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 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: safileio.c,v $
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
48  *SASetupUtf8Hooks tosetup the hooks and check SHPAPI_UTF8_HOOKS for its
49  *availability. Currently, this is only available on the Windows platform that
50  *decodes the UTF-8 filenames to wide character strings and feeds them to
51  *_wfopen and _wremove.
52  *
53  * Revision 1.3 2007/12/18 18:28:11 bram
54  * - create hook for client specific atof (bugzilla ticket 1615)
55  * - check for NULL handle before closing cpCPG file, and close after reading.
56  *
57  * Revision 1.2 2007/12/15 20:25:30 bram
58  * dbfopen.c now reads the Code Page information from the DBF file, and exports
59  * this information as a string through the DBFGetCodePage function. This is
60  * either the number from the LDID header field ("LDID/<number>") or as the
61  * content of an accompanying .CPG file. When creating a DBF file, the code can
62  * be set using DBFCreateEx.
63  *
64  * Revision 1.1 2007/12/06 06:56:41 fwarmerdam
65  * new
66  *
67  */
68 
69 #include "shapefil.h"
70 
71 #include <math.h>
72 #include <limits.h>
73 #include <assert.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <stdio.h>
77 
78 SHP_CVSID("$Id: safileio.c,v 1.6 2018-06-15 19:56:32 erouault Exp $")
79 
80 #ifdef SHPAPI_UTF8_HOOKS
81 #ifdef SHPAPI_WINDOWS
82 #define WIN32_LEAN_AND_MEAN
83 #define NOMINMAX
84 #include <windows.h>
85 #pragma comment(lib, "kernel32.lib")
86 #endif
87 #endif
88 
89 /************************************************************************/
90 /* SADFOpen() */
91 /************************************************************************/
92 
93 SAFile SADFOpen(const char *pszFilename, const char *pszAccess)
94 
95 {
96  return (SAFile)fopen(pszFilename, pszAccess);
97 }
98 
99 /************************************************************************/
100 /* SADFRead() */
101 /************************************************************************/
102 
104 
105 {
106  return (SAOffset)fread(p, (size_t)size, (size_t)nmemb, (FILE *)file);
107 }
108 
109 /************************************************************************/
110 /* SADFWrite() */
111 /************************************************************************/
112 
114 
115 {
116  return (SAOffset)fwrite(p, (size_t)size, (size_t)nmemb, (FILE *)file);
117 }
118 
119 /************************************************************************/
120 /* SADFSeek() */
121 /************************************************************************/
122 
123 SAOffset SADFSeek(SAFile file, SAOffset offset, int whence)
124 
125 {
126  return (SAOffset)fseek((FILE *)file, (long)offset, whence);
127 }
128 
129 /************************************************************************/
130 /* SADFTell() */
131 /************************************************************************/
132 
134 
135 {
136  return (SAOffset)ftell((FILE *)file);
137 }
138 
139 /************************************************************************/
140 /* SADFFlush() */
141 /************************************************************************/
142 
144 
145 {
146  return fflush((FILE *)file);
147 }
148 
149 /************************************************************************/
150 /* SADFClose() */
151 /************************************************************************/
152 
154 
155 {
156  return fclose((FILE *)file);
157 }
158 
159 /************************************************************************/
160 /* SADFClose() */
161 /************************************************************************/
162 
163 int SADRemove(const char *filename)
164 
165 {
166  return remove(filename);
167 }
168 
169 /************************************************************************/
170 /* SADError() */
171 /************************************************************************/
172 
173 void SADError(const char *message)
174 
175 {
176  fprintf(stderr, "%s\n", message);
177 }
178 
179 /************************************************************************/
180 /* SASetupDefaultHooks() */
181 /************************************************************************/
182 
184 
185 {
186  psHooks->FOpen = SADFOpen;
187  psHooks->FRead = SADFRead;
188  psHooks->FWrite = SADFWrite;
189  psHooks->FSeek = SADFSeek;
190  psHooks->FTell = SADFTell;
191  psHooks->FFlush = SADFFlush;
192  psHooks->FClose = SADFClose;
193  psHooks->Remove = SADRemove;
194 
195  psHooks->Error = SADError;
196  psHooks->Atof = atof;
197 }
198 
199 #ifdef SHPAPI_WINDOWS
200 
201 /************************************************************************/
202 /* Utf8ToWideChar */
203 /************************************************************************/
204 
205 const wchar_t *Utf8ToWideChar(const char *pszFilename)
206 {
207  int nMulti, nWide;
208  wchar_t *pwszFileName;
209 
210  nMulti = strlen(pszFilename) + 1;
211  nWide = MultiByteToWideChar(CP_UTF8, 0, pszFilename, nMulti, 0, 0);
212  if (nWide == 0) {
213  return NULL;
214  }
215  pwszFileName = (wchar_t *)malloc(nWide * sizeof(wchar_t));
216  if (pwszFileName == NULL) {
217  return NULL;
218  }
219  if (MultiByteToWideChar(CP_UTF8, 0, pszFilename, nMulti, pwszFileName,
220  nWide) == 0) {
221  free(pwszFileName);
222  return NULL;
223  }
224  return pwszFileName;
225 }
226 
227 /************************************************************************/
228 /* SAUtf8WFOpen */
229 /************************************************************************/
230 
231 SAFile SAUtf8WFOpen(const char *pszFilename, const char *pszAccess)
232 {
233  SAFile file = NULL;
234  const wchar_t *pwszFileName, *pwszAccess;
235  pwszFileName = Utf8ToWideChar(pszFilename);
236  pwszAccess = Utf8ToWideChar(pszAccess);
237  if (pwszFileName != NULL && pwszAccess != NULL) {
238  file = (SAFile)_wfopen(pwszFileName, pwszAccess);
239  }
240  free((wchar_t *)pwszFileName);
241  free((wchar_t *)pwszAccess);
242  return file;
243 }
244 
245 /************************************************************************/
246 /* SAUtf8WRemove() */
247 /************************************************************************/
248 
249 int SAUtf8WRemove(const char *pszFilename)
250 {
251  const wchar_t *pwszFileName = Utf8ToWideChar(pszFilename);
252  int rc = -1;
253  if (pwszFileName != NULL) {
254  rc = _wremove(pwszFileName);
255  }
256  free((wchar_t *)pwszFileName);
257  return rc;
258 }
259 
260 #endif
261 
262 #ifdef SHPAPI_UTF8_HOOKS
263 
264 /************************************************************************/
265 /* SASetupUtf8Hooks() */
266 /************************************************************************/
267 
268 void SASetupUtf8Hooks(SAHooks *psHooks)
269 {
270 #ifdef SHPAPI_WINDOWS
271  psHooks->FOpen = SAUtf8WFOpen;
272  psHooks->Remove = SAUtf8WRemove;
273 #else
274 #error "no implementations of UTF-8 hooks available for this platform"
275 #endif
276  psHooks->FRead = SADFRead;
277  psHooks->FWrite = SADFWrite;
278  psHooks->FSeek = SADFSeek;
279  psHooks->FTell = SADFTell;
280  psHooks->FFlush = SADFFlush;
281  psHooks->FClose = SADFClose;
282 
283  psHooks->Error = SADError;
284  psHooks->Atof = atof;
285 }
286 
287 #endif
#define NULL
Definition: ccmath.h:32
#define file
int SADFClose(SAFile file)
Definition: safileio.c:153
SAOffset SADFTell(SAFile file)
Definition: safileio.c:133
SAOffset SADFWrite(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: safileio.c:113
int SADRemove(const char *filename)
Definition: safileio.c:163
SAOffset SADFRead(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: safileio.c:103
SAFile SADFOpen(const char *pszFilename, const char *pszAccess)
Definition: safileio.c:93
void SASetupDefaultHooks(SAHooks *psHooks)
Definition: safileio.c:183
int SADFFlush(SAFile file)
Definition: safileio.c:143
SAOffset SADFSeek(SAFile file, SAOffset offset, int whence)
Definition: safileio.c:123
void SADError(const char *message)
Definition: safileio.c:173
#define SHP_CVSID(string)
Definition: shapefil.h:125
int * SAFile
Definition: shapefil.h:148
unsigned long SAOffset
Definition: shapefil.h:151
void * malloc(YYSIZE_T)
void free(void *)
void(* Error)(const char *message)
Definition: shapefil.h:164
SAFile(* FOpen)(const char *filename, const char *access)
Definition: shapefil.h:155
SAOffset(* FTell)(SAFile file)
Definition: shapefil.h:159
int(* FFlush)(SAFile file)
Definition: shapefil.h:160
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:157
double(* Atof)(const char *str)
Definition: shapefil.h:165
int(* Remove)(const char *filename)
Definition: shapefil.h:162
int(* FClose)(SAFile file)
Definition: shapefil.h:161
SAOffset(* FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:156
SAOffset(* FSeek)(SAFile file, SAOffset offset, int whence)
Definition: shapefil.h:158