summaryrefslogtreecommitdiff
path: root/cgats
diff options
context:
space:
mode:
Diffstat (limited to 'cgats')
-rw-r--r--cgats/cgats.c68
-rw-r--r--cgats/cgats.h2
-rw-r--r--cgats/pars.c30
3 files changed, 81 insertions, 19 deletions
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 <locale.h>
+ 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 <stdio.h>
@@ -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;
diff --git a/cgats/cgats.h b/cgats/cgats.h
index 2a6b1fd..7537a48 100644
--- a/cgats/cgats.h
+++ b/cgats/cgats.h
@@ -20,7 +20,7 @@
#define CGATSLIB_VERSION 0x020005
#define CGATSLIB_VERSION_STR "2.05"
-#define CGATS_ERRM_LENGTH 200
+#define CGATS_ERRM_LENGTH 2000
#ifdef __cplusplus
extern "C" {
diff --git a/cgats/pars.c b/cgats/pars.c
index 606957c..a0e694c 100644
--- a/cgats/pars.c
+++ b/cgats/pars.c
@@ -41,6 +41,8 @@ extern void error(const char *fmt, ...), warning(const char *fmt, ...);
#include "pars.h"
+#undef DEBUG /* Print each token returned */
+
static void del_parse(parse *p);
static int read_line(parse *p);
static void reset_del(parse *p);
@@ -420,6 +422,9 @@ read_line(parse *p) {
if ((c = p->fp->getch(p->fp)) == EOF) {
if (p->bo == 0) { /* If there is nothing in the buffer */
p->line = 0;
+#ifdef DEBUG
+ printf("pars: read_line() got EOF\n");
+#endif
return 0;
}
c = 0; /* Finish the line */
@@ -443,16 +448,16 @@ read_line(parse *p) {
}
if (c == '\r') {
- p->line++; /* Increment line number */
+ p->line++; /* Increment line number */
p->ltflag = 1; /* Remember to allow 1 of '\n' before next line */
if (p->q == 0)
- c = 0; /* Finish the line */
+ c = 0; /* Finish the line */
} else if (p->q == 0 && (p->delf[c] & PARS_COMM) != 0) { /* Hit a comment */
- p->line++; /* Increment line number */
+ p->line++; /* Increment line number */
p->ltflag = 2; /* Remember to flush all chars up to end of line */
- c = 0; /* Finish the line */
+ c = 0; /* Finish the line */
} else if (c == '\n') {
- p->line++; /* Increment line number */
+ p->line++; /* Increment line number */
if (p->q == 0)
c = 0; /* Finish the the line */
}
@@ -477,6 +482,9 @@ read_line(parse *p) {
} while (c != 0); /* Null means we've done the end of the line */
p->to = 0; /* Reset token pointer to the start of the line buffer */
p->q = 0; /* Reset quoted flag */
+#ifdef DEBUG
+ printf("pars: read_line() got buffer '%s'\n",p->b);
+#endif
return 1;
}
@@ -525,8 +533,12 @@ get_token(parse *p) {
p->errc = 0; /* Reset error status */
p->err[0] = '\000';
- if (p->b == NULL)
+ if (p->b == NULL) {
+#ifdef DEBUG
+ printf("pars: read_token() NULL buffe\n");
+#endif
return NULL;
+ }
p->token++; /* Increment token number */
p->q = 0;
do {
@@ -566,8 +578,14 @@ get_token(parse *p) {
p->q = 0;
if (tbo <= 1) {
p->token = 0;
+#ifdef DEBUG
+ printf("pars: read_token() got nothing useful\n");
+#endif
return NULL; /* Haven't read anything useful */
}
+#ifdef DEBUG
+ printf("pars: read_token() returning '%s'\n",p->tb);
+#endif
return p->tb;
}