]> git.lyx.org Git - lyx.git/blob - src/chset.C
d83a7451162d079f40cebb0df99d1b734ca5daed
[lyx.git] / src / chset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include <fstream>
8 using std::ifstream;
9
10 #include "chset.h"
11 #include "support/filetools.h"
12 #include "support/LRegex.h"
13 #include "support/LSubstring.h"
14 #include "debug.h"
15
16 bool CharacterSet::loadFile(string const & fname)
17 {
18         map_.clear();
19         name_.clear();
20         
21         if (fname.empty() || fname == "ascii") 
22                 return true;    // ascii 7-bit
23         
24         // open definition file
25         lyxerr[Debug::KBMAP]
26                 << "Reading keymap file " << fname << ".cdef" << endl;
27         string filename = LibFileSearch("kbd", fname.c_str(), "cdef");
28         ifstream ifs(filename.c_str());
29         if (!ifs) {
30                 lyxerr << "Unable to open keymap file" << endl;
31                 return true;            // no definition, use 7-bit ascii
32         }
33         name_ = fname;
34         
35         string str;
36         int n;
37         string line;
38         LRegex reg("^([12][0-9][0-9])[ \t]+\"([^ ]+)\".*");
39         while(getline(ifs, line)) {
40                 if (reg.exact_match(line)) {
41                         LRegex::SubMatches const & sub = reg.exec(line);
42                         n = atoi(line.substr(sub[1].first,
43                                              sub[1].second).c_str());
44                         str = LSubstring(line, sub[2].first, sub[2].second);
45                         if (lyxerr.debugging(Debug::KBMAP))
46                                 lyxerr << "Chardef: " << n
47                                        << " to [" << str << "]" << endl;
48                         map_[str] = n;
49                 }
50         }
51         lyxerr[Debug::KBMAP] << "End of parsing of .cdef file." << endl;
52         return false;
53 }
54
55
56 bool CharacterSet::encodeString(string & str) const
57 {
58         Cdef::const_iterator cit = map_.find(str);
59         if (cit != map_.end()) return true;
60         return false;
61 }
62
63
64 string const & CharacterSet::getName() const
65 {
66         return name_;
67 }