"Fossies" - the Fresh Open Source Software Archive

Member "proj-6.2.1/src/fileapi.cpp" (6 Mar 2019, 7846 Bytes) of package /linux/privat/proj-6.2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "fileapi.cpp" see the Fossies "Dox" file reference documentation.

    1 /******************************************************************************
    2  * Project:  PROJ.4
    3  * Purpose:  Implementation of the pj_ctx_* file api, and the default stdio
    4  *           based implementation.
    5  * Author:   Frank Warmerdam, warmerdam@pobox.com
    6  *
    7  ******************************************************************************
    8  * Copyright (c) 2013, Frank Warmerdam
    9  *
   10  * Permission is hereby granted, free of charge, to any person obtaining a
   11  * copy of this software and associated documentation files (the "Software"),
   12  * to deal in the Software without restriction, including without limitation
   13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   14  * and/or sell copies of the Software, and to permit persons to whom the
   15  * Software is furnished to do so, subject to the following conditions:
   16  *
   17  * The above copyright notice and this permission notice shall be included
   18  * in all copies or substantial portions of the Software.
   19  *
   20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   26  * DEALINGS IN THE SOFTWARE.
   27  *****************************************************************************/
   28 
   29 #include <errno.h>
   30 #include <stddef.h>
   31 #include <stdio.h>
   32 #include <stdlib.h>
   33 #include <string.h>
   34 
   35 #include "proj.h"
   36 #include "proj_internal.h"
   37 
   38 static PAFile stdio_fopen(projCtx ctx, const char *filename,
   39                              const char *access);
   40 static size_t stdio_fread(void *buffer, size_t size, size_t nmemb,
   41                              PAFile file);
   42 static int stdio_fseek(PAFile file, long offset, int whence);
   43 static long stdio_ftell(PAFile file);
   44 static void stdio_fclose(PAFile file);
   45 
   46 static projFileAPI default_fileapi = {
   47     stdio_fopen,
   48     stdio_fread,
   49     stdio_fseek,
   50     stdio_ftell,
   51     stdio_fclose
   52 };
   53 
   54 typedef struct {
   55     projCtx ctx;
   56     FILE *fp;
   57 } stdio_pafile;
   58 
   59 /************************************************************************/
   60 /*                       pj_get_default_fileapi()                       */
   61 /************************************************************************/
   62 
   63 projFileAPI *pj_get_default_fileapi(void)
   64 {
   65     return &default_fileapi;
   66 }
   67 
   68 /************************************************************************/
   69 /*                           stdio_fopen()                           */
   70 /************************************************************************/
   71 
   72 static PAFile stdio_fopen(projCtx ctx, const char *filename,
   73                              const char *access)
   74 {
   75     stdio_pafile *pafile;
   76     FILE *fp;
   77 
   78     fp = fopen(filename, access);
   79     if (fp == nullptr)
   80     {
   81         return nullptr;
   82     }
   83 
   84     pafile = (stdio_pafile *) malloc(sizeof(stdio_pafile));
   85     if (!pafile)
   86     {
   87         pj_ctx_set_errno(ctx, ENOMEM);
   88         fclose(fp);
   89         return nullptr;
   90     }
   91 
   92     pafile->fp = fp;
   93     pafile->ctx = ctx;
   94     return (PAFile) pafile;
   95 }
   96 
   97 /************************************************************************/
   98 /*                           stdio_fread()                           */
   99 /************************************************************************/
  100 
  101 static size_t stdio_fread(void *buffer, size_t size, size_t nmemb,
  102                              PAFile file)
  103 {
  104     stdio_pafile *pafile = (stdio_pafile *) file;
  105     return fread(buffer, size, nmemb, pafile->fp);
  106 }
  107 
  108 /************************************************************************/
  109 /*                           stdio_fseek()                           */
  110 /************************************************************************/
  111 static int stdio_fseek(PAFile file, long offset, int whence)
  112 {
  113     stdio_pafile *pafile = (stdio_pafile *) file;
  114     return fseek(pafile->fp, offset, whence);
  115 }
  116 
  117 /************************************************************************/
  118 /*                           stdio_ftell()                           */
  119 /************************************************************************/
  120 static long stdio_ftell(PAFile file)
  121 {
  122     stdio_pafile *pafile = (stdio_pafile *) file;
  123     return ftell(pafile->fp);
  124 }
  125 
  126 /************************************************************************/
  127 /*                          stdio_fclose()                           */
  128 /************************************************************************/
  129 static void stdio_fclose(PAFile file)
  130 {
  131     stdio_pafile *pafile = (stdio_pafile *) file;
  132     fclose(pafile->fp);
  133     free(pafile);
  134 }
  135 
  136 /************************************************************************/
  137 /*                            pj_ctx_fopen()                            */
  138 /*                                                                      */
  139 /*      Open a file using the provided file io hooks.                   */
  140 /************************************************************************/
  141 
  142 PAFile pj_ctx_fopen(projCtx ctx, const char *filename, const char *access)
  143 {
  144     return ctx->fileapi->FOpen(ctx, filename, access);
  145 }
  146 
  147 /************************************************************************/
  148 /*                            pj_ctx_fread()                            */
  149 /************************************************************************/
  150 size_t pj_ctx_fread(projCtx ctx, void *buffer, size_t size, size_t nmemb, PAFile file)
  151 {
  152     return ctx->fileapi->FRead(buffer, size, nmemb, file);
  153 }
  154 
  155 /************************************************************************/
  156 /*                            pj_ctx_fseek()                            */
  157 /************************************************************************/
  158 int    pj_ctx_fseek(projCtx ctx, PAFile file, long offset, int whence)
  159 {
  160     return ctx->fileapi->FSeek(file, offset, whence);
  161 }
  162 
  163 /************************************************************************/
  164 /*                            pj_ctx_ftell()                            */
  165 /************************************************************************/
  166 long   pj_ctx_ftell(projCtx ctx, PAFile file)
  167 {
  168     return ctx->fileapi->FTell(file);
  169 }
  170 
  171 /************************************************************************/
  172 /*                           pj_ctx_fclose()                            */
  173 /************************************************************************/
  174 void   pj_ctx_fclose(projCtx ctx, PAFile file)
  175 {
  176     ctx->fileapi->FClose(file);
  177 }
  178 
  179 /************************************************************************/
  180 /*                            pj_ctx_fgets()                            */
  181 /*                                                                      */
  182 /*      A not very optimal implementation of fgets on top of            */
  183 /*      fread().  If we end up using this a lot more care should be     */
  184 /*      taken.                                                          */
  185 /************************************************************************/
  186 
  187 char *pj_ctx_fgets(projCtx ctx, char *line, int size, PAFile file)
  188 {
  189     long start = pj_ctx_ftell(ctx, file);
  190     size_t bytes_read;
  191     int i;
  192     int max_size;
  193 
  194     line[size-1] = '\0';
  195     bytes_read = pj_ctx_fread(ctx, line, 1, size-1, file);
  196     if(bytes_read == 0)
  197         return nullptr;
  198     if(bytes_read < (size_t)size)
  199     {
  200         line[bytes_read] = '\0';
  201     }
  202 
  203     max_size = (int)MIN(bytes_read, (size_t)(size > 2 ? size - 2 : 0));
  204     for( i = 0; i < max_size; i++)
  205     {
  206         if (line[i] == '\n')
  207         {
  208             line[i+1] = '\0';
  209             pj_ctx_fseek(ctx, file, start + i + 1, SEEK_SET);
  210             break;
  211         }
  212     }
  213     return line;
  214 }