/* * 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("@(#) LinearCode.cc (Gunther Schadow) 12/19/96"); #pragma implementation "LinearCode.h" #include "LinearCode.h" #include "logfile.h" #include "exception.h" #include "misc.h" #include "xios.h" #include #include int LinearCode::lookup(const char *str) const { if(str == NULL) return not_present; else if(*str == 0 || strcmp(str,NULLVAL) == 0) return null; else for(int i = 0; i < _size; i++) if(tolower(*str) == tolower(*_table[i])) if(strcasecmp(&str[1], &_table[i][1]) == 0) return i; return invalid; } LinearCode::LinearCode(const char *table[], size_t size) : Code() { _size = size; _table = table; _val = not_present; } LinearCode::LinearCode(int val, const char *table[], size_t size) : Code() { _size = size; _table = table; _val = val; switch(_val) { case not_present: Code::unset(); break; case null: Code::nullify(); break; default: if(null<_val && _val<_size) Code::set(); else { _val = invalid; ERROR("illegal value `%d'", val); } } } const LinearCode& LinearCode::operator = (int val) { _val = val; switch(_val) { case not_present: Code::unset(); break; case null: Code::nullify(); break; default: if(null<_val && _val<_size) Code::set(); else { _val = invalid; ERROR("illegal value `%d'", val); } } return *this; } const LinearCode& LinearCode::operator = (const LinearCode &x) { Code::operator = (x); if(_table != x._table) FATAL("type mismatch %p for %p", x._table, _table); else _val = x._val; return *this; } LinearCode::LinearCode(const char *s, const char *table[], size_t size) : Code() { _size = size; _table = table; _val = lookup(s); switch(_val) { case invalid: ERROR("illegal code `%s'", s); case not_present: Code::unset(); break; case null: Code::nullify(); break; default: Code::set(); break; } } const LinearCode& LinearCode::operator = (const char *s) { _val = lookup(s); switch(_val) { case invalid: ERROR("illegal code `%s'", s); case not_present: Code::unset(); break; case null: Code::nullify(); break; default: Code::set(); break; } return *this; } LinearCode::operator const char* () const { if (ispresent()) if (isnull()) return ""; else return _table[_val]; else return NULL; } bool LinearCode::OK() const { if(_table == NULL) FATAL("no table"); else if(_val