/* file: revise.c G. Moody 8 February 1991 Last revised: 10 February 1994 Convert obsolete-format header files into new ones Copyright (C) Massachusetts Institute of Technology 1994. All rights reserved. */ #include "dblib.h" #ifndef __STDC__ extern double atof(); extern void exit(); #endif #ifndef BSD #include #else #include #endif static FILE *iheader; DB_Siginfo si[DB_MAXSIG]; DB_Time nsamples; main(argc, argv) int argc; char *argv[]; { char *record; int nsig; if (argc < 2) { (void)fprintf(stderr, "usage: %s record\n", argv[0]); (void)fprintf(stderr, " This program converts obsolete-format header files into new ones.\n"); exit(1); } record = argv[1]; if (strncmp(record, "header.", 7) == 0) record += 7; if ((nsig = readoldheader(record, si)) < 0) exit(2); (void)setheader(record, si, (unsigned)nsig); exit(0); /*NOTREACHED*/ } int readoldheader(record, siarray) char *record; DB_Siginfo *siarray; { char linebuf[256], *p; DB_Frequency f; DB_Signal s; DB_Time ns; unsigned int i; static char sep[] = " \t\n"; #ifndef atof double atof(); #endif #ifndef atol long atol(); #endif /* Try to open the header file. */ if ((iheader = db_open("header", record, READ)) == NULL) { db_error("init: can't open header for record %s\n", record); return (-1); } /* Get the first token (the record name) from the first non-empty, non-comment line. */ do { if (fgets(linebuf, 256, iheader) == NULL) { db_error("init: can't find record name in record %s header\n", record); return (-2); } } while ((p = strtok(linebuf, sep)) == NULL || *p == '#'); if (iheader != stdin && strcmp(p, record) != 0) { db_error("init: record name in record %s header is incorrect\n", record); return (-2); } /* Identify which type of header file is being read by trying to get another token from the line which contains the record name. (Old-style headers have only one token on the first line, but new-style headers have two or more.) */ if (p = strtok((char *)NULL, sep)) { (void)fprintf(stderr, "The header for record %s appears not to be an old-style header.\n", record); return (-2); } else { /* We come here if there were no other tokens on the line which contained the record name. The file appears to be an old-style header file. */ DB_Group g, ng; int mpx; /* Determine the number of signal groups. */ if (fgets(linebuf, 256, iheader) == NULL || (p = strtok(linebuf, sep)) == NULL) { db_error("init: incorrect format in record %s header\n", record); return (-2); } ng = atoi(p); /* Now get information for each signal group. */ for (g = s = 0; g < ng; g++) { /* Set the group number for the first signal in the group. */ siarray[s].group = g; /* Get the signal file name. */ if (fgets(linebuf, 256, iheader) == NULL || (p = strtok(linebuf, sep)) == NULL || (siarray[s].fname = (char *)malloc((unsigned)(strlen(p) + 1))) == NULL) { db_error("init: incorrect format in record %s header\n", record); return (-2); } (void)strcpy(siarray[s].fname, p); /* Determine the gain. */ if (fgets(linebuf, 256, iheader) == NULL || (p = strtok(linebuf, sep)) == NULL) { db_error("init: incorrect format in record %s header\n", record); return (-2); } siarray[s].gain = atof(p); /* Determine the sampling frequency. */ if (fgets(linebuf, 256, iheader) == NULL || (p = strtok(linebuf, sep)) == NULL || (f = atof(p)) <= 0.) { db_error("init: incorrect format in record %s header\n", record); return (-2); } (void)setsampfreq(f); /* Determine the format, block size, and number of signals in this group. In old-style headers these three quantities are encoded into two fields. */ if (fgets(linebuf, 256, iheader) == NULL || (p = strtok(linebuf, sep)) == NULL) { db_error("init: incorrect format in record %s header\n", record); return (-2); } i = (unsigned)atoi(p); mpx = (p = strtok((char *)NULL, sep)) ? atoi(p) : 1; if (mpx == 0) mpx = 1; if (i != 8 && i != 16) { siarray[s].bsize = i; if (mpx < 0) { mpx = -mpx; siarray[s].fmt = 8; } else siarray[s].fmt = 16; } else { siarray[s].fmt = i; siarray[s].bsize = 0; } /* Determine the ADC resolution. */ siarray[s].adcres = (p = strtok((char *)NULL, sep)) ? atoi(p) : DEFRES; /* Determine the ADC zero. */ siarray[s].adczero = (p = strtok((char *)NULL, sep)) ? atoi(p) : 0; /* Determine the initial value. */ if (fgets(linebuf, 256, iheader) == NULL || (p = strtok(linebuf, sep)) == NULL) { db_error("init: incorrect format in record %s header\n", record); return (-2); } siarray[s].initval = atoi(p); /* Determine the number of samples. */ ns = (p = strtok((char *)NULL, sep)) ? atol(p) : 0L; if (ns < 0L) { db_error("init: incorrect format in record %s header\n", record); return (-2); } if (nsamples == (DB_Time)0L) nsamples = ns; else if (ns > (DB_Time)0L && ns != nsamples) { db_error("warning (init):\n"); db_error(" record %s durations are inconsistent\n", record); /* nsamples must match the shortest record duration. */ if (nsamples > ns) nsamples = ns; } /* Determine the checksum. */ if (p = strtok((char *)NULL, sep)) { siarray[s].cksum = atoi(p); siarray[s].nsamp = ns; } else { siarray[s].cksum = 0; siarray[s].nsamp = (DB_Time)0L; } /* If this signal group contains more than one signal, make additional copies of the siginfo structure for the other signals. */ while (++s < DB_MAXSIG && --mpx > 0) siarray[s] = siarray[s-1]; if (s == DB_MAXSIG && (mpx > 0 || g < ng - 1)) { db_error("init: too many signals in record %s\n", record); return (-2); } } /* Now generate signal descriptions for each signal. */ for (i = 0; i < s; i++) { if ((siarray[i].desc=(char *)malloc((unsigned)(strlen(record)+20))) == NULL) { db_error("init: insufficient memory\n"); return (-2); } else (void)sprintf(siarray[i].desc, "record %s, signal %d", record, i); } } return (s); /* return number of available signals */ }