Update EPG Cache(thanks to open source community)
[vuplus_dvbapp] / lib / base / encoding.cpp
1 #include <cstdio>
2 #include <cstdlib>
3 #include <lib/base/encoding.h>
4 #include <lib/base/eerror.h>
5 #include <lib/base/eenv.h>
6
7 eDVBTextEncodingHandler encodingHandler;  // the one and only instance
8 int defaultEncodingTable = 1; // ISO8859-1 / Latin1
9
10 inline char toupper(char c)
11 {
12         return (c >= 'a' && c <= 'z') ? c - ('a' - 'A') : c;
13 }
14
15 inline char tolower(char c)
16 {
17         return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c;
18 }
19
20 eDVBTextEncodingHandler::eDVBTextEncodingHandler()
21 {
22         std::string file = eEnv::resolve("${datadir}/enigma2/encoding.conf");
23         FILE *f = fopen(file.c_str(), "rt");
24         if (f)
25         {
26                 char *line = (char*) malloc(256);
27                 size_t bufsize=256;
28                 char countrycode[256];
29                 while( getline(&line, &bufsize, f) != -1 )
30                 {
31                         if ( line[0] == '#' )
32                                 continue;
33                         int tsid, onid, encoding;
34                         if ( (sscanf( line, "0x%x 0x%x ISO8859-%d", &tsid, &onid, &encoding ) == 3 )
35                                         ||(sscanf( line, "%d %d ISO8859-%d", &tsid, &onid, &encoding ) == 3 ) )
36                                 m_TransponderDefaultMapping[(tsid<<16)|onid]=encoding;
37                         else if ( sscanf( line, "%s ISO8859-%d", countrycode, &encoding ) == 2 )
38                         {
39                                 m_CountryCodeDefaultMapping[countrycode]=encoding;
40                                 countrycode[0]=toupper(countrycode[0]);
41                                 countrycode[1]=toupper(countrycode[1]);
42                                 countrycode[2]=toupper(countrycode[2]);
43                                 m_CountryCodeDefaultMapping[countrycode]=encoding;
44                         }
45                         else if ( (sscanf( line, "0x%x 0x%x ISO%d", &tsid, &onid, &encoding ) == 3 && encoding == 6937 )
46                                         ||(sscanf( line, "%d %d ISO%d", &tsid, &onid, &encoding ) == 3 && encoding == 6937 ) )
47                                 m_TransponderDefaultMapping[(tsid<<16)|onid]=0;
48                         else if ( sscanf( line, "%s ISO%d", countrycode, &encoding ) == 2 && encoding == 6937 )
49                         {
50                                 m_CountryCodeDefaultMapping[countrycode]=0;
51                                 countrycode[0]=toupper(countrycode[0]);
52                                 countrycode[1]=toupper(countrycode[1]);
53                                 countrycode[2]=toupper(countrycode[2]);
54                                 m_CountryCodeDefaultMapping[countrycode]=0;
55                         }
56                         else if ( (sscanf( line, "0x%x 0x%x", &tsid, &onid ) == 2 )
57                                         ||(sscanf( line, "%d %d", &tsid, &onid ) == 2 ) )
58                                 m_TransponderUseTwoCharMapping.insert((tsid<<16)|onid);
59                         else
60                                 eDebug("couldn't parse %s", line);
61                 }
62                 fclose(f);
63                 free(line);
64         }
65         else
66                 eDebug("[eDVBTextEncodingHandler] couldn't open %s !", file.c_str());
67 }
68
69 void eDVBTextEncodingHandler::getTransponderDefaultMapping(int tsidonid, int &table)
70 {
71         std::map<unsigned int, int>::iterator it =
72                 m_TransponderDefaultMapping.find(tsidonid);
73         if ( it != m_TransponderDefaultMapping.end() )
74                 table = it->second;
75 }
76
77 bool eDVBTextEncodingHandler::getTransponderUseTwoCharMapping(int tsidonid)
78 {
79         return m_TransponderUseTwoCharMapping.find(tsidonid) != m_TransponderUseTwoCharMapping.end();
80 }
81
82 int eDVBTextEncodingHandler::getCountryCodeDefaultMapping( const std::string &country_code )
83 {
84         std::map<std::string, int>::iterator it =
85                 m_CountryCodeDefaultMapping.find(country_code);
86         if ( it != m_CountryCodeDefaultMapping.end() )
87                 return it->second;
88         return defaultEncodingTable;
89 }