Merge branch 'pootle-import'
[enigma2.git] / 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
9 inline char toupper(char c)
10 {
11         switch (c)
12         {
13                 case 'a' ... 'z':
14                         return c-32;
15         }
16         return c;
17 }
18
19 eDVBTextEncodingHandler::eDVBTextEncodingHandler()
20 {
21         std::string file = eEnv::resolve("${datadir}/enigma2/encoding.conf");
22         FILE *f = fopen(file.c_str(), "rt");
23         if (f)
24         {
25                 char *line = (char*) malloc(256);
26                 size_t bufsize=256;
27                 char countrycode[256];
28                 while( getline(&line, &bufsize, f) != -1 )
29                 {
30                         if ( line[0] == '#' )
31                                 continue;
32                         int tsid, onid, encoding;
33                         if ( (sscanf( line, "0x%x 0x%x ISO8859-%d", &tsid, &onid, &encoding ) == 3 )
34                                         ||(sscanf( line, "%d %d ISO8859-%d", &tsid, &onid, &encoding ) == 3 ) )
35                                 m_TransponderDefaultMapping[(tsid<<16)|onid]=encoding;
36                         else if ( sscanf( line, "%s ISO8859-%d", countrycode, &encoding ) == 2 )
37                         {
38                                 m_CountryCodeDefaultMapping[countrycode]=encoding;
39                                 countrycode[0]=toupper(countrycode[0]);
40                                 countrycode[1]=toupper(countrycode[1]);
41                                 countrycode[2]=toupper(countrycode[2]);
42                                 m_CountryCodeDefaultMapping[countrycode]=encoding;
43                         }
44                         else if ( (sscanf( line, "0x%x 0x%x ISO%d", &tsid, &onid, &encoding ) == 3 && encoding == 6397 )
45                                         ||(sscanf( line, "%d %d ISO%d", &tsid, &onid, &encoding ) == 3 && encoding == 6397 ) )
46                                 m_TransponderDefaultMapping[(tsid<<16)|onid]=0;
47                         else if ( sscanf( line, "%s ISO%d", countrycode, &encoding ) == 2 && encoding == 6397 )
48                         {
49                                 m_CountryCodeDefaultMapping[countrycode]=0;
50                                 countrycode[0]=toupper(countrycode[0]);
51                                 countrycode[1]=toupper(countrycode[1]);
52                                 countrycode[2]=toupper(countrycode[2]);
53                                 m_CountryCodeDefaultMapping[countrycode]=0;
54                         }
55                         else if ( (sscanf( line, "0x%x 0x%x", &tsid, &onid ) == 2 )
56                                         ||(sscanf( line, "%d %d", &tsid, &onid ) == 2 ) )
57                                 m_TransponderUseTwoCharMapping.insert((tsid<<16)|onid);
58                         else
59                                 eDebug("couldn't parse %s", line);
60                 }
61                 fclose(f);
62                 free(line);
63         }
64         else
65                 eDebug("[eDVBTextEncodingHandler] couldn't open %s !", file.c_str());
66 }
67
68 void eDVBTextEncodingHandler::getTransponderDefaultMapping(int tsidonid, int &table)
69 {
70         std::map<int, int>::iterator it =
71                 m_TransponderDefaultMapping.find(tsidonid);
72         if ( it != m_TransponderDefaultMapping.end() )
73                 table = it->second;
74 }
75
76 bool eDVBTextEncodingHandler::getTransponderUseTwoCharMapping(int tsidonid)
77 {
78         return m_TransponderUseTwoCharMapping.find(tsidonid) != m_TransponderUseTwoCharMapping.end();
79 }
80
81 int eDVBTextEncodingHandler::getCountryCodeDefaultMapping( const std::string &country_code )
82 {
83         std::map<std::string, int>::iterator it =
84                 m_CountryCodeDefaultMapping.find(country_code);
85         if ( it != m_CountryCodeDefaultMapping.end() )
86                 return it->second;
87         return 1;  // ISO8859-1 / Latin1
88 }