experimental PVR commit support
[enigma2.git] / lib / base / encoding.cpp
1 #include <lib/base/encoding.h>
2 #include <lib/base/eerror.h>
3 #include <config.h>
4
5 eDVBTextEncodingHandler encodingHandler;  // the one and only instance
6
7 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, "%s ISO8859-%d", countrycode, &encoding ) == 2 )
32                         {
33                                 m_CountryCodeDefaultMapping[countrycode]=encoding;
34                                 countrycode[0]=toupper(countrycode[0]);
35                                 countrycode[1]=toupper(countrycode[1]);
36                                 countrycode[2]=toupper(countrycode[2]);
37                                 m_CountryCodeDefaultMapping[countrycode]=encoding;
38                         }
39                         else if ( (sscanf( line, "0x%x 0x%x ISO8859-%d", &tsid, &onid, &encoding ) == 3 )
40                                         ||(sscanf( line, "%d %d ISO8859-%d", &tsid, &onid, &encoding ) == 3 ) )
41                                 m_TransponderDefaultMapping[(tsid<<16)|onid]=encoding;
42                         else if ( (sscanf( line, "0x%x 0x%x", &tsid, &onid ) == 2 )
43                                         ||(sscanf( line, "%d %d", &tsid, &onid ) == 2 ) )
44                                 m_TransponderUseTwoCharMapping.insert((tsid<<16)|onid);
45                         else
46                                 eDebug("couldn't parse %s", line);
47                 }
48                 fclose(f);
49                 free(line);
50         }
51         else
52                 eDebug("[eDVBTextEncodingHandler] couldn't open %s !", file);
53 }
54
55 void eDVBTextEncodingHandler::getTransponderDefaultMapping(int tsidonid, int &table)
56 {
57         std::map<int, int>::iterator it =
58                 m_TransponderDefaultMapping.find(tsidonid);
59         if ( it != m_TransponderDefaultMapping.end() )
60                 table = it->second;
61 }
62
63 bool eDVBTextEncodingHandler::getTransponderUseTwoCharMapping(int tsidonid)
64 {
65         return m_TransponderUseTwoCharMapping.find(tsidonid) != m_TransponderUseTwoCharMapping.end();
66 }
67
68 int eDVBTextEncodingHandler::getCountryCodeDefaultMapping( const std::string &country_code )
69 {
70         std::map<std::string, int>::iterator it =
71                 m_CountryCodeDefaultMapping.find(country_code);
72         if ( it != m_CountryCodeDefaultMapping.end() )
73                 return it->second;
74         return 0;  // ISO8859-1 / Latin1
75 }