From c07d0c2d2f6f7b0eb6e92cc6204bf05037957e82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Mon, 1 Sep 2014 15:43:52 +0200 Subject: Imported Upstream version 1.6.3 --- cgats/cgats.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 12 deletions(-) (limited to 'cgats/cgats.c') diff --git a/cgats/cgats.c b/cgats/cgats.c index baf9992..84ff2c9 100644 --- a/cgats/cgats.c +++ b/cgats/cgats.c @@ -14,6 +14,34 @@ * see the License.txt file in this directory for licensing details. */ +/* + + Should add a function to promote a field type, ie. + promote integer to float, to avoid misrecognition + problems. + + + To make this more portable for independent use, + should save/set/restore LC_NUMERIC locale before + printf/scanf from file. e.g. + + include + char *old_locale, *saved_locale; + + old_locale = setlocale (LC_NUMERIC, NULL); + saved_locale = strdup (old_locale); + if (saved_locale == NULL) + error ("Out of memory"); + setlocale (LC_NUMERIC, "C"); + + .... read or write ... + + setlocale (LC_NUMERIC, saved_locale); + free (saved_locale); + + Also apply to pars.c + */ + #define _CGATS_C_ /* Turn on implimentation code */ #include @@ -23,8 +51,8 @@ #undef DEBUG /* Debug only in slected places */ #ifdef DEBUG -# define DBGA g_log, 0 /* First argument to DBGF() */ -# define DBGF(xx) a1logd xx +# define DBGA stderr +# define DBGF(xx) fprintf xx #else # define DBGF(xx) #endif @@ -40,7 +68,7 @@ extern void error(const char *fmt, ...), warning(const char *fmt, ...); #include "pars.h" #include "cgats.h" -#define REAL_SIGDIG 5 /* Number of significant digits in real representation */ +#define REAL_SIGDIG 6 /* Number of significant digits in real representation */ static int cgats_read(cgats *p, cgatsFile *fp); static int find_kword(cgats *p, int table, const char *ksym); @@ -374,6 +402,14 @@ cgats_read(cgats *p, cgatsFile *fp) { if (tp == NULL) break; /* EOF */ + /* This seems unlikely and will cause err() to barf */ + if (strlen(tp) > CGATS_ERRM_LENGTH/2) { + tp[CGATS_ERRM_LENGTH/2] = '\000'; + err(p,-1,"Read line got symbol '%s' that's too long\n",tp); + pp->del(pp); + return p->errc; + } + switch(rstate) { case R_IDENT: /* Expecting file identifier */ case R_KWORDS: { /* Expecting keyword, field def or data */ @@ -938,16 +974,19 @@ add_field(cgats *p, int table, const char *fsym, data_type ftype) { p->errc = 0; p->err[0] = '\000'; - if (table < 0 || table >= p->ntables) + if (table < 0 || table >= p->ntables) { return err(p,-1,"cgats.add_field(), table parameter out of range"); + } t = &p->t[table]; - if (t->nsets != 0) + if (t->nsets != 0) { return err(p,-1,"cgats.add_field(), attempt to add field to non-empty table"); + } /* Check the field name is reasonable */ - if (cs_has_ws(fsym)) + if (cs_has_ws(fsym)) { return err(p,-1,"cgats.add_kword(), field name '%s'is illegal",fsym); + } if (ftype == none_t) ftype = cs_t; /* Fudge - unknown type yet, used for reads */ @@ -956,22 +995,27 @@ add_field(cgats *p, int table, const char *fsym, data_type ftype) { st = standard_field(fsym); if (st == nqcs_t && ftype == cs_t) /* Fudge - standard type to non-quoted if normal */ ftype = nqcs_t; - if (st != none_t && st != ftype) + if (st != none_t && st != ftype) { return err(p,-1,"cgats.add_field(): unexpected data type for standard field name"); + } } t->nfields++; if (t->nfields > t->nfieldsa) { - /* Allocate fields in groups of 4 */ - t->nfieldsa += 4; - if ((t->fsym = (char **)al->realloc(al, t->fsym, t->nfieldsa * sizeof(char *))) == NULL) + /* Allocate fields in groups of 32 */ + t->nfieldsa += 32; + if ((t->fsym = (char **)al->realloc(al, t->fsym, t->nfieldsa * sizeof(char *))) == NULL) { return err(p,-2,"cgats.add_field(), realloc failed!"); + } if ((t->ftype = (data_type *)al->realloc(al, t->ftype, t->nfieldsa * sizeof(data_type))) - == NULL) + == NULL) { return err(p,-2,"cgats.add_field(), realloc failed!"); + } } - if ((t->fsym[t->nfields-1] = (char *)alloc_copy_data_type(al, cs_t, (void *)fsym)) == NULL) + if ((t->fsym[t->nfields-1] = (char *)alloc_copy_data_type(al, cs_t, (void *)fsym)) == NULL) { return err(p,-2,"cgats.alloc_copy_data_type() malloc fail"); + } + t->ftype[t->nfields-1] = ftype; return t->nfields-1; -- cgit v1.2.3