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