]> git.lyx.org Git - lyx.git/blobdiff - src/chset.C
fix to #241 and #300 from John
[lyx.git] / src / chset.C
index 3c9464d19dc00c4624e52da7a4fabde29f3e46eb..2815485bf16d60108bb78784eee91429a96496fd 100644 (file)
 #pragma implementation
 #endif
 
-#include "chset.h"
-#include "filetools.h"
-#include "lyxlex.h"
-#include "error.h"
-
-
-CharacterSet::CharacterSet()
-{
-       map_=NULL;
-}
-
+#include <fstream>
 
-CharacterSet::~CharacterSet()
+#include "chset.h"
+#include "support/filetools.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;
+
+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_.clean();
-}
-
-
-bool CharacterSet::loadFile(const LString& 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("Opening keymap file "+ fname+ ".cdef",Error::KBMAP);
-       LString filename = LibFileSearch("kbd", fname.c_str(), "cdef");
-       FilePtr f(filename, FilePtr::read);
-       if (filename.empty() || !f()) {
-               lyxerr.print("Unable to open keymap file");
+       lyxerr[Debug::KBMAP]
+               << "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(NULL,0);
-       lex.setFile(f());
-
-       bool error=false;
-       LString str;
-       int n;
-       
-       while(lex.IsOK() && !error) {
-               
-               switch(lex.lex()){
-               case LyXLex::LEX_FEOF :
-                       lyxerr.debug("End of parsing of .cdef file",
-                                    Error::KBMAP);
-                       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;
-       
-                       if (lyxerr.debugging(Error::KBMAP))
-                               fprintf(stderr, "Chardef: %d to [%s]\n", 
-                                       n, tempc->str.c_str());
-                       break;
+       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 [" << str << "]" << endl;
+                       map_[str] = n;
                }
        }
-       
+       lyxerr[Debug::KBMAP] << "End of parsing of .cdef file." << endl;
        return false;
 }
 
 
-bool CharacterSet::encodeString(LString& str)
+pair<bool, int> const CharacterSet::encodeString(string const & str) const
 {
-       Cdef *t=map_;
-    
-       while(t) {
-               if (t->str==str) {
-                       str=LString(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!=NULL);
+       lyxerr[Debug::KBMAP] << "   "
+                            << (ret ? "yes we" : "no we don't")
+                            <<  " know [" << str << "]" << endl;
+       return make_pair(ret, val);
 }
 
 
-LString CharacterSet::getName()
+string const & CharacterSet::getName() const
 {
        return name_;
 }