]> git.lyx.org Git - lyx.git/blobdiff - src/chset.C
fix typo that put too many include paths for most people
[lyx.git] / src / chset.C
index 3cb66481060eec90f999e2aac499ce91930abd0b..2815485bf16d60108bb78784eee91429a96496fd 100644 (file)
 #pragma implementation
 #endif
 
+#include <fstream>
+
 #include "chset.h"
 #include "support/filetools.h"
-#include "lyxlex.h"
+#include "support/LRegex.h"
+#include "support/LSubstring.h"
+#include "support/lyxlib.h"
 #include "debug.h"
 
+using std::ifstream;
+using std::getline;
+using std::pair;
+using std::make_pair;
+using std::endl;
 
-CharacterSet::CharacterSet()
-{
-       map_=0;
-}
-
-
-CharacterSet::~CharacterSet()
+bool CharacterSet::loadFile(string const & fname)
 {
-       freeMap();
-}
+       map_.clear();
+       name_.erase();
 
-
-void CharacterSet::freeMap()
-{
-       Cdef* t;
-       while(map_) {
-               t=map_;
-               map_=map_->next;
-               delete t;
-       }
-
-       name_.clear();
-}
-
-
-bool CharacterSet::loadFile(const string& fname)
-{
-       freeMap();
-    
-       if (fname.empty() || fname=="ascii") 
-               return true;    // ascii 7-bit
+       // ascii 7-bit
+       if (fname.empty() || fname == "ascii")
+               return true;
 
        // open definition file
        lyxerr[Debug::KBMAP]
-               << "Opening keymap file " << fname << ".cdef" << endl;
-       string filename = LibFileSearch("kbd", fname.c_str(), "cdef");
-       FilePtr f(filename, FilePtr::read);
-       if (filename.empty() || !f()) {
-               lyxerr << "Unable to open keymap file" << endl;
+               << "Reading character set file " << fname << ".cdef" << endl;
+       string const filename = LibFileSearch("kbd", fname, "cdef");
+       ifstream ifs(filename.c_str());
+       if (!ifs) {
+               lyxerr << "Unable to open character set file" << endl;
                return true;            // no definition, use 7-bit ascii
        }
-
-       name_=fname;
-
-       // now read the file
-       LyXLex lex(0,0);
-       lex.setFile(f());
-
-       bool error=false;
-       string str;
-       int n;
-       
-       while(lex.IsOK() && !error) {
-               
-               switch(lex.lex()){
-               case LyXLex::LEX_FEOF :
-                       lyxerr[Debug::KBMAP] << "End of parsing of .cdef file"
-                                            << endl;
-                       break;
-               default:
-                       // Get Integer
-                       n=lex.GetInteger();
-                       if (n<0) {
-                               error=true;
-                               continue;
-                       }
-       
-                       // Get String
-                       lex.next(true);
-                       str=lex.GetString();
-
-                       Cdef* tempc=new Cdef;
-                       tempc->str=str;
-                       tempc->ic=n;
-                       tempc->next=map_;
-                       map_=tempc;
-       
+       name_ = fname;
+
+       string line;
+       // Ok, I'll be the first to admit that this is probably not
+       // the fastest way to parse the cdef files, but I though it
+       // was a bit neat. Anyway it is wrong to use the lyxlex parse
+       // without the use of a keyword table.
+       LRegex reg("^([12][0-9][0-9])[ \t]+\"([^ ]+)\".*");
+       while (getline(ifs, line)) {
+               if (reg.exact_match(line)) {
+                       LRegex::SubMatches const & sub = reg.exec(line);
+                       int const n = lyx::atoi(line.substr(sub[1].first,
+                                                 sub[1].second));
+                       string const str = LSubstring(line, sub[2].first,
+                                                     sub[2].second);
                        if (lyxerr.debugging(Debug::KBMAP))
                                lyxerr << "Chardef: " << n
-                                      << " to [" << tempc->str << "]" << endl;
-                       break;
+                                      << " to [" << str << "]" << endl;
+                       map_[str] = n;
                }
        }
-       
+       lyxerr[Debug::KBMAP] << "End of parsing of .cdef file." << endl;
        return false;
 }
 
 
-bool CharacterSet::encodeString(string & str)
+pair<bool, int> const CharacterSet::encodeString(string const & str) const
 {
-       Cdef *t=map_;
-    
-       while(t) {
-               if (t->str==str) {
-                       // Can this fail? Why is ic an unsigned char?
-                       str = char(t->ic);
-                       break;
-               }
-               t=t->next;
+       lyxerr[Debug::KBMAP] << "Checking if we know [" << str << "]" << endl;
+       bool ret = false;
+       int val = 0;
+       Cdef::const_iterator cit = map_.find(str);
+       if (cit != map_.end()) {
+               ret =  true;
+               val = cit->second;
        }
-       return (t!=0);
+       lyxerr[Debug::KBMAP] << "   "
+                            << (ret ? "yes we" : "no we don't")
+                            <<  " know [" << str << "]" << endl;
+       return make_pair(ret, val);
 }
 
 
-string CharacterSet::getName()
+string const & CharacterSet::getName() const
 {
        return name_;
 }