]> git.lyx.org Git - lyx.git/blob - src/chset.C
const fixes and alway generate/run latex on view/update
[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 "debug.h"
14
15 using std::ifstream;
16 using std::getline;
17 using std::pair;
18 using std::make_pair;
19 using std::endl;
20
21 bool CharacterSet::loadFile(string const & fname)
22 {
23         map_.clear();
24         name_.erase();
25         
26         if (fname.empty() || fname == "ascii") 
27                 return true;    // ascii 7-bit
28         
29         // open definition file
30         lyxerr[Debug::KBMAP]
31                 << "Reading character set file " << fname << ".cdef" << endl;
32         string filename = LibFileSearch("kbd", fname.c_str(), "cdef");
33         ifstream ifs(filename.c_str());
34         if (!ifs) {
35                 lyxerr << "Unable to open character set file" << endl;
36                 return true;            // no definition, use 7-bit ascii
37         }
38         name_ = fname;
39         
40         string str;
41         int n;
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                         n = atoi(line.substr(sub[1].first,
52                                              sub[1].second).c_str());
53                         str = LSubstring(line, sub[2].first, 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> 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 }