File indexing completed on 2024-05-12 16:46:38

0001 /***************************************************************************
0002     Copyright (C) 2008-2020 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "lccnvalidator.h"
0026 #include "../tellico_debug.h"
0027 
0028 using Tellico::LCCNValidator;
0029 
0030 LCCNValidator::LCCNValidator(QObject* parent_) : QRegularExpressionValidator(parent_) {
0031   QRegularExpression rx(QLatin1String("[a-z ]{0,3}"
0032                                       "("
0033                                       "\\d{2}-?\\d{1,6}"
0034                                       "|"
0035                                       "\\d{4}-?\\d{1,6}"
0036                                       ")"
0037                                       " ?\\w*"));
0038   setRegularExpression(rx);
0039 }
0040 
0041 // static
0042 QString LCCNValidator::formalize(const QString& value_) {
0043   QString value = value_.simplified();
0044   // remove spaces
0045   value.remove(QLatin1Char(' '));
0046   // remove everything after the forward slash
0047   value = value.section(QLatin1Char('/'), 0, 0);
0048 
0049   const int len = value_.length();
0050   // first remove alpha prefix
0051   QString alpha;
0052   for(int pos = 0; pos < len; ++pos) {
0053     QChar c = value.at(pos);
0054     if(c.isNumber()) {
0055       break;
0056     }
0057     alpha += value.at(pos);
0058   }
0059   QString afterAlpha = value.mid(alpha.length());
0060   alpha = alpha.trimmed(); // possible to have a space at the end
0061 
0062   QString year;
0063   QString serial;
0064   // have to be able to differentiate 2 and 4-digit years, first check for hyphen position
0065   int pos = afterAlpha.indexOf(QLatin1Char('-'));
0066   if(pos > -1) {
0067     year = afterAlpha.section(QLatin1Char('-'), 0, 0);
0068     serial = afterAlpha.section(QLatin1Char('-'), 1);
0069   } else {
0070     // make two assumptions, the user will never have a book from the year 1920
0071     // or from any year after 2100. Reasonable, right?
0072     // so if the string starts with '20' we take the first 4 digits as the year
0073     // otherwise the first 2
0074     if(afterAlpha.startsWith(QLatin1String("20"))) {
0075       year = afterAlpha.left(4);
0076       serial = afterAlpha.mid(4);
0077     } else {
0078       year = afterAlpha.left(2);
0079       serial = afterAlpha.mid(2);
0080     }
0081   }
0082 
0083   // now check for non digits in the serial
0084   pos = 0;
0085   for( ; pos < serial.length() && serial.at(pos).isNumber(); ++pos) { ; }
0086   QString suffix = serial.mid(pos);
0087   serial = serial.left(pos);
0088   // serial must be left-padded with zeros to 6 characters
0089   serial = serial.rightJustified(6, QLatin1Char('0'));
0090   return alpha + year + serial + suffix;
0091 }