1
/******************************************************************************
5
* Purpose: Implementation of the pj_ctx_* file api, and the default stdio
6
* based implementation.
7
* Author: Frank Warmerdam, warmerdam@pobox.com
9
******************************************************************************
10
* Copyright (c) 2013, Frank Warmerdam
12
* Permission is hereby granted, free of charge, to any person obtaining a
13
* copy of this software and associated documentation files (the "Software"),
14
* to deal in the Software without restriction, including without limitation
15
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
16
* and/or sell copies of the Software, and to permit persons to whom the
17
* Software is furnished to do so, subject to the following conditions:
19
* The above copyright notice and this permission notice shall be included
20
* in all copies or substantial portions of the Software.
22
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28
* DEALINGS IN THE SOFTWARE.
29
*****************************************************************************/
36
static PAFile pj_stdio_fopen(projCtx ctx, const char *filename,
38
static size_t pj_stdio_fread(void *buffer, size_t size, size_t nmemb,
40
static int pj_stdio_fseek(PAFile file, long offset, int whence);
41
static long pj_stdio_ftell(PAFile file);
42
static void pj_stdio_fclose(PAFile file);
44
static projFileAPI default_fileapi = {
57
/************************************************************************/
58
/* pj_get_default_fileapi() */
59
/************************************************************************/
61
projFileAPI *pj_get_default_fileapi()
63
return &default_fileapi;
66
/************************************************************************/
67
/* pj_stdio_fopen() */
68
/************************************************************************/
70
static PAFile pj_stdio_fopen(projCtx ctx, const char *filename,
76
fp = fopen(filename, access);
82
pafile = (stdio_pafile *) malloc(sizeof(stdio_pafile));
85
return (PAFile) pafile;
88
/************************************************************************/
89
/* pj_stdio_fread() */
90
/************************************************************************/
92
static size_t pj_stdio_fread(void *buffer, size_t size, size_t nmemb,
95
stdio_pafile *pafile = (stdio_pafile *) file;
96
return fread(buffer, size, nmemb, pafile->fp);
99
/************************************************************************/
100
/* pj_stdio_fseek() */
101
/************************************************************************/
102
static int pj_stdio_fseek(PAFile file, long offset, int whence)
104
stdio_pafile *pafile = (stdio_pafile *) file;
105
return fseek(pafile->fp, offset, whence);
108
/************************************************************************/
109
/* pj_stdio_ftell() */
110
/************************************************************************/
111
static long pj_stdio_ftell(PAFile file)
113
stdio_pafile *pafile = (stdio_pafile *) file;
114
return ftell(pafile->fp);
117
/************************************************************************/
118
/* pj_stdio_fclose() */
119
/************************************************************************/
120
static void pj_stdio_fclose(PAFile file)
122
stdio_pafile *pafile = (stdio_pafile *) file;
127
/************************************************************************/
130
/* Open a file using the provided file io hooks. */
131
/************************************************************************/
133
PAFile pj_ctx_fopen(projCtx ctx, const char *filename, const char *access)
135
return ctx->fileapi->FOpen(ctx, filename, access);
138
/************************************************************************/
140
/************************************************************************/
141
size_t pj_ctx_fread(projCtx ctx, void *buffer, size_t size, size_t nmemb, PAFile file)
143
return ctx->fileapi->FRead(buffer, size, nmemb, file);
146
/************************************************************************/
148
/************************************************************************/
149
int pj_ctx_fseek(projCtx ctx, PAFile file, long offset, int whence)
151
return ctx->fileapi->FSeek(file, offset, whence);
154
/************************************************************************/
156
/************************************************************************/
157
long pj_ctx_ftell(projCtx ctx, PAFile file)
159
return ctx->fileapi->FTell(file);
162
/************************************************************************/
163
/* pj_ctx_fclose() */
164
/************************************************************************/
165
void pj_ctx_fclose(projCtx ctx, PAFile file)
167
ctx->fileapi->FClose(file);
170
/************************************************************************/
173
/* A not very optimal implementation of fgets on top of */
174
/* fread(). If we end up using this a lot more care should be */
176
/************************************************************************/
178
char *pj_ctx_fgets(projCtx ctx, char *line, int size, PAFile file)
180
long start = pj_ctx_ftell(ctx, file);
185
bytes_read = pj_ctx_fread(ctx, line, 1, size-1, file);
188
if(bytes_read < size)
190
line[bytes_read] = '\0';
193
for( i = 0; i < size-2; i++)
198
pj_ctx_fseek(ctx, file, start + i + 1, SEEK_SET);