Changeset View
Changeset View
Standalone View
Standalone View
src/address.cpp
Show All 14 Lines | 1 | /* | |||
---|---|---|---|---|---|
15 | You should have received a copy of the GNU Library General Public License | 15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | 16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | 18 | Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "address.h" | 21 | #include "address.h" | ||
22 | #include "geo.h" | 22 | #include "geo.h" | ||
23 | #include "countrytoisomap_data.cpp" | ||||
23 | 24 | | |||
24 | #include "kcontacts_debug.h" | 25 | #include "kcontacts_debug.h" | ||
25 | #include <krandom.h> | 26 | #include <krandom.h> | ||
26 | #include <klocalizedstring.h> | 27 | #include <klocalizedstring.h> | ||
27 | #include <kconfig.h> | 28 | #include <kconfig.h> | ||
28 | 29 | | |||
29 | #include <kconfiggroup.h> | 30 | #include <kconfiggroup.h> | ||
30 | 31 | | |||
▲ Show 20 Lines • Show All 608 Lines • ▼ Show 20 Line(s) | 639 | } else if (QLatin1String("above") == cpos) { | |||
639 | ret = country() + QLatin1String("\n\n") + ret; | 640 | ret = country() + QLatin1String("\n\n") + ret; | ||
640 | } | 641 | } | ||
641 | } | 642 | } | ||
642 | 643 | | |||
643 | return ret; | 644 | return ret; | ||
644 | } | 645 | } | ||
645 | 646 | | |||
646 | struct IsoCache { | 647 | struct IsoCache { | ||
647 | QHash<QString, QString> countryToIso; | | |||
648 | QHash<QString, QString> isoToCountry; | 648 | QHash<QString, QString> isoToCountry; | ||
649 | }; | 649 | }; | ||
650 | Q_GLOBAL_STATIC(IsoCache, sIsoCache) | 650 | Q_GLOBAL_STATIC(IsoCache, sIsoCache) | ||
651 | 651 | | |||
652 | QString Address::countryToISO(const QString &cname) | 652 | QString Address::countryToISO(const QString &cname) | ||
653 | { | 653 | { | ||
654 | // we search a map file for translations from country names to | 654 | const auto lookupKey = cname.toCaseFolded().toUtf8(); | ||
655 | // iso codes, storing caching things in a QHash for faster future | | |||
656 | // access. | | |||
657 | 655 | | |||
658 | const auto it = sIsoCache->countryToIso.constFind(cname); | 656 | // look for an exact match | ||
659 | if (it != sIsoCache->countryToIso.constEnd()) { | 657 | auto it = std::lower_bound(std::begin(country_to_iso_index), std::end(country_to_iso_index), lookupKey, [](const CountryToIsoIndex &lhs, const QByteArray &rhs) { | ||
660 | return it.value(); | 658 | return strcmp(country_name_stringtable + lhs.m_offset, rhs.constData()) < 0; | ||
661 | } | 659 | }); | ||
662 | 660 | if (it != std::end(country_to_iso_index) && strcmp(country_name_stringtable + (*it).m_offset, lookupKey.constData()) == 0) { | |||
663 | QFile file(QStringLiteral(":/org.kde.kcontacts/countrytransl.map")); | 661 | return (*it).isoCode(); | ||
664 | if (file.open(QIODevice::ReadOnly)) { | 662 | } | ||
665 | QTextStream s(&file); | 663 | | ||
666 | QString strbuf = s.readLine(); | 664 | // a unique prefix will do too | ||
667 | while (!strbuf.isEmpty()) { | 665 | it = std::lower_bound(std::begin(country_to_iso_index), std::end(country_to_iso_index), lookupKey, [](const CountryToIsoIndex &lhs, const QByteArray &rhs) { | ||
668 | const auto countryInfo = strbuf.splitRef(QLatin1Char('\t'), QString::KeepEmptyParts); | 666 | return strncmp(country_name_stringtable + lhs.m_offset, rhs.constData(), strlen(country_name_stringtable + lhs.m_offset)) < 0; | ||
669 | if (countryInfo[0] == cname) { | 667 | }); | ||
670 | file.close(); | 668 | const auto endIt = std::upper_bound(std::begin(country_to_iso_index), std::end(country_to_iso_index), lookupKey, [](const QByteArray &lhs, const CountryToIsoIndex &rhs) { | ||
671 | const auto iso = countryInfo[1].toString(); | 669 | return strncmp(lhs.constData(), country_name_stringtable + rhs.m_offset, strlen(country_name_stringtable + rhs.m_offset)) < 0; | ||
672 | sIsoCache->countryToIso.insert(cname, iso); | 670 | }); | ||
673 | return iso; | 671 | if (it != std::end(country_to_iso_index) && endIt == (it + 1) && strncmp(country_name_stringtable + (*it).m_offset, lookupKey.constData(), strlen(country_name_stringtable + (*it).m_offset)) == 0) { | ||
674 | } | 672 | return (*it).isoCode(); | ||
675 | strbuf = s.readLine(); | | |||
676 | } | | |||
677 | file.close(); | | |||
678 | } | 673 | } | ||
679 | 674 | | |||
680 | return {}; | 675 | return {}; | ||
681 | } | 676 | } | ||
682 | 677 | | |||
683 | QString Address::ISOtoCountry(const QString &ISOname) | 678 | QString Address::ISOtoCountry(const QString &ISOname) | ||
684 | { | 679 | { | ||
685 | // get country name from ISO country code (e.g. "no" -> i18n("Norway")) | 680 | // get country name from ISO country code (e.g. "no" -> i18n("Norway")) | ||
Show All 13 Lines | 692 | if (file.open(QIODevice::ReadOnly)) { | |||
699 | QString searchStr = QLatin1Char('\t') + iso; | 694 | QString searchStr = QLatin1Char('\t') + iso; | ||
700 | QString strbuf = s.readLine(); | 695 | QString strbuf = s.readLine(); | ||
701 | while (!strbuf.isEmpty()) { | 696 | while (!strbuf.isEmpty()) { | ||
702 | int pos; | 697 | int pos; | ||
703 | if ((pos = strbuf.indexOf(searchStr)) != -1) { | 698 | if ((pos = strbuf.indexOf(searchStr)) != -1) { | ||
704 | file.close(); | 699 | file.close(); | ||
705 | const auto country = i18nd("iso_3166-1", strbuf.leftRef(pos).toUtf8().constData()); | 700 | const auto country = i18nd("iso_3166-1", strbuf.leftRef(pos).toUtf8().constData()); | ||
706 | sIsoCache->isoToCountry.insert(iso, country); | 701 | sIsoCache->isoToCountry.insert(iso, country); | ||
707 | sIsoCache->countryToIso.insert(country, iso); | | |||
708 | return country; | 702 | return country; | ||
709 | } | 703 | } | ||
710 | strbuf = s.readLine(); | 704 | strbuf = s.readLine(); | ||
711 | } | 705 | } | ||
712 | file.close(); | 706 | file.close(); | ||
713 | } | 707 | } | ||
714 | 708 | | |||
715 | return ISOname; | 709 | return ISOname; | ||
Show All 22 Lines |