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