X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Flibr2c2%2Farticlenumber.cpp;fp=source%2Flibr2c2%2Farticlenumber.cpp;h=45e526ca18597056ed14c5dfc14134648a7c6fa0;hb=1ff06c5bc46a677fa389ef86c6b26664368f1653;hp=0000000000000000000000000000000000000000;hpb=9b05c573a38639827697fe393d55b7c76f5bde45;p=r2c2.git diff --git a/source/libr2c2/articlenumber.cpp b/source/libr2c2/articlenumber.cpp new file mode 100644 index 0000000..45e526c --- /dev/null +++ b/source/libr2c2/articlenumber.cpp @@ -0,0 +1,90 @@ +/* $Id$ + +This file is part of R²C² +Copyright © 2010 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#include +#include "articlenumber.h" + +using namespace std; +using namespace Msp; + +namespace R2C2 { + +ArticleNumber::ArticleNumber(unsigned n) +{ + Part part; + part.number = n; + part.letter = 0; + parts.push_back(part); +} + +ArticleNumber::ArticleNumber(const string &s) +{ + vector sparts = split(s, '-'); + for(vector::iterator i=sparts.begin(); i!=sparts.end(); ++i) + { + if(i->empty()) + throw InvalidParameterValue("Malformed article number"); + + unsigned nondigit = i->size(); + for(unsigned j=0; jsize(); ++j) + if(!isdigit((*i)[j])) + { + nondigit = j; + break; + } + + if(!nondigit || nondigitsize()-1) + throw InvalidParameterValue("Malformed article number"); + + Part part; + part.number = lexical_cast(i->substr(0, nondigit)); + part.letter = nondigitsize() ? (*i)[nondigit] : 0; + parts.push_back(part); + } +} + +string ArticleNumber::str() const +{ + string result; + for(vector::const_iterator i=parts.begin(); i!=parts.end(); ++i) + { + if(!result.empty()) + result += '-'; + + result += lexical_cast(i->number); + if(i->letter) + result += i->letter; + } + + return result; +} + +bool ArticleNumber::operator<(const ArticleNumber &other) const +{ + return parts>(const LexicalConverter &conv, ArticleNumber &art_nr) +{ + art_nr = ArticleNumber(conv.get()); +} + +void operator<<(LexicalConverter &conv, const ArticleNumber &art_nr) +{ + conv.result(art_nr.str()); +} + +} // namespace R2C2