From: Mikko Rasa Date: Sat, 28 May 2011 08:54:12 +0000 (+0300) Subject: Exception changes for Fmt and Formatter X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=02df44f46e4aaa3989d0ee9c9e2e25f8a808e0ec;p=libs%2Fcore.git Exception changes for Fmt and Formatter --- diff --git a/source/strings/fmt.cpp b/source/strings/fmt.cpp index e06f71a..7026fe0 100644 --- a/source/strings/fmt.cpp +++ b/source/strings/fmt.cpp @@ -5,15 +5,15 @@ Copyright © 2006-2008 Mikko Rasa Distributed under the LGPL */ -#include #include "fmt.h" using namespace std; namespace Msp { -void Fmt::parse(const char *f) +void Fmt::parse(const char *s) { + const char *f = s; if(*f=='%') ++f; for(; *f; ++f) @@ -82,7 +82,7 @@ void Fmt::parse(const char *f) else if(*f=='i') base = AUTOBASE; else - throw InvalidParameterValue("Invalid conversion specifier"); + throw format_error(s); if(*f=='E' || *f=='F' || *f=='G' || *f=='X' || *f=='P') ucase = true; @@ -90,7 +90,7 @@ void Fmt::parse(const char *f) ++f; if(*f) - throw InvalidParameterValue("Extra characters in conversion specification"); + throw format_error(s); } Fmt &Fmt::reset() diff --git a/source/strings/fmt.h b/source/strings/fmt.h index ba75958..8633324 100644 --- a/source/strings/fmt.h +++ b/source/strings/fmt.h @@ -9,10 +9,19 @@ Distributed under the LGPL #define MSP_STRINGS_FMT_H_ #include +#include #include namespace Msp { +class format_error: public std::logic_error +{ +public: + format_error(const std::string &w): std::logic_error(w) { } + virtual ~format_error() throw() { } +}; + + /** Stores formatting information for converting variables into strings. Can be applied to an std::ostream or fed to lexical_cast. Also used internally by diff --git a/source/strings/format.cpp b/source/strings/format.cpp index dfd3800..4fa5e2c 100644 --- a/source/strings/format.cpp +++ b/source/strings/format.cpp @@ -25,7 +25,7 @@ values have been fed to the formatter. const string &Formatter::str() const { if(pos!=fmt.end()) - throw Exception("Too few arguments for format"); + throw format_error("Too few arguments for format"); return result; } @@ -43,7 +43,7 @@ void Formatter::advance() { ++pos; if(pos==fmt.end()) - throw Exception("Malformed format string"); + throw format_error("Malformed format string"); if(*pos!='%') break; } @@ -59,7 +59,7 @@ Fmt object. Fmt Formatter::get_conversion() { if(pos==fmt.end()) - throw Exception("Too many arguments for format"); + throw format_error("Too many arguments for format"); string::iterator i = pos; for(; i!=fmt.end(); ++i) @@ -67,7 +67,7 @@ Fmt Formatter::get_conversion() break; if(i==fmt.end()) - throw Exception("Malformed format string"); + throw format_error("Malformed format string"); ++i; string c(pos, i);