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