+ // Set a sensible default encoding.
+ // This is used until an encoding command is found.
+ // For child documents use the encoding of the master, else try to
+ // detect it from the preamble, since setting an encoding of an open
+ // fstream does currently not work on OS X.
+ // Always start with ISO-8859-1, (formerly known by its latex name
+ // latin1), since ISO-8859-1 does not cause an iconv error if the
+ // actual encoding is different (bug 7509).
+ if (encoding.empty()) {
+ Encoding const * enc = 0;
+ if (preamble.inputencoding() == "auto-legacy") {
+ ifdocstream is(setEncoding("ISO-8859-1"));
+ // forbid buffering on this stream
+ is.rdbuf()->pubsetbuf(0, 0);
+ is.open(infilename.toFilesystemEncoding().c_str());
+ if (is.good()) {
+ Parser ep(is, string());
+ ep.setEncoding("ISO-8859-1");
+ Preamble encodingpreamble;
+ string const e = encodingpreamble
+ .parseEncoding(ep, documentclass);
+ if (!e.empty())
+ enc = encodings.fromLyXName(e, true);
+ }
+ } else
+ enc = encodings.fromLyXName(
+ preamble.inputencoding(), true);
+ if (enc)
+ encoding = enc->iconvName();
+ else
+ encoding = "ISO-8859-1";
+ // store
+ preamble.docencoding = encoding;
+ }
+
+ ifdocstream is(setEncoding(encoding));