#include "bufferparams.h"
#include "dispatchresult.h"
#include "debug.h"
+#include "encoding.h"
#include "funcrequest.h"
#include "gettext.h"
#include "LaTeXFeatures.h"
#include <boost/tokenizer.hpp>
-#include <fstream>
-#include <sstream>
-
namespace lyx {
using std::endl;
using std::getline;
using std::string;
-using std::ifstream;
using std::ostream;
using std::pair;
using std::vector;
// files. All it does is to look for lines starting
// in @ and not being @preamble and @string entries.
// It does NOT do any syntax checking!
- ifstream ifs(it->toFilesystemEncoding().c_str());
- string linebuf0;
+
+ // Officially bibtex does only support ASCII, but in practice
+ // you can use the encoding of the main document as long as
+ // some elements like keys and names are pure ASCII. Therefore
+ // we convert the file from the buffer encoding.
+ idocfstream ifs(it->toFilesystemEncoding().c_str(),
+ std::ios_base::in,
+ buffer.params().encoding().iconvName());
+ docstring linebuf0;
while (getline(ifs, linebuf0)) {
- string linebuf = trim(linebuf0);
+ docstring linebuf = trim(linebuf0);
if (linebuf.empty()) continue;
- if (prefixIs(linebuf, "@")) {
+ if (prefixIs(linebuf, from_ascii("@"))) {
linebuf = subst(linebuf, '{', '(');
- string tmp;
+ docstring tmp;
linebuf = split(linebuf, tmp, '(');
tmp = ascii_lowercase(tmp);
- if (!prefixIs(tmp, "@string")
- && !prefixIs(tmp, "@preamble")) {
+ if (!prefixIs(tmp, from_ascii("@string")) &&
+ !prefixIs(tmp, from_ascii("@preamble"))) {
linebuf = split(linebuf, tmp, ',');
tmp = ltrim(tmp, " \t");
if (!tmp.empty()) {
- keys.push_back(pair<string,string>(tmp,string()));
+ // to_ascii because bibtex keys may
+ // only consist of ASCII characters
+ keys.push_back(pair<string, string>(to_ascii(tmp), string()));
}
}
} else if (!keys.empty()) {
- keys.back().second += linebuf + "\n";
+ // FIXME UNICODE
+ keys.back().second += to_utf8(linebuf + '\n');
}
}
}
namespace {
-char const * utf8_codeset = "UTF-8";
-
// We use C IO throughout this file, because the facets might be used with
// lyxerr in the future.
}
-idocfstream::idocfstream() : base()
+idocfstream::idocfstream(string const & encoding) : base()
{
std::locale global;
- std::locale locale(global, new iconv_codecvt_facet(utf8_codeset, in));
+ std::locale locale(global, new iconv_codecvt_facet(encoding, in));
imbue(locale);
}
-idocfstream::idocfstream(const char* s, std::ios_base::openmode mode)
+idocfstream::idocfstream(const char* s, std::ios_base::openmode mode,
+ string const & encoding)
: base()
{
// We must imbue the stream before openening the file
std::locale global;
- std::locale locale(global, new iconv_codecvt_facet(utf8_codeset, in));
+ std::locale locale(global, new iconv_codecvt_facet(encoding, in));
imbue(locale);
open(s, mode);
}
class idocfstream : public std::basic_ifstream<char_type> {
typedef std::basic_ifstream<char_type> base;
public:
- idocfstream();
+ idocfstream(std::string const & encoding = "UTF-8");
explicit idocfstream(const char* s,
- std::ios_base::openmode mode = std::ios_base::in);
+ std::ios_base::openmode mode = std::ios_base::in,
+ std::string const & encoding = "UTF-8");
~idocfstream() {}
};
}
};
-struct local_ascii_lowercase {
- char operator()(char c) const {
+template<typename Char> struct local_ascii_lowercase {
+ Char operator()(Char c) const {
return ascii_tolower(c);
}
};
{
string tmp(a);
transform(tmp.begin(), tmp.end(), tmp.begin(),
- local_ascii_lowercase());
+ local_ascii_lowercase<char>());
+ return tmp;
+}
+
+
+docstring const ascii_lowercase(docstring const & a)
+{
+ docstring tmp(a);
+ transform(tmp.begin(), tmp.end(), tmp.begin(),
+ local_ascii_lowercase<char_type>());
return tmp;
}