/* * 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("@(#) ExternalCode.cc (Gunther Schadow) 12/19/96"); #pragma implementation "ExternalCode.h" #include #include #include "ExternalCode.h" #include "logfile.h" #include "exception.h" #include "misc.h" #include "xios.h" static const int gdbm_blocksize = 512; static void fatal_handler(const char *msg) // __attribute__((__noreturn__)) { FATAL("gdbm fatal error: %s", msg); } const char *ExternalCode::_dbf_libdir = NULL; #ifndef PGCODE # define PGCODE "/usr/hl7/lib/codes" #endif /* If the parameter dbf_path is not already initialized, set it to the * full pathname of the database; thereby initializing the static variable * ExternalCode::_dbf_libdir to the path of the code database directory. * The base name of the database is passed by the dbf_base parameter. * (``initialized'' means != NULL) */ char * ExternalCode::new_dbf_path(char* &dbf_path, const char *dbf_base) { const char *path; if(dbf_path == NULL) { if(_dbf_libdir == NULL) { if((path = getenv("PG_CODELIB")) == NULL) path = PGCODE; _dbf_libdir = path; } else path = _dbf_libdir; dbf_path = new char [strlen(path) + strlen(dbf_base) + 2]; strcpy(dbf_path, path); strcat(dbf_path, "/"); strcat(dbf_path, dbf_base); } return dbf_path; } int ExternalCode::lookup(const char *str) const { if(str == NULL) return not_present; else if(strcmp(str, NULLVAL) == 0) return null; else { GDBM_FILE dbf = gdbm_open((char *)_dbf_path, gdbm_blocksize, GDBM_READER, 0, fatal_handler); if(dbf == NULL) { FATAL("gdbm error: %s", gdbm_strerror(gdbm_errno)); /* no, FATAL does not return! */ return 0; } else { datum key; key.dptr = (char *)str; key.dsize = strlen(str) + 1; bool found = gdbm_exists(dbf, key); gdbm_close(dbf); if(found) return member; else return invalid; } } } ExternalCode::ExternalCode(char* &dbf_path, const char *dbf_base) : Code() { set_dbf_path(dbf_path, dbf_base); _string = NULL; } ExternalCode::ExternalCode(const char *s, char* &dbf_path, const char *dbf_base) : Code() { set_dbf_path(dbf_path, dbf_base); _string = NULL; switch(lookup(s)) { case invalid: ERROR("illegal code `%s'", s); break; case not_present: Code::unset(); break; case null: Code::nullify(); break; case member: _string = strdup(s); Code::set(); break; default: FATAL("how did I come to this point??"); } } const ExternalCode& ExternalCode::operator = (const char *s) { if( _string != s ) { if( _string != NULL ) delete [] _string; _string = NULL; switch(lookup(s)) { case invalid: ERROR("illegal code `%s'", s); break; case not_present: unset(); break; case null: nullify(); break; case member: _string = strdup(s); Code::set(); break; default: FATAL("how did I come to this point??"); } } return *this; } ExternalCode::ExternalCode(const ExternalCode& x) : Code(x) { _dbf_path = x._dbf_path; if(x._string == NULL) _string = NULL; else _string = strdup(x._string); } const ExternalCode& ExternalCode::operator = (const ExternalCode& x) { if(&x != this) { if(_string != NULL) delete [] _string; if(_dbf_path != x._dbf_path) FATAL("code mismatch `%s' for `%s'", x._dbf_path, _dbf_path); Code::operator = (x); if(x._string != NULL) _string = strdup(x._string); } else LOGDEBUG("Huh? assign myself to me?"); return *this; } void ExternalCode::unset() { if(_string != NULL) { delete [] _string; _string = NULL; } Code::unset(); } void ExternalCode::nullify() { if(_string != NULL) { delete [] _string; _string = NULL; } Code::nullify(); } ExternalCode::operator const char* () const { if(ispresent()) return NULL; if(isnull()) return ""; return _string; } bool ExternalCode::OK() const { Code::OK(); if (_dbf_path == NULL) FATAL("no database file"); // FIXME! check _val and _string return TRUE; }