00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "config.h"
00036
00037 static char rcsid[] not_used =
00038 {"$Id: Error.cc 17002 2007-08-27 19:16:51Z pwest $"
00039 };
00040
00041 #include <stdio.h>
00042 #include <assert.h>
00043
00044 #include "Error.h"
00045 #include "parser.h"
00046 #include "InternalErr.h"
00047 #include "debug.h"
00048
00049 using namespace std;
00050
00051
00052 extern void Error_switch_to_buffer(void *new_buffer);
00053 extern void Error_delete_buffer(void * buffer);
00054 extern void *Error_buffer(FILE *fp);
00055
00056 extern void Errorrestart(FILE *yyin);
00057 extern int Errorparse(void *arg);
00058
00059
00060
00061 static const char *err_messages[] = {
00062 "Undefined error",
00063 "Unknown error",
00064 "Internal error",
00065 "No such file",
00066 "No such variable",
00067 "Malformed expression",
00068 "No authorization",
00069 "Cannot read file",
00070 "Cannot read file"
00071 };
00072
00075 Error::Error() : _error_code(undefined_error), _error_message("")
00076 {}
00077
00087 Error::Error(ErrorCode ec, string msg)
00088 : _error_code(ec), _error_message(msg)
00089 {}
00090
00096 Error::Error(string msg)
00097 : _error_code(unknown_error), _error_message(msg)
00098 {}
00099
00100 Error::Error(const Error ©_from)
00101 : _error_code(copy_from._error_code),
00102 _error_message(copy_from._error_message)
00103 {
00104 }
00105
00106 Error::~Error()
00107 {
00108 }
00109
00110 Error &
00111 Error::operator=(const Error &rhs)
00112 {
00113 assert(OK());
00114
00115 if (&rhs == this)
00116 return *this;
00117 else {
00118 _error_code = rhs._error_code;
00119 _error_message = rhs._error_message;
00120
00121 assert(this->OK());
00122
00123 return *this;
00124 }
00125 }
00126
00133 bool
00134 Error::OK() const
00135 {
00136
00137 bool empty = ((_error_code == undefined_error)
00138 && (_error_message.empty()));
00139
00140
00141 bool message = ((_error_code != undefined_error)
00142 && (!_error_message.empty()));
00143
00144 DBG(cerr << "empty: " << empty << ", message: " << message << endl);
00145 return empty || message;
00146 }
00147
00156 bool
00157 Error::parse(FILE *fp)
00158 {
00159 if (!fp)
00160 throw InternalErr(__FILE__, __LINE__, "Null input stream");
00161
00162 void *buffer = Error_buffer(fp);
00163 Error_switch_to_buffer(buffer);
00164
00165 parser_arg arg(this);
00166
00167 bool status;
00168 try {
00169 status = Errorparse((void *) & arg) == 0;
00170 Error_delete_buffer(buffer);
00171 }
00172 catch (Error &e) {
00173 Error_delete_buffer(buffer);
00174 throw InternalErr(__FILE__, __LINE__, e.get_error_message());
00175 }
00176
00177
00178
00179
00180
00181 if (!status || !arg.status())
00182 throw InternalErr(__FILE__, __LINE__, "Error parsing error object!");
00183 else
00184 return OK();
00185 }
00186
00187
00198 void
00199 Error::print(FILE *out) const
00200 {
00201 assert(OK());
00202
00203 fprintf(out, "Error {\n") ;
00204
00205 fprintf(out, " code = %d;\n", static_cast<int>(_error_code)) ;
00206
00207
00208
00209 if (*_error_message.begin() == '"' && *(_error_message.end() - 1) == '"')
00210 fprintf(out, " message = %s;\n", _error_message.c_str()) ;
00211 else
00212 fprintf(out, " message = \"%s\";\n", _error_message.c_str()) ;
00213
00214 fprintf(out, "};\n") ;
00215 }
00216
00227 void
00228 Error::print(ostream &strm) const
00229 {
00230 assert(OK());
00231
00232 strm << "Error {\n" ;
00233
00234 strm << " code = " << static_cast<int>(_error_code) << ";\n" ;
00235
00236
00237
00238 if (*_error_message.begin() == '"' && *(_error_message.end() - 1) == '"')
00239 strm << " message = " << _error_message.c_str() << ";\n" ;
00240 else
00241 strm << " message = \"" << _error_message.c_str() << "\";\n" ;
00242
00243 strm << "};\n" ;
00244 }
00245
00247 ErrorCode
00248 Error::get_error_code() const
00249 {
00250 assert(OK());
00251 return _error_code;
00252 }
00253
00260 void
00261 Error::set_error_code(ErrorCode ec)
00262 {
00263 _error_code = ec;
00264
00265
00266 if (_error_message.empty()
00267 && ec > undefined_error && ec <= cannot_read_file) {
00268 _error_message = err_messages[ec - undefined_error];
00269 }
00270 else {
00271 _error_message = err_messages[0];
00272 }
00273 }
00274
00276 string
00277 Error::get_error_message() const
00278 {
00279 assert(OK());
00280
00281 return string(_error_message);
00282 }
00283
00285 void
00286 Error::set_error_message(string msg)
00287 {
00288 _error_message = msg;
00289 }