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