From 47ed7427c8c87229963eba585408b690082e2f72 Mon Sep 17 00:00:00 2001 From: Frank Warmerdam Date: Sat, 22 Jun 2013 17:07:26 +0000 Subject: add pj_ctx_fgets() implementation (untested) git-svn-id: http://svn.osgeo.org/metacrs/proj/trunk@2347 4e78687f-474d-0410-85f9-8d5e500ac6b2 --- src/pj_fileapi.c | 35 +++++++++++++++++++++++++++++++++++ src/projects.h | 1 + 2 files changed, 36 insertions(+) (limited to 'src') diff --git a/src/pj_fileapi.c b/src/pj_fileapi.c index 1a1a0f21..d0d2a442 100644 --- a/src/pj_fileapi.c +++ b/src/pj_fileapi.c @@ -165,3 +165,38 @@ void pj_ctx_fclose(projCtx ctx, PAFile file) { ctx->fileapi->FClose(file); } + +/************************************************************************/ +/* pj_ctx_fgets() */ +/* */ +/* A not very optimal implementation of fgets on top of */ +/* fread(). If we end up using this a lot more care should be */ +/* taken. */ +/************************************************************************/ + +char *pj_ctx_fgets(projCtx ctx, char *line, int size, PAFile file) +{ + long start = pj_ctx_ftell(ctx, file); + size_t bytes_read; + int i; + + line[size-1] = '\0'; + bytes_read = pj_ctx_fread(ctx, line, 1, size-1, file); + if(bytes_read == 0) + return NULL; + if(bytes_read < size) + { + line[bytes_read] = '\0'; + } + + for( i = 0; i < size-2; i++) + { + if (line[i] == '\n') + { + line[i+1] = '\0'; + pj_ctx_fseek(ctx, file, start + i + 1, SEEK_SET); + break; + } + } + return line; +} diff --git a/src/projects.h b/src/projects.h index 0e24566e..442c483f 100644 --- a/src/projects.h +++ b/src/projects.h @@ -400,6 +400,7 @@ size_t pj_ctx_fread(projCtx ctx, void *buffer, size_t size, size_t nmemb, PAFile 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); +char *pj_ctx_fgets(projCtx ctx, char *line, int size, PAFile file); /* procedure prototypes */ double dmstor(const char *, char **); -- cgit v1.2.3