This commit was manufactured by cvs2svn to create tag
[lyx.git] / src / chset.C
blob3cb66481060eec90f999e2aac499ce91930abd0b
1 #include <config.h>
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
7 #include "chset.h"
8 #include "support/filetools.h"
9 #include "lyxlex.h"
10 #include "debug.h"
13 CharacterSet::CharacterSet()
15         map_=0;
19 CharacterSet::~CharacterSet()
21         freeMap();
25 void CharacterSet::freeMap()
27         Cdef* t;
28         while(map_) {
29                 t=map_;
30                 map_=map_->next;
31                 delete t;
32         }
34         name_.clear();
38 bool CharacterSet::loadFile(const string& fname)
40         freeMap();
41     
42         if (fname.empty() || fname=="ascii") 
43                 return true;    // ascii 7-bit
45         // open definition file
46         lyxerr[Debug::KBMAP]
47                 << "Opening keymap file " << fname << ".cdef" << endl;
48         string filename = LibFileSearch("kbd", fname.c_str(), "cdef");
49         FilePtr f(filename, FilePtr::read);
50         if (filename.empty() || !f()) {
51                 lyxerr << "Unable to open keymap file" << endl;
52                 return true;            // no definition, use 7-bit ascii
53         }
55         name_=fname;
57         // now read the file
58         LyXLex lex(0,0);
59         lex.setFile(f());
61         bool error=false;
62         string str;
63         int n;
64         
65         while(lex.IsOK() && !error) {
66                 
67                 switch(lex.lex()){
68                 case LyXLex::LEX_FEOF :
69                         lyxerr[Debug::KBMAP] << "End of parsing of .cdef file"
70                                              << endl;
71                         break;
72                 default:
73                         // Get Integer
74                         n=lex.GetInteger();
75                         if (n<0) {
76                                 error=true;
77                                 continue;
78                         }
79         
80                         // Get String
81                         lex.next(true);
82                         str=lex.GetString();
84                         Cdef* tempc=new Cdef;
85                         tempc->str=str;
86                         tempc->ic=n;
87                         tempc->next=map_;
88                         map_=tempc;
89         
90                         if (lyxerr.debugging(Debug::KBMAP))
91                                 lyxerr << "Chardef: " << n
92                                        << " to [" << tempc->str << "]" << endl;
93                         break;
94                 }
95         }
96         
97         return false;
101 bool CharacterSet::encodeString(string & str)
103         Cdef *t=map_;
104     
105         while(t) {
106                 if (t->str==str) {
107                         // Can this fail? Why is ic an unsigned char?
108                         str = char(t->ic);
109                         break;
110                 }
111                 t=t->next;
112         }
113         return (t!=0);
117 string CharacterSet::getName()
119         return name_;