]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/articlenumber.cpp
Rename the project to R²C²
[r2c2.git] / source / libr2c2 / articlenumber.cpp
diff --git a/source/libr2c2/articlenumber.cpp b/source/libr2c2/articlenumber.cpp
new file mode 100644 (file)
index 0000000..45e526c
--- /dev/null
@@ -0,0 +1,90 @@
+/* $Id$
+
+This file is part of R²C²
+Copyright © 2010  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <msp/strings/utils.h>
+#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<string> sparts = split(s, '-');
+       for(vector<string>::iterator i=sparts.begin(); i!=sparts.end(); ++i)
+       {
+               if(i->empty())
+                       throw InvalidParameterValue("Malformed article number");
+
+               unsigned nondigit = i->size();
+               for(unsigned j=0; j<i->size(); ++j)
+                       if(!isdigit((*i)[j]))
+                       {
+                               nondigit = j;
+                               break;
+                       }
+
+               if(!nondigit || nondigit<i->size()-1)
+                       throw InvalidParameterValue("Malformed article number");
+
+               Part part;
+               part.number = lexical_cast<unsigned>(i->substr(0, nondigit));
+               part.letter = nondigit<i->size() ? (*i)[nondigit] : 0;
+               parts.push_back(part);
+       }
+}
+
+string ArticleNumber::str() const
+{
+       string result;
+       for(vector<Part>::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<other.parts;
+}
+
+
+bool ArticleNumber::Part::operator<(const Part &other) const
+{
+       if(number!=other.number)
+               return number<other.number;
+       return letter<other.letter;
+}
+
+
+void operator>>(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