diff options
| author | Frank Warmerdam <warmerdam@pobox.com> | 2013-06-22 01:26:52 +0000 |
|---|---|---|
| committer | Frank Warmerdam <warmerdam@pobox.com> | 2013-06-22 01:26:52 +0000 |
| commit | 07695742d49418f87338f0ffa48a16b9fae25767 (patch) | |
| tree | 72d30bc5aaa4fcf39a06cd36dabf63cf777c748d /src | |
| parent | b9b25dd4d55b42ae2db30926a9038228085f8188 (diff) | |
| download | PROJ-07695742d49418f87338f0ffa48a16b9fae25767.tar.gz PROJ-07695742d49418f87338f0ffa48a16b9fae25767.zip | |
preliminary step implementing virtual fileio
git-svn-id: http://svn.osgeo.org/metacrs/proj/trunk@2344 4e78687f-474d-0410-85f9-8d5e500ac6b2
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile.am | 2 | ||||
| -rw-r--r-- | src/pj_ctx.c | 1 | ||||
| -rw-r--r-- | src/pj_fileapi.c | 167 | ||||
| -rw-r--r-- | src/proj_api.h | 14 | ||||
| -rw-r--r-- | src/projects.h | 10 |
5 files changed, 193 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index c1d1998a..dab00b3b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -57,7 +57,7 @@ libproj_la_SOURCES = \ pj_open_lib.c pj_param.c pj_phi2.c pj_pr_list.c \ pj_qsfn.c pj_strerrno.c pj_tsfn.c pj_units.c pj_ctx.c pj_log.c \ pj_zpoly1.c rtodms.c vector1.c pj_release.c pj_gauss.c \ - PJ_healpix.c PJ_natearth.c \ + PJ_healpix.c PJ_natearth.c pj_fileapi.c \ \ pj_gc_reader.c pj_gridcatalog.c \ nad_cvt.c nad_init.c nad_intr.c emess.c emess.h \ diff --git a/src/pj_ctx.c b/src/pj_ctx.c index a9dc7ab8..ccd2292c 100644 --- a/src/pj_ctx.c +++ b/src/pj_ctx.c @@ -73,6 +73,7 @@ projCtx pj_get_default_ctx() default_context.debug_level = PJ_LOG_NONE; default_context.logger = pj_stderr_logger; default_context.app_data = NULL; + default_context.fileapi = pj_get_default_fileapi(); if( getenv("PROJ_DEBUG") != NULL ) { diff --git a/src/pj_fileapi.c b/src/pj_fileapi.c new file mode 100644 index 00000000..1a1a0f21 --- /dev/null +++ b/src/pj_fileapi.c @@ -0,0 +1,167 @@ +/****************************************************************************** + * $Id$ + * + * Project: PROJ.4 + * Purpose: Implementation of the pj_ctx_* file api, and the default stdio + * based implementation. + * Author: Frank Warmerdam, warmerdam@pobox.com + * + ****************************************************************************** + * Copyright (c) 2013, Frank Warmerdam + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + *****************************************************************************/ + +#include <projects.h> +#include <string.h> + +PJ_CVSID("$Id$"); + +static PAFile pj_stdio_fopen(projCtx ctx, const char *filename, + const char *access); +static size_t pj_stdio_fread(void *buffer, size_t size, size_t nmemb, + PAFile file); +static int pj_stdio_fseek(PAFile file, long offset, int whence); +static long pj_stdio_ftell(PAFile file); +static void pj_stdio_fclose(PAFile file); + +static projFileAPI default_fileapi = { + pj_stdio_fopen, + pj_stdio_fread, + pj_stdio_fseek, + pj_stdio_ftell, + pj_stdio_fclose +}; + +typedef struct { + projCtx ctx; + FILE *fp; +} stdio_pafile; + +/************************************************************************/ +/* pj_get_default_fileapi() */ +/************************************************************************/ + +projFileAPI *pj_get_default_fileapi() +{ + return &default_fileapi; +} + +/************************************************************************/ +/* pj_stdio_fopen() */ +/************************************************************************/ + +static PAFile pj_stdio_fopen(projCtx ctx, const char *filename, + const char *access) +{ + FILE *fp; + + fp = fopen(filename, access); + if (fp == NULL) + { + return NULL; + } + + stdio_pafile *pafile = (stdio_pafile *) malloc(sizeof(stdio_pafile)); + pafile->fp = fp; + pafile->ctx = ctx; + return (PAFile) pafile; +} + +/************************************************************************/ +/* pj_stdio_fread() */ +/************************************************************************/ + +static size_t pj_stdio_fread(void *buffer, size_t size, size_t nmemb, + PAFile file) +{ + stdio_pafile *pafile = (stdio_pafile *) file; + return fread(buffer, size, nmemb, pafile->fp); +} + +/************************************************************************/ +/* pj_stdio_fseek() */ +/************************************************************************/ +static int pj_stdio_fseek(PAFile file, long offset, int whence) +{ + stdio_pafile *pafile = (stdio_pafile *) file; + return fseek(pafile->fp, offset, whence); +} + +/************************************************************************/ +/* pj_stdio_ftell() */ +/************************************************************************/ +static long pj_stdio_ftell(PAFile file) +{ + stdio_pafile *pafile = (stdio_pafile *) file; + return ftell(pafile->fp); +} + +/************************************************************************/ +/* pj_stdio_fclose() */ +/************************************************************************/ +static void pj_stdio_fclose(PAFile file) +{ + stdio_pafile *pafile = (stdio_pafile *) file; + fclose(pafile->fp); + free(pafile); +} + +/************************************************************************/ +/* pj_ctx_fopen() */ +/* */ +/* Open a file using the provided file io hooks. */ +/************************************************************************/ + +PAFile pj_ctx_fopen(projCtx ctx, const char *filename, const char *access) +{ + return ctx->fileapi->FOpen(ctx, filename, access); +} + +/************************************************************************/ +/* pj_ctx_fread() */ +/************************************************************************/ +size_t pj_ctx_fread(projCtx ctx, void *buffer, size_t size, size_t nmemb, PAFile file) +{ + return ctx->fileapi->FRead(buffer, size, nmemb, file); +} + +/************************************************************************/ +/* pj_ctx_fseek() */ +/************************************************************************/ +int pj_ctx_fseek(projCtx ctx, PAFile file, long offset, int whence) +{ + return ctx->fileapi->FSeek(file, offset, whence); +} + +/************************************************************************/ +/* pj_ctx_ftell() */ +/************************************************************************/ +long pj_ctx_ftell(projCtx ctx, PAFile file) +{ + return ctx->fileapi->FTell(file); +} + +/************************************************************************/ +/* pj_ctx_fclose() */ +/************************************************************************/ +void pj_ctx_fclose(projCtx ctx, PAFile file) +{ + ctx->fileapi->FClose(file); +} diff --git a/src/proj_api.h b/src/proj_api.h index feae9aff..edc3b587 100644 --- a/src/proj_api.h +++ b/src/proj_api.h @@ -63,6 +63,16 @@ extern int pj_errno; /* global error return code */ # define projLP LP #endif +/* file reading api, like stdio */ +typedef int *PAFile; +typedef struct projFileAPI_t { + PAFile (*FOpen)(projCtx ctx, const char *filename, const char *access); + size_t (*FRead)(void *buffer, size_t size, size_t nmemb, PAFile file); + int (*FSeek)(PAFile file, long offset, int whence); + long (*FTell)(PAFile file); + void (*FClose)(PAFile); +} projFileAPI; + /* procedure prototypes */ projXY pj_fwd(projLP, projPJ); @@ -117,6 +127,10 @@ void pj_ctx_set_debug( projCtx, int ); void pj_ctx_set_logger( projCtx, void (*)(void *, int, const char *) ); void pj_ctx_set_app_data( projCtx, void * ); void *pj_ctx_get_app_data( projCtx ); +void pj_ctx_set_file( projCtx, projFileAPI *); +projFileAPI *pj_ctx_get_fileapi( projCtx ); + +projFileAPI *pj_get_default_fileapi(); void pj_log( projCtx ctx, int level, const char *fmt, ... ); void pj_stderr_logger( void *, int, const char * ); diff --git a/src/projects.h b/src/projects.h index f9dcea64..fc64ba59 100644 --- a/src/projects.h +++ b/src/projects.h @@ -125,12 +125,15 @@ extern double hypot(double, double); #define DIR_CHAR '/' #endif +struct projFileAPI_t; + /* proj thread context */ typedef struct { int last_errno; int debug_level; void (*logger)(void *, int, const char *); void *app_data; + struct projFileAPI_t *fileapi; } projCtx_t; /* datum_type values */ @@ -391,6 +394,13 @@ typedef struct _PJ_GridCatalog { } PJ_GridCatalog; +/* file api */ +PAFile pj_ctx_fopen(projCtx ctx, const char *filename, const char *access); +size_t pj_ctx_fread(projCtx ctx, void *buffer, size_t size, size_t nmemb, PAFile file); +int pj_ctx_fseek(projCtx ctx, PAFile file, long offset, int whence); +long pj_ctx_ftell(projCtx ctx, PAFile file); +void pj_ctx_fclose(projCtx ctx, PAFile file); + /* procedure prototypes */ double dmstor(const char *, char **); double dmstor_ctx(projCtx ctx, const char *, char **); |
