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