Buffer.cpp:

Extract convertLyXFormat function that runs LyX2LyX.

(and some compile fixes)

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35831 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Vincent van Ravesteijn 2010-10-25 13:55:39 +00:00
parent 5f9b12ec39
commit 5a30a98346
2 changed files with 83 additions and 53 deletions

View File

@ -910,6 +910,65 @@ Buffer::ReadStatus Buffer::parseLyXFormat(Lexer & lex,
}
Buffer::ReadStatus Buffer::convertLyXFormat(FileName const & fn,
FileName & tmpfile, int from_format)
{
tmpfile = FileName::tempName("Buffer_convertLyXFormat");
if(tmpfile.empty()) {
Alert::error(_("Conversion failed"),
bformat(_("%1$s is from a different"
" version of LyX, but a temporary"
" file for converting it could"
" not be created."),
from_utf8(fn.absFileName())));
return LyX2LyXNoTempFile;
}
FileName const lyx2lyx = libFileSearch("lyx2lyx", "lyx2lyx");
if (lyx2lyx.empty()) {
Alert::error(_("Conversion script not found"),
bformat(_("%1$s is from a different"
" version of LyX, but the"
" conversion script lyx2lyx"
" could not be found."),
from_utf8(fn.absFileName())));
return LyX2LyXNotFound;
}
// Run lyx2lyx:
// $python$ "$lyx2lyx$" -t $LYX_FORMAT$ -o "$tempfile$" "$filetoread$"
ostringstream command;
command << os::python()
<< ' ' << quoteName(lyx2lyx.toFilesystemEncoding())
<< " -t " << convert<string>(LYX_FORMAT)
<< " -o " << quoteName(tmpfile.toFilesystemEncoding())
<< ' ' << quoteName(fn.toSafeFilesystemEncoding());
string const command_str = command.str();
LYXERR(Debug::INFO, "Running '" << command_str << '\'');
cmd_ret const ret = runCommand(command_str);
if (ret.first != 0) {
if (from_format < LYX_FORMAT) {
Alert::error(_("Conversion script failed"),
bformat(_("%1$s is from an older version"
" of LyX, but the lyx2lyx script"
" failed to convert it."),
from_utf8(fn.absFileName())));
return LyX2LyXOlderFormat;
} else {
Alert::error(_("Conversion script failed"),
bformat(_("%1$s is from an newer version"
" of LyX, but the lyx2lyx script"
" failed to convert it."),
from_utf8(fn.absFileName())));
return LyX2LyXNewerFormat;
}
}
return ReadSuccess;
}
Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & fn,
bool fromstring)
{
@ -923,56 +982,12 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & fn,
// lyx2lyx would fail
return ReadWrongVersion;
FileName const tmpfile = FileName::tempName("Buffer_readFile");
if (tmpfile.empty()) {
Alert::error(_("Conversion failed"),
bformat(_("%1$s is from a different"
" version of LyX, but a temporary"
" file for converting it could"
" not be created."),
from_utf8(fn.absFileName())));
return ReadFailure;
}
FileName const lyx2lyx = libFileSearch("lyx2lyx", "lyx2lyx");
if (lyx2lyx.empty()) {
Alert::error(_("Conversion script not found"),
bformat(_("%1$s is from a different"
" version of LyX, but the"
" conversion script lyx2lyx"
" could not be found."),
from_utf8(fn.absFileName())));
return ReadFailure;
}
ostringstream command;
command << os::python()
<< ' ' << quoteName(lyx2lyx.toFilesystemEncoding())
<< " -t " << convert<string>(LYX_FORMAT)
<< " -o " << quoteName(tmpfile.toFilesystemEncoding())
<< ' ' << quoteName(fn.toSafeFilesystemEncoding());
string const command_str = command.str();
LYXERR(Debug::INFO, "Running '" << command_str << '\'');
cmd_ret const ret = runCommand(command_str);
if (ret.first != 0) {
if (file_format < LYX_FORMAT)
Alert::error(_("Conversion script failed"),
bformat(_("%1$s is from an older version"
" of LyX, but the lyx2lyx script"
" failed to convert it."),
from_utf8(fn.absFileName())));
else
Alert::error(_("Conversion script failed"),
bformat(_("%1$s is from a newer version"
" of LyX and cannot be converted by the"
" lyx2lyx script."),
from_utf8(fn.absFileName())));
return ReadFailure;
} else {
// Do stuff with tmpfile name and buffer name here.
return readFile(tmpfile);
}
FileName tmpFile;
ReadStatus const ret_clf = convertLyXFormat(fn, tmpFile, file_format);
if (ret_clf != ReadSuccess)
return ret_clf;
else
return readFile(tmpFile);
}
if (readDocument(lex)) {
@ -980,7 +995,7 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & fn,
bformat(_("%1$s ended unexpectedly, which means"
" that it is probably corrupted."),
from_utf8(fn.absFileName())));
return ReadFailure;
return ReadDocumentFailure;
}
d->file_fully_loaded = true;

View File

@ -102,8 +102,15 @@ public:
ReadWrongVersion,
ReadFileNotFound,
ReadVCError,
ReadAutosaveFailure,
ReadAutosaveFailure,
ReadEmergencyFailure,
ReadNoLyXFormat,
ReadDocumentFailure,
// lyx2lyx
LyX2LyXNoTempFile,
LyX2LyXNotFound,
LyX2LyXOlderFormat,
LyX2LyXNewerFormat,
// other
ReadOriginal
};
@ -160,14 +167,22 @@ public:
/// read a new document from a string
bool readString(std::string const &);
/// Reads the first tag of a LyX File and
/// returns the file format number.
ReadStatus parseLyXFormat(Lexer & lex,
support::FileName const & fn, int & file_format) const;
/// read the header, returns number of unknown tokens
int readHeader(Lexer & lex);
/** Reads a file without header.
\param par if != 0 insert the file.
\return \c true if file is not completely read.
*/
bool readDocument(Lexer &);
/// Convert the LyX file to the LYX_FORMAT using
/// the lyx2lyx script and returns the filename
/// of the temporary file to be read
ReadStatus convertLyXFormat(support::FileName const & fn,
support::FileName & tmpfile, int from_format);
///
DocIterator getParFromID(int id) const;