case LFUN_FILE_INSERT_PLAINTEXT_PARA:
// FIXME UNICODE
- insertPlaintextFile(to_utf8(cmd.argument()), true);
+ insertPlaintextFile(FileName(to_utf8(cmd.argument())), true);
break;
case LFUN_FILE_INSERT_PLAINTEXT:
// FIXME UNICODE
- insertPlaintextFile(to_utf8(cmd.argument()), false);
+ insertPlaintextFile(FileName(to_utf8(cmd.argument())), false);
break;
case LFUN_FONT_STATE:
// FIXME: Move this out of BufferView again
-docstring BufferView::contentsOfPlaintextFile(string const & f,
+docstring BufferView::contentsOfPlaintextFile(FileName const & fname,
bool asParagraph)
{
- FileName fname(f);
-
if (fname.empty()) {
FileDialog dlg(_("Select file to insert"),
( asParagraph
if (result.second.empty())
return docstring();
- fname = makeAbsPath(to_utf8(result.second));
+ return contentsOfPlaintextFile(FileName(to_utf8(result.second)), false);
}
if (!fname.isReadableFile()) {
return docstring();
}
- ifstream ifs(fname.toFilesystemEncoding().c_str());
- if (!ifs) {
- docstring const error = from_ascii(strerror(errno));
+ if (!fname.isReadableFile()) {
docstring const file = makeDisplayPath(fname.absFilename(), 50);
docstring const text =
- bformat(_("Could not open the specified document\n"
- "%1$s\ndue to the error: %2$s"), file, error);
+ bformat(_("%1$s\n is not readable."), file);
Alert::error(_("Could not open file"), text);
return docstring();
}
- ifs.unsetf(std::ios::skipws);
- istream_iterator<char> ii(ifs);
- istream_iterator<char> end;
-#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
- // We use this until the compilers get better...
- std::vector<char> tmp;
- copy(ii, end, back_inserter(tmp));
- string const tmpstr(tmp.begin(), tmp.end());
-#else
- // This is what we want to use and what we will use once the
- // compilers get good enough.
- //string tmpstr(ii, end); // yet a reason for using std::string
- // alternate approach to get the file into a string:
- string tmpstr;
- copy(ii, end, back_inserter(tmpstr));
-#endif
-
// FIXME UNICODE: We don't know the encoding of the file
- docstring file_content = from_utf8(tmpstr);
+ docstring file_content = fname.fileContents("UTF-8");
if (file_content.empty()) {
Alert::error(_("Reading not UTF-8 encoded file"),
_("The file is not UTF-8 encoded.\n"
"If this does not give the correct result\n"
"then please change the encoding of the file\n"
"to UTF-8 with a program other than LyX.\n"));
- file_content = from_local8bit(tmpstr);
+ file_content = fname.fileContents("local8bit");
}
return normalize_c(file_content);
}
-void BufferView::insertPlaintextFile(string const & f, bool asParagraph)
+void BufferView::insertPlaintextFile(FileName const & f, bool asParagraph)
{
docstring const tmpstr = contentsOfPlaintextFile(f, asParagraph);
void setGuiDelegate(frontend::GuiBufferViewDelegate *);
///
- docstring contentsOfPlaintextFile(std::string const & f, bool asParagraph);
+ docstring contentsOfPlaintextFile(support::FileName const & f,
+ bool asParagraph);
// Insert plain text file (if filename is empty, prompt for one)
- void insertPlaintextFile(std::string const & fileName, bool asParagraph);
+ void insertPlaintextFile(support::FileName const & f, bool asParagraph);
private:
/// noncopyable
string filename2 = (loader_format == format) ? filename.absFilename()
: changeExtension(filename.absFilename(),
formats.extension(loader_format));
- lv->view()->insertPlaintextFile(filename2, as_paragraphs);
+ lv->view()->insertPlaintextFile(FileName(filename2), as_paragraphs);
theLyXFunc().setLyXView(lv);
lyx::dispatch(FuncRequest(LFUN_MARK_OFF));
}
{
setWindowTitle(toqstr(onlyFilename(filename_.absFilename())));
- std::string contents = filename_.fileContents();
+ docstring contents = filename_.fileContents("UTF-8");
if (contents.empty())
- contents = "Error -> Cannot load file!";
+ contents = _("Error -> Cannot load file!");
textTB->setPlainText(toqstr(contents));
}
lyxerr << "File `'" << lstfile << "' not found." << endl;
return string();
}
- string const allClasses = abslstfile.fileContents();
+ // FIXME UNICODE
+ string const allClasses = to_utf8(abslstfile.fileContents("UTF-8"));
int entries = 0;
string classfile = token(allClasses, '\n', entries);
int count = 0;
if (file.empty())
return;
- list = getVectorFromString(file.fileContents(), "\n");
+ // FIXME Unicode.
+ std::vector<docstring> doclist =
+ getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
// Normalise paths like /foo//bar ==> /foo/bar
boost::RegEx regex("/{2,}");
- std::vector<string>::iterator it = list.begin();
- std::vector<string>::iterator end = list.end();
+ std::vector<docstring>::iterator it = doclist.begin();
+ std::vector<docstring>::iterator end = doclist.end();
for (; it != end; ++it)
- *it = regex.Merge((*it), "/");
+ list.push_back(regex.Merge(to_utf8(*it), "/"));
// remove empty items and duplicates
list.erase(std::remove(list.begin(), list.end(), ""), list.end());
FileName const absfile(
support::makeAbsPath(file, masterBuffer->temppath()));
if (absfile.isReadableFile())
- contents = absfile.fileContents();
+ // FIXME UNICODE
+ contents = to_utf8(absfile.fileContents("UTF-8"));
size_t const pos = result.find("$$Contents(\"");
size_t const end = result.find("\")", pos);
std::pair<GraphicsCopyStatus, FileName> const
copyFileIfNeeded(FileName const & file_in, FileName const & file_out)
{
+ LYXERR(Debug::FILES, "Comparing " << file_in << " and " << file_out);
unsigned long const checksum_in = file_in.checksum();
unsigned long const checksum_out = file_out.checksum();
{
if (isVerbatim(params()) || isListings(params())) {
os << '[' << getScreenLabel(buffer) << '\n';
- // FIXME: We don't know the encoding of the file
- docstring const str =
- from_utf8(includedFilename(buffer, params()).fileContents());
- os << str;
+ // FIXME: We don't know the encoding of the file, default to UTF-8.
+ os << includedFilename(buffer, params()).fileContents("UTF-8");
os << "\n]";
return PLAINTEXT_NEWLINE + 1; // one char on a separate line
} else {
#include "support/convert.h"
#include "support/docstream.h"
+#include "support/FileName.h"
#include "support/lstrings.h"
#include "frontends/alert.h"
namespace lyx {
using support::prefixIs;
+using support::FileName;
using support::ltrim;
using support::rtrim;
using support::suffixIs;
case LFUN_FILE_INSERT_PLAINTEXT_PARA:
case LFUN_FILE_INSERT_PLAINTEXT: {
// FIXME UNICODE
- docstring const tmpstr = cur.bv().contentsOfPlaintextFile(to_utf8(cmd.argument()), false);
+ docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
+ FileName(to_utf8(cmd.argument())), false);
if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
cur.undispatched();
break;
}
-string FileName::fileContents() const
-{
- if (exists()) {
- string const encodedname = toFilesystemEncoding();
- ifstream ifs(encodedname.c_str());
- ostringstream ofs;
- if (ifs && ofs) {
- ofs << ifs.rdbuf();
- ifs.close();
- return ofs.str();
- }
+docstring FileName::fileContents(string const & encoding) const
+{
+ if (!isReadableFile()) {
+ LYXERR0("File '" << *this
+ << "' is not redable!");
+ return docstring();
}
- lyxerr << "LyX was not able to read file '" << *this << '\'' << std::endl;
- return string();
+
+ QFile file(d->fi.absoluteFilePath());
+ if (!file.open(QIODevice::ReadOnly)) {
+ LYXERR0("File '" << *this
+ << "' could not be opened in read only mode!");
+ return docstring();
+ }
+ QByteArray contents = file.readAll();
+ file.close();
+
+ if (contents.isEmpty()) {
+ LYXERR(Debug::FILES, "File '" << *this
+ << "' is either empty or some error happened while reading it.");
+ return docstring();
+ }
+
+ QString s;
+ if (encoding.empty() || encoding == "UTF-8")
+ s = QString::fromUtf8(contents.data());
+ else if (encoding == "ascii")
+ s = QString::fromAscii(contents.data());
+ else if (encoding == "local8bit")
+ s = QString::fromLocal8Bit(contents.data());
+ else if (encoding == "latin1")
+ s = QString::fromLatin1(contents.data());
+
+ return qstring_to_ucs4(s);
}
/// Creates directory. Returns true on success
bool createDirectory(int permissions) const;
- /// Get the contents of a file as a huge std::string
- std::string fileContents() const;
- /**
- * Get a FileName from \p name in the encoding used by the file system.
- * Only use this for filenames you got directly from the file system,
- * e.g. from reading a directory.
- * \p name must have an absolute path.
- */
+ /// Get the contents of a file as a huge docstring.
+ /// \param encoding defines the encoding of the file contents.
+ /// Only four encodings are supported:
+ /// "UTF-8", "ascii", "latin1" and "local8bit" which uses the
+ /// current system locale.
+ docstring fileContents(std::string const & encoding) const;
/// Change extension.
/**