~ubuntu-branches/ubuntu/wily/proj/wily

« back to all changes in this revision

Viewing changes to src/pj_fileapi.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-05-03 21:15:10 UTC
  • mfrom: (12.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20150503211510-35jung6npvio2e5k
Tags: 4.9.1-1
* Move from experimental to unstable.
* Use jquery.js from libjs-jquery instead of doxygen copy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * $Id$
 
3
 *
 
4
 * Project:  PROJ.4
 
5
 * Purpose:  Implementation of the pj_ctx_* file api, and the default stdio
 
6
 *           based implementation.
 
7
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 
8
 *
 
9
 ******************************************************************************
 
10
 * Copyright (c) 2013, Frank Warmerdam
 
11
 *
 
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:
 
18
 *
 
19
 * The above copyright notice and this permission notice shall be included
 
20
 * in all copies or substantial portions of the Software.
 
21
 *
 
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
 *****************************************************************************/
 
30
 
 
31
#include <projects.h>
 
32
#include <string.h>
 
33
 
 
34
PJ_CVSID("$Id$");
 
35
 
 
36
static PAFile pj_stdio_fopen(projCtx ctx, const char *filename, 
 
37
                             const char *access);
 
38
static size_t pj_stdio_fread(void *buffer, size_t size, size_t nmemb, 
 
39
                             PAFile file);
 
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);
 
43
 
 
44
static projFileAPI default_fileapi = { 
 
45
    pj_stdio_fopen, 
 
46
    pj_stdio_fread,
 
47
    pj_stdio_fseek,
 
48
    pj_stdio_ftell,
 
49
    pj_stdio_fclose
 
50
};
 
51
 
 
52
typedef struct {
 
53
    projCtx ctx;
 
54
    FILE *fp;
 
55
} stdio_pafile;
 
56
 
 
57
/************************************************************************/
 
58
/*                       pj_get_default_fileapi()                       */
 
59
/************************************************************************/
 
60
 
 
61
projFileAPI *pj_get_default_fileapi() 
 
62
{
 
63
    return &default_fileapi;
 
64
}
 
65
 
 
66
/************************************************************************/
 
67
/*                           pj_stdio_fopen()                           */
 
68
/************************************************************************/
 
69
 
 
70
static PAFile pj_stdio_fopen(projCtx ctx, const char *filename, 
 
71
                             const char *access)
 
72
{
 
73
    stdio_pafile *pafile;
 
74
    FILE *fp;
 
75
 
 
76
    fp = fopen(filename, access);
 
77
    if (fp == NULL) 
 
78
    {
 
79
        return NULL;
 
80
    }
 
81
 
 
82
    pafile = (stdio_pafile *) malloc(sizeof(stdio_pafile));
 
83
    pafile->fp = fp;
 
84
    pafile->ctx = ctx;
 
85
    return (PAFile) pafile;
 
86
}
 
87
 
 
88
/************************************************************************/
 
89
/*                           pj_stdio_fread()                           */
 
90
/************************************************************************/
 
91
 
 
92
static size_t pj_stdio_fread(void *buffer, size_t size, size_t nmemb, 
 
93
                             PAFile file)
 
94
{
 
95
    stdio_pafile *pafile = (stdio_pafile *) file;
 
96
    return fread(buffer, size, nmemb, pafile->fp);
 
97
}
 
98
 
 
99
/************************************************************************/
 
100
/*                           pj_stdio_fseek()                           */
 
101
/************************************************************************/
 
102
static int pj_stdio_fseek(PAFile file, long offset, int whence)
 
103
{
 
104
    stdio_pafile *pafile = (stdio_pafile *) file;
 
105
    return fseek(pafile->fp, offset, whence);
 
106
}
 
107
 
 
108
/************************************************************************/
 
109
/*                           pj_stdio_ftell()                           */
 
110
/************************************************************************/
 
111
static long pj_stdio_ftell(PAFile file)
 
112
{
 
113
    stdio_pafile *pafile = (stdio_pafile *) file;
 
114
    return ftell(pafile->fp);
 
115
}
 
116
 
 
117
/************************************************************************/
 
118
/*                          pj_stdio_fclose()                           */
 
119
/************************************************************************/
 
120
static void pj_stdio_fclose(PAFile file)
 
121
{
 
122
    stdio_pafile *pafile = (stdio_pafile *) file;
 
123
    fclose(pafile->fp);
 
124
    free(pafile);
 
125
}
 
126
 
 
127
/************************************************************************/
 
128
/*                            pj_ctx_fopen()                            */
 
129
/*                                                                      */
 
130
/*      Open a file using the provided file io hooks.                   */
 
131
/************************************************************************/
 
132
 
 
133
PAFile pj_ctx_fopen(projCtx ctx, const char *filename, const char *access)
 
134
{
 
135
    return ctx->fileapi->FOpen(ctx, filename, access);
 
136
}
 
137
 
 
138
/************************************************************************/
 
139
/*                            pj_ctx_fread()                            */
 
140
/************************************************************************/
 
141
size_t pj_ctx_fread(projCtx ctx, void *buffer, size_t size, size_t nmemb, PAFile file)
 
142
{
 
143
    return ctx->fileapi->FRead(buffer, size, nmemb, file);
 
144
}
 
145
 
 
146
/************************************************************************/
 
147
/*                            pj_ctx_fseek()                            */
 
148
/************************************************************************/
 
149
int    pj_ctx_fseek(projCtx ctx, PAFile file, long offset, int whence)
 
150
{
 
151
    return ctx->fileapi->FSeek(file, offset, whence);
 
152
}
 
153
 
 
154
/************************************************************************/
 
155
/*                            pj_ctx_ftell()                            */
 
156
/************************************************************************/
 
157
long   pj_ctx_ftell(projCtx ctx, PAFile file)
 
158
{
 
159
    return ctx->fileapi->FTell(file);
 
160
}
 
161
 
 
162
/************************************************************************/
 
163
/*                           pj_ctx_fclose()                            */
 
164
/************************************************************************/
 
165
void   pj_ctx_fclose(projCtx ctx, PAFile file)
 
166
{
 
167
    ctx->fileapi->FClose(file);
 
168
}
 
169
 
 
170
/************************************************************************/
 
171
/*                            pj_ctx_fgets()                            */
 
172
/*                                                                      */
 
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     */
 
175
/*      taken.                                                          */
 
176
/************************************************************************/
 
177
 
 
178
char *pj_ctx_fgets(projCtx ctx, char *line, int size, PAFile file) 
 
179
{
 
180
    long start = pj_ctx_ftell(ctx, file);
 
181
    size_t bytes_read;
 
182
    int i;
 
183
 
 
184
    line[size-1] = '\0';
 
185
    bytes_read = pj_ctx_fread(ctx, line, 1, size-1, file);
 
186
    if(bytes_read == 0)
 
187
        return NULL;
 
188
    if(bytes_read < size) 
 
189
    {
 
190
        line[bytes_read] = '\0';
 
191
    }
 
192
    
 
193
    for( i = 0; i < size-2; i++) 
 
194
    {
 
195
        if (line[i] == '\n') 
 
196
        {
 
197
            line[i+1] = '\0';
 
198
            pj_ctx_fseek(ctx, file, start + i + 1, SEEK_SET);
 
199
            break;
 
200
        }
 
201
    }
 
202
    return line;
 
203
}