/* * Copyright (c) 1995, 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "pg_config.h" IDENT("@(#) LabEventInfoMgr.cc (Gunther Schadow) 08/13/96"); #include #include "LabEventInfo.h" #include #include #include #include #include #include #include #include #include #include class LabEventInfoMgr : public LabEventInfo { char **string; const int nostrings = 6; double *real; const int noreals = 8; public: LabEventInfoMgr(int mode = odbm::write) : LabEventInfo(mode | ( mode & odbm::write ? odbm::fast : 0 )) { string = &loinc_num; real = &constVal; } /****************************************************************** * READ RECORD */ result readrecord(istream &is) { // read strings int i = 0; while(is.ipfx(1) && i < nostrings) { char del; if(is.peek() == '"') { del = '"'; is.get(); } else del = ':'; is.gets(&string[i], del); if(string[i++] == NULL) return FAIL - i; if(del == '"' && is.peek() == ':') is.get(); if(is.peek() == '\n') { is.get(); return FAIL - i; } } // read scalars (8x double 1x int) i = 0; while(is.ipfx(1) && i < noreals) { char del; char *nstr; if(is.peek() == '"') { del = '"'; is.get(); } else del = ':'; is.gets(&nstr, del); if(nstr == NULL) return FAIL - nostrings - i; real[i++] = strtod(nstr,NULL); delete [] nstr; if(del == '"' && is.peek() == ':') is.get(); if(is.peek() == '\n') { is.get(); return FAIL - nostrings - i; } } if(is.ipfx(1)) { char *del; if(is.peek() == '"') { del = "\"\n"; is.get(); } else del = ":\n"; char *nstr = gets(is, del); if(nstr == NULL) return FAIL - nostrings - noreals - 1; // number of fractional digits -> number of valid digits // NFD + NID = NVD int nfd = atoi(nstr); delete [] nstr; double mvf = floor(maxValue); if(mvf == 0.) precision = nfd; else precision = nfd + (int)log10(mvf) + 1; } is.ignore(1000, '\n'); // insert record odbm::insert(); return SUCCESS; } /****************************************************************** * DUMP DATABASE */ void dump() { for(const char *k = firstkey(); k != NULL; k = nextkey()) { fetch(k); for(int i = 0; i < nostrings; i++) cout << string[i] << ':'; for(int i = 0; i < noreals; i++) cout << real[i] << ':'; // number of fractional digits <- number of valid digits // NFD = NVD - NID double mvf = floor(maxValue); if(mvf == 0.) cout << precision << endl; else cout << ( precision - (int)log10(mvf) - 1 ) << endl; } } /****************************************************************** * CREATE DBLOAD SCRIPT FOR LabEventMap */ void map() { for(const char *k = firstkey(); k != NULL; k = nextkey()) { fetch(k); cout << endl << "INSERT INTO LabEventMap" << endl << " WITH VALUES" << endl << " testId = \"" << loinc_num << "\"," << endl << " mappedPtEvent = \"" << labEventClass << "\"," << endl << " mappedPropName = \"" << property << "\"," << endl << " careunitName = (\"CISCareUnitCfg\",$OID);" << endl; } } }; static void usage(char *prog) { cerr << "usage: " << prog << " -[cru] files ..." << endl << " " << prog << " -[DM]" << endl; exit(1); } /* Options: * -c files ... create database * -r files ... update database (update) * -u files ... update database (insert) * -D dump database * -M create dbload script for LabEventMap */ int main(int argc, char *argv[]) { log_init(argv[0], "./labEventInfo.log", "test"); log_level(L_JUNK); char *prog = argv[0]; enum action_t { a_null, a_create, a_update, a_insert, a_dump, a_map, }; action_t action = a_null; GetOpt getopt(argc, argv, "cruDM"); int option_char; while((option_char = getopt()) != EOF) switch(option_char) { case 'c': action = a_create; break; case 'r': action = a_update; break; case 'u': action = a_insert; break; case 'D': action = a_dump; break; case 'M': action = a_map; break; case '?': usage(prog); } argv += getopt.optind - 1; argc -= getopt.optind - 1; if(action == a_null) usage(prog); if(action == a_dump) { LabEventInfoMgr labEventInfo(odbm::read); labEventInfo.dump(); return SUCCESS; } else if(action == a_map) { LabEventInfoMgr labEventInfo(odbm::read); labEventInfo.map(); return SUCCESS; } else if(action == a_update) { cerr << "rewrite is not yet implemented, sorry" << endl; exit(1); } else // a_create or a_insert or a_exclude { int mode = odbm::write; if(action == a_create) mode |= odbm::newdb; LabEventInfoMgr labEventInfo(mode); int lines_total = 0; Loinc loinc; while(--argc > 0) { char *file = *++argv; cout << file << endl; fstream fin(file, ios::in); int lineno = 1; while(fin.ipfx(1)) { if(fin.peek() == '#') { fin.ignore(10000000, '\n'); lineno++; continue; } result res; res = labEventInfo.readrecord(fin); // check numbers against LOINC if(loinc.lookup(labEventInfo.getLoincNum())) { loinc = labEventInfo.getLoincNum(); cout << labEventInfo.getLabEventClass() << '\t' << loinc.getSpecimen() << '\t' << loinc.getMeasure() << endl; } else { cerr << file << ":" << lineno << ": warning no LOINC number: `" << labEventInfo.getLoincNum() << "'" << endl; LOGWARNING("%s:%d: warning: no LOINC number: `%s'", file, lineno, labEventInfo.getLoincNum()); } if(res != SUCCESS) { if(res == FAIL) break; else if(fin.eof()) { cerr << file << ":" << lineno << ": premature end of file at field " << FAIL - res << endl; ERROR("%s:%d: premature end of file at field %d", file, lineno, FAIL - res); } else if(! fin) { cerr << file << ":" << lineno << ": error in file at field " << FAIL - res << ": " << sys_errlist[errno] << endl; ERROS("%s:%d: error in file at field %d", file, lineno, FAIL - res); } else { cerr << file << ":" << lineno << ": premature end of record at field " << FAIL - res << endl; } } else lines_total++; if(lineno % 100 == 0) cout << lineno << " lines read" << endl; lineno++; } } cout << "total " << lines_total << " lines read" << endl; return SUCCESS; } abort(); }