Move guessCountryTLD to CountryCode.h from LanguageIdentifier.h

This commit is contained in:
Ai Lin Chia
2017-06-23 15:13:50 +02:00
parent 8030c75e75
commit 3fcaf21a34
5 changed files with 34 additions and 33 deletions

@ -275,6 +275,35 @@ const char *getCountryCode ( uint16_t crid ) {
return s_countryCode[crid];
}
uint8_t guessCountryTLD(const char *url) {
uint8_t country = 0;
char code[3];
code[0] = code[1] = code [2] = 0;
// check for prefix
if(url[9] == '.') {
code[0] = url[7];
code[1] = url[8];
code[2] = 0;
country = g_countryCode.getIndexOfAbbr(code);
if(country) return(country);
}
// Check for two letter TLD
const char *cp = strchr(url+7, ':');
if(!cp)
cp = strchr(url+7, '/');
if(cp && *(cp -3) == '.') {
cp -= 2;
code[0] = cp[0];
code[1] = cp[1];
code[2] = 0;
country = g_countryCode.getIndexOfAbbr(code);
if(country) return(country);
}
return(country);
}
// get the id from a 2 character country code
uint8_t getCountryId ( char *cc ) {
static bool s_init = false;

@ -11,6 +11,8 @@ uint8_t getCountryId ( char *cc ) ;
// map a country id to the two letter country abbr
const char *getCountryCode ( uint16_t crid );
uint8_t guessCountryTLD(const char *url);
class CountryCode {
public:
CountryCode();

@ -1,31 +1,2 @@
#include "LanguageIdentifier.h"
#include "CountryCode.h"
uint8_t LanguageIdentifier::guessCountryTLD(const char *url) {
uint8_t country = 0;
char code[3];
code[0] = code[1] = code [2] = 0;
// check for prefix
if(url[9] == '.') {
code[0] = url[7];
code[1] = url[8];
code[2] = 0;
country = g_countryCode.getIndexOfAbbr(code);
if(country) return(country);
}
// Check for two letter TLD
const char *cp = strchr(url+7, ':');
if(!cp)
cp = strchr(url+7, '/');
if(cp && *(cp -3) == '.') {
cp -= 2;
code[0] = cp[0];
code[1] = cp[1];
code[2] = 0;
country = g_countryCode.getIndexOfAbbr(code);
if(country) return(country);
}
return(country);
}

@ -11,9 +11,8 @@
#include <stdint.h>
/// Contains methods of language identification by various means.
class LanguageIdentifier {
public:
static uint8_t guessCountryTLD(const char *url);
namespace LanguageIdentifier {
};
#endif // GB_LANGUAGEIDENTIFIER_H

@ -5731,7 +5731,7 @@ uint16_t *XmlDoc::getCountryId ( ) {
if ( ! u || u == (void *)-1) return (uint16_t *)u;
// use the url's tld to guess the country
uint16_t country = LanguageIdentifier::guessCountryTLD ( u->getUrl ( ) );
uint16_t country = guessCountryTLD ( u->getUrl ( ) );
m_countryIdValid = true;
m_countryId = country;