mirror of
https://git.lyx.org/repos/lyx.git
synced 2025-01-03 08:28:25 +00:00
* FileName::fileContents(): new argument to define the file encoding.
* BufferView: - insertPlaintextString(): now accept a FileName. - contentsOfPlaintextFile(): ditto and use FileName::fileContents(). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21915 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
parent
fecbd33fb8
commit
b1db731a98
@ -901,12 +901,12 @@ Update::flags BufferView::dispatch(FuncRequest const & cmd)
|
|||||||
|
|
||||||
case LFUN_FILE_INSERT_PLAINTEXT_PARA:
|
case LFUN_FILE_INSERT_PLAINTEXT_PARA:
|
||||||
// FIXME UNICODE
|
// FIXME UNICODE
|
||||||
insertPlaintextFile(to_utf8(cmd.argument()), true);
|
insertPlaintextFile(FileName(to_utf8(cmd.argument())), true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LFUN_FILE_INSERT_PLAINTEXT:
|
case LFUN_FILE_INSERT_PLAINTEXT:
|
||||||
// FIXME UNICODE
|
// FIXME UNICODE
|
||||||
insertPlaintextFile(to_utf8(cmd.argument()), false);
|
insertPlaintextFile(FileName(to_utf8(cmd.argument())), false);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LFUN_FONT_STATE:
|
case LFUN_FONT_STATE:
|
||||||
@ -1995,11 +1995,9 @@ void BufferView::setGuiDelegate(frontend::GuiBufferViewDelegate * gui)
|
|||||||
|
|
||||||
|
|
||||||
// FIXME: Move this out of BufferView again
|
// FIXME: Move this out of BufferView again
|
||||||
docstring BufferView::contentsOfPlaintextFile(string const & f,
|
docstring BufferView::contentsOfPlaintextFile(FileName const & fname,
|
||||||
bool asParagraph)
|
bool asParagraph)
|
||||||
{
|
{
|
||||||
FileName fname(f);
|
|
||||||
|
|
||||||
if (fname.empty()) {
|
if (fname.empty()) {
|
||||||
FileDialog dlg(_("Select file to insert"),
|
FileDialog dlg(_("Select file to insert"),
|
||||||
( asParagraph
|
( asParagraph
|
||||||
@ -2016,7 +2014,7 @@ docstring BufferView::contentsOfPlaintextFile(string const & f,
|
|||||||
if (result.second.empty())
|
if (result.second.empty())
|
||||||
return docstring();
|
return docstring();
|
||||||
|
|
||||||
fname = makeAbsPath(to_utf8(result.second));
|
return contentsOfPlaintextFile(FileName(to_utf8(result.second)), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fname.isReadableFile()) {
|
if (!fname.isReadableFile()) {
|
||||||
@ -2029,36 +2027,16 @@ docstring BufferView::contentsOfPlaintextFile(string const & f,
|
|||||||
return docstring();
|
return docstring();
|
||||||
}
|
}
|
||||||
|
|
||||||
ifstream ifs(fname.toFilesystemEncoding().c_str());
|
if (!fname.isReadableFile()) {
|
||||||
if (!ifs) {
|
|
||||||
docstring const error = from_ascii(strerror(errno));
|
|
||||||
docstring const file = makeDisplayPath(fname.absFilename(), 50);
|
docstring const file = makeDisplayPath(fname.absFilename(), 50);
|
||||||
docstring const text =
|
docstring const text =
|
||||||
bformat(_("Could not open the specified document\n"
|
bformat(_("%1$s\n is not readable."), file);
|
||||||
"%1$s\ndue to the error: %2$s"), file, error);
|
|
||||||
Alert::error(_("Could not open file"), text);
|
Alert::error(_("Could not open file"), text);
|
||||||
return docstring();
|
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
|
// 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()) {
|
if (file_content.empty()) {
|
||||||
Alert::error(_("Reading not UTF-8 encoded file"),
|
Alert::error(_("Reading not UTF-8 encoded file"),
|
||||||
_("The file is not UTF-8 encoded.\n"
|
_("The file is not UTF-8 encoded.\n"
|
||||||
@ -2066,14 +2044,14 @@ docstring BufferView::contentsOfPlaintextFile(string const & f,
|
|||||||
"If this does not give the correct result\n"
|
"If this does not give the correct result\n"
|
||||||
"then please change the encoding of the file\n"
|
"then please change the encoding of the file\n"
|
||||||
"to UTF-8 with a program other than LyX.\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);
|
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);
|
docstring const tmpstr = contentsOfPlaintextFile(f, asParagraph);
|
||||||
|
|
||||||
|
@ -241,9 +241,10 @@ public:
|
|||||||
void setGuiDelegate(frontend::GuiBufferViewDelegate *);
|
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)
|
// 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:
|
private:
|
||||||
/// noncopyable
|
/// noncopyable
|
||||||
|
@ -194,7 +194,7 @@ bool import(LyXView * lv, FileName const & filename,
|
|||||||
string filename2 = (loader_format == format) ? filename.absFilename()
|
string filename2 = (loader_format == format) ? filename.absFilename()
|
||||||
: changeExtension(filename.absFilename(),
|
: changeExtension(filename.absFilename(),
|
||||||
formats.extension(loader_format));
|
formats.extension(loader_format));
|
||||||
lv->view()->insertPlaintextFile(filename2, as_paragraphs);
|
lv->view()->insertPlaintextFile(FileName(filename2), as_paragraphs);
|
||||||
theLyXFunc().setLyXView(lv);
|
theLyXFunc().setLyXView(lv);
|
||||||
lyx::dispatch(FuncRequest(LFUN_MARK_OFF));
|
lyx::dispatch(FuncRequest(LFUN_MARK_OFF));
|
||||||
}
|
}
|
||||||
|
@ -51,9 +51,9 @@ void GuiShowFile::updateContents()
|
|||||||
{
|
{
|
||||||
setWindowTitle(toqstr(onlyFilename(filename_.absFilename())));
|
setWindowTitle(toqstr(onlyFilename(filename_.absFilename())));
|
||||||
|
|
||||||
std::string contents = filename_.fileContents();
|
docstring contents = filename_.fileContents("UTF-8");
|
||||||
if (contents.empty())
|
if (contents.empty())
|
||||||
contents = "Error -> Cannot load file!";
|
contents = _("Error -> Cannot load file!");
|
||||||
|
|
||||||
textTB->setPlainText(toqstr(contents));
|
textTB->setPlainText(toqstr(contents));
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,8 @@ static string texFileFromList(string const & file, string const & type)
|
|||||||
lyxerr << "File `'" << lstfile << "' not found." << endl;
|
lyxerr << "File `'" << lstfile << "' not found." << endl;
|
||||||
return string();
|
return string();
|
||||||
}
|
}
|
||||||
string const allClasses = abslstfile.fileContents();
|
// FIXME UNICODE
|
||||||
|
string const allClasses = to_utf8(abslstfile.fileContents("UTF-8"));
|
||||||
int entries = 0;
|
int entries = 0;
|
||||||
string classfile = token(allClasses, '\n', entries);
|
string classfile = token(allClasses, '\n', entries);
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
@ -348,14 +348,16 @@ void getTexFileList(string const & filename, std::vector<string> & list)
|
|||||||
if (file.empty())
|
if (file.empty())
|
||||||
return;
|
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
|
// Normalise paths like /foo//bar ==> /foo/bar
|
||||||
boost::RegEx regex("/{2,}");
|
boost::RegEx regex("/{2,}");
|
||||||
std::vector<string>::iterator it = list.begin();
|
std::vector<docstring>::iterator it = doclist.begin();
|
||||||
std::vector<string>::iterator end = list.end();
|
std::vector<docstring>::iterator end = doclist.end();
|
||||||
for (; it != end; ++it)
|
for (; it != end; ++it)
|
||||||
*it = regex.Merge((*it), "/");
|
list.push_back(regex.Merge(to_utf8(*it), "/"));
|
||||||
|
|
||||||
// remove empty items and duplicates
|
// remove empty items and duplicates
|
||||||
list.erase(std::remove(list.begin(), list.end(), ""), list.end());
|
list.erase(std::remove(list.begin(), list.end(), ""), list.end());
|
||||||
|
@ -187,7 +187,8 @@ string const doSubstitution(InsetExternalParams const & params,
|
|||||||
FileName const absfile(
|
FileName const absfile(
|
||||||
support::makeAbsPath(file, masterBuffer->temppath()));
|
support::makeAbsPath(file, masterBuffer->temppath()));
|
||||||
if (absfile.isReadableFile())
|
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 pos = result.find("$$Contents(\"");
|
||||||
size_t const end = result.find("\")", pos);
|
size_t const end = result.find("\")", pos);
|
||||||
|
@ -495,6 +495,7 @@ enum GraphicsCopyStatus {
|
|||||||
std::pair<GraphicsCopyStatus, FileName> const
|
std::pair<GraphicsCopyStatus, FileName> const
|
||||||
copyFileIfNeeded(FileName const & file_in, FileName const & file_out)
|
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_in = file_in.checksum();
|
||||||
unsigned long const checksum_out = file_out.checksum();
|
unsigned long const checksum_out = file_out.checksum();
|
||||||
|
|
||||||
|
@ -563,10 +563,8 @@ int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
|
|||||||
{
|
{
|
||||||
if (isVerbatim(params()) || isListings(params())) {
|
if (isVerbatim(params()) || isListings(params())) {
|
||||||
os << '[' << getScreenLabel(buffer) << '\n';
|
os << '[' << getScreenLabel(buffer) << '\n';
|
||||||
// FIXME: We don't know the encoding of the file
|
// FIXME: We don't know the encoding of the file, default to UTF-8.
|
||||||
docstring const str =
|
os << includedFilename(buffer, params()).fileContents("UTF-8");
|
||||||
from_utf8(includedFilename(buffer, params()).fileContents());
|
|
||||||
os << str;
|
|
||||||
os << "\n]";
|
os << "\n]";
|
||||||
return PLAINTEXT_NEWLINE + 1; // one char on a separate line
|
return PLAINTEXT_NEWLINE + 1; // one char on a separate line
|
||||||
} else {
|
} else {
|
||||||
|
@ -48,6 +48,7 @@
|
|||||||
|
|
||||||
#include "support/convert.h"
|
#include "support/convert.h"
|
||||||
#include "support/docstream.h"
|
#include "support/docstream.h"
|
||||||
|
#include "support/FileName.h"
|
||||||
#include "support/lstrings.h"
|
#include "support/lstrings.h"
|
||||||
|
|
||||||
#include "frontends/alert.h"
|
#include "frontends/alert.h"
|
||||||
@ -83,6 +84,7 @@ using boost::dynamic_pointer_cast;
|
|||||||
namespace lyx {
|
namespace lyx {
|
||||||
|
|
||||||
using support::prefixIs;
|
using support::prefixIs;
|
||||||
|
using support::FileName;
|
||||||
using support::ltrim;
|
using support::ltrim;
|
||||||
using support::rtrim;
|
using support::rtrim;
|
||||||
using support::suffixIs;
|
using support::suffixIs;
|
||||||
@ -3408,7 +3410,8 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
|
|||||||
case LFUN_FILE_INSERT_PLAINTEXT_PARA:
|
case LFUN_FILE_INSERT_PLAINTEXT_PARA:
|
||||||
case LFUN_FILE_INSERT_PLAINTEXT: {
|
case LFUN_FILE_INSERT_PLAINTEXT: {
|
||||||
// FIXME UNICODE
|
// 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))
|
if (!tmpstr.empty() && !insertPlaintextString(cur.bv(), tmpstr, false))
|
||||||
cur.undispatched();
|
cur.undispatched();
|
||||||
break;
|
break;
|
||||||
|
@ -338,20 +338,40 @@ docstring FileName::displayName(int threshold) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
string FileName::fileContents() const
|
docstring FileName::fileContents(string const & encoding) const
|
||||||
{
|
{
|
||||||
if (exists()) {
|
if (!isReadableFile()) {
|
||||||
string const encodedname = toFilesystemEncoding();
|
LYXERR0("File '" << *this
|
||||||
ifstream ifs(encodedname.c_str());
|
<< "' is not redable!");
|
||||||
ostringstream ofs;
|
return docstring();
|
||||||
if (ifs && ofs) {
|
|
||||||
ofs << ifs.rdbuf();
|
|
||||||
ifs.close();
|
|
||||||
return ofs.str();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -98,14 +98,12 @@ public:
|
|||||||
/// Creates directory. Returns true on success
|
/// Creates directory. Returns true on success
|
||||||
bool createDirectory(int permissions) const;
|
bool createDirectory(int permissions) const;
|
||||||
|
|
||||||
/// Get the contents of a file as a huge std::string
|
/// Get the contents of a file as a huge docstring.
|
||||||
std::string fileContents() const;
|
/// \param encoding defines the encoding of the file contents.
|
||||||
/**
|
/// Only four encodings are supported:
|
||||||
* Get a FileName from \p name in the encoding used by the file system.
|
/// "UTF-8", "ascii", "latin1" and "local8bit" which uses the
|
||||||
* Only use this for filenames you got directly from the file system,
|
/// current system locale.
|
||||||
* e.g. from reading a directory.
|
docstring fileContents(std::string const & encoding) const;
|
||||||
* \p name must have an absolute path.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/// Change extension.
|
/// Change extension.
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user