aboutsummaryrefslogtreecommitdiff
path: root/src/pj_fileapi.c
diff options
context:
space:
mode:
authorFrank Warmerdam <warmerdam@pobox.com>2013-06-22 01:26:52 +0000
committerFrank Warmerdam <warmerdam@pobox.com>2013-06-22 01:26:52 +0000
commit07695742d49418f87338f0ffa48a16b9fae25767 (patch)
tree72d30bc5aaa4fcf39a06cd36dabf63cf777c748d /src/pj_fileapi.c
parentb9b25dd4d55b42ae2db30926a9038228085f8188 (diff)
downloadPROJ-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/pj_fileapi.c')
-rw-r--r--src/pj_fileapi.c167
1 files changed, 167 insertions, 0 deletions
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);
+}