1 /******************************************************************************
2  * Copyright (c) 1998, Frank Warmerdam
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  ******************************************************************************
22  *
23  * cpl_vsisimple.cpp
24  *
25  * This is a simple implementation (direct to Posix) of the Virtual System
26  * Interface (VSI).  See gdal_vsi.h.
27  *
28  * TODO:
29  *  - add some assertions to ensure that arguments are widely legal.  For
30  *    instance validation of access strings to fopen().
31  *
32  * $Log: cpl_vsisimple.c,v $
33  * Revision 1.3  2002/04/16 21:18:15  daniel
34  * Added missing headers for rmdir() in WIN32
35  *
36  * Revision 1.2  2002/03/18 18:59:26  daniel
37  * Added VSIMkdir(), VSIRmdir() and VSIUnlink()
38  *
39  * Revision 1.1  1999/08/27 14:09:55  daniel
40  * Update CPL files
41  *
42  * Revision 1.3  1998/12/14 04:50:33  warmerda
43  * Avoid C++ comments so it will be C compilable as well.
44  *
45  * Revision 1.2  1998/12/04 21:42:57  danmo
46  * Added #ifndef WIN32 arounf #include <unistd.h>
47  *
48  * Revision 1.1  1998/12/03 18:26:03  warmerda
49  * New
50  *
51  */
52 
53 #include "cpl_vsi.h"
54 
55 /* for stat() */
56 
57 #ifndef WIN32
58 #  include <unistd.h>
59 #else
60 #  include <io.h>
61 #  include <fcntl.h>
62 #  include <direct.h>
63 #endif
64 #include <sys/stat.h>
65 
66 /************************************************************************/
67 /*                              VSIFOpen()                              */
68 /************************************************************************/
69 
VSIFOpen(const char * pszFilename,const char * pszAccess)70 FILE *VSIFOpen( const char * pszFilename, const char * pszAccess )
71 
72 {
73     return( fopen( (char *) pszFilename, (char *) pszAccess ) );
74 }
75 
76 /************************************************************************/
77 /*                             VSIFClose()                              */
78 /************************************************************************/
79 
VSIFClose(FILE * fp)80 int VSIFClose( FILE * fp )
81 
82 {
83     return( fclose(fp) );
84 }
85 
86 /************************************************************************/
87 /*                              VSIFSeek()                              */
88 /************************************************************************/
89 
VSIFSeek(FILE * fp,long nOffset,int nWhence)90 int VSIFSeek( FILE * fp, long nOffset, int nWhence )
91 
92 {
93     return( fseek( fp, nOffset, nWhence ) );
94 }
95 
96 /************************************************************************/
97 /*                              VSIFTell()                              */
98 /************************************************************************/
99 
VSIFTell(FILE * fp)100 long VSIFTell( FILE * fp )
101 
102 {
103     return( ftell( fp ) );
104 }
105 
106 /************************************************************************/
107 /*                             VSIRewind()                              */
108 /************************************************************************/
109 
VSIRewind(FILE * fp)110 void VSIRewind( FILE * fp )
111 
112 {
113     rewind( fp );
114 }
115 
116 /************************************************************************/
117 /*                              VSIFRead()                              */
118 /************************************************************************/
119 
VSIFRead(void * pBuffer,size_t nSize,size_t nCount,FILE * fp)120 size_t VSIFRead( void * pBuffer, size_t nSize, size_t nCount, FILE * fp )
121 
122 {
123     return( fread( pBuffer, nSize, nCount, fp ) );
124 }
125 
126 /************************************************************************/
127 /*                             VSIFWrite()                              */
128 /************************************************************************/
129 
VSIFWrite(void * pBuffer,size_t nSize,size_t nCount,FILE * fp)130 size_t VSIFWrite( void * pBuffer, size_t nSize, size_t nCount, FILE * fp )
131 
132 {
133     return( fwrite( pBuffer, nSize, nCount, fp ) );
134 }
135 
136 /************************************************************************/
137 /*                              VSIFGets()                              */
138 /************************************************************************/
139 
VSIFGets(char * pszBuffer,int nBufferSize,FILE * fp)140 char *VSIFGets( char *pszBuffer, int nBufferSize, FILE * fp )
141 
142 {
143     return( fgets( pszBuffer, nBufferSize, fp ) );
144 }
145 
146 /************************************************************************/
147 /*                              VSIFGetc()                              */
148 /************************************************************************/
149 
VSIFGetc(FILE * fp)150 int VSIFGetc( FILE * fp )
151 
152 {
153     return( fgetc( fp ) );
154 }
155 
156 /************************************************************************/
157 /*                             VSIUngetc()                              */
158 /************************************************************************/
159 
VSIUngetc(int c,FILE * fp)160 int VSIUngetc( int c, FILE * fp )
161 
162 {
163     return( ungetc( c, fp ) );
164 }
165 
166 /************************************************************************/
167 /*                             VSIFPrintf()                             */
168 /*                                                                      */
169 /*      This is a little more complicated than just calling             */
170 /*      fprintf() because of the variable arguments.  Instead we        */
171 /*      have to use vfprintf().                                         */
172 /************************************************************************/
173 
VSIFPrintf(FILE * fp,const char * pszFormat,...)174 int	VSIFPrintf( FILE * fp, const char * pszFormat, ... )
175 
176 {
177     va_list 	args;
178     int		nReturn;
179 
180     va_start( args, pszFormat );
181     nReturn = vfprintf( fp, pszFormat, args );
182     va_end( args );
183 
184     return( nReturn );
185 }
186 
187 /************************************************************************/
188 /*                              VSIFEof()                               */
189 /************************************************************************/
190 
VSIFEof(FILE * fp)191 int VSIFEof( FILE * fp )
192 
193 {
194     return( feof( fp ) );
195 }
196 
197 /************************************************************************/
198 /*                              VSIFPuts()                              */
199 /************************************************************************/
200 
VSIFPuts(const char * pszString,FILE * fp)201 int VSIFPuts( const char * pszString, FILE * fp )
202 
203 {
204     return fputs( pszString, fp );
205 }
206 
207 /************************************************************************/
208 /*                              VSIFPutc()                              */
209 /************************************************************************/
210 
VSIFPutc(int nChar,FILE * fp)211 int VSIFPutc( int nChar, FILE * fp )
212 
213 {
214     return( fputc( nChar, fp ) );
215 }
216 
217 /************************************************************************/
218 /*                             VSICalloc()                              */
219 /************************************************************************/
220 
VSICalloc(size_t nCount,size_t nSize)221 void *VSICalloc( size_t nCount, size_t nSize )
222 
223 {
224     return( calloc( nCount, nSize ) );
225 }
226 
227 /************************************************************************/
228 /*                             VSIMalloc()                              */
229 /************************************************************************/
230 
VSIMalloc(size_t nSize)231 void *VSIMalloc( size_t nSize )
232 
233 {
234     return( malloc( nSize ) );
235 }
236 
237 /************************************************************************/
238 /*                             VSIRealloc()                             */
239 /************************************************************************/
240 
VSIRealloc(void * pData,size_t nNewSize)241 void * VSIRealloc( void * pData, size_t nNewSize )
242 
243 {
244     return( realloc( pData, nNewSize ) );
245 }
246 
247 /************************************************************************/
248 /*                              VSIFree()                               */
249 /************************************************************************/
250 
VSIFree(void * pData)251 void VSIFree( void * pData )
252 
253 {
254     if( pData != NULL )
255         free( pData );
256 }
257 
258 /************************************************************************/
259 /*                             VSIStrdup()                              */
260 /************************************************************************/
261 
VSIStrdup(const char * pszString)262 char *VSIStrdup( const char * pszString )
263 
264 {
265     return( strdup( pszString ) );
266 }
267 
268 /************************************************************************/
269 /*                              VSIStat()                               */
270 /************************************************************************/
271 
VSIStat(const char * pszFilename,VSIStatBuf * pStatBuf)272 int VSIStat( const char * pszFilename, VSIStatBuf * pStatBuf )
273 
274 {
275     return( stat( pszFilename, pStatBuf ) );
276 }
277 
278 /************************************************************************/
279 /*                              VSIMkdir()                              */
280 /************************************************************************/
281 
VSIMkdir(const char * pszPathname,long mode)282 int VSIMkdir( const char *pszPathname, long mode )
283 
284 {
285 #ifdef WIN32
286     return mkdir( pszPathname );
287 #elif defined(macos_pre10)
288     return -1;
289 #else
290     return mkdir( pszPathname, mode );
291 #endif
292 }
293 
294 /************************************************************************/
295 /*                             VSIUnlink()                              */
296 /*************************a***********************************************/
297 
VSIUnlink(const char * pszFilename)298 int VSIUnlink( const char * pszFilename )
299 
300 {
301     return unlink( pszFilename );
302 }
303 
304 /************************************************************************/
305 /*                              VSIRmdir()                              */
306 /************************************************************************/
307 
VSIRmdir(const char * pszFilename)308 int VSIRmdir( const char * pszFilename )
309 
310 {
311     return rmdir( pszFilename );
312 }
313 
314 
315