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