/* * 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("@(#) ANYmsg.cc (Gunther Schadow) 12/19/96"); #pragma implementation #include "ANYmsg.h" #include "UniMesg.h" #include "logfile.h" #include "exception.h" #include "xios.h" #include "ioext.h" #include "misc.h" #include "ParseTrace.h" #include "ANYmsg-config.h" ANYmsg::ANYmsg(const ANYmsg &x) { _allowed = x._allowed; _frozen = false; UniMesIdCode::Value t = x.type(); switch (t) { ANYMSG_CASES(_msg) default: FATAL("illegal message type %d", t); } *_msg = *x._msg; if(x._services != NULL) { _services = new service_t[UniMesIdCode::size]; for(int i = UniMesIdCode::first; i <= UniMesIdCode::last; i++) _services[i] = x._services[i]; } else _services = NULL; } ANYmsg::~ANYmsg() { if(_msg != NULL && !_frozen) delete _msg; if(_services != NULL) delete [] _services; } const ANYmsg& ANYmsg::operator = (const ANYmsg &x) { _allowed = x._allowed; UniMesIdCode::Value t = x.type(); if(_msg != NULL && !_frozen) { delete _msg; _msg = NULL; } _frozen = false; switch (t) { ANYMSG_CASES(_msg) default: FATAL("illegal message type %d", t); } *_msg = *x._msg; if(_services != NULL) delete [] _services; if(x._services != NULL) { _services = new service_t[UniMesIdCode::size]; for(int i = UniMesIdCode::first; i <= UniMesIdCode::last; i++) _services[i] = x._services[i]; } else _services = NULL; return *this; } const ANYmsg& ANYmsg::operator = (const HL7Message &x) { UniMesIdCode::Value t = x.type(); if(_allowed.test(t)) { if(_msg != NULL && !_frozen) delete _msg; switch (t) { ANYMSG_CASES(_msg) default: goto illegal; } *_msg=x; return *this; } illegal: FATAL("illegal message type %d", t); } void ANYmsg::unset() { if( _msg != NULL && !_frozen ) delete _msg; _msg = NULL; _frozen = false; } HL7Message* const ANYmsg::operator -> () const { if(_msg != NULL) return _msg; else FATAL("message pointer is NULL"); } HL7Message& ANYmsg::operator * () const { if(_msg != NULL) return *_msg; else FATAL("message pointer is NULL"); } ANYmsg::operator HL7Message& () const { if(_msg != NULL) return *_msg; else FATAL("message pointer is NULL"); } result ANYmsg::input(istream& is) { SegStrucTracer tracer(PGObject::message); LOGDEBUG("begin"); if(!is.ipfx(1)) { if(is.eof()) { LOGDEBUG("end of stream"); return FAIL; } else ERROS("error on input stream"); } if(istouched(is)) { lprintf(L_FATAL, "ANYmsg::input: can't read from a touched stream"); untouch(is); return FAIL; } streammarker spos(is.rdbuf()); // remember the stream position MSHseg msh; if(msh.input(is) != SUCCESS) { LOGWARNING("could not read MSH segment"); unset(); is.rdbuf()->seekmark(spos); /* restore old stream position */ return FAIL; } /* was: { record_parse_error("could not read MSH segment"); EPARSE("could not read MSH segment"); } */ if(!msh.getMesType().ispresent() || msh.getMesType().isnull()) { record_parse_error("message type is not present or null"); EPARSE("message type is not present or null"); } UniMesIdCode::Value t = UniMesId(msh.getMesType()); if(t==UniMesIdCode::invalid) { record_parse_error("invalid message type"); EPARSE("invalid message type"); } if(!_allowed.test(t)) goto unexpected; unset(); switch(t) { ANYMSG_CASES(_msg) default: goto unexpected; } _msg->setMesHea(msh); tracer++; touch(is); if(_msg->input(is) == SUCCESS) return SUCCESS; else FATAL("input failed on touched stream (while reading %d)", (int)t); unexpected: record_parse_warning("unexpected message %d", (int)t); LOGWARNING("unexpected message %d", (int)t); unset(); is.rdbuf()->seekmark(spos); /* restore old stream position */ return FAIL; } UniMesIdCode::Value ANYmsg::type() const { if ( _msg != NULL ) return (UniMesIdCode::Value)_msg->type(); else return UniMesIdCode::invalid; } bool ANYmsg::OK() const { if(_msg == NULL) return true; else return _msg->OK(); }