/* * SNumber.cpp * * Created on: May 19, 2009 * Author: saemy */ #include "snumber.h" #include #include #include #include #include /** * Format: * c country code (1-digit) * cc country code (2-digits) * CC country code trim zeros * * a area code (1-digit) * aa area code (2-digits) * aaa area code (3-digits) * AAA area code trim zeros * * u The user number * * 'foo' Ignored part */ const QString SNumber::IsoFormat = "'+'cc' 'aaa' 'u"; // +41 079 1234567 const QString SNumber::IsoFormatShort = "'+'CC' 'AAA' 'u"; // +41 79 1234567 const QString SNumber::GuessFormat = "%%guess%%"; SNumber::SNumber() { clear(); } SNumber::SNumber(const QString& numberStr, const QString& format) { setNumber(numberStr, format); } SNumber::SNumber(const SNumber& other) { fromString(other.toString("ccaaau"), "ccaaau"); } void SNumber::clear() { countryCode_ = 0; areaCode_ = 0; userNumber_ = ""; } bool SNumber::isEmpty() const { return countryCode() == -1; } bool SNumber::isValid() const { return (countryCode_ > 0) && (areaCode_ > 0) && (userNumber_ != ""); } short SNumber::countryCode() const{ return countryCode_; } short SNumber::areaCode() const{ return areaCode_; } QString SNumber::userNumber() const{ return userNumber_; } /* CC & AAA are not applicable here! */ bool SNumber::setNumber(const QString& numberStr, const QString& format){ if (format == SNumber::GuessFormat) { QStringList formats; formats.append("'+'cc' 'aaa' 'u");// +41 079 1234567 formats.append("'+'cc' 'aa' 'u"); // +41 79 1234567 formats.append("'+'cc' 'a' 'u"); // +41 7 1234567 formats.append("'+'c' 'aaa' 'u"); // +1 079 1234567 formats.append("'+'c' 'aa' 'u"); // +1 79 1234567 formats.append("'+'c' 'a' 'u"); // +1 7 1234567 formats.append("'+'cc' 'aaau"); // +41 0791234567 (Attention! A part of the usernumber could be interpreted as the areacode) formats.append("'+'cc' 'aau"); // +41 791234567 (Attention! A part of the usernumber could be interpreted as the areacode) formats.append("'+'cc' 'au"); // +41 71234567 (Attention! A part of the usernumber could be interpreted as the areacode) formats.append("'+'c' 'aaau"); // +1 0791234567 (Attention! A part of the usernumber could be interpreted as the areacode) formats.append("'+'c' 'aau"); // +1 791234567 (Attention! A part of the usernumber could be interpreted as the areacode) formats.append("'+'c' 'au"); // +1 71234567 (Attention! A part of the usernumber could be interpreted as the areacode) foreach(QString format_, formats) { if (setNumber(numberStr, format_)) { return true; } } return false; } clear(); qDebug() << "Checking " + format + " on " + numberStr; const QLatin1Char quote('\''); if (format.isEmpty()) return false; bool inQuote = false; int j = 0; for (int i = 0; i < format.count(); i++) { if (numberStr.count() <= j) { clear(); break; } if (format.at(i) == quote) { inQuote = !inQuote; continue; } if (inQuote) { if (format.at(i) == numberStr.at(j)) { j++; continue; } else { clear(); break; } } if (format.mid(i, 2) == "cc") { setCountryCode(numberStr.mid(j, 2).toShort()); j += 2; i++; } else if (format.mid(i, 1) == "c") { setCountryCode(numberStr.mid(i, 1).toShort()); j++; } else if (format.mid(i, 3) == "aaa") { setAreaCode(numberStr.mid(j, 3).toShort()); j += 3; i += 2; } else if (format.mid(i, 2) == "aa") { setAreaCode(numberStr.mid(j, 2).toShort()); j += 2; i++; } else if (format.mid(i, 1) == "a") { setAreaCode(numberStr.mid(j, 1).toShort()); j++; } else if (format.at(i) == 'u') { QString userNumber = ""; while (j < numberStr.count()) { userNumber += numberStr.at(j); j++; } setUserNumber(userNumber); j++; } else if (format.at(i) == numberStr.at(j)) { j++; } else { if (format.mid(i, 2) == "CC") { qWarning() << "CC not supported here!"; } else if (format.mid(i, 3) == "AAA") { qWarning() << "AAA not supported here!"; } clear(); break; } } return isValid(); } bool SNumber::isStringPositiveNumber(const QString& str) { if (str == "") return false; for (int i = 0; i < str.count(); i++) { if (!str.at(i).isNumber()) return false; } return true; } void SNumber::setCountryCode(short countryCode){ if ((countryCode <= 0) || (countryCode >= 100)) return; countryCode_ = countryCode; } void SNumber::setAreaCode(short areaCode){ if ((areaCode <= 0) || (areaCode >= 1000)) return; areaCode_ = areaCode; } void SNumber::setUserNumber(const QString& userNumberStr){ if (userNumberStr.toInt() <= 0) return; userNumber_ = userNumberStr; } QString SNumber::toString(const QString& format) const { if (!isValid()) { return ""; } const QLatin1Char quote('\''); if (format.isEmpty()) return QString(); QString res = ""; bool inQuote = false; for (int i = 0; i < format.length(); i++) { if (format.at(i) == quote) { inQuote = !inQuote; continue; } if (inQuote) { res += format.at(i); continue; } if (format.mid(i, 2) == "CC") { res += QString::number(countryCode()); i++; } else if (format.mid(i, 2) == "cc") { res += QString::number(countryCode()).rightJustified(2, '0'); i++; } else if (format.mid(i, 1) == "c") { res += QString::number(countryCode()).rightJustified(1, '0'); } else if (format.mid(i, 3) == "AAA") { res += QString::number(areaCode()); i += 2; } else if (format.mid(i, 3) == "aaa") { res += QString::number(areaCode()).rightJustified(3, '0'); i += 2; } else if (format.mid(i, 2) == "aa") { res += QString::number(areaCode()).rightJustified(2, '0'); i += 1; } else if (format.mid(i, 1) == "a") { res += QString::number(areaCode()).rightJustified(1, '0'); } else if (format.at(i) == 'u') { res += userNumber(); } else { res += format.at(i); } } return res; } bool SNumber::readFromString(const QString& string, const QString& format) { return setNumber(string, format); } SNumber SNumber::fromString(const QString& string, const QString& format) { return SNumber(string, format); } bool SNumber::operator==(const SNumber& n) const { return this->toString() == n.toString(); }