-#include <config.h>
+/**
+ * \file chset.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Lars Gullik Bjønnes
+ * \author Jean-Marc Lasgouttes
+ *
+ * Full author contact details are available in file CREDITS.
+ */
-#ifdef __GNUG__
-#pragma implementation
-#endif
+#include <config.h>
#include "chset.h"
-#include "support/filetools.h"
-#include "lyxlex.h"
+
#include "debug.h"
+#include "support/convert.h"
+#include "support/filetools.h"
-CharacterSet::CharacterSet()
-{
- map_=0;
-}
+#include <boost/regex.hpp>
+#include <fstream>
-CharacterSet::~CharacterSet()
-{
- freeMap();
-}
+using lyx::support::libFileSearch;
+using boost::regex;
+using boost::smatch;
-void CharacterSet::freeMap()
-{
- Cdef* t;
- while(map_) {
- t=map_;
- map_=map_->next;
- delete t;
- }
+using std::endl;
+using std::getline;
+using std::make_pair;
- name_.clear();
-}
+using std::ifstream;
+using std::pair;
+using std::string;
-bool CharacterSet::loadFile(const string& fname)
+bool CharacterSet::loadFile(string const & fname)
{
- freeMap();
-
- if (fname.empty() || fname=="ascii")
- return true; // ascii 7-bit
+ map_.clear();
+ name_.erase();
+
+ // 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.
+ regex reg("^([12][0-9][0-9])[ \t]+\"([^ ]+)\".*");
+ while (getline(ifs, line)) {
+ smatch sub;
+ if (regex_match(line, sub, reg)) {
+ int const n = convert<int>(sub.str(1));
+ string const str = sub.str(2);
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_;
}