Ticket #17: 305d03f9bcea.patch
File 305d03f9bcea.patch, 43.0 KB (added by , 16 years ago) |
---|
-
demo/lgf_demo.cc
# HG changeset patch # User Balazs Dezso <deba@inf.elte.hu> # Date 1222674159 -7200 # Node ID 305d03f9bceade28b5411268d2c2270fec56ffff # Parent bb40b6db0a587e98535e84eb2b9c92f3df972b6a Simplifying exceptions - Using asserts instead of exceptions for unitialized parameters - Only the IO exceptions are used in the lemon - DataFormatError is renamed to IoError - The IoError is simplified diff -r bb40b6db0a58 -r 305d03f9bcea demo/lgf_demo.cc
a b 49 49 node("source", s). // read 'source' node to s 50 50 node("target", t). // read 'target' node to t 51 51 run(); 52 } catch ( DataFormatError& error) { // check if there was any error52 } catch (IoError& error) { // check if there was any error 53 53 std::cerr << "Error: " << error.what() << std::endl; 54 54 return -1; 55 55 } -
lemon/arg_parser.h
diff -r bb40b6db0a58 -r 305d03f9bcea lemon/arg_parser.h
a b 310 310 311 311 ///This is the type of the return value of ArgParser::operator[](). 312 312 ///It automatically converts to \c int, \c double, \c bool or 313 ///\c std::string if the type of the option matches, otherwise it 314 ///throws an exception (i.e. it performs runtime type checking). 313 ///\c std::string if the type of the option matches, which is checked 314 ///with an \ref LEMON_ASSERT "assertion" (i.e. it performs runtime 315 ///type checking). 315 316 class RefType 316 317 { 317 318 const ArgParser &_parser; -
lemon/bfs.h
diff -r bb40b6db0a58 -r 305d03f9bcea lemon/bfs.h
a b 135 135 #endif 136 136 class Bfs { 137 137 public: 138 ///\ref Exception for uninitialized parameters.139 140 ///This error represents problems in the initialization of the141 ///parameters of the algorithm.142 class UninitializedParameter : public lemon::UninitializedParameter {143 public:144 virtual const char* what() const throw() {145 return "lemon::Bfs::UninitializedParameter";146 }147 };148 138 149 139 ///The type of the digraph the algorithm runs on. 150 140 typedef typename TR::Digraph Digraph; … … 232 222 typedef T PredMap; 233 223 static PredMap *createPredMap(const Digraph &) 234 224 { 235 throw UninitializedParameter();225 LEMON_ASSERT(false, "PredMap is not initialized"); 236 226 } 237 227 }; 238 228 ///\brief \ref named-templ-param "Named parameter" for setting … … 250 240 typedef T DistMap; 251 241 static DistMap *createDistMap(const Digraph &) 252 242 { 253 throw UninitializedParameter();243 LEMON_ASSERT(false, "DistMap is not initialized"); 254 244 } 255 245 }; 256 246 ///\brief \ref named-templ-param "Named parameter" for setting … … 268 258 typedef T ReachedMap; 269 259 static ReachedMap *createReachedMap(const Digraph &) 270 260 { 271 throw UninitializedParameter();261 LEMON_ASSERT(false, "ReachedMap is not initialized"); 272 262 } 273 263 }; 274 264 ///\brief \ref named-templ-param "Named parameter" for setting … … 286 276 typedef T ProcessedMap; 287 277 static ProcessedMap *createProcessedMap(const Digraph &) 288 278 { 289 throw UninitializedParameter();279 LEMON_ASSERT(false, "ProcessedMap is not initialized"); 290 280 } 291 281 }; 292 282 ///\brief \ref named-templ-param "Named parameter" for setting … … 1040 1030 ///\return \c true if \c t is reachable form \c s. 1041 1031 bool run(Node s, Node t) 1042 1032 { 1043 if (s==INVALID || t==INVALID) throw UninitializedParameter();1044 1033 Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); 1045 1034 if (Base::_pred) 1046 1035 alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); … … 1323 1312 class BfsVisit { 1324 1313 public: 1325 1314 1326 /// \brief \ref Exception for uninitialized parameters.1327 ///1328 /// This error represents problems in the initialization1329 /// of the parameters of the algorithm.1330 class UninitializedParameter : public lemon::UninitializedParameter {1331 public:1332 virtual const char* what() const throw()1333 {1334 return "lemon::BfsVisit::UninitializedParameter";1335 }1336 };1337 1338 1315 ///The traits class. 1339 1316 typedef _Traits Traits; 1340 1317 … … 1389 1366 struct SetReachedMapTraits : public Traits { 1390 1367 typedef T ReachedMap; 1391 1368 static ReachedMap *createReachedMap(const Digraph &digraph) { 1392 throw UninitializedParameter();1369 LEMON_ASSERT(false, "ReachedMap is not initialized"); 1393 1370 } 1394 1371 }; 1395 1372 /// \brief \ref named-templ-param "Named parameter" for setting -
lemon/concepts/heap.h
diff -r bb40b6db0a58 -r 305d03f9bcea lemon/concepts/heap.h
a b 129 129 /// already stored in the heap. 130 130 /// Otherwise it inserts the given item with the given priority. 131 131 /// 132 /// It may throw an \ref UnderflowPriorityException.133 132 /// \param i The item. 134 133 /// \param p The priority. 135 134 void set(const Item &i, const Prio &p) {} -
lemon/dfs.h
diff -r bb40b6db0a58 -r 305d03f9bcea lemon/dfs.h
a b 136 136 #endif 137 137 class Dfs { 138 138 public: 139 ///\ref Exception for uninitialized parameters.140 141 ///This error represents problems in the initialization of the142 ///parameters of the algorithm.143 class UninitializedParameter : public lemon::UninitializedParameter {144 public:145 virtual const char* what() const throw() {146 return "lemon::Dfs::UninitializedParameter";147 }148 };149 139 150 140 ///The type of the digraph the algorithm runs on. 151 141 typedef typename TR::Digraph Digraph; … … 232 222 typedef T PredMap; 233 223 static PredMap *createPredMap(const Digraph &) 234 224 { 235 throw UninitializedParameter();225 LEMON_ASSERT(false, "PredMap is not initialized"); 236 226 } 237 227 }; 238 228 ///\brief \ref named-templ-param "Named parameter" for setting … … 250 240 typedef T DistMap; 251 241 static DistMap *createDistMap(const Digraph &) 252 242 { 253 throw UninitializedParameter();243 LEMON_ASSERT(false, "DistMap is not initialized"); 254 244 } 255 245 }; 256 246 ///\brief \ref named-templ-param "Named parameter" for setting … … 268 258 typedef T ReachedMap; 269 259 static ReachedMap *createReachedMap(const Digraph &) 270 260 { 271 throw UninitializedParameter();261 LEMON_ASSERT(false, "ReachedMap is not initialized"); 272 262 } 273 263 }; 274 264 ///\brief \ref named-templ-param "Named parameter" for setting … … 286 276 typedef T ProcessedMap; 287 277 static ProcessedMap *createProcessedMap(const Digraph &) 288 278 { 289 throw UninitializedParameter();279 LEMON_ASSERT(false, "ProcessedMap is not initialized"); 290 280 } 291 281 }; 292 282 ///\brief \ref named-templ-param "Named parameter" for setting … … 974 964 ///\return \c true if \c t is reachable form \c s. 975 965 bool run(Node s, Node t) 976 966 { 977 if (s==INVALID || t==INVALID) throw UninitializedParameter();978 967 Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); 979 968 if (Base::_pred) 980 969 alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); … … 1270 1259 class DfsVisit { 1271 1260 public: 1272 1261 1273 /// \brief \ref Exception for uninitialized parameters.1274 ///1275 /// This error represents problems in the initialization1276 /// of the parameters of the algorithm.1277 class UninitializedParameter : public lemon::UninitializedParameter {1278 public:1279 virtual const char* what() const throw()1280 {1281 return "lemon::DfsVisit::UninitializedParameter";1282 }1283 };1284 1285 1262 ///The traits class. 1286 1263 typedef _Traits Traits; 1287 1264 … … 1336 1313 struct SetReachedMapTraits : public Traits { 1337 1314 typedef T ReachedMap; 1338 1315 static ReachedMap *createReachedMap(const Digraph &digraph) { 1339 throw UninitializedParameter();1316 LEMON_ASSERT(false, "ReachedMap is not initialized"); 1340 1317 } 1341 1318 }; 1342 1319 /// \brief \ref named-templ-param "Named parameter" for setting -
lemon/dijkstra.h
diff -r bb40b6db0a58 -r 305d03f9bcea lemon/dijkstra.h
a b 225 225 #endif 226 226 class Dijkstra { 227 227 public: 228 ///\ref Exception for uninitialized parameters.229 230 ///This error represents problems in the initialization of the231 ///parameters of the algorithm.232 class UninitializedParameter : public lemon::UninitializedParameter {233 public:234 virtual const char* what() const throw() {235 return "lemon::Dijkstra::UninitializedParameter";236 }237 };238 228 239 229 ///The type of the digraph the algorithm runs on. 240 230 typedef typename TR::Digraph Digraph; … … 332 322 typedef T PredMap; 333 323 static PredMap *createPredMap(const Digraph &) 334 324 { 335 throw UninitializedParameter();325 LEMON_ASSERT(false, "PredMap is not initialized"); 336 326 } 337 327 }; 338 328 ///\brief \ref named-templ-param "Named parameter" for setting … … 351 341 typedef T DistMap; 352 342 static DistMap *createDistMap(const Digraph &) 353 343 { 354 throw UninitializedParameter();344 LEMON_ASSERT(false, "DistMap is not initialized"); 355 345 } 356 346 }; 357 347 ///\brief \ref named-templ-param "Named parameter" for setting … … 370 360 typedef T ProcessedMap; 371 361 static ProcessedMap *createProcessedMap(const Digraph &) 372 362 { 373 throw UninitializedParameter();363 LEMON_ASSERT(false, "ProcessedMap is not initialized"); 374 364 } 375 365 }; 376 366 ///\brief \ref named-templ-param "Named parameter" for setting … … 408 398 typedef CR HeapCrossRef; 409 399 typedef H Heap; 410 400 static HeapCrossRef *createHeapCrossRef(const Digraph &) { 411 throw UninitializedParameter();401 LEMON_ASSERT(false, "HeapCrossRef is not initialized"); 412 402 } 413 403 static Heap *createHeap(HeapCrossRef &) 414 404 { 415 throw UninitializedParameter();405 LEMON_ASSERT(false, "Heap is not initialized"); 416 406 } 417 407 }; 418 408 ///\brief \ref named-templ-param "Named parameter" for setting … … 1158 1148 ///in order to compute the shortest path to each node. 1159 1149 void run(Node s) 1160 1150 { 1161 if (s==INVALID) throw UninitializedParameter();1162 1151 Dijkstra<Digraph,LengthMap,TR> 1163 1152 dijk(*reinterpret_cast<const Digraph*>(Base::_g), 1164 1153 *reinterpret_cast<const LengthMap*>(Base::_length)); … … 1180 1169 ///\return \c true if \c t is reachable form \c s. 1181 1170 bool run(Node s, Node t) 1182 1171 { 1183 if (s==INVALID || t==INVALID) throw UninitializedParameter();1184 1172 Dijkstra<Digraph,LengthMap,TR> 1185 1173 dijk(*reinterpret_cast<const Digraph*>(Base::_g), 1186 1174 *reinterpret_cast<const LengthMap*>(Base::_length)); -
lemon/error.h
diff -r bb40b6db0a58 -r 305d03f9bcea lemon/error.h
a b 35 35 /// \addtogroup exceptions 36 36 /// @{ 37 37 38 /// \brief Exception safe wrapperclass.38 /// \brief Generic exception class. 39 39 /// 40 /// Exception safe wrapper class to implement the members of exceptions.41 template <typename _Type>42 class ExceptionMember {43 public:44 typedef _Type Type;45 46 ExceptionMember() throw() {47 try {48 ptr.reset(new Type());49 } catch (...) {}50 }51 52 ExceptionMember(const Type& type) throw() {53 try {54 ptr.reset(new Type());55 if (ptr.get() == 0) return;56 *ptr = type;57 } catch (...) {}58 }59 60 ExceptionMember(const ExceptionMember& copy) throw() {61 try {62 if (!copy.valid()) return;63 ptr.reset(new Type());64 if (ptr.get() == 0) return;65 *ptr = copy.get();66 } catch (...) {}67 }68 69 ExceptionMember& operator=(const ExceptionMember& copy) throw() {70 if (ptr.get() == 0) return;71 try {72 if (!copy.valid()) return;73 *ptr = copy.get();74 } catch (...) {}75 }76 77 void set(const Type& type) throw() {78 if (ptr.get() == 0) return;79 try {80 *ptr = type;81 } catch (...) {}82 }83 84 const Type& get() const {85 return *ptr;86 }87 88 bool valid() const throw() {89 return ptr.get() != 0;90 }91 92 private:93 std::auto_ptr<_Type> ptr;94 };95 96 /// Exception-safe convenient error message builder class.97 98 /// Helper class which provides a convenient ostream-like (operator <<99 /// based) interface to create a string message. Mostly useful in100 /// exception classes (therefore the name).101 class ErrorMessage {102 protected:103 ///\e104 105 mutable std::auto_ptr<std::ostringstream> buf;106 107 ///\e108 bool init() throw() {109 try {110 buf.reset(new std::ostringstream);111 }112 catch(...) {113 buf.reset();114 }115 return buf.get();116 }117 118 public:119 120 ///\e121 ErrorMessage() throw() { init(); }122 123 ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }124 125 ///\e126 ErrorMessage(const char *msg) throw() {127 init();128 *this << msg;129 }130 131 ///\e132 ErrorMessage(const std::string &msg) throw() {133 init();134 *this << msg;135 }136 137 ///\e138 template <typename T>139 ErrorMessage& operator<<(const T &t) throw() {140 if( ! buf.get() ) return *this;141 142 try {143 *buf << t;144 }145 catch(...) {146 buf.reset();147 }148 return *this;149 }150 151 ///\e152 const char* message() throw() {153 if( ! buf.get() ) return 0;154 155 const char* mes = 0;156 try {157 mes = buf->str().c_str();158 }159 catch(...) {}160 return mes;161 }162 163 };164 165 /// Generic exception class.166 167 40 /// Base class for exceptions used in LEMON. 168 41 /// 169 42 class Exception : public std::exception { 170 43 public: 171 ///\e 44 ///\e Constructor 172 45 Exception() {} 173 ///\e 46 ///\e Virtual destructor 174 47 virtual ~Exception() throw() {} 175 ///\e 48 ///\e A short description of the exception 176 49 virtual const char* what() const throw() { 177 50 return "lemon::Exception"; 178 51 } 179 52 }; 180 53 181 /// One of the two main subclasses of \ref Exception.182 183 /// Logic errors represent problems in the internal logic of a program;184 /// in theory, these are preventable, and even detectable before the185 /// program runs (e.g. violations of class invariants).186 ///187 /// A typical example for this is \ref UninitializedParameter.188 class LogicError : public Exception {189 public:190 virtual const char* what() const throw() {191 return "lemon::LogicError";192 }193 };194 195 /// \ref Exception for uninitialized parameters.196 197 /// This error represents problems in the initialization198 /// of the parameters of the algorithms.199 class UninitializedParameter : public LogicError {200 public:201 virtual const char* what() const throw() {202 return "lemon::UninitializedParameter";203 }204 };205 206 207 /// One of the two main subclasses of \ref Exception.208 209 /// Runtime errors represent problems outside the scope of a program;210 /// they cannot be easily predicted and can generally only be caught211 /// as the program executes.212 class RuntimeError : public Exception {213 public:214 virtual const char* what() const throw() {215 return "lemon::RuntimeError";216 }217 };218 219 54 ///\e 220 class RangeError : public RuntimeError { 221 public: 222 virtual const char* what() const throw() { 223 return "lemon::RangeError"; 224 } 225 }; 226 227 ///\e 228 class IoError : public RuntimeError { 229 public: 230 virtual const char* what() const throw() { 231 return "lemon::IoError"; 232 } 233 }; 234 235 ///\e 236 class DataFormatError : public IoError { 55 class IoError : Exception { 237 56 protected: 238 ExceptionMember<std::string>_message;239 ExceptionMember<std::string>_file;57 std::string _message; 58 std::string _file; 240 59 int _line; 241 60 242 mutable ExceptionMember<std::string> _message_holder;61 mutable std::string _what; 243 62 public: 244 63 245 DataFormatError(const DataFormatError &dfe) : 246 IoError(dfe), _message(dfe._message), _file(dfe._file), 247 _line(dfe._line) {} 248 249 ///\e 250 explicit DataFormatError(const char *the_message) 251 : _message(the_message), _line(0) {} 252 253 ///\e 254 DataFormatError(const std::string &file_name, int line_num, 255 const char *the_message) 256 : _message(the_message), _line(line_num) { file(file_name); } 257 258 ///\e 259 void line(int ln) { _line = ln; } 260 ///\e 261 void message(const std::string& msg) { _message.set(msg); } 262 ///\e 263 void file(const std::string &fl) { _file.set(fl); } 264 265 ///\e 266 int line() const { return _line; } 267 ///\e 268 const char* message() const { 269 if (_message.valid() && !_message.get().empty()) { 270 return _message.get().c_str(); 271 } else { 272 return 0; 273 } 64 /// Copy constructor 65 IoError(const IoError &error) { 66 message(error._message); 67 file(error._file); 68 line(error._line); 274 69 } 275 70 276 /// \brief Returns the filename. 277 /// 278 /// Returns \e null if the filename was not specified. 279 const char* file() const { 280 if (_file.valid() && !_file.get().empty()) { 281 return _file.get().c_str(); 282 } else { 283 return 0; 284 } 71 /// Constructor 72 explicit IoError(const char *message) { 73 IoError::message(message); 74 _line = 0; 285 75 } 286 76 287 ///\e 77 /// Constructor 78 explicit IoError(const std::string &message) { 79 IoError::message(message); 80 _line = 0; 81 } 82 83 /// Constructor 84 IoError(const std::string &file, int line, const char *message) { 85 IoError::message(message); 86 IoError::file(file); 87 IoError::line(line); 88 } 89 90 /// Constructor 91 IoError(const std::string &file, int line, const std::string &message) { 92 IoError::message(message); 93 IoError::file(file); 94 IoError::line(line); 95 } 96 97 /// Virtual destructor 98 virtual ~IoError() throw() {} 99 100 /// Set the line number 101 void line(int line) { _line = line; } 102 103 /// Set the error message 104 void message(const char *message) { 105 try { 106 _message = message; 107 } catch (...) {} 108 } 109 110 /// Set the error message 111 void message(const std::string& message) { 112 try { 113 _message = message; 114 } catch (...) {} 115 } 116 117 /// Set the file name 118 void file(const std::string &file) { 119 try { 120 _file = file; 121 } catch (...) {} 122 } 123 124 /// \brief Returns the line number 125 /// 126 /// Returns the line number or zero if it was not specified. 127 int line() const { return _line; } 128 129 /// Returns the error message 130 const std::string& message() const { 131 return _message; 132 } 133 134 /// \brief Returns the filename 135 /// 136 /// Returns the filename or empty string if the filename was not 137 /// specified. 138 const std::string& file() const { 139 return _file; 140 } 141 142 /// \brief Returns a short error message 143 /// 144 /// Returns a short error message which contains the message, the 145 /// file name and the line number. 288 146 virtual const char* what() const throw() { 289 147 try { 290 std::ostringstream ostr; 291 ostr << "lemon:DataFormatError" << ": "; 292 if (message()) ostr << message(); 293 if( file() || line() != 0 ) { 294 ostr << " ("; 295 if( file() ) ostr << "in file '" << file() << "'"; 296 if( file() && line() != 0 ) ostr << " "; 297 if( line() != 0 ) ostr << "at line " << line(); 298 ostr << ")"; 148 _what.clear(); 149 std::ostringstream oss; 150 oss << "lemon:IoError" << ": "; 151 oss << message(); 152 if (!file().empty() || line() != 0) { 153 oss << " ("; 154 if (!file().empty()) oss << "in file '" << file() << "'"; 155 if (!file().empty() && line() != 0) oss << " "; 156 if (line() != 0) oss << "at line " << line(); 157 oss << ")"; 299 158 } 300 _ message_holder.set(ostr.str());159 _what = oss.str(); 301 160 } 302 161 catch (...) {} 303 if ( _message_holder.valid()) return _message_holder.get().c_str();304 return "lemon:DataFormatError";162 if (!_what.empty()) return _what.c_str(); 163 else return "lemon:IoError"; 305 164 } 306 165 307 virtual ~DataFormatError() throw() {}308 };309 310 ///\e311 class FileOpenError : public IoError {312 protected:313 ExceptionMember<std::string> _file;314 315 mutable ExceptionMember<std::string> _message_holder;316 public:317 318 FileOpenError(const FileOpenError &foe) :319 IoError(foe), _file(foe._file) {}320 321 ///\e322 explicit FileOpenError(const std::string& fl)323 : _file(fl) {}324 325 326 ///\e327 void file(const std::string &fl) { _file.set(fl); }328 329 /// \brief Returns the filename.330 ///331 /// Returns \e null if the filename was not specified.332 const char* file() const {333 if (_file.valid() && !_file.get().empty()) {334 return _file.get().c_str();335 } else {336 return 0;337 }338 }339 340 ///\e341 virtual const char* what() const throw() {342 try {343 std::ostringstream ostr;344 ostr << "lemon::FileOpenError" << ": ";345 ostr << "Cannot open file - " << file();346 _message_holder.set(ostr.str());347 }348 catch (...) {}349 if( _message_holder.valid()) return _message_holder.get().c_str();350 return "lemon::FileOpenError";351 }352 virtual ~FileOpenError() throw() {}353 };354 355 class IoParameterError : public IoError {356 protected:357 ExceptionMember<std::string> _message;358 ExceptionMember<std::string> _file;359 360 mutable ExceptionMember<std::string> _message_holder;361 public:362 363 IoParameterError(const IoParameterError &ile) :364 IoError(ile), _message(ile._message), _file(ile._file) {}365 366 ///\e367 explicit IoParameterError(const char *the_message)368 : _message(the_message) {}369 370 ///\e371 IoParameterError(const char *file_name, const char *the_message)372 : _message(the_message), _file(file_name) {}373 374 ///\e375 void message(const std::string& msg) { _message.set(msg); }376 ///\e377 void file(const std::string &fl) { _file.set(fl); }378 379 ///\e380 const char* message() const {381 if (_message.valid()) {382 return _message.get().c_str();383 } else {384 return 0;385 }386 }387 388 /// \brief Returns the filename.389 ///390 /// Returns \c 0 if the filename was not specified.391 const char* file() const {392 if (_file.valid()) {393 return _file.get().c_str();394 } else {395 return 0;396 }397 }398 399 ///\e400 virtual const char* what() const throw() {401 try {402 std::ostringstream ostr;403 if (message()) ostr << message();404 if (file()) ostr << "(when reading file '" << file() << "')";405 _message_holder.set(ostr.str());406 }407 catch (...) {}408 if( _message_holder.valid() ) return _message_holder.get().c_str();409 return "lemon:IoParameterError";410 }411 virtual ~IoParameterError() throw() {}412 166 }; 413 167 414 168 /// @} -
lemon/lgf_reader.h
diff -r bb40b6db0a58 -r 305d03f9bcea lemon/lgf_reader.h
a b 52 52 53 53 char c; 54 54 if (is >> std::ws >> c) { 55 throw DataFormatError("Remaining characters in token");55 throw IoError("Remaining characters in token"); 56 56 } 57 57 return value; 58 58 } … … 166 166 if (it == _map.end()) { 167 167 std::ostringstream msg; 168 168 msg << "Item not found: " << str; 169 throw DataFormatError(msg.str().c_str());169 throw IoError(msg.str()); 170 170 } 171 171 return it->second; 172 172 } … … 184 184 185 185 typename Graph::Arc operator()(const std::string& str) { 186 186 if (str.empty() || (str[0] != '+' && str[0] != '-')) { 187 throw DataFormatError("Item must start with '+' or '-'");187 throw IoError("Item must start with '+' or '-'"); 188 188 } 189 189 typename std::map<std::string, typename Graph::Edge> 190 190 ::const_iterator it = _map.find(str.substr(1)); 191 191 if (it == _map.end()) { 192 throw DataFormatError("Item not found");192 throw IoError("Item not found"); 193 193 } 194 194 return _graph.direct(it->second, str[0] == '+'); 195 195 } … … 235 235 inline char readEscape(std::istream& is) { 236 236 char c; 237 237 if (!is.get(c)) 238 throw DataFormatError("Escape format error");238 throw IoError("Escape format error"); 239 239 240 240 switch (c) { 241 241 case '\\': … … 264 264 { 265 265 int code; 266 266 if (!is.get(c) || !isHex(c)) 267 throw DataFormatError("Escape format error");267 throw IoError("Escape format error"); 268 268 else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); 269 269 else code = code * 16 + valueHex(c); 270 270 return code; … … 273 273 { 274 274 int code; 275 275 if (!isOct(c)) 276 throw DataFormatError("Escape format error");276 throw IoError("Escape format error"); 277 277 else if (code = valueOct(c), !is.get(c) || !isOct(c)) 278 278 is.putback(c); 279 279 else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) … … 300 300 os << c; 301 301 } 302 302 if (!is) 303 throw DataFormatError("Quoted format error");303 throw IoError("Quoted format error"); 304 304 } else { 305 305 is.putback(c); 306 306 while (is.get(c) && !isWhiteSpace(c)) { … … 845 845 if (!readLine() || !(line >> c) || c == '@') { 846 846 if (readSuccess() && line) line.putback(c); 847 847 if (!_node_maps.empty()) 848 throw DataFormatError("Cannot find map names");848 throw IoError("Cannot find map names"); 849 849 return; 850 850 } 851 851 line.putback(c); … … 859 859 if (maps.find(map) != maps.end()) { 860 860 std::ostringstream msg; 861 861 msg << "Multiple occurence of node map: " << map; 862 throw DataFormatError(msg.str().c_str());862 throw IoError(msg.str()); 863 863 } 864 864 maps.insert(std::make_pair(map, index)); 865 865 ++index; … … 871 871 if (jt == maps.end()) { 872 872 std::ostringstream msg; 873 873 msg << "Map not found in file: " << _node_maps[i].first; 874 throw DataFormatError(msg.str().c_str());874 throw IoError(msg.str()); 875 875 } 876 876 map_index[i] = jt->second; 877 877 } … … 895 895 if (!_reader_bits::readToken(line, tokens[i])) { 896 896 std::ostringstream msg; 897 897 msg << "Column not found (" << i + 1 << ")"; 898 throw DataFormatError(msg.str().c_str());898 throw IoError(msg.str()); 899 899 } 900 900 } 901 901 if (line >> std::ws >> c) 902 throw DataFormatError("Extra character on the end of line");902 throw IoError("Extra character on the end of line"); 903 903 904 904 Node n; 905 905 if (!_use_nodes) { … … 908 908 _node_index.insert(std::make_pair(tokens[label_index], n)); 909 909 } else { 910 910 if (label_index == -1) 911 throw DataFormatError("Label map not found in file");911 throw IoError("Label map not found in file"); 912 912 typename std::map<std::string, Node>::iterator it = 913 913 _node_index.find(tokens[label_index]); 914 914 if (it == _node_index.end()) { 915 915 std::ostringstream msg; 916 916 msg << "Node with label not found: " << tokens[label_index]; 917 throw DataFormatError(msg.str().c_str());917 throw IoError(msg.str()); 918 918 } 919 919 n = it->second; 920 920 } … … 938 938 if (!readLine() || !(line >> c) || c == '@') { 939 939 if (readSuccess() && line) line.putback(c); 940 940 if (!_arc_maps.empty()) 941 throw DataFormatError("Cannot find map names");941 throw IoError("Cannot find map names"); 942 942 return; 943 943 } 944 944 line.putback(c); … … 952 952 if (maps.find(map) != maps.end()) { 953 953 std::ostringstream msg; 954 954 msg << "Multiple occurence of arc map: " << map; 955 throw DataFormatError(msg.str().c_str());955 throw IoError(msg.str()); 956 956 } 957 957 maps.insert(std::make_pair(map, index)); 958 958 ++index; … … 964 964 if (jt == maps.end()) { 965 965 std::ostringstream msg; 966 966 msg << "Map not found in file: " << _arc_maps[i].first; 967 throw DataFormatError(msg.str().c_str());967 throw IoError(msg.str()); 968 968 } 969 969 map_index[i] = jt->second; 970 970 } … … 987 987 std::string target_token; 988 988 989 989 if (!_reader_bits::readToken(line, source_token)) 990 throw DataFormatError("Source not found");990 throw IoError("Source not found"); 991 991 992 992 if (!_reader_bits::readToken(line, target_token)) 993 throw DataFormatError("Target not found");993 throw IoError("Target not found"); 994 994 995 995 std::vector<std::string> tokens(map_num); 996 996 for (int i = 0; i < map_num; ++i) { 997 997 if (!_reader_bits::readToken(line, tokens[i])) { 998 998 std::ostringstream msg; 999 999 msg << "Column not found (" << i + 1 << ")"; 1000 throw DataFormatError(msg.str().c_str());1000 throw IoError(msg.str()); 1001 1001 } 1002 1002 } 1003 1003 if (line >> std::ws >> c) 1004 throw DataFormatError("Extra character on the end of line");1004 throw IoError("Extra character on the end of line"); 1005 1005 1006 1006 Arc a; 1007 1007 if (!_use_arcs) { … … 1012 1012 if (it == _node_index.end()) { 1013 1013 std::ostringstream msg; 1014 1014 msg << "Item not found: " << source_token; 1015 throw DataFormatError(msg.str().c_str());1015 throw IoError(msg.str()); 1016 1016 } 1017 1017 Node source = it->second; 1018 1018 … … 1020 1020 if (it == _node_index.end()) { 1021 1021 std::ostringstream msg; 1022 1022 msg << "Item not found: " << target_token; 1023 throw DataFormatError(msg.str().c_str());1023 throw IoError(msg.str()); 1024 1024 } 1025 1025 Node target = it->second; 1026 1026 … … 1029 1029 _arc_index.insert(std::make_pair(tokens[label_index], a)); 1030 1030 } else { 1031 1031 if (label_index == -1) 1032 throw DataFormatError("Label map not found in file");1032 throw IoError("Label map not found in file"); 1033 1033 typename std::map<std::string, Arc>::iterator it = 1034 1034 _arc_index.find(tokens[label_index]); 1035 1035 if (it == _arc_index.end()) { 1036 1036 std::ostringstream msg; 1037 1037 msg << "Arc with label not found: " << tokens[label_index]; 1038 throw DataFormatError(msg.str().c_str());1038 throw IoError(msg.str()); 1039 1039 } 1040 1040 a = it->second; 1041 1041 } … … 1060 1060 1061 1061 std::string attr, token; 1062 1062 if (!_reader_bits::readToken(line, attr)) 1063 throw DataFormatError("Attribute name not found");1063 throw IoError("Attribute name not found"); 1064 1064 if (!_reader_bits::readToken(line, token)) 1065 throw DataFormatError("Attribute value not found");1065 throw IoError("Attribute value not found"); 1066 1066 if (line >> c) 1067 throw DataFormatError("Extra character on the end of line");1067 throw IoError("Extra character on the end of line"); 1068 1068 1069 1069 { 1070 1070 std::set<std::string>::iterator it = read_attr.find(attr); 1071 1071 if (it != read_attr.end()) { 1072 1072 std::ostringstream msg; 1073 1073 msg << "Multiple occurence of attribute " << attr; 1074 throw DataFormatError(msg.str().c_str());1074 throw IoError(msg.str()); 1075 1075 } 1076 1076 read_attr.insert(attr); 1077 1077 } … … 1093 1093 if (read_attr.find(it->first) == read_attr.end()) { 1094 1094 std::ostringstream msg; 1095 1095 msg << "Attribute not found in file: " << it->first; 1096 throw DataFormatError(msg.str().c_str());1096 throw IoError(msg.str()); 1097 1097 } 1098 1098 } 1099 1099 } … … 1109 1109 void run() { 1110 1110 LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); 1111 1111 if (!*_is) { 1112 throw DataFormatError("Cannot find file");1112 throw IoError("Cannot find file"); 1113 1113 } 1114 1114 1115 1115 bool nodes_done = _skip_nodes; … … 1129 1129 _reader_bits::readToken(line, caption); 1130 1130 1131 1131 if (line >> c) 1132 throw DataFormatError("Extra character on the end of line");1132 throw IoError("Extra character on the end of line"); 1133 1133 1134 1134 if (section == "nodes" && !nodes_done) { 1135 1135 if (_nodes_caption.empty() || _nodes_caption == caption) { … … 1151 1151 readLine(); 1152 1152 skipSection(); 1153 1153 } 1154 } catch ( DataFormatError& error) {1154 } catch (IoError& error) { 1155 1155 error.line(line_num); 1156 1156 throw; 1157 1157 } 1158 1158 } 1159 1159 1160 1160 if (!nodes_done) { 1161 throw DataFormatError("Section @nodes not found");1161 throw IoError("Section @nodes not found"); 1162 1162 } 1163 1163 1164 1164 if (!arcs_done) { 1165 throw DataFormatError("Section @arcs not found");1165 throw IoError("Section @arcs not found"); 1166 1166 } 1167 1167 1168 1168 if (!attributes_done && !_attributes.empty()) { 1169 throw DataFormatError("Section @attributes not found");1169 throw IoError("Section @attributes not found"); 1170 1170 } 1171 1171 1172 1172 } … … 1673 1673 if (!readLine() || !(line >> c) || c == '@') { 1674 1674 if (readSuccess() && line) line.putback(c); 1675 1675 if (!_node_maps.empty()) 1676 throw DataFormatError("Cannot find map names");1676 throw IoError("Cannot find map names"); 1677 1677 return; 1678 1678 } 1679 1679 line.putback(c); … … 1687 1687 if (maps.find(map) != maps.end()) { 1688 1688 std::ostringstream msg; 1689 1689 msg << "Multiple occurence of node map: " << map; 1690 throw DataFormatError(msg.str().c_str());1690 throw IoError(msg.str()); 1691 1691 } 1692 1692 maps.insert(std::make_pair(map, index)); 1693 1693 ++index; … … 1699 1699 if (jt == maps.end()) { 1700 1700 std::ostringstream msg; 1701 1701 msg << "Map not found in file: " << _node_maps[i].first; 1702 throw DataFormatError(msg.str().c_str());1702 throw IoError(msg.str()); 1703 1703 } 1704 1704 map_index[i] = jt->second; 1705 1705 } … … 1723 1723 if (!_reader_bits::readToken(line, tokens[i])) { 1724 1724 std::ostringstream msg; 1725 1725 msg << "Column not found (" << i + 1 << ")"; 1726 throw DataFormatError(msg.str().c_str());1726 throw IoError(msg.str()); 1727 1727 } 1728 1728 } 1729 1729 if (line >> std::ws >> c) 1730 throw DataFormatError("Extra character on the end of line");1730 throw IoError("Extra character on the end of line"); 1731 1731 1732 1732 Node n; 1733 1733 if (!_use_nodes) { … … 1736 1736 _node_index.insert(std::make_pair(tokens[label_index], n)); 1737 1737 } else { 1738 1738 if (label_index == -1) 1739 throw DataFormatError("Label map not found in file");1739 throw IoError("Label map not found in file"); 1740 1740 typename std::map<std::string, Node>::iterator it = 1741 1741 _node_index.find(tokens[label_index]); 1742 1742 if (it == _node_index.end()) { 1743 1743 std::ostringstream msg; 1744 1744 msg << "Node with label not found: " << tokens[label_index]; 1745 throw DataFormatError(msg.str().c_str());1745 throw IoError(msg.str()); 1746 1746 } 1747 1747 n = it->second; 1748 1748 } … … 1766 1766 if (!readLine() || !(line >> c) || c == '@') { 1767 1767 if (readSuccess() && line) line.putback(c); 1768 1768 if (!_edge_maps.empty()) 1769 throw DataFormatError("Cannot find map names");1769 throw IoError("Cannot find map names"); 1770 1770 return; 1771 1771 } 1772 1772 line.putback(c); … … 1780 1780 if (maps.find(map) != maps.end()) { 1781 1781 std::ostringstream msg; 1782 1782 msg << "Multiple occurence of edge map: " << map; 1783 throw DataFormatError(msg.str().c_str());1783 throw IoError(msg.str()); 1784 1784 } 1785 1785 maps.insert(std::make_pair(map, index)); 1786 1786 ++index; … … 1792 1792 if (jt == maps.end()) { 1793 1793 std::ostringstream msg; 1794 1794 msg << "Map not found in file: " << _edge_maps[i].first; 1795 throw DataFormatError(msg.str().c_str());1795 throw IoError(msg.str()); 1796 1796 } 1797 1797 map_index[i] = jt->second; 1798 1798 } … … 1815 1815 std::string target_token; 1816 1816 1817 1817 if (!_reader_bits::readToken(line, source_token)) 1818 throw DataFormatError("Node u not found");1818 throw IoError("Node u not found"); 1819 1819 1820 1820 if (!_reader_bits::readToken(line, target_token)) 1821 throw DataFormatError("Node v not found");1821 throw IoError("Node v not found"); 1822 1822 1823 1823 std::vector<std::string> tokens(map_num); 1824 1824 for (int i = 0; i < map_num; ++i) { 1825 1825 if (!_reader_bits::readToken(line, tokens[i])) { 1826 1826 std::ostringstream msg; 1827 1827 msg << "Column not found (" << i + 1 << ")"; 1828 throw DataFormatError(msg.str().c_str());1828 throw IoError(msg.str()); 1829 1829 } 1830 1830 } 1831 1831 if (line >> std::ws >> c) 1832 throw DataFormatError("Extra character on the end of line");1832 throw IoError("Extra character on the end of line"); 1833 1833 1834 1834 Edge e; 1835 1835 if (!_use_edges) { … … 1840 1840 if (it == _node_index.end()) { 1841 1841 std::ostringstream msg; 1842 1842 msg << "Item not found: " << source_token; 1843 throw DataFormatError(msg.str().c_str());1843 throw IoError(msg.str()); 1844 1844 } 1845 1845 Node source = it->second; 1846 1846 … … 1848 1848 if (it == _node_index.end()) { 1849 1849 std::ostringstream msg; 1850 1850 msg << "Item not found: " << target_token; 1851 throw DataFormatError(msg.str().c_str());1851 throw IoError(msg.str()); 1852 1852 } 1853 1853 Node target = it->second; 1854 1854 … … 1857 1857 _edge_index.insert(std::make_pair(tokens[label_index], e)); 1858 1858 } else { 1859 1859 if (label_index == -1) 1860 throw DataFormatError("Label map not found in file");1860 throw IoError("Label map not found in file"); 1861 1861 typename std::map<std::string, Edge>::iterator it = 1862 1862 _edge_index.find(tokens[label_index]); 1863 1863 if (it == _edge_index.end()) { 1864 1864 std::ostringstream msg; 1865 1865 msg << "Edge with label not found: " << tokens[label_index]; 1866 throw DataFormatError(msg.str().c_str());1866 throw IoError(msg.str()); 1867 1867 } 1868 1868 e = it->second; 1869 1869 } … … 1888 1888 1889 1889 std::string attr, token; 1890 1890 if (!_reader_bits::readToken(line, attr)) 1891 throw DataFormatError("Attribute name not found");1891 throw IoError("Attribute name not found"); 1892 1892 if (!_reader_bits::readToken(line, token)) 1893 throw DataFormatError("Attribute value not found");1893 throw IoError("Attribute value not found"); 1894 1894 if (line >> c) 1895 throw DataFormatError("Extra character on the end of line");1895 throw IoError("Extra character on the end of line"); 1896 1896 1897 1897 { 1898 1898 std::set<std::string>::iterator it = read_attr.find(attr); 1899 1899 if (it != read_attr.end()) { 1900 1900 std::ostringstream msg; 1901 1901 msg << "Multiple occurence of attribute " << attr; 1902 throw DataFormatError(msg.str().c_str());1902 throw IoError(msg.str()); 1903 1903 } 1904 1904 read_attr.insert(attr); 1905 1905 } … … 1921 1921 if (read_attr.find(it->first) == read_attr.end()) { 1922 1922 std::ostringstream msg; 1923 1923 msg << "Attribute not found in file: " << it->first; 1924 throw DataFormatError(msg.str().c_str());1924 throw IoError(msg.str()); 1925 1925 } 1926 1926 } 1927 1927 } … … 1955 1955 _reader_bits::readToken(line, caption); 1956 1956 1957 1957 if (line >> c) 1958 throw DataFormatError("Extra character on the end of line");1958 throw IoError("Extra character on the end of line"); 1959 1959 1960 1960 if (section == "nodes" && !nodes_done) { 1961 1961 if (_nodes_caption.empty() || _nodes_caption == caption) { … … 1977 1977 readLine(); 1978 1978 skipSection(); 1979 1979 } 1980 } catch ( DataFormatError& error) {1980 } catch (IoError& error) { 1981 1981 error.line(line_num); 1982 1982 throw; 1983 1983 } 1984 1984 } 1985 1985 1986 1986 if (!nodes_done) { 1987 throw DataFormatError("Section @nodes not found");1987 throw IoError("Section @nodes not found"); 1988 1988 } 1989 1989 1990 1990 if (!edges_done) { 1991 throw DataFormatError("Section @edges not found");1991 throw IoError("Section @edges not found"); 1992 1992 } 1993 1993 1994 1994 if (!attributes_done && !_attributes.empty()) { 1995 throw DataFormatError("Section @attributes not found");1995 throw IoError("Section @attributes not found"); 1996 1996 } 1997 1997 1998 1998 } … … 2236 2236 _reader_bits::readToken(line, caption); 2237 2237 2238 2238 if (line >> c) 2239 throw DataFormatError("Extra character on the end of line");2239 throw IoError("Extra character on the end of line"); 2240 2240 2241 2241 if (extra_sections.find(section) != extra_sections.end()) { 2242 2242 std::ostringstream msg; 2243 2243 msg << "Multiple occurence of section " << section; 2244 throw DataFormatError(msg.str().c_str());2244 throw IoError(msg.str()); 2245 2245 } 2246 2246 Sections::iterator it = _sections.find(section); 2247 2247 if (it != _sections.end()) { … … 2250 2250 } 2251 2251 readLine(); 2252 2252 skipSection(); 2253 } catch ( DataFormatError& error) {2253 } catch (IoError& error) { 2254 2254 error.line(line_num); 2255 2255 throw; 2256 2256 } … … 2260 2260 if (extra_sections.find(it->first) == extra_sections.end()) { 2261 2261 std::ostringstream os; 2262 2262 os << "Cannot find section: " << it->first; 2263 throw DataFormatError(os.str().c_str());2263 throw IoError(os.str()); 2264 2264 } 2265 2265 } 2266 2266 } -
lemon/lgf_writer.h
diff -r bb40b6db0a58 -r 305d03f9bcea lemon/lgf_writer.h
a b 55 55 56 56 template <typename T> 57 57 bool operator<(const T&, const T&) { 58 throw DataFormatError("Label map is not comparable");58 throw IoError("Label map is not comparable"); 59 59 } 60 60 61 61 template <typename _Map> … … 203 203 typename std::map<Value, std::string>::const_iterator it = 204 204 _map.find(str); 205 205 if (it == _map.end()) { 206 throw DataFormatError("Item not found");206 throw IoError("Item not found"); 207 207 } 208 208 return it->second; 209 209 } … … 223 223 typename std::map<typename Graph::Edge, std::string> 224 224 ::const_iterator it = _map.find(val); 225 225 if (it == _map.end()) { 226 throw DataFormatError("Item not found");226 throw IoError("Item not found"); 227 227 } 228 228 return (_graph.direction(val) ? '+' : '-') + it->second; 229 229 }