]> git.lyx.org Git - lyx.git/blob - src/chset.C
don't copy if a reference is fine
[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         if (fname.empty() || fname == "ascii") 
28                 return true;    // ascii 7-bit
29         
30         // open definition file
31         lyxerr[Debug::KBMAP]
32                 << "Reading character set file " << fname << ".cdef" << endl;
33         string const filename = LibFileSearch("kbd", fname, "cdef");
34         ifstream ifs(filename.c_str());
35         if (!ifs) {
36                 lyxerr << "Unable to open character set file" << endl;
37                 return true;            // no definition, use 7-bit ascii
38         }
39         name_ = fname;
40         
41         string line;
42         // Ok, I'll be the first to admit that this is probably not
43         // the fastest way to parse the cdef files, but I though it
44         // was a bit neat. Anyway it is wrong to use the lyxlex parse
45         // without the use of a keyword table.     
46         LRegex reg("^([12][0-9][0-9])[ \t]+\"([^ ]+)\".*");
47         while (getline(ifs, line)) {
48                 if (reg.exact_match(line)) {
49                         LRegex::SubMatches const & sub = reg.exec(line);
50                         int const n = lyx::atoi(line.substr(sub[1].first,
51                                                   sub[1].second));
52                         string const str = LSubstring(line, sub[2].first,
53                                                       sub[2].second);
54                         if (lyxerr.debugging(Debug::KBMAP))
55                                 lyxerr << "Chardef: " << n
56                                        << " to [" << str << "]" << endl;
57                         map_[str] = n;
58                 }
59         }
60         lyxerr[Debug::KBMAP] << "End of parsing of .cdef file." << endl;
61         return false;
62 }
63
64
65 pair<bool, int> const CharacterSet::encodeString(string const & str) const
66 {
67         lyxerr[Debug::KBMAP] << "Checking if we know [" << str << "]" << endl;
68         bool ret = false;
69         int val = 0;
70         Cdef::const_iterator cit = map_.find(str);
71         if (cit != map_.end()) {
72                 ret =  true;
73                 val = cit->second;
74         }
75         lyxerr[Debug::KBMAP] << "   "
76                              << (ret ? "yes we" : "no we don't")
77                              <<  " know [" << str << "]" << endl;
78         return make_pair(ret, val);
79 }
80
81
82 string const & CharacterSet::getName() const
83 {
84         return name_;
85 }