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