aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFrank Warmerdam <warmerdam@pobox.com>2013-06-22 07:54:57 +0000
committerFrank Warmerdam <warmerdam@pobox.com>2013-06-22 07:54:57 +0000
commit44ef2bd6b0184d334b020e401026207b38aaa064 (patch)
tree347b721fe196a12765ad5c869cb51e77fa3a64ec /src
parent07695742d49418f87338f0ffa48a16b9fae25767 (diff)
downloadPROJ-44ef2bd6b0184d334b020e401026207b38aaa064.tar.gz
PROJ-44ef2bd6b0184d334b020e401026207b38aaa064.zip
bulk of file api implementation complete and working
git-svn-id: http://svn.osgeo.org/metacrs/proj/trunk@2345 4e78687f-474d-0410-85f9-8d5e500ac6b2
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.in7
-rw-r--r--src/nad_init.c24
-rw-r--r--src/pj_gc_reader.c8
-rw-r--r--src/pj_gridinfo.c67
-rw-r--r--src/pj_init.c191
-rw-r--r--src/pj_open_lib.c8
-rw-r--r--src/projects.h10
7 files changed, 226 insertions, 89 deletions
diff --git a/src/Makefile.in b/src/Makefile.in
index dd32d94b..f02a0f52 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -102,8 +102,8 @@ am_libproj_la_OBJECTS = PJ_aeqd.lo PJ_gnom.lo PJ_laea.lo \
pj_open_lib.lo pj_param.lo pj_phi2.lo pj_pr_list.lo pj_qsfn.lo \
pj_strerrno.lo pj_tsfn.lo pj_units.lo pj_ctx.lo pj_log.lo \
pj_zpoly1.lo rtodms.lo vector1.lo pj_release.lo pj_gauss.lo \
- PJ_healpix.lo PJ_natearth.lo pj_gc_reader.lo pj_gridcatalog.lo \
- nad_cvt.lo nad_init.lo nad_intr.lo emess.lo \
+ PJ_healpix.lo PJ_natearth.lo pj_fileapi.lo pj_gc_reader.lo \
+ pj_gridcatalog.lo nad_cvt.lo nad_init.lo nad_intr.lo emess.lo \
pj_apply_gridshift.lo pj_datums.lo pj_datum_set.lo \
pj_transform.lo geocent.lo pj_utils.lo pj_gridinfo.lo \
pj_gridlist.lo jniproj.lo pj_mutex.lo pj_initcache.lo \
@@ -317,7 +317,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 \
@@ -601,6 +601,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pj_ellps.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pj_errno.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pj_factors.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pj_fileapi.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pj_fwd.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pj_gauss.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pj_gc_reader.Plo@am__quote@
diff --git a/src/nad_init.c b/src/nad_init.c
index bb62bd6f..84dc9867 100644
--- a/src/nad_init.c
+++ b/src/nad_init.c
@@ -82,18 +82,18 @@ static void swap_words( void *data_in, int word_size, int word_count )
/* Load the data portion of a ctable formatted grid. */
/************************************************************************/
-int nad_ctable_load( projCtx ctx, struct CTABLE *ct, FILE *fid )
+int nad_ctable_load( projCtx ctx, struct CTABLE *ct, PAFile fid )
{
int a_size;
- fseek( fid, sizeof(struct CTABLE), SEEK_SET );
+ pj_ctx_fseek( ctx, fid, sizeof(struct CTABLE), SEEK_SET );
/* read all the actual shift values */
a_size = ct->lim.lam * ct->lim.phi;
ct->cvs = (FLP *) pj_malloc(sizeof(FLP) * a_size);
if( ct->cvs == NULL
- || fread(ct->cvs, sizeof(FLP), a_size, fid) != a_size )
+ || pj_ctx_fread(ctx, ct->cvs, sizeof(FLP), a_size, fid) != a_size )
{
pj_dalloc( ct->cvs );
ct->cvs = NULL;
@@ -113,7 +113,7 @@ int nad_ctable_load( projCtx ctx, struct CTABLE *ct, FILE *fid )
/* Read the header portion of a "ctable" format grid. */
/************************************************************************/
-struct CTABLE *nad_ctable_init( projCtx ctx, FILE * fid )
+struct CTABLE *nad_ctable_init( projCtx ctx, PAFile fid )
{
struct CTABLE *ct;
int id_end;
@@ -121,7 +121,7 @@ struct CTABLE *nad_ctable_init( projCtx ctx, FILE * fid )
/* read the table header */
ct = (struct CTABLE *) pj_malloc(sizeof(struct CTABLE));
if( ct == NULL
- || fread( ct, sizeof(struct CTABLE), 1, fid ) != 1 )
+ || pj_ctx_fread( ctx, ct, sizeof(struct CTABLE), 1, fid ) != 1 )
{
pj_ctx_set_errno( ctx, -38 );
return NULL;
@@ -155,18 +155,18 @@ struct CTABLE *nad_ctable_init( projCtx ctx, FILE * fid )
/* Load the data portion of a ctable2 formatted grid. */
/************************************************************************/
-int nad_ctable2_load( projCtx ctx, struct CTABLE *ct, FILE *fid )
+int nad_ctable2_load( projCtx ctx, struct CTABLE *ct, PAFile fid )
{
int a_size;
- fseek( fid, 160, SEEK_SET );
+ pj_ctx_fseek( ctx, fid, 160, SEEK_SET );
/* read all the actual shift values */
a_size = ct->lim.lam * ct->lim.phi;
ct->cvs = (FLP *) pj_malloc(sizeof(FLP) * a_size);
if( ct->cvs == NULL
- || fread(ct->cvs, sizeof(FLP), a_size, fid) != a_size )
+ || pj_ctx_fread(ctx, ct->cvs, sizeof(FLP), a_size, fid) != a_size )
{
pj_dalloc( ct->cvs );
ct->cvs = NULL;
@@ -195,13 +195,13 @@ int nad_ctable2_load( projCtx ctx, struct CTABLE *ct, FILE *fid )
/* Read the header portion of a "ctable2" format grid. */
/************************************************************************/
-struct CTABLE *nad_ctable2_init( projCtx ctx, FILE * fid )
+struct CTABLE *nad_ctable2_init( projCtx ctx, PAFile fid )
{
struct CTABLE *ct;
int id_end;
char header[160];
- if( fread( header, sizeof(header), 1, fid ) != 1 )
+ if( pj_ctx_fread( ctx, header, sizeof(header), 1, fid ) != 1 )
{
pj_ctx_set_errno( ctx, -38 );
return NULL;
@@ -268,7 +268,7 @@ struct CTABLE *nad_init(projCtx ctx, char *name)
{
char fname[MAX_PATH_FILENAME+1];
struct CTABLE *ct;
- FILE *fid;
+ PAFile fid;
ctx->last_errno = 0;
@@ -290,7 +290,7 @@ struct CTABLE *nad_init(projCtx ctx, char *name)
}
}
- fclose(fid);
+ pj_ctx_fclose(ctx, fid);
return ct;
}
diff --git a/src/pj_gc_reader.c b/src/pj_gc_reader.c
index e22aa993..f944b0f1 100644
--- a/src/pj_gc_reader.c
+++ b/src/pj_gc_reader.c
@@ -32,7 +32,7 @@
#include <projects.h>
#include <string.h>
-static int pj_gc_readentry( projCtx ctx, FILE *fp, PJ_GridCatalogEntry *entry );
+static int pj_gc_readentry(projCtx ctx, PAFile fp, PJ_GridCatalogEntry *entry);
static void pj_gc_sortcatalog( projCtx ctx, PJ_GridCatalog *catalog );
/************************************************************************/
@@ -43,7 +43,7 @@ static void pj_gc_sortcatalog( projCtx ctx, PJ_GridCatalog *catalog );
PJ_GridCatalog *pj_gc_readcatalog( projCtx ctx, const char *catalog_name )
{
- FILE *fp;
+ PAFile fp;
PJ_GridCatalog *catalog;
int entry_max, err;
char line[302];
@@ -105,7 +105,7 @@ static void pj_gc_sortcatalog( projCtx ctx, PJ_GridCatalog *catalog )
/* token count. */
/************************************************************************/
-static int pj_gc_read_csv_line( projCtx ctx, FILE *fp,
+static int pj_gc_read_csv_line( projCtx ctx, PAFile fp,
char **tokens, int max_tokens )
{
char line[302];
@@ -180,7 +180,7 @@ double pj_gc_parsedate( projCtx ctx, const char *date_string )
/* gridname,ll_long,ll_lat,ur_long,ur_lat,priority,date */
/************************************************************************/
-static int pj_gc_readentry( projCtx ctx, FILE *fp, PJ_GridCatalogEntry *entry )
+static int pj_gc_readentry( projCtx ctx, PAFile fp, PJ_GridCatalogEntry *entry)
{
#define MAX_TOKENS 30
char *tokens[MAX_TOKENS];
diff --git a/src/pj_gridinfo.c b/src/pj_gridinfo.c
index a153dcf5..3ce7ecd3 100644
--- a/src/pj_gridinfo.c
+++ b/src/pj_gridinfo.c
@@ -126,7 +126,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
/* -------------------------------------------------------------------- */
if( strcmp(gi->format,"ctable") == 0 )
{
- FILE *fid;
+ PAFile fid;
int result;
fid = pj_open_lib( ctx, gi->filename, "rb" );
@@ -139,7 +139,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
result = nad_ctable_load( ctx, gi->ct, fid );
- fclose( fid );
+ pj_ctx_fclose( ctx, fid );
return result;
}
@@ -149,7 +149,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
/* -------------------------------------------------------------------- */
else if( strcmp(gi->format,"ctable2") == 0 )
{
- FILE *fid;
+ PAFile fid;
int result;
fid = pj_open_lib( ctx, gi->filename, "rb" );
@@ -162,7 +162,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
result = nad_ctable2_load( ctx, gi->ct, fid );
- fclose( fid );
+ pj_ctx_fclose( ctx, fid );
return result;
}
@@ -178,7 +178,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
{
double *row_buf;
int row;
- FILE *fid;
+ PAFile fid;
fid = pj_open_lib( ctx, gi->filename, "rb" );
@@ -188,7 +188,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
return 0;
}
- fseek( fid, gi->grid_offset, SEEK_SET );
+ pj_ctx_fseek( ctx, fid, gi->grid_offset, SEEK_SET );
row_buf = (double *) pj_malloc(gi->ct->lim.lam * sizeof(double) * 2);
gi->ct->cvs = (FLP *) pj_malloc(gi->ct->lim.lam*gi->ct->lim.phi*sizeof(FLP));
@@ -204,7 +204,8 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
FLP *cvs;
double *diff_seconds;
- if( fread( row_buf, sizeof(double), gi->ct->lim.lam * 2, fid )
+ if( pj_ctx_fread( ctx, row_buf,
+ sizeof(double), gi->ct->lim.lam * 2, fid )
!= 2 * gi->ct->lim.lam )
{
pj_dalloc( row_buf );
@@ -231,7 +232,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
pj_dalloc( row_buf );
- fclose( fid );
+ pj_ctx_fclose( ctx, fid );
return 1;
}
@@ -247,7 +248,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
{
float *row_buf;
int row;
- FILE *fid;
+ PAFile fid;
pj_log( ctx, PJ_LOG_DEBUG_MINOR,
"NTv2 - loading grid %s", gi->ct->id );
@@ -260,7 +261,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
return 0;
}
- fseek( fid, gi->grid_offset, SEEK_SET );
+ pj_ctx_fseek( ctx, fid, gi->grid_offset, SEEK_SET );
row_buf = (float *) pj_malloc(gi->ct->lim.lam * sizeof(float) * 4);
gi->ct->cvs = (FLP *) pj_malloc(gi->ct->lim.lam*gi->ct->lim.phi*sizeof(FLP));
@@ -276,7 +277,8 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
FLP *cvs;
float *diff_seconds;
- if( fread( row_buf, sizeof(float), gi->ct->lim.lam*4, fid )
+ if( pj_ctx_fread( ctx, row_buf, sizeof(float),
+ gi->ct->lim.lam*4, fid )
!= 4 * gi->ct->lim.lam )
{
pj_dalloc( row_buf );
@@ -305,8 +307,8 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
}
pj_dalloc( row_buf );
-
- fclose( fid );
+
+ pj_ctx_fclose( ctx, fid );
return 1;
}
@@ -317,7 +319,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
else if( strcmp(gi->format,"gtx") == 0 )
{
int words = gi->ct->lim.lam * gi->ct->lim.phi;
- FILE *fid;
+ PAFile fid;
fid = pj_open_lib( ctx, gi->filename, "rb" );
@@ -327,7 +329,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
return 0;
}
- fseek( fid, gi->grid_offset, SEEK_SET );
+ pj_ctx_fseek( ctx, fid, gi->grid_offset, SEEK_SET );
gi->ct->cvs = (FLP *) pj_malloc(words*sizeof(float));
if( gi->ct->cvs == NULL )
@@ -336,7 +338,8 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
return 0;
}
- if( fread( gi->ct->cvs, sizeof(float), words, fid ) != words )
+ if( pj_ctx_fread( ctx, gi->ct->cvs, sizeof(float), words, fid )
+ != words )
{
pj_dalloc( gi->ct->cvs );
gi->ct->cvs = NULL;
@@ -346,7 +349,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
if( IS_LSB )
swap_words( (unsigned char *) gi->ct->cvs, 4, words );
- fclose( fid );
+ pj_ctx_fclose( ctx, fid );
return 1;
}
@@ -362,7 +365,7 @@ int pj_gridinfo_load( projCtx ctx, PJ_GRIDINFO *gi )
/* Load a ntv2 (.gsb) file. */
/************************************************************************/
-static int pj_gridinfo_init_ntv2( projCtx ctx, FILE *fid, PJ_GRIDINFO *gilist )
+static int pj_gridinfo_init_ntv2( projCtx ctx, PAFile fid, PJ_GRIDINFO *gilist )
{
unsigned char header[11*16];
@@ -381,7 +384,7 @@ static int pj_gridinfo_init_ntv2( projCtx ctx, FILE *fid, PJ_GRIDINFO *gilist )
/* -------------------------------------------------------------------- */
/* Read the overview header. */
/* -------------------------------------------------------------------- */
- if( fread( header, sizeof(header), 1, fid ) != 1 )
+ if( pj_ctx_fread( ctx, header, sizeof(header), 1, fid ) != 1 )
{
pj_ctx_set_errno( ctx, -38 );
return 0;
@@ -419,7 +422,7 @@ static int pj_gridinfo_init_ntv2( projCtx ctx, FILE *fid, PJ_GRIDINFO *gilist )
/* -------------------------------------------------------------------- */
/* Read header. */
/* -------------------------------------------------------------------- */
- if( fread( header, sizeof(header), 1, fid ) != 1 )
+ if( pj_ctx_fread( ctx, header, sizeof(header), 1, fid ) != 1 )
{
pj_ctx_set_errno( ctx, -38 );
return 0;
@@ -507,7 +510,7 @@ static int pj_gridinfo_init_ntv2( projCtx ctx, FILE *fid, PJ_GRIDINFO *gilist )
gi->ct = ct;
gi->format = "ntv2";
- gi->grid_offset = ftell( fid );
+ gi->grid_offset = pj_ctx_ftell( ctx, fid );
/* -------------------------------------------------------------------- */
/* Attach to the correct list or sublist. */
@@ -556,7 +559,7 @@ static int pj_gridinfo_init_ntv2( projCtx ctx, FILE *fid, PJ_GRIDINFO *gilist )
/* -------------------------------------------------------------------- */
/* Seek past the data. */
/* -------------------------------------------------------------------- */
- fseek( fid, gs_count * 16, SEEK_CUR );
+ pj_ctx_fseek( ctx, fid, gs_count * 16, SEEK_CUR );
}
return 1;
@@ -568,7 +571,7 @@ static int pj_gridinfo_init_ntv2( projCtx ctx, FILE *fid, PJ_GRIDINFO *gilist )
/* Load an NTv1 style Canadian grid shift file. */
/************************************************************************/
-static int pj_gridinfo_init_ntv1( projCtx ctx, FILE * fid, PJ_GRIDINFO *gi )
+static int pj_gridinfo_init_ntv1( projCtx ctx, PAFile fid, PJ_GRIDINFO *gi )
{
unsigned char header[176];
@@ -588,7 +591,7 @@ static int pj_gridinfo_init_ntv1( projCtx ctx, FILE * fid, PJ_GRIDINFO *gi )
/* -------------------------------------------------------------------- */
/* Read the header. */
/* -------------------------------------------------------------------- */
- if( fread( header, sizeof(header), 1, fid ) != 1 )
+ if( pj_ctx_fread( ctx, header, sizeof(header), 1, fid ) != 1 )
{
pj_ctx_set_errno( ctx, -38 );
return 0;
@@ -643,7 +646,7 @@ static int pj_gridinfo_init_ntv1( projCtx ctx, FILE * fid, PJ_GRIDINFO *gi )
ct->cvs = NULL;
gi->ct = ct;
- gi->grid_offset = ftell( fid );
+ gi->grid_offset = pj_ctx_ftell( ctx, fid );
gi->format = "ntv1";
return 1;
@@ -655,7 +658,7 @@ static int pj_gridinfo_init_ntv1( projCtx ctx, FILE * fid, PJ_GRIDINFO *gi )
/* Load a NOAA .gtx vertical datum shift file. */
/************************************************************************/
-static int pj_gridinfo_init_gtx( projCtx ctx, FILE * fid, PJ_GRIDINFO *gi )
+static int pj_gridinfo_init_gtx( projCtx ctx, PAFile fid, PJ_GRIDINFO *gi )
{
unsigned char header[40];
@@ -676,7 +679,7 @@ static int pj_gridinfo_init_gtx( projCtx ctx, FILE * fid, PJ_GRIDINFO *gi )
/* -------------------------------------------------------------------- */
/* Read the header. */
/* -------------------------------------------------------------------- */
- if( fread( header, sizeof(header), 1, fid ) != 1 )
+ if( pj_ctx_fread( ctx, header, sizeof(header), 1, fid ) != 1 )
{
pj_ctx_set_errno( ctx, -38 );
return 0;
@@ -766,7 +769,7 @@ PJ_GRIDINFO *pj_gridinfo_init( projCtx ctx, const char *gridname )
{
char fname[MAX_PATH_FILENAME+1];
PJ_GRIDINFO *gilist;
- FILE *fp;
+ PAFile fp;
char header[160];
errno = pj_errno = 0;
@@ -800,14 +803,14 @@ PJ_GRIDINFO *pj_gridinfo_init( projCtx ctx, const char *gridname )
/* -------------------------------------------------------------------- */
/* Load a header, to determine the file type. */
/* -------------------------------------------------------------------- */
- if( fread( header, sizeof(header), 1, fp ) != 1 )
+ if( pj_ctx_fread( ctx, header, sizeof(header), 1, fp ) != 1 )
{
- fclose( fp );
+ pj_ctx_fclose( ctx, fp );
pj_ctx_set_errno( ctx, -38 );
return gilist;
}
- fseek( fp, SEEK_SET, 0 );
+ pj_ctx_fseek( ctx, fp, SEEK_SET, 0 );
/* -------------------------------------------------------------------- */
/* Determine file type. */
@@ -864,7 +867,7 @@ PJ_GRIDINFO *pj_gridinfo_init( projCtx ctx, const char *gridname )
(ct->ll.phi + (ct->lim.phi-1)*ct->del.phi) * RAD_TO_DEG );
}
- fclose(fp);
+ pj_ctx_fclose(ctx, fp);
return gilist;
}
diff --git a/src/pj_init.c b/src/pj_init.c
index bc8691f4..fef2b5ff 100644
--- a/src/pj_init.c
+++ b/src/pj_init.c
@@ -35,48 +35,181 @@
#include <string.h>
#include <errno.h>
#include <locale.h>
+#include <ctype.h>
PJ_CVSID("$Id$");
+typedef struct {
+ projCtx ctx;
+ PAFile fid;
+ char buffer[8193];
+ int buffer_filled;
+ int at_eof;
+} pj_read_state;
+
+/************************************************************************/
+/* fill_buffer() */
+/************************************************************************/
+
+static const char *fill_buffer(pj_read_state *state, const char *last_char)
+{
+ size_t bytes_read;
+ int char_remaining, char_requested;
+
+/* -------------------------------------------------------------------- */
+/* Don't bother trying to read more if we are at eof, or if the */
+/* buffer is still over half full. */
+/* -------------------------------------------------------------------- */
+ if (last_char == NULL)
+ last_char = state->buffer;
+
+ if (state->at_eof)
+ return last_char;
+
+ char_remaining = state->buffer_filled - (last_char - state->buffer);
+ if (char_remaining >= sizeof(state->buffer) / 2)
+ return last_char;
+
+/* -------------------------------------------------------------------- */
+/* Move the existing data to the start of the buffer. */
+/* -------------------------------------------------------------------- */
+ memmove(state->buffer, last_char, char_remaining);
+ state->buffer_filled = char_remaining;
+ last_char = state->buffer;
+
+/* -------------------------------------------------------------------- */
+/* Refill. */
+/* -------------------------------------------------------------------- */
+ char_requested = sizeof(state->buffer) - state->buffer_filled - 1;
+ bytes_read = pj_ctx_fread( state->ctx, state->buffer + state->buffer_filled,
+ 1, char_requested, state->fid );
+ if (bytes_read < char_requested)
+ {
+ state->at_eof = 1;
+ state->buffer[state->buffer_filled + bytes_read] = '\0';
+ }
+
+ state->buffer_filled += bytes_read;
+ return last_char;
+}
+
/************************************************************************/
/* get_opt() */
/************************************************************************/
static paralist *
-get_opt(projCtx ctx, paralist **start, FILE *fid, char *name, paralist *next) {
- char sword[302], *word = sword+1;
- int first = 1, len, c;
+get_opt(projCtx ctx, paralist **start, PAFile fid, char *name, paralist *next) {
+ pj_read_state *state = (pj_read_state*) calloc(1,sizeof(pj_read_state));
+ char sword[302];
+ int len;
+ int in_target = 0;
+ const char *next_char = NULL;
+
+ state->fid = fid;
+ state->ctx = ctx;
+ next_char = fill_buffer(state, NULL);
len = strlen(name);
*sword = 't';
- while (fscanf(fid, "%300s", word) == 1) {
- if (*word == '#') /* skip comments */
- while((c = fgetc(fid)) != EOF && c != '\n') ;
- else if (*word == '<') { /* control name */
- if (first && !strncmp(name, word + 1, len)
- && word[len + 1] == '>')
- first = 0;
- else if (!first && *word == '<') {
- while((c = fgetc(fid)) != EOF && c != '\n') ;
+
+ /* loop till we find our target keyword */
+ while (*next_char)
+ {
+ next_char = fill_buffer(state, next_char);
+
+ /* Skip white space. */
+ while( isspace(*next_char) )
+ next_char++;
+
+ next_char = fill_buffer(state, next_char);
+
+ /* for comments, skip past end of line. */
+ if( *next_char == '#' )
+ {
+ while( *next_char && *next_char != '\n' )
+ next_char++;
+
+ next_char = fill_buffer(state, next_char);
+ if (*next_char == '\n')
+ next_char++;
+ if (*next_char == '\r')
+ next_char++;
+
+ }
+
+ /* Is this our target? */
+ else if( *next_char == '<' )
+ {
+ /* terminate processing target on the next block definition */
+ if (in_target)
break;
+
+ next_char++;
+ if (strncmp(name, next_char, len) == 0
+ && next_char[len] == '>')
+ {
+ /* skip past target word */
+ next_char += len + 1;
+ in_target = 1;
}
- } else if (!first && !pj_param(ctx, *start, sword).i) {
- /* don't default ellipse if datum, ellps or any earth model
- information is set. */
- if( strncmp(word,"ellps=",6) != 0
- || (!pj_param(ctx, *start, "tdatum").i
- && !pj_param(ctx, *start, "tellps").i
- && !pj_param(ctx, *start, "ta").i
- && !pj_param(ctx, *start, "tb").i
- && !pj_param(ctx, *start, "trf").i
- && !pj_param(ctx, *start, "tf").i) )
+ else
{
- next = next->next = pj_mkparam(word);
+ /* skip past end of line */
+ while( *next_char && *next_char != '\n' )
+ next_char++;
}
}
- }
+ else if (in_target)
+ {
+ const char *start_of_word = next_char;
+ int word_len = 0;
+
+ if (*start_of_word == '+')
+ {
+ start_of_word++;
+ next_char++;
+ }
+
+ /* capture parameter */
+ while( *next_char && !isspace(*next_char) )
+ {
+ next_char++;
+ word_len++;
+ }
+
+ strncpy(sword+1, start_of_word, word_len);
+ sword[word_len+1] = '\0';
+
+ /* do not override existing parameter value of same name */
+ if (!pj_param(ctx, *start, sword).i) {
+ /* don't default ellipse if datum, ellps or any earth model
+ information is set. */
+ if( strncmp(sword+1,"ellps=",6) != 0
+ || (!pj_param(ctx, *start, "tdatum").i
+ && !pj_param(ctx, *start, "tellps").i
+ && !pj_param(ctx, *start, "ta").i
+ && !pj_param(ctx, *start, "tb").i
+ && !pj_param(ctx, *start, "trf").i
+ && !pj_param(ctx, *start, "tf").i) )
+ {
+ next = next->next = pj_mkparam(sword+1);
+ }
+ }
+
+ }
+ else
+ {
+ /* skip past word */
+ while( *next_char && !isspace(*next_char) )
+ next_char++;
+
+ }
+ }
if (errno == 25)
errno = 0;
+
+ free(state);
+
return next;
}
@@ -85,13 +218,13 @@ get_opt(projCtx ctx, paralist **start, FILE *fid, char *name, paralist *next) {
/************************************************************************/
static paralist *
get_defaults(projCtx ctx, paralist **start, paralist *next, char *name) {
- FILE *fid;
+ PAFile fid;
if ( (fid = pj_open_lib(ctx,"proj_def.dat", "rt")) != NULL) {
next = get_opt(ctx, start, fid, "general", next);
- rewind(fid);
+ pj_ctx_fseek(ctx, fid, 0, SEEK_SET);
next = get_opt(ctx, start, fid, name, next);
- (void)fclose(fid);
+ pj_ctx_fclose(ctx, fid);
}
if (errno)
errno = 0; /* don't care if can't open file */
@@ -106,7 +239,7 @@ get_defaults(projCtx ctx, paralist **start, paralist *next, char *name) {
static paralist *
get_init(projCtx ctx, paralist **start, paralist *next, char *name) {
char fname[MAX_PATH_FILENAME+ID_TAG_MAX+3], *opt;
- FILE *fid;
+ PAFile fid;
paralist *init_items = NULL;
const paralist *orig_next = next;
@@ -136,7 +269,7 @@ get_init(projCtx ctx, paralist **start, paralist *next, char *name) {
next = get_opt(ctx, start, fid, opt, next);
else
return NULL;
- (void)fclose(fid);
+ pj_ctx_fclose(ctx, fid);
if (errno == 25)
errno = 0; /* unknown problem with some sys errno<-25 */
diff --git a/src/pj_open_lib.c b/src/pj_open_lib.c
index bc8c98ac..0a11dab4 100644
--- a/src/pj_open_lib.c
+++ b/src/pj_open_lib.c
@@ -98,11 +98,11 @@ void pj_set_searchpath ( int count, const char **path )
/* pj_open_lib() */
/************************************************************************/
-FILE *
+PAFile
pj_open_lib(projCtx ctx, char *name, char *mode) {
char fname[MAX_PATH_FILENAME+1];
const char *sysname;
- FILE *fid;
+ PAFile fid;
int n = 0;
int i;
#ifdef WIN32
@@ -145,7 +145,7 @@ pj_open_lib(projCtx ctx, char *name, char *mode) {
} else /* just try it bare bones */
sysname = name;
- if ((fid = fopen(sysname, mode)) != NULL)
+ if ((fid = pj_ctx_fopen(ctx, sysname, mode)) != NULL)
errno = 0;
/* If none of those work and we have a search path, try it */
@@ -155,7 +155,7 @@ pj_open_lib(projCtx ctx, char *name, char *mode) {
{
sprintf(fname, "%s%c%s", search_path[i], DIR_CHAR, name);
sysname = fname;
- fid = fopen (sysname, mode);
+ fid = pj_ctx_fopen(ctx, sysname, mode);
}
if (fid)
errno = 0;
diff --git a/src/projects.h b/src/projects.h
index fc64ba59..0e24566e 100644
--- a/src/projects.h
+++ b/src/projects.h
@@ -431,7 +431,7 @@ double *pj_authset(double);
double pj_authlat(double, double *);
COMPLEX pj_zpoly1(COMPLEX, COMPLEX *, int);
COMPLEX pj_zpolyd1(COMPLEX, COMPLEX *, int, COMPLEX *);
-FILE *pj_open_lib(projCtx, char *, char *);
+PAFile pj_open_lib(projCtx, char *, char *);
int pj_deriv(LP, double, PJ *, struct DERIVS *);
int pj_factors(LP, PJ *, double, struct FACTORS *);
@@ -462,10 +462,10 @@ int bch2bps(projUV, projUV, projUV **, int, int);
LP nad_intr(LP, struct CTABLE *);
LP nad_cvt(LP, int, struct CTABLE *);
struct CTABLE *nad_init(projCtx ctx, char *);
-struct CTABLE *nad_ctable_init( projCtx ctx, FILE * fid );
-int nad_ctable_load( projCtx ctx, struct CTABLE *, FILE * fid );
-struct CTABLE *nad_ctable2_init( projCtx ctx, FILE * fid );
-int nad_ctable2_load( projCtx ctx, struct CTABLE *, FILE * fid );
+struct CTABLE *nad_ctable_init( projCtx ctx, PAFile fid );
+int nad_ctable_load( projCtx ctx, struct CTABLE *, PAFile fid );
+struct CTABLE *nad_ctable2_init( projCtx ctx, PAFile fid );
+int nad_ctable2_load( projCtx ctx, struct CTABLE *, PAFile fid );
void nad_free(struct CTABLE *);
/* higher level handling of datum grid shift files */