X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fchset.C;h=ed8ee462b502203144b737d0757b80bbdab36333;hb=c4320d24cd2d29c2e77958b4a8fd44f2bd587ca7;hp=0cd8c56382c856ae7f00e76eb3985e174335f91d;hpb=35584afc1162dec2cf9fff79305e95cb3b75aefb;p=lyx.git diff --git a/src/chset.C b/src/chset.C index 0cd8c56382..ed8ee462b5 100644 --- a/src/chset.C +++ b/src/chset.C @@ -1,121 +1,107 @@ -#include +/** + * \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 #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 +#include -CharacterSet::~CharacterSet() -{ - freeMap(); -} +namespace lyx { -void CharacterSet::freeMap() -{ - Cdef* t; - while(map_) { - t=map_; - map_=map_->next; - delete t; - } +using support::libFileSearch; - name_.erase(); -} +using boost::regex; +using boost::smatch; + +using std::endl; +using std::getline; +using std::make_pair; +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; + support::FileName const filename = libFileSearch("kbd", fname, "cdef"); + ifstream ifs(filename.toFilesystemEncoding().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(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 const CharacterSet::encodeString(string const & str) const { - Cdef *t=map_; - - while(t) { - if (t->str==str) { - str.erase(); - str += t->ic; - //str = tostr(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_; } + + +} // namespace lyx