]> git.lyx.org Git - lyx.git/blob - src/chset.C
Initial revision
[lyx.git] / src / chset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "chset.h"
8 #include "filetools.h"
9 #include "lyxlex.h"
10 #include "error.h"
11
12
13 CharacterSet::CharacterSet()
14 {
15         map_=NULL;
16 }
17
18
19 CharacterSet::~CharacterSet()
20 {
21         freeMap();
22 }
23
24
25 void CharacterSet::freeMap()
26 {
27         Cdef* t;
28         while(map_) {
29                 t=map_;
30                 map_=map_->next;
31                 delete t;
32         }
33
34         name_.clean();
35 }
36
37
38 bool CharacterSet::loadFile(const LString& fname)
39 {
40         freeMap();
41     
42         if (fname.empty() || fname=="ascii") 
43                 return true;    // ascii 7-bit
44
45         // open definition file
46         lyxerr.debug("Opening keymap file "+ fname+ ".cdef",Error::KBMAP);
47         LString filename = LibFileSearch("kbd", fname.c_str(), "cdef");
48         FilePtr f(filename, FilePtr::read);
49         if (filename.empty() || !f()) {
50                 lyxerr.print("Unable to open keymap file");
51                 return true;            // no definition, use 7-bit ascii
52         }
53
54         name_=fname;
55
56         // now read the file
57         LyXLex lex(NULL,0);
58         lex.setFile(f());
59
60         bool error=false;
61         LString str;
62         int n;
63         
64         while(lex.IsOK() && !error) {
65                 
66                 switch(lex.lex()){
67                 case LyXLex::LEX_FEOF :
68                         lyxerr.debug("End of parsing of .cdef file",
69                                      Error::KBMAP);
70                         break;
71                 default:
72                         // Get Integer
73                         n=lex.GetInteger();
74                         if (n<0) {
75                                 error=true;
76                                 continue;
77                         }
78         
79                         // Get String
80                         lex.next(true);
81                         str=lex.GetString();
82
83                         Cdef* tempc=new Cdef;
84                         tempc->str=str;
85                         tempc->ic=n;
86                         tempc->next=map_;
87                         map_=tempc;
88         
89                         if (lyxerr.debugging(Error::KBMAP))
90                                 fprintf(stderr, "Chardef: %d to [%s]\n", 
91                                         n, tempc->str.c_str());
92                         break;
93                 }
94         }
95         
96         return false;
97 }
98
99
100 bool CharacterSet::encodeString(LString& str)
101 {
102         Cdef *t=map_;
103     
104         while(t) {
105                 if (t->str==str) {
106                         str=LString(t->ic);
107                         break;
108                 }
109                 t=t->next;
110         }
111         return (t!=NULL);
112 }
113
114
115 LString CharacterSet::getName()
116 {
117         return name_;
118 }