]> git.lyx.org Git - lyx.git/blob - src/chset.C
965616d811d33f0d71811f8902a461e6ca338e6d
[lyx.git] / src / chset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include <fstream>
8 using std::ifstream;
9
10 #include "chset.h"
11 #include "support/filetools.h"
12 #include "support/LRegex.h"
13 #include "support/LSubstring.h"
14 #include "debug.h"
15
16 using std::make_pair;
17
18 bool CharacterSet::loadFile(string const & fname)
19 {
20         map_.clear();
21         name_.clear();
22         
23         if (fname.empty() || fname == "ascii") 
24                 return true;    // ascii 7-bit
25         
26         // open definition file
27         lyxerr[Debug::KBMAP]
28                 << "Reading character set file " << fname << ".cdef" << endl;
29         string filename = LibFileSearch("kbd", fname.c_str(), "cdef");
30         ifstream ifs(filename.c_str());
31         if (!ifs) {
32                 lyxerr << "Unable to open character set file" << endl;
33                 return true;            // no definition, use 7-bit ascii
34         }
35         name_ = fname;
36         
37         string str;
38         int n;
39         string line;
40         // Ok, I'll be the first to admit that this is probably not
41         // the fastest way to parse the cdef files, but I though it
42         // was a bit neat. Anyway it is wrong to use the lyxlex parse
43         // without the use of a keyword table.     
44         LRegex reg("^([12][0-9][0-9])[ \t]+\"([^ ]+)\".*");
45         while(getline(ifs, line)) {
46                 if (reg.exact_match(line)) {
47                         LRegex::SubMatches const & sub = reg.exec(line);
48                         n = atoi(line.substr(sub[1].first,
49                                              sub[1].second).c_str());
50                         str = LSubstring(line, sub[2].first, sub[2].second);
51                         if (lyxerr.debugging(Debug::KBMAP))
52                                 lyxerr << "Chardef: " << n
53                                        << " to [" << str << "]" << endl;
54                         map_[str] = n;
55                 }
56         }
57         lyxerr[Debug::KBMAP] << "End of parsing of .cdef file." << endl;
58         return false;
59 }
60
61
62 pair<bool, int> CharacterSet::encodeString(string & str) const
63 {
64         lyxerr[Debug::KBMAP] << "Checking if we know [" << str << "]" << endl;
65         bool ret = false;
66         int val = 0;
67         Cdef::const_iterator cit = map_.find(str);
68         if (cit != map_.end()) {
69                 ret =  true;
70                 val = (*cit).second;
71         }
72         lyxerr[Debug::KBMAP] << "   "
73                              << (ret ? "yes we" : "no we don't")
74                              <<  " know [" << str << "]" << endl;
75         return make_pair(ret, val);
76 }
77
78
79 string const & CharacterSet::getName() const
80 {
81         return name_;
82 }