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