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