/* file: lab2ann.c G. Moody 13 August 1995 Create an annotation file from a `labdata' file Copyright (C) Massachusetts Institute of Technology 1995. All rights reserved. */ #include #include #include #include main(argc, argv) int argc; char **argv; { char buf[256], outbuf[256], ptnam[80], timebuf[30]; char *date, *time, *tnam, *pnam, *pval, *pcom; DB_Anninfo ai; static DB_Annotation annot; static DB_Siginfo s[DB_MAXSIG]; DB_Time t; FILE *tfile; int ampm, hour, minute, month, day, year; int phour, pminute, pmonth, pday, pyear; int vflag = 0; if (argc < 2) { fprintf(stderr, "usage: %s record [-v] 2 && strcmp(argv[2], "-v") == 0) vflag = 1; if (isigopen(argv[1], s, -DB_MAXSIG) < 0) exit(2); date = timstr(0L); sscanf(date, "[%*d:%*d:%*d %*d/%*d/%d]", &year); if ((tfile = fopen("labdata.tmp", "w")) == NULL) { fprintf(stderr, "%s: can't create intermediate file\n", argv[0]); exit(1); } while (fgets(buf, sizeof(buf), stdin)) { /* read a line, parse into tab-delimited tokens */ date = strtok(buf, "\t\r\n"); time = strtok(NULL, "\t\r\n"); tnam = strtok(NULL, "\t\r\n"); pnam = strtok(NULL, "\t\r\n"); pval = strtok(NULL, " \t\r\n"); pcom = strtok(NULL, "\t\r\n"); while (*pcom == ' ') pcom++; if (pval == NULL) continue; sscanf(date, "%d/%d/%d", &month, &day, &year); if (year < 1900) year += 1900; fprintf(tfile, "%d/%02d/%02d", year, month, day); sscanf(time, "%d:%d%c", &hour, &minute, &m); if (ampm == 'P' || ampm == 'p') hour += 12; else if (hour == 12) hour = 0; fprintf(tfile, "/%02d/%02d\t", hour, minute); fprintf(tfile, "%s\t%s=%s", tnam, pnam, pval); if (vflag && pcom && *pcom) fprintf(tfile, "[%s]", pcom); fprintf(tfile, "\n"); } fclose(tfile); system("sort labdata.sorted"); if ((tfile = fopen("labdata.sorted", "r")) == NULL) { fprintf(stderr, "%s: can't open intermediate file\n", argv[0]); exit(1); } ai.name = "lab"; ai.stat = WRITE; if (annopen(argv[1], &ai, 1) < 0) exit(2); annot.anntyp = NOTE; annot.subtyp = annot.num = 0; annot.aux = outbuf; pyear = pmonth = pday = phour = pminute = -1; strcpy(ptnam, ""); while (fgets(buf, sizeof(buf), tfile)) { /* read a line, parse into tab-delimited tokens */ date = strtok(buf, "\t\n"); tnam = strtok(NULL, "\t\n"); pval = strtok(NULL, "\n"); if (pval == NULL) continue; sscanf(date, "%d/%d/%d/%d/%d", &year, &month, &day, &hour, &minute); if (minute == pminute && hour == phour && day == pday && month == pmonth && year == pyear && strcmp(ptnam, tnam) == 0) { /* another entry in the same row of the same table -- append to outbuf */ sprintf(outbuf+1+strlen(outbuf+1), ":%s", pval); if (strlen(outbuf+1) > 80) /* the buffer is getting too long; force flush on next read */ strcpy(ptnam, ""); } else { /* new table or new time */ if (outbuf[1]) { /* flush pending output */ sprintf(timebuf, "[%02d:%02d:00 %02d/%02d/%d]", phour, pminute, pday, pmonth, pyear); t = -strtim(timebuf); if (t >= 0L) { if (annot.time == t) /* same time, use next chan */ annot.chan++; else { /* new time, use chan 0 */ annot.time = t; annot.chan = 0; } outbuf[0] = strlen(outbuf+1); putann(0, &annot); } } /* save data from this entry for later output */ sprintf(outbuf+1, "%s:%s", tnam, pval); pminute = minute; phour = hour; pday = day; pmonth = month; pyear = year; strcpy(ptnam, tnam); } } if (outbuf[1]) { /* flush last pending output */ sprintf(timebuf, "[%02d:%02d:00 %02d/%02d/%d]", phour, pminute, pday, pmonth, pyear); t = -strtim(timebuf); if (annot.time == t) /* same time, use next chan */ annot.chan++; else { /* new time, use chan 0 */ annot.time = t; annot.chan = 0; } outbuf[0] = strlen(outbuf+1); putann(0, &annot); } dbquit(); }