alpar@1: /* glpmpl06.c */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #define _GLPSTD_ERRNO alpar@1: #define _GLPSTD_STDIO alpar@1: #include "glpmpl.h" alpar@1: #include "glpsql.h" alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: #define CSV_FIELD_MAX 50 alpar@1: /* maximal number of fields in record */ alpar@1: alpar@1: #define CSV_FDLEN_MAX 100 alpar@1: /* maximal field length */ alpar@1: alpar@1: struct csv alpar@1: { /* comma-separated values file */ alpar@1: int mode; alpar@1: /* 'R' = reading; 'W' = writing */ alpar@1: char *fname; alpar@1: /* name of csv file */ alpar@1: FILE *fp; alpar@1: /* stream assigned to csv file */ alpar@1: jmp_buf jump; alpar@1: /* address for non-local go to in case of error */ alpar@1: int count; alpar@1: /* record count */ alpar@1: /*--------------------------------------------------------------*/ alpar@1: /* used only for input csv file */ alpar@1: int c; alpar@1: /* current character or EOF */ alpar@1: int what; alpar@1: /* current marker: */ alpar@1: #define CSV_EOF 0 /* end-of-file */ alpar@1: #define CSV_EOR 1 /* end-of-record */ alpar@1: #define CSV_NUM 2 /* floating-point number */ alpar@1: #define CSV_STR 3 /* character string */ alpar@1: char field[CSV_FDLEN_MAX+1]; alpar@1: /* current field just read */ alpar@1: int nf; alpar@1: /* number of fields in the csv file */ alpar@1: int ref[1+CSV_FIELD_MAX]; alpar@1: /* ref[k] = k', if k-th field of the csv file corresponds to alpar@1: k'-th field in the table statement; if ref[k] = 0, k-th field alpar@1: of the csv file is ignored */ alpar@1: #if 1 /* 01/VI-2010 */ alpar@1: int nskip; alpar@1: /* number of comment records preceding the header record */ alpar@1: #endif alpar@1: }; alpar@1: alpar@1: #undef read_char alpar@1: alpar@1: static void read_char(struct csv *csv) alpar@1: { /* read character from csv data file */ alpar@1: int c; alpar@1: xassert(csv->c != EOF); alpar@1: if (csv->c == '\n') csv->count++; alpar@1: loop: c = fgetc(csv->fp); alpar@1: if (ferror(csv->fp)) alpar@1: { xprintf("%s:%d: read error - %s\n", csv->fname, csv->count, alpar@1: strerror(errno)); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: if (feof(csv->fp)) alpar@1: { if (csv->c == '\n') alpar@1: { csv->count--; alpar@1: c = EOF; alpar@1: } alpar@1: else alpar@1: { xprintf("%s:%d: warning: missing final end-of-line\n", alpar@1: csv->fname, csv->count); alpar@1: c = '\n'; alpar@1: } alpar@1: } alpar@1: else if (c == '\r') alpar@1: goto loop; alpar@1: else if (c == '\n') alpar@1: ; alpar@1: else if (iscntrl(c)) alpar@1: { xprintf("%s:%d: invalid control character 0x%02X\n", alpar@1: csv->fname, csv->count, c); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: csv->c = c; alpar@1: return; alpar@1: } alpar@1: alpar@1: static void read_field(struct csv *csv) alpar@1: { /* read field from csv data file */ alpar@1: /* check for end of file */ alpar@1: if (csv->c == EOF) alpar@1: { csv->what = CSV_EOF; alpar@1: strcpy(csv->field, "EOF"); alpar@1: goto done; alpar@1: } alpar@1: /* check for end of record */ alpar@1: if (csv->c == '\n') alpar@1: { csv->what = CSV_EOR; alpar@1: strcpy(csv->field, "EOR"); alpar@1: read_char(csv); alpar@1: if (csv->c == ',') alpar@1: err1: { xprintf("%s:%d: empty field not allowed\n", csv->fname, alpar@1: csv->count); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: if (csv->c == '\n') alpar@1: { xprintf("%s:%d: empty record not allowed\n", csv->fname, alpar@1: csv->count); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: #if 1 /* 01/VI-2010 */ alpar@1: /* skip comment records; may appear only before the very first alpar@1: record containing field names */ alpar@1: if (csv->c == '#' && csv->count == 1) alpar@1: { while (csv->c == '#') alpar@1: { while (csv->c != '\n') alpar@1: read_char(csv); alpar@1: read_char(csv); alpar@1: csv->nskip++; alpar@1: } alpar@1: } alpar@1: #endif alpar@1: goto done; alpar@1: } alpar@1: /* skip comma before next field */ alpar@1: if (csv->c == ',') alpar@1: read_char(csv); alpar@1: /* read field */ alpar@1: if (csv->c == '\'' || csv->c == '"') alpar@1: { /* read a field enclosed in quotes */ alpar@1: int quote = csv->c, len = 0; alpar@1: csv->what = CSV_STR; alpar@1: /* skip opening quote */ alpar@1: read_char(csv); alpar@1: /* read field characters within quotes */ alpar@1: for (;;) alpar@1: { /* check for closing quote and read it */ alpar@1: if (csv->c == quote) alpar@1: { read_char(csv); alpar@1: if (csv->c == quote) alpar@1: ; alpar@1: else if (csv->c == ',' || csv->c == '\n') alpar@1: break; alpar@1: else alpar@1: { xprintf("%s:%d: invalid field\n", csv->fname, alpar@1: csv->count); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: } alpar@1: /* check the current field length */ alpar@1: if (len == CSV_FDLEN_MAX) alpar@1: err2: { xprintf("%s:%d: field too long\n", csv->fname, alpar@1: csv->count); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: /* add the current character to the field */ alpar@1: csv->field[len++] = (char)csv->c; alpar@1: /* read the next character */ alpar@1: read_char(csv); alpar@1: } alpar@1: /* the field has been read */ alpar@1: if (len == 0) goto err1; alpar@1: csv->field[len] = '\0'; alpar@1: } alpar@1: else alpar@1: { /* read a field not enclosed in quotes */ alpar@1: int len = 0; alpar@1: double temp; alpar@1: csv->what = CSV_NUM; alpar@1: while (!(csv->c == ',' || csv->c == '\n')) alpar@1: { /* quotes within the field are not allowed */ alpar@1: if (csv->c == '\'' || csv->c == '"') alpar@1: { xprintf("%s:%d: invalid use of single or double quote wi" alpar@1: "thin field\n", csv->fname, csv->count); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: /* check the current field length */ alpar@1: if (len == CSV_FDLEN_MAX) goto err2; alpar@1: /* add the current character to the field */ alpar@1: csv->field[len++] = (char)csv->c; alpar@1: /* read the next character */ alpar@1: read_char(csv); alpar@1: } alpar@1: /* the field has been read */ alpar@1: if (len == 0) goto err1; alpar@1: csv->field[len] = '\0'; alpar@1: /* check the field type */ alpar@1: if (str2num(csv->field, &temp)) csv->what = CSV_STR; alpar@1: } alpar@1: done: return; alpar@1: } alpar@1: alpar@1: static struct csv *csv_open_file(TABDCA *dca, int mode) alpar@1: { /* open csv data file */ alpar@1: struct csv *csv; alpar@1: /* create control structure */ alpar@1: csv = xmalloc(sizeof(struct csv)); alpar@1: csv->mode = mode; alpar@1: csv->fname = NULL; alpar@1: csv->fp = NULL; alpar@1: if (setjmp(csv->jump)) goto fail; alpar@1: csv->count = 0; alpar@1: csv->c = '\n'; alpar@1: csv->what = 0; alpar@1: csv->field[0] = '\0'; alpar@1: csv->nf = 0; alpar@1: /* try to open the csv data file */ alpar@1: if (mpl_tab_num_args(dca) < 2) alpar@1: { xprintf("csv_driver: file name not specified\n"); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: csv->fname = xmalloc(strlen(mpl_tab_get_arg(dca, 2))+1); alpar@1: strcpy(csv->fname, mpl_tab_get_arg(dca, 2)); alpar@1: if (mode == 'R') alpar@1: { /* open the file for reading */ alpar@1: int k; alpar@1: csv->fp = fopen(csv->fname, "r"); alpar@1: if (csv->fp == NULL) alpar@1: { xprintf("csv_driver: unable to open %s - %s\n", alpar@1: csv->fname, strerror(errno)); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: #if 1 /* 01/VI-2010 */ alpar@1: csv->nskip = 0; alpar@1: #endif alpar@1: /* skip fake new-line */ alpar@1: read_field(csv); alpar@1: xassert(csv->what == CSV_EOR); alpar@1: /* read field names */ alpar@1: xassert(csv->nf == 0); alpar@1: for (;;) alpar@1: { read_field(csv); alpar@1: if (csv->what == CSV_EOR) alpar@1: break; alpar@1: if (csv->what != CSV_STR) alpar@1: { xprintf("%s:%d: invalid field name\n", csv->fname, alpar@1: csv->count); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: if (csv->nf == CSV_FIELD_MAX) alpar@1: { xprintf("%s:%d: too many fields\n", csv->fname, alpar@1: csv->count); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: csv->nf++; alpar@1: /* find corresponding field in the table statement */ alpar@1: for (k = mpl_tab_num_flds(dca); k >= 1; k--) alpar@1: { if (strcmp(mpl_tab_get_name(dca, k), csv->field) == 0) alpar@1: break; alpar@1: } alpar@1: csv->ref[csv->nf] = k; alpar@1: } alpar@1: /* find dummy RECNO field in the table statement */ alpar@1: for (k = mpl_tab_num_flds(dca); k >= 1; k--) alpar@1: if (strcmp(mpl_tab_get_name(dca, k), "RECNO") == 0) break; alpar@1: csv->ref[0] = k; alpar@1: } alpar@1: else if (mode == 'W') alpar@1: { /* open the file for writing */ alpar@1: int k, nf; alpar@1: csv->fp = fopen(csv->fname, "w"); alpar@1: if (csv->fp == NULL) alpar@1: { xprintf("csv_driver: unable to create %s - %s\n", alpar@1: csv->fname, strerror(errno)); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: /* write field names */ alpar@1: nf = mpl_tab_num_flds(dca); alpar@1: for (k = 1; k <= nf; k++) alpar@1: fprintf(csv->fp, "%s%c", mpl_tab_get_name(dca, k), alpar@1: k < nf ? ',' : '\n'); alpar@1: csv->count++; alpar@1: } alpar@1: else alpar@1: xassert(mode != mode); alpar@1: /* the file has been open */ alpar@1: return csv; alpar@1: fail: /* the file cannot be open */ alpar@1: if (csv->fname != NULL) xfree(csv->fname); alpar@1: if (csv->fp != NULL) fclose(csv->fp); alpar@1: xfree(csv); alpar@1: return NULL; alpar@1: } alpar@1: alpar@1: static int csv_read_record(TABDCA *dca, struct csv *csv) alpar@1: { /* read next record from csv data file */ alpar@1: int k, ret = 0; alpar@1: xassert(csv->mode == 'R'); alpar@1: if (setjmp(csv->jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: /* read dummy RECNO field */ alpar@1: if (csv->ref[0] > 0) alpar@1: #if 0 /* 01/VI-2010 */ alpar@1: mpl_tab_set_num(dca, csv->ref[0], csv->count-1); alpar@1: #else alpar@1: mpl_tab_set_num(dca, csv->ref[0], csv->count-csv->nskip-1); alpar@1: #endif alpar@1: /* read fields */ alpar@1: for (k = 1; k <= csv->nf; k++) alpar@1: { read_field(csv); alpar@1: if (csv->what == CSV_EOF) alpar@1: { /* end-of-file reached */ alpar@1: xassert(k == 1); alpar@1: ret = -1; alpar@1: goto done; alpar@1: } alpar@1: else if (csv->what == CSV_EOR) alpar@1: { /* end-of-record reached */ alpar@1: int lack = csv->nf - k + 1; alpar@1: if (lack == 1) alpar@1: xprintf("%s:%d: one field missing\n", csv->fname, alpar@1: csv->count); alpar@1: else alpar@1: xprintf("%s:%d: %d fields missing\n", csv->fname, alpar@1: csv->count, lack); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: else if (csv->what == CSV_NUM) alpar@1: { /* floating-point number */ alpar@1: if (csv->ref[k] > 0) alpar@1: { double num; alpar@1: xassert(str2num(csv->field, &num) == 0); alpar@1: mpl_tab_set_num(dca, csv->ref[k], num); alpar@1: } alpar@1: } alpar@1: else if (csv->what == CSV_STR) alpar@1: { /* character string */ alpar@1: if (csv->ref[k] > 0) alpar@1: mpl_tab_set_str(dca, csv->ref[k], csv->field); alpar@1: } alpar@1: else alpar@1: xassert(csv != csv); alpar@1: } alpar@1: /* now there must be NL */ alpar@1: read_field(csv); alpar@1: xassert(csv->what != CSV_EOF); alpar@1: if (csv->what != CSV_EOR) alpar@1: { xprintf("%s:%d: too many fields\n", csv->fname, csv->count); alpar@1: longjmp(csv->jump, 0); alpar@1: } alpar@1: done: return ret; alpar@1: } alpar@1: alpar@1: static int csv_write_record(TABDCA *dca, struct csv *csv) alpar@1: { /* write next record to csv data file */ alpar@1: int k, nf, ret = 0; alpar@1: const char *c; alpar@1: xassert(csv->mode == 'W'); alpar@1: nf = mpl_tab_num_flds(dca); alpar@1: for (k = 1; k <= nf; k++) alpar@1: { switch (mpl_tab_get_type(dca, k)) alpar@1: { case 'N': alpar@1: fprintf(csv->fp, "%.*g", DBL_DIG, alpar@1: mpl_tab_get_num(dca, k)); alpar@1: break; alpar@1: case 'S': alpar@1: fputc('"', csv->fp); alpar@1: for (c = mpl_tab_get_str(dca, k); *c != '\0'; c++) alpar@1: { if (*c == '"') alpar@1: fputc('"', csv->fp), fputc('"', csv->fp); alpar@1: else alpar@1: fputc(*c, csv->fp); alpar@1: } alpar@1: fputc('"', csv->fp); alpar@1: break; alpar@1: default: alpar@1: xassert(dca != dca); alpar@1: } alpar@1: fputc(k < nf ? ',' : '\n', csv->fp); alpar@1: } alpar@1: csv->count++; alpar@1: if (ferror(csv->fp)) alpar@1: { xprintf("%s:%d: write error - %s\n", csv->fname, csv->count, alpar@1: strerror(errno)); alpar@1: ret = 1; alpar@1: } alpar@1: return ret; alpar@1: } alpar@1: alpar@1: static int csv_close_file(TABDCA *dca, struct csv *csv) alpar@1: { /* close csv data file */ alpar@1: int ret = 0; alpar@1: xassert(dca == dca); alpar@1: if (csv->mode == 'W') alpar@1: { fflush(csv->fp); alpar@1: if (ferror(csv->fp)) alpar@1: { xprintf("%s:%d: write error - %s\n", csv->fname, alpar@1: csv->count, strerror(errno)); alpar@1: ret = 1; alpar@1: } alpar@1: } alpar@1: xfree(csv->fname); alpar@1: fclose(csv->fp); alpar@1: xfree(csv); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: #define DBF_FIELD_MAX 50 alpar@1: /* maximal number of fields in record */ alpar@1: alpar@1: #define DBF_FDLEN_MAX 100 alpar@1: /* maximal field length */ alpar@1: alpar@1: struct dbf alpar@1: { /* xBASE data file */ alpar@1: int mode; alpar@1: /* 'R' = reading; 'W' = writing */ alpar@1: char *fname; alpar@1: /* name of xBASE file */ alpar@1: FILE *fp; alpar@1: /* stream assigned to xBASE file */ alpar@1: jmp_buf jump; alpar@1: /* address for non-local go to in case of error */ alpar@1: int offset; alpar@1: /* offset of a byte to be read next */ alpar@1: int count; alpar@1: /* record count */ alpar@1: int nf; alpar@1: /* number of fields */ alpar@1: int ref[1+DBF_FIELD_MAX]; alpar@1: /* ref[k] = k', if k-th field of the csv file corresponds to alpar@1: k'-th field in the table statement; if ref[k] = 0, k-th field alpar@1: of the csv file is ignored */ alpar@1: int type[1+DBF_FIELD_MAX]; alpar@1: /* type[k] is type of k-th field */ alpar@1: int len[1+DBF_FIELD_MAX]; alpar@1: /* len[k] is length of k-th field */ alpar@1: int prec[1+DBF_FIELD_MAX]; alpar@1: /* prec[k] is precision of k-th field */ alpar@1: }; alpar@1: alpar@1: static int read_byte(struct dbf *dbf) alpar@1: { /* read byte from xBASE data file */ alpar@1: int b; alpar@1: b = fgetc(dbf->fp); alpar@1: if (ferror(dbf->fp)) alpar@1: { xprintf("%s:0x%X: read error - %s\n", dbf->fname, alpar@1: dbf->offset, strerror(errno)); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: if (feof(dbf->fp)) alpar@1: { xprintf("%s:0x%X: unexpected end of file\n", dbf->fname, alpar@1: dbf->offset); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: xassert(0x00 <= b && b <= 0xFF); alpar@1: dbf->offset++; alpar@1: return b; alpar@1: } alpar@1: alpar@1: static void read_header(TABDCA *dca, struct dbf *dbf) alpar@1: { /* read xBASE data file header */ alpar@1: int b, j, k, recl; alpar@1: char name[10+1]; alpar@1: /* (ignored) */ alpar@1: for (j = 1; j <= 10; j++) alpar@1: read_byte(dbf); alpar@1: /* length of each record, in bytes */ alpar@1: recl = read_byte(dbf); alpar@1: recl += read_byte(dbf) << 8; alpar@1: /* (ignored) */ alpar@1: for (j = 1; j <= 20; j++) alpar@1: read_byte(dbf); alpar@1: /* field descriptor array */ alpar@1: xassert(dbf->nf == 0); alpar@1: for (;;) alpar@1: { /* check for end of array */ alpar@1: b = read_byte(dbf); alpar@1: if (b == 0x0D) break; alpar@1: if (dbf->nf == DBF_FIELD_MAX) alpar@1: { xprintf("%s:0x%X: too many fields\n", dbf->fname, alpar@1: dbf->offset); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: dbf->nf++; alpar@1: /* field name */ alpar@1: name[0] = (char)b; alpar@1: for (j = 1; j < 10; j++) alpar@1: { b = read_byte(dbf); alpar@1: name[j] = (char)b; alpar@1: } alpar@1: name[10] = '\0'; alpar@1: b = read_byte(dbf); alpar@1: if (b != 0x00) alpar@1: { xprintf("%s:0x%X: invalid field name\n", dbf->fname, alpar@1: dbf->offset); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: /* find corresponding field in the table statement */ alpar@1: for (k = mpl_tab_num_flds(dca); k >= 1; k--) alpar@1: if (strcmp(mpl_tab_get_name(dca, k), name) == 0) break; alpar@1: dbf->ref[dbf->nf] = k; alpar@1: /* field type */ alpar@1: b = read_byte(dbf); alpar@1: if (!(b == 'C' || b == 'N')) alpar@1: { xprintf("%s:0x%X: invalid field type\n", dbf->fname, alpar@1: dbf->offset); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: dbf->type[dbf->nf] = b; alpar@1: /* (ignored) */ alpar@1: for (j = 1; j <= 4; j++) alpar@1: read_byte(dbf); alpar@1: /* field length */ alpar@1: b = read_byte(dbf); alpar@1: if (b == 0) alpar@1: { xprintf("%s:0x%X: invalid field length\n", dbf->fname, alpar@1: dbf->offset); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: if (b > DBF_FDLEN_MAX) alpar@1: { xprintf("%s:0x%X: field too long\n", dbf->fname, alpar@1: dbf->offset); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: dbf->len[dbf->nf] = b; alpar@1: recl -= b; alpar@1: /* (ignored) */ alpar@1: for (j = 1; j <= 15; j++) alpar@1: read_byte(dbf); alpar@1: } alpar@1: if (recl != 1) alpar@1: { xprintf("%s:0x%X: invalid file header\n", dbf->fname, alpar@1: dbf->offset); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: /* find dummy RECNO field in the table statement */ alpar@1: for (k = mpl_tab_num_flds(dca); k >= 1; k--) alpar@1: if (strcmp(mpl_tab_get_name(dca, k), "RECNO") == 0) break; alpar@1: dbf->ref[0] = k; alpar@1: return; alpar@1: } alpar@1: alpar@1: static void parse_third_arg(TABDCA *dca, struct dbf *dbf) alpar@1: { /* parse xBASE file format (third argument) */ alpar@1: int j, k, temp; alpar@1: const char *arg; alpar@1: dbf->nf = mpl_tab_num_flds(dca); alpar@1: arg = mpl_tab_get_arg(dca, 3), j = 0; alpar@1: for (k = 1; k <= dbf->nf; k++) alpar@1: { /* parse specification of k-th field */ alpar@1: if (arg[j] == '\0') alpar@1: { xprintf("xBASE driver: field %s: specification missing\n", alpar@1: mpl_tab_get_name(dca, k)); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: /* parse field type */ alpar@1: if (arg[j] == 'C' || arg[j] == 'N') alpar@1: dbf->type[k] = arg[j], j++; alpar@1: else alpar@1: { xprintf("xBASE driver: field %s: invalid field type\n", alpar@1: mpl_tab_get_name(dca, k)); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: /* check for left parenthesis */ alpar@1: if (arg[j] == '(') alpar@1: j++; alpar@1: else alpar@1: err: { xprintf("xBASE driver: field %s: invalid field format\n", alpar@1: mpl_tab_get_name(dca, k)); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: /* parse field length */ alpar@1: temp = 0; alpar@1: while (isdigit(arg[j])) alpar@1: { if (temp > DBF_FDLEN_MAX) break; alpar@1: temp = 10 * temp + (arg[j] - '0'), j++; alpar@1: } alpar@1: if (!(1 <= temp && temp <= DBF_FDLEN_MAX)) alpar@1: { xprintf("xBASE driver: field %s: invalid field length\n", alpar@1: mpl_tab_get_name(dca, k)); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: dbf->len[k] = temp; alpar@1: /* parse optional field precision */ alpar@1: if (dbf->type[k] == 'N' && arg[j] == ',') alpar@1: { j++; alpar@1: temp = 0; alpar@1: while (isdigit(arg[j])) alpar@1: { if (temp > dbf->len[k]) break; alpar@1: temp = 10 * temp + (arg[j] - '0'), j++; alpar@1: } alpar@1: if (temp > dbf->len[k]) alpar@1: { xprintf("xBASE driver: field %s: invalid field precision" alpar@1: "\n", mpl_tab_get_name(dca, k)); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: dbf->prec[k] = temp; alpar@1: } alpar@1: else alpar@1: dbf->prec[k] = 0; alpar@1: /* check for right parenthesis */ alpar@1: if (arg[j] == ')') alpar@1: j++; alpar@1: else alpar@1: goto err; alpar@1: } alpar@1: /* ignore other specifications */ alpar@1: return; alpar@1: } alpar@1: alpar@1: static void write_byte(struct dbf *dbf, int b) alpar@1: { /* write byte to xBASE data file */ alpar@1: fputc(b, dbf->fp); alpar@1: dbf->offset++; alpar@1: return; alpar@1: } alpar@1: alpar@1: static void write_header(TABDCA *dca, struct dbf *dbf) alpar@1: { /* write xBASE data file header */ alpar@1: int j, k, temp; alpar@1: const char *name; alpar@1: /* version number */ alpar@1: write_byte(dbf, 0x03 /* file without DBT */); alpar@1: /* date of last update (YYMMDD) */ alpar@1: write_byte(dbf, 70 /* 1970 */); alpar@1: write_byte(dbf, 1 /* January */); alpar@1: write_byte(dbf, 1 /* 1st */); alpar@1: /* number of records (unknown so far) */ alpar@1: for (j = 1; j <= 4; j++) alpar@1: write_byte(dbf, 0xFF); alpar@1: /* length of the header, in bytes */ alpar@1: temp = 32 + dbf->nf * 32 + 1; alpar@1: write_byte(dbf, temp); alpar@1: write_byte(dbf, temp >> 8); alpar@1: /* length of each record, in bytes */ alpar@1: temp = 1; alpar@1: for (k = 1; k <= dbf->nf; k++) alpar@1: temp += dbf->len[k]; alpar@1: write_byte(dbf, temp); alpar@1: write_byte(dbf, temp >> 8); alpar@1: /* (reserved) */ alpar@1: for (j = 1; j <= 20; j++) alpar@1: write_byte(dbf, 0x00); alpar@1: /* field descriptor array */ alpar@1: for (k = 1; k <= dbf->nf; k++) alpar@1: { /* field name (terminated by 0x00) */ alpar@1: name = mpl_tab_get_name(dca, k); alpar@1: for (j = 0; j < 10 && name[j] != '\0'; j++) alpar@1: write_byte(dbf, name[j]); alpar@1: for (j = j; j < 11; j++) alpar@1: write_byte(dbf, 0x00); alpar@1: /* field type */ alpar@1: write_byte(dbf, dbf->type[k]); alpar@1: /* (reserved) */ alpar@1: for (j = 1; j <= 4; j++) alpar@1: write_byte(dbf, 0x00); alpar@1: /* field length */ alpar@1: write_byte(dbf, dbf->len[k]); alpar@1: /* field precision */ alpar@1: write_byte(dbf, dbf->prec[k]); alpar@1: /* (reserved) */ alpar@1: for (j = 1; j <= 14; j++) alpar@1: write_byte(dbf, 0x00); alpar@1: } alpar@1: /* end of header */ alpar@1: write_byte(dbf, 0x0D); alpar@1: return; alpar@1: } alpar@1: alpar@1: static struct dbf *dbf_open_file(TABDCA *dca, int mode) alpar@1: { /* open xBASE data file */ alpar@1: struct dbf *dbf; alpar@1: /* create control structure */ alpar@1: dbf = xmalloc(sizeof(struct dbf)); alpar@1: dbf->mode = mode; alpar@1: dbf->fname = NULL; alpar@1: dbf->fp = NULL; alpar@1: if (setjmp(dbf->jump)) goto fail; alpar@1: dbf->offset = 0; alpar@1: dbf->count = 0; alpar@1: dbf->nf = 0; alpar@1: /* try to open the xBASE data file */ alpar@1: if (mpl_tab_num_args(dca) < 2) alpar@1: { xprintf("xBASE driver: file name not specified\n"); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: dbf->fname = xmalloc(strlen(mpl_tab_get_arg(dca, 2))+1); alpar@1: strcpy(dbf->fname, mpl_tab_get_arg(dca, 2)); alpar@1: if (mode == 'R') alpar@1: { /* open the file for reading */ alpar@1: dbf->fp = fopen(dbf->fname, "rb"); alpar@1: if (dbf->fp == NULL) alpar@1: { xprintf("xBASE driver: unable to open %s - %s\n", alpar@1: dbf->fname, strerror(errno)); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: read_header(dca, dbf); alpar@1: } alpar@1: else if (mode == 'W') alpar@1: { /* open the file for writing */ alpar@1: if (mpl_tab_num_args(dca) < 3) alpar@1: { xprintf("xBASE driver: file format not specified\n"); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: parse_third_arg(dca, dbf); alpar@1: dbf->fp = fopen(dbf->fname, "wb"); alpar@1: if (dbf->fp == NULL) alpar@1: { xprintf("xBASE driver: unable to create %s - %s\n", alpar@1: dbf->fname, strerror(errno)); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: write_header(dca, dbf); alpar@1: } alpar@1: else alpar@1: xassert(mode != mode); alpar@1: /* the file has been open */ alpar@1: return dbf; alpar@1: fail: /* the file cannot be open */ alpar@1: if (dbf->fname != NULL) xfree(dbf->fname); alpar@1: if (dbf->fp != NULL) fclose(dbf->fp); alpar@1: xfree(dbf); alpar@1: return NULL; alpar@1: } alpar@1: alpar@1: static int dbf_read_record(TABDCA *dca, struct dbf *dbf) alpar@1: { /* read next record from xBASE data file */ alpar@1: int b, j, k, ret = 0; alpar@1: char buf[DBF_FDLEN_MAX+1]; alpar@1: xassert(dbf->mode == 'R'); alpar@1: if (setjmp(dbf->jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: /* check record flag */ alpar@1: b = read_byte(dbf); alpar@1: if (b == 0x1A) alpar@1: { /* end of data */ alpar@1: ret = -1; alpar@1: goto done; alpar@1: } alpar@1: if (b != 0x20) alpar@1: { xprintf("%s:0x%X: invalid record flag\n", dbf->fname, alpar@1: dbf->offset); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: /* read dummy RECNO field */ alpar@1: if (dbf->ref[0] > 0) alpar@1: mpl_tab_set_num(dca, dbf->ref[0], dbf->count+1); alpar@1: /* read fields */ alpar@1: for (k = 1; k <= dbf->nf; k++) alpar@1: { /* read k-th field */ alpar@1: for (j = 0; j < dbf->len[k]; j++) alpar@1: buf[j] = (char)read_byte(dbf); alpar@1: buf[dbf->len[k]] = '\0'; alpar@1: /* set field value */ alpar@1: if (dbf->type[k] == 'C') alpar@1: { /* character field */ alpar@1: if (dbf->ref[k] > 0) alpar@1: mpl_tab_set_str(dca, dbf->ref[k], strtrim(buf)); alpar@1: } alpar@1: else if (dbf->type[k] == 'N') alpar@1: { /* numeric field */ alpar@1: if (dbf->ref[k] > 0) alpar@1: { double num; alpar@1: strspx(buf); alpar@1: xassert(str2num(buf, &num) == 0); alpar@1: mpl_tab_set_num(dca, dbf->ref[k], num); alpar@1: } alpar@1: } alpar@1: else alpar@1: xassert(dbf != dbf); alpar@1: } alpar@1: /* increase record count */ alpar@1: dbf->count++; alpar@1: done: return ret; alpar@1: } alpar@1: alpar@1: static int dbf_write_record(TABDCA *dca, struct dbf *dbf) alpar@1: { /* write next record to xBASE data file */ alpar@1: int j, k, ret = 0; alpar@1: char buf[255+1]; alpar@1: xassert(dbf->mode == 'W'); alpar@1: if (setjmp(dbf->jump)) alpar@1: { ret = 1; alpar@1: goto done; alpar@1: } alpar@1: /* record flag */ alpar@1: write_byte(dbf, 0x20); alpar@1: xassert(dbf->nf == mpl_tab_num_flds(dca)); alpar@1: for (k = 1; k <= dbf->nf; k++) alpar@1: { if (dbf->type[k] == 'C') alpar@1: { /* character field */ alpar@1: const char *str; alpar@1: if (mpl_tab_get_type(dca, k) == 'N') alpar@1: { sprintf(buf, "%.*g", DBL_DIG, mpl_tab_get_num(dca, k)); alpar@1: str = buf; alpar@1: } alpar@1: else if (mpl_tab_get_type(dca, k) == 'S') alpar@1: str = mpl_tab_get_str(dca, k); alpar@1: else alpar@1: xassert(dca != dca); alpar@1: if ((int)strlen(str) > dbf->len[k]) alpar@1: { xprintf("xBASE driver: field %s: cannot convert %.15s..." alpar@1: " to field format\n", mpl_tab_get_name(dca, k), str); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: for (j = 0; j < dbf->len[k] && str[j] != '\0'; j++) alpar@1: write_byte(dbf, str[j]); alpar@1: for (j = j; j < dbf->len[k]; j++) alpar@1: write_byte(dbf, ' '); alpar@1: } alpar@1: else if (dbf->type[k] == 'N') alpar@1: { /* numeric field */ alpar@1: double num = mpl_tab_get_num(dca, k); alpar@1: if (fabs(num) > 1e20) alpar@1: err: { xprintf("xBASE driver: field %s: cannot convert %g to fi" alpar@1: "eld format\n", mpl_tab_get_name(dca, k), num); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: sprintf(buf, "%*.*f", dbf->len[k], dbf->prec[k], num); alpar@1: xassert(strlen(buf) < sizeof(buf)); alpar@1: if ((int)strlen(buf) != dbf->len[k]) goto err; alpar@1: for (j = 0; j < dbf->len[k]; j++) alpar@1: write_byte(dbf, buf[j]); alpar@1: } alpar@1: else alpar@1: xassert(dbf != dbf); alpar@1: } alpar@1: /* increase record count */ alpar@1: dbf->count++; alpar@1: done: return ret; alpar@1: } alpar@1: alpar@1: static int dbf_close_file(TABDCA *dca, struct dbf *dbf) alpar@1: { /* close xBASE data file */ alpar@1: int ret = 0; alpar@1: xassert(dca == dca); alpar@1: if (dbf->mode == 'W') alpar@1: { if (setjmp(dbf->jump)) alpar@1: { ret = 1; alpar@1: goto skip; alpar@1: } alpar@1: /* end-of-file flag */ alpar@1: write_byte(dbf, 0x1A); alpar@1: /* number of records */ alpar@1: dbf->offset = 4; alpar@1: if (fseek(dbf->fp, dbf->offset, SEEK_SET)) alpar@1: { xprintf("%s:0x%X: seek error - %s\n", dbf->fname, alpar@1: dbf->offset, strerror(errno)); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: write_byte(dbf, dbf->count); alpar@1: write_byte(dbf, dbf->count >> 8); alpar@1: write_byte(dbf, dbf->count >> 16); alpar@1: write_byte(dbf, dbf->count >> 24); alpar@1: fflush(dbf->fp); alpar@1: if (ferror(dbf->fp)) alpar@1: { xprintf("%s:0x%X: write error - %s\n", dbf->fname, alpar@1: dbf->offset, strerror(errno)); alpar@1: longjmp(dbf->jump, 0); alpar@1: } alpar@1: skip: ; alpar@1: } alpar@1: xfree(dbf->fname); alpar@1: fclose(dbf->fp); alpar@1: xfree(dbf); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: #define TAB_CSV 1 alpar@1: #define TAB_XBASE 2 alpar@1: #define TAB_ODBC 3 alpar@1: #define TAB_MYSQL 4 alpar@1: alpar@1: void mpl_tab_drv_open(MPL *mpl, int mode) alpar@1: { TABDCA *dca = mpl->dca; alpar@1: xassert(dca->id == 0); alpar@1: xassert(dca->link == NULL); alpar@1: xassert(dca->na >= 1); alpar@1: if (strcmp(dca->arg[1], "CSV") == 0) alpar@1: { dca->id = TAB_CSV; alpar@1: dca->link = csv_open_file(dca, mode); alpar@1: } alpar@1: else if (strcmp(dca->arg[1], "xBASE") == 0) alpar@1: { dca->id = TAB_XBASE; alpar@1: dca->link = dbf_open_file(dca, mode); alpar@1: } alpar@1: else if (strcmp(dca->arg[1], "ODBC") == 0 || alpar@1: strcmp(dca->arg[1], "iODBC") == 0) alpar@1: { dca->id = TAB_ODBC; alpar@1: dca->link = db_iodbc_open(dca, mode); alpar@1: } alpar@1: else if (strcmp(dca->arg[1], "MySQL") == 0) alpar@1: { dca->id = TAB_MYSQL; alpar@1: dca->link = db_mysql_open(dca, mode); alpar@1: } alpar@1: else alpar@1: xprintf("Invalid table driver `%s'\n", dca->arg[1]); alpar@1: if (dca->link == NULL) alpar@1: error(mpl, "error on opening table %s", alpar@1: mpl->stmt->u.tab->name); alpar@1: return; alpar@1: } alpar@1: alpar@1: int mpl_tab_drv_read(MPL *mpl) alpar@1: { TABDCA *dca = mpl->dca; alpar@1: int ret; alpar@1: switch (dca->id) alpar@1: { case TAB_CSV: alpar@1: ret = csv_read_record(dca, dca->link); alpar@1: break; alpar@1: case TAB_XBASE: alpar@1: ret = dbf_read_record(dca, dca->link); alpar@1: break; alpar@1: case TAB_ODBC: alpar@1: ret = db_iodbc_read(dca, dca->link); alpar@1: break; alpar@1: case TAB_MYSQL: alpar@1: ret = db_mysql_read(dca, dca->link); alpar@1: break; alpar@1: default: alpar@1: xassert(dca != dca); alpar@1: } alpar@1: if (ret > 0) alpar@1: error(mpl, "error on reading data from table %s", alpar@1: mpl->stmt->u.tab->name); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: void mpl_tab_drv_write(MPL *mpl) alpar@1: { TABDCA *dca = mpl->dca; alpar@1: int ret; alpar@1: switch (dca->id) alpar@1: { case TAB_CSV: alpar@1: ret = csv_write_record(dca, dca->link); alpar@1: break; alpar@1: case TAB_XBASE: alpar@1: ret = dbf_write_record(dca, dca->link); alpar@1: break; alpar@1: case TAB_ODBC: alpar@1: ret = db_iodbc_write(dca, dca->link); alpar@1: break; alpar@1: case TAB_MYSQL: alpar@1: ret = db_mysql_write(dca, dca->link); alpar@1: break; alpar@1: default: alpar@1: xassert(dca != dca); alpar@1: } alpar@1: if (ret) alpar@1: error(mpl, "error on writing data to table %s", alpar@1: mpl->stmt->u.tab->name); alpar@1: return; alpar@1: } alpar@1: alpar@1: void mpl_tab_drv_close(MPL *mpl) alpar@1: { TABDCA *dca = mpl->dca; alpar@1: int ret; alpar@1: switch (dca->id) alpar@1: { case TAB_CSV: alpar@1: ret = csv_close_file(dca, dca->link); alpar@1: break; alpar@1: case TAB_XBASE: alpar@1: ret = dbf_close_file(dca, dca->link); alpar@1: break; alpar@1: case TAB_ODBC: alpar@1: ret = db_iodbc_close(dca, dca->link); alpar@1: break; alpar@1: case TAB_MYSQL: alpar@1: ret = db_mysql_close(dca, dca->link); alpar@1: break; alpar@1: default: alpar@1: xassert(dca != dca); alpar@1: } alpar@1: dca->id = 0; alpar@1: dca->link = NULL; alpar@1: if (ret) alpar@1: error(mpl, "error on closing table %s", alpar@1: mpl->stmt->u.tab->name); alpar@1: return; alpar@1: } alpar@1: alpar@1: /* eof */