3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include <msp/strings/utils.h>
9 #include "articlenumber.h"
16 ArticleNumber::ArticleNumber(unsigned n)
21 parts.push_back(part);
24 ArticleNumber::ArticleNumber(const string &s)
26 vector<string> sparts = split(s, '-');
27 for(vector<string>::iterator i=sparts.begin(); i!=sparts.end(); ++i)
30 throw InvalidParameterValue("Malformed article number");
32 unsigned nondigit = i->size();
33 for(unsigned j=0; j<i->size(); ++j)
40 if(!nondigit || nondigit<i->size()-1)
41 throw InvalidParameterValue("Malformed article number");
44 part.number = lexical_cast<unsigned>(i->substr(0, nondigit));
45 part.letter = nondigit<i->size() ? (*i)[nondigit] : 0;
46 parts.push_back(part);
50 string ArticleNumber::str() const
53 for(vector<Part>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
58 result += lexical_cast(i->number);
66 bool ArticleNumber::operator<(const ArticleNumber &other) const
68 return parts<other.parts;
72 bool ArticleNumber::Part::operator<(const Part &other) const
74 if(number!=other.number)
75 return number<other.number;
76 return letter<other.letter;
80 void operator>>(const LexicalConverter &conv, ArticleNumber &art_nr)
82 art_nr = ArticleNumber(conv.get());
85 void operator<<(LexicalConverter &conv, const ArticleNumber &art_nr)
87 conv.result(art_nr.str());
90 } // namespace Marklin