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