]> git.lyx.org Git - lyx.git/blob - src/chset.C
cleanups from John and Juergen, bib files parsing fix from Herbert
[lyx.git] / src / chset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include <fstream>
8
9 #include "chset.h"
10 #include "support/filetools.h"
11 #include "support/LRegex.h"
12 #include "support/LSubstring.h"
13 #include "support/lyxlib.h"
14 #include "debug.h"
15
16 using std::ifstream;
17 using std::getline;
18 using std::pair;
19 using std::make_pair;
20 using std::endl;
21
22 bool CharacterSet::loadFile(string const & fname)
23 {
24         map_.clear();
25         name_.erase();
26  
27         // ascii 7-bit
28         if (fname.empty() || fname == "ascii")
29                 return true;
30         
31         // open definition file
32         lyxerr[Debug::KBMAP]
33                 << "Reading character set file " << fname << ".cdef" << endl;
34         string const filename = LibFileSearch("kbd", fname, "cdef");
35         ifstream ifs(filename.c_str());
36         if (!ifs) {
37                 lyxerr << "Unable to open character set file" << endl;
38                 return true;            // no definition, use 7-bit ascii
39         }
40         name_ = fname;
41         
42         string line;
43         // Ok, I'll be the first to admit that this is probably not
44         // the fastest way to parse the cdef files, but I though it
45         // was a bit neat. Anyway it is wrong to use the lyxlex parse
46         // without the use of a keyword table.     
47         LRegex reg("^([12][0-9][0-9])[ \t]+\"([^ ]+)\".*");
48         while (getline(ifs, line)) {
49                 if (reg.exact_match(line)) {
50                         LRegex::SubMatches const & sub = reg.exec(line);
51                         int const n = lyx::atoi(line.substr(sub[1].first,
52                                                   sub[1].second));
53                         string const str = LSubstring(line, sub[2].first,
54                                                       sub[2].second);
55                         if (lyxerr.debugging(Debug::KBMAP))
56                                 lyxerr << "Chardef: " << n
57                                        << " to [" << str << "]" << endl;
58                         map_[str] = n;
59                 }
60         }
61         lyxerr[Debug::KBMAP] << "End of parsing of .cdef file." << endl;
62         return false;
63 }
64
65
66 pair<bool, int> const CharacterSet::encodeString(string const & str) const
67 {
68         lyxerr[Debug::KBMAP] << "Checking if we know [" << str << "]" << endl;
69         bool ret = false;
70         int val = 0;
71         Cdef::const_iterator cit = map_.find(str);
72         if (cit != map_.end()) {
73                 ret =  true;
74                 val = cit->second;
75         }
76         lyxerr[Debug::KBMAP] << "   "
77                              << (ret ? "yes we" : "no we don't")
78                              <<  " know [" << str << "]" << endl;
79         return make_pair(ret, val);
80 }
81
82
83 string const & CharacterSet::getName() const
84 {
85         return name_;
86 }