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