diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-03-01 19:17:32 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-03-01 19:17:32 +0000 |
commit | e38d2351b83fa65c66ccde443777647ef5cb6cff (patch) | |
tree | 1897fc20e9f73a81c520a5b9f76f8ed042124883 /src/lccnvalidator.cpp | |
download | tellico-e38d2351b83fa65c66ccde443777647ef5cb6cff.tar.gz tellico-e38d2351b83fa65c66ccde443777647ef5cb6cff.zip |
Added KDE3 version of Tellico
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/tellico@1097620 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'src/lccnvalidator.cpp')
-rw-r--r-- | src/lccnvalidator.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/lccnvalidator.cpp b/src/lccnvalidator.cpp new file mode 100644 index 0000000..8c47931 --- /dev/null +++ b/src/lccnvalidator.cpp @@ -0,0 +1,74 @@ +/*************************************************************************** + copyright : (C) 2008 by Robby Stephenson + email : [email protected] + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of version 2 of the GNU General Public License as * + * published by the Free Software Foundation; * + * * + ***************************************************************************/ + +#include "lccnvalidator.h" +#include "tellico_debug.h" + +using Tellico::LCCNValidator; + +LCCNValidator::LCCNValidator(QObject* parent_) : QRegExpValidator(parent_) { + QRegExp rx(QString::fromLatin1("[a-z ]{0,3}" + "(" + "\\d{2}-?\\d{1,6}" + "|" + "\\d{4}-?\\d{1,6}" + ")" + " ?\\w*")); + setRegExp(rx); +} + +// static +QString LCCNValidator::formalize(const QString& value_) { + const int len = value_.length(); + // first remove alpha prefix + QString alpha; + for(int pos = 0; pos < len; ++pos) { + QChar c = value_.at(pos); + if(c.isNumber()) { + break; + } + alpha += value_.at(pos); + } + QString afterAlpha = value_.mid(alpha.length()); + alpha = alpha.stripWhiteSpace(); // possible to have a space at the end + + QString year; + QString serial; + // have to be able to differentiate 2 and 4-digit years, first check for hyphen position + int pos = afterAlpha.find('-'); + if(pos > -1) { + year = afterAlpha.section('-', 0, 0); + serial = afterAlpha.section('-', 1); + } else { + // make two assumptions, the user will never have a book from the year 1920 + // or from any year after 2100. Reasonable, right? + // so if the string starts with '20' we take the first 4 digits as the year + // otherwise the first 2 + if(afterAlpha.startsWith(QString::fromLatin1("20"))) { + year = afterAlpha.left(4); + serial = afterAlpha.mid(4); + } else { + year = afterAlpha.left(2); + serial = afterAlpha.mid(2); + } + } + + // now check for non digits in the serial + pos = 0; + for( ; pos < serial.length() && serial.at(pos).isNumber(); ++pos) { ; } + QString suffix = serial.mid(pos); + serial = serial.left(pos); + // serial must be left-padded with zeros to 6 characters + serial = serial.rightJustify(6, '0'); + return alpha + year + serial + suffix; +} |