/* * 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("@(#) LOINC.cc (Gunther Schadow) 12/19/96"); #pragma implementation "LOINC.h" #include "LOINC.h" #include #include #include #include #include #include #include #include // for sprintf(3) only static const char *loinc_db_base = "LOINC.db"; static char *loinc_db_file = NULL; odbm::odbm_file Loinc::odbmf = odbm::null_dbf; u_int Loinc::odbm_obj_cnt = 0; Loinc::Loinc() : Code(), odbm(&loinc_num, &measure, &num_loinc, sizeof(num_loinc), &odbmf, &odbm_obj_cnt, ExternalCode::new_dbf_path(loinc_db_file, loinc_db_base)) { index_on(&measure); index_on(&mclass); index_on(&source); index_on(&euclide_cd); index_on(&astm_cd); index_on(&iupac_cd); index_on(&metpath_code); num_loinc = 0; } Loinc::Loinc(int mode) : Code(), odbm(&loinc_num, &measure, &num_loinc, sizeof(num_loinc), &odbmf, &odbm_obj_cnt, ExternalCode::new_dbf_path(loinc_db_file, loinc_db_base), mode) { index_on(&measure); index_on(&mclass); index_on(&source); index_on(&euclide_cd); index_on(&astm_cd); index_on(&iupac_cd); index_on(&metpath_code); } Loinc::Loinc(const char *name) : Code(), odbm(&loinc_num, &measure, &num_loinc, sizeof(num_loinc), &odbmf, &odbm_obj_cnt, ExternalCode::new_dbf_path(loinc_db_file, loinc_db_base)) { index_on(&measure); index_on(&mclass); index_on(&source); index_on(&euclide_cd); index_on(&astm_cd); index_on(&iupac_cd); index_on(&metpath_code); set(name); } /* * Set the LOINC code to name is the loinc_num with check digit */ void Loinc::set(const char *s) { if(fetch(s) == FAIL) ERROR("loinc code `%s' not found", s); num_loinc = atoi(loinc_num); Code::set(); } Loinc& Loinc::operator = (const char *s) { free(); set(s); return *this; } // compute the LOINC check digit (FIX also in support/checkdigit) int Loinc::chkdg(int x) { int odd = 0, even = 0, sum = 0; while(x != 0) { odd = odd * 10 + x % 10; x = x / 10; if(x == 0) break; even = even * 10 + x % 10; x = x / 10; } odd = odd * 2; while(odd != 0 || even != 0) { sum = sum + odd % 10 + even % 10; odd = odd / 10; even = even / 10; } return (10 - (sum % 10)) % 10; } Loinc& Loinc::operator = (int x) { char buf[10]; sprintf(&buf[0], "%d-%d", x, chkdg(x)); free(); set(buf); return *this; } bool Loinc::lookup(const char *s) { if(fetch(s) == SUCCESS) return TRUE; else return FALSE; } bool Loinc::lookup(int x) { char buf[10]; sprintf(&buf[0], "%d-%d", x, chkdg(x)); if(fetch(buf) == SUCCESS) return TRUE; else return FALSE; } inline bool Loinc::lookup() { u_int cursor = 0; if(odbm::fetch(&cursor) == SUCCESS) return TRUE; else return FALSE; } bool Loinc::exists(int x) const { char buf[10]; sprintf(&buf[0], "%d-%d", x, chkdg(x)); return odbm::exists(buf); }