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