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