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