X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fstrings%2Fformatter.cpp;fp=source%2Fstrings%2Fformatter.cpp;h=d9d311d5f43813a921016f0694661ce5da67fed4;hp=0000000000000000000000000000000000000000;hb=b42ed73a1b241c0e93ee03c43c4584b41c549bac;hpb=5b1368cb791cab043f0435628cacbaff36e39b7b diff --git a/source/strings/formatter.cpp b/source/strings/formatter.cpp new file mode 100644 index 0000000..d9d311d --- /dev/null +++ b/source/strings/formatter.cpp @@ -0,0 +1,79 @@ +/* $Id$ + +This file is part of libmspstrings +Copyright © 2006-2007 Mikko Rasa +Distributed under the LGPL +*/ + +#include "formatter.h" + +using namespace std; + +namespace Msp { + +Formatter::Formatter(const string &f): + fmt(f), + pos(fmt.begin()) +{ + advance(); +} + +/** +Returns the result of the formatting operation. Will throw if not enough +values have been fed to the formatter. +*/ +const string &Formatter::str() const +{ + if(pos!=fmt.end()) + throw Exception("Too few arguments for format"); + + return result; +} + +/** +Advances the pos iterator to the next conversion, adding literal characters to +the result. The iterator is left at the second character of the conversion +(i.e. after the %). +*/ +void Formatter::advance() +{ + for(; pos!=fmt.end(); ++pos) + { + if(*pos=='%') + { + ++pos; + if(pos==fmt.end()) + throw Exception("Malformed format string"); + if(*pos!='%') + break; + } + + result += *pos; + } +} + +/** +Reads the next conversion from the format string and returns a corresponding +Fmt object. +*/ +Fmt Formatter::get_conversion() +{ + if(pos==fmt.end()) + throw Exception("Too many arguments for format"); + + string::iterator i = pos; + for(; i!=fmt.end(); ++i) + if(isalpha(*i)) + break; + + if(i==fmt.end()) + throw Exception("Malformed format string"); + + ++i; + string c(pos, i); + pos = i; + + return Fmt(c); +} + +} // namespace Msp