Use Buffer::ReadStatus as a return value for Buffer::loadLyXFile.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35818 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Vincent van Ravesteijn 2010-10-25 10:18:42 +00:00
parent 058035bf2c
commit 75773f3c22
6 changed files with 29 additions and 28 deletions

View File

@ -839,16 +839,16 @@ bool Buffer::readString(string const & s)
lex.setStream(is); lex.setStream(is);
FileName const name = FileName::tempName("Buffer_readString"); FileName const name = FileName::tempName("Buffer_readString");
switch (readFile(lex, name, true)) { switch (readFile(lex, name, true)) {
case failure: case ReadFailure:
return false; return false;
case wrongversion: { case ReadWrongVersion: {
// We need to call lyx2lyx, so write the input to a file // We need to call lyx2lyx, so write the input to a file
ofstream os(name.toFilesystemEncoding().c_str()); ofstream os(name.toFilesystemEncoding().c_str());
os << s; os << s;
os.close(); os.close();
return readFile(name); return readFile(name);
} }
case success: case ReadSuccess:
break; break;
} }
@ -866,7 +866,7 @@ bool Buffer::readFile(FileName const & filename)
paragraphs().clear(); paragraphs().clear();
Lexer lex; Lexer lex;
lex.setFile(fname); lex.setFile(fname);
if (readFile(lex, fname) != success) if (readFile(lex, fname) != ReadSuccess)
return false; return false;
return true; return true;
@ -895,7 +895,7 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
Alert::error(_("Document format failure"), Alert::error(_("Document format failure"),
bformat(_("%1$s is not a readable LyX document."), bformat(_("%1$s is not a readable LyX document."),
from_utf8(filename.absFileName()))); from_utf8(filename.absFileName())));
return failure; return ReadFailure;
} }
string tmp_format; string tmp_format;
@ -926,7 +926,7 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
if (fromstring) if (fromstring)
// lyx2lyx would fail // lyx2lyx would fail
return wrongversion; return ReadWrongVersion;
FileName const tmpfile = FileName::tempName("Buffer_readFile"); FileName const tmpfile = FileName::tempName("Buffer_readFile");
if (tmpfile.empty()) { if (tmpfile.empty()) {
@ -936,7 +936,7 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
" file for converting it could" " file for converting it could"
" not be created."), " not be created."),
from_utf8(filename.absFileName()))); from_utf8(filename.absFileName())));
return failure; return ReadFailure;
} }
FileName const lyx2lyx = libFileSearch("lyx2lyx", "lyx2lyx"); FileName const lyx2lyx = libFileSearch("lyx2lyx", "lyx2lyx");
if (lyx2lyx.empty()) { if (lyx2lyx.empty()) {
@ -946,7 +946,7 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
" conversion script lyx2lyx" " conversion script lyx2lyx"
" could not be found."), " could not be found."),
from_utf8(filename.absFileName()))); from_utf8(filename.absFileName())));
return failure; return ReadFailure;
} }
ostringstream command; ostringstream command;
command << os::python() command << os::python()
@ -972,11 +972,11 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
" of LyX and cannot be converted by the" " of LyX and cannot be converted by the"
" lyx2lyx script."), " lyx2lyx script."),
from_utf8(filename.absFileName()))); from_utf8(filename.absFileName())));
return failure; return ReadFailure;
} else { } else {
bool const ret = readFile(tmpfile); bool const ret = readFile(tmpfile);
// Do stuff with tmpfile name and buffer name here. // Do stuff with tmpfile name and buffer name here.
return ret ? success : failure; return ret ? ReadSuccess : ReadFailure;
} }
} }
@ -986,11 +986,11 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
bformat(_("%1$s ended unexpectedly, which means" bformat(_("%1$s ended unexpectedly, which means"
" that it is probably corrupted."), " that it is probably corrupted."),
from_utf8(filename.absFileName()))); from_utf8(filename.absFileName())));
return failure; return ReadFailure;
} }
d->file_fully_loaded = true; d->file_fully_loaded = true;
return success; return ReadSuccess;
} }
@ -3620,7 +3620,7 @@ bool Buffer::readFileHelper(FileName const & s)
docstring str; docstring str;
bool res; bool res;
if ((res = readFile(e)) == success) if (res = readFile(e))
str = _("Document was successfully recovered."); str = _("Document was successfully recovered.");
else else
str = _("Document was NOT successfully recovered."); str = _("Document was NOT successfully recovered.");
@ -3630,7 +3630,7 @@ bool Buffer::readFileHelper(FileName const & s)
if (!Alert::prompt(_("Delete emergency file?"), str, 1, 1, if (!Alert::prompt(_("Delete emergency file?"), str, 1, 1,
_("&Remove"), _("&Keep it"))) { _("&Remove"), _("&Keep it"))) {
e.removeFile(); e.removeFile();
if (res == success) if (res)
Alert::warning(_("Emergency file deleted"), Alert::warning(_("Emergency file deleted"),
_("Do not forget to save your file now!"), true); _("Do not forget to save your file now!"), true);
} }
@ -3676,23 +3676,23 @@ bool Buffer::readFileHelper(FileName const & s)
} }
bool Buffer::loadLyXFile(FileName const & s) Buffer::ReadStatus Buffer::loadLyXFile(FileName const & s)
{ {
// If the file is not readable, we try to // If the file is not readable, we try to
// retrieve the file from version control. // retrieve the file from version control.
if (!s.isReadableFile() if (!s.isReadableFile()
&& !LyXVC::file_not_found_hook(s)) && !LyXVC::file_not_found_hook(s))
return false; return ReadFailure;
if (s.isReadableFile()){ if (s.isReadableFile()){
// InsetInfo needs to know if file is under VCS // InsetInfo needs to know if file is under VCS
lyxvc().file_found_hook(s); lyxvc().file_found_hook(s);
if (readFileHelper(s)) { if (readFileHelper(s)) {
d->read_only = !s.isWritable(); d->read_only = !s.isWritable();
return true; return ReadSuccess;
} }
} }
return false; return ReadFailure;
} }
@ -4067,7 +4067,7 @@ bool Buffer::reload()
d->filename.refresh(); d->filename.refresh();
docstring const disp_fn = makeDisplayPath(d->filename.absFileName()); docstring const disp_fn = makeDisplayPath(d->filename.absFileName());
bool const success = loadLyXFile(d->filename); bool const success = (loadLyXFile(d->filename) == ReadSuccess);
if (success) { if (success) {
updateBuffer(); updateBuffer();
changed(true); changed(true);

View File

@ -95,9 +95,10 @@ public:
/// Result of \c readFile() /// Result of \c readFile()
enum ReadStatus { enum ReadStatus {
failure, ///< The file could not be read ReadSuccess,
success, ///< The file could not be read ReadCancel,
wrongversion ///< The version of the file does not match ours ReadFailure,
ReadWrongVersion
}; };
@ -186,7 +187,7 @@ public:
bool writeFile(support::FileName const &) const; bool writeFile(support::FileName const &) const;
/// Loads LyX file \c filename into buffer, * and return success /// Loads LyX file \c filename into buffer, * and return success
bool loadLyXFile(support::FileName const & s); ReadStatus loadLyXFile(support::FileName const & s);
/// Reloads the LyX file /// Reloads the LyX file
bool reload(); bool reload();

View File

@ -2507,7 +2507,7 @@ void BufferView::insertLyXFile(FileName const & fname)
docstring res; docstring res;
Buffer buf("", false); Buffer buf("", false);
if (buf.loadLyXFile(filename)) { if (buf.loadLyXFile(filename) == Buffer::ReadSuccess) {
ErrorList & el = buffer_.errorList("Parse"); ErrorList & el = buffer_.errorList("Parse");
// Copy the inserted document error list into the current buffer one. // Copy the inserted document error list into the current buffer one.
el = buf.errorList("Parse"); el = buf.errorList("Parse");

View File

@ -496,7 +496,7 @@ bool LyX::loadFiles()
continue; continue;
Buffer * buf = pimpl_->buffer_list_.newBuffer(fname.absFileName(), false); Buffer * buf = pimpl_->buffer_list_.newBuffer(fname.absFileName(), false);
if (buf->loadLyXFile(fname)) { if (buf->loadLyXFile(fname) == Buffer::ReadSuccess) {
ErrorList const & el = buf->errorList("Parse"); ErrorList const & el = buf->errorList("Parse");
if (!el.empty()) if (!el.empty())
for_each(el.begin(), el.end(), for_each(el.begin(), el.end(),

View File

@ -94,7 +94,7 @@ Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
// Buffer creation is not possible. // Buffer creation is not possible.
return 0; return 0;
} }
if (!b->loadLyXFile(filename)) { if (b->loadLyXFile(filename) != Buffer::ReadSuccess) {
// do not save an emergency file when releasing the buffer // do not save an emergency file when releasing the buffer
b->markClean(); b->markClean();
theBufferList().release(b); theBufferList().release(b);
@ -268,7 +268,7 @@ Buffer * loadIfNeeded(FileName const & fname)
// Buffer creation is not possible. // Buffer creation is not possible.
return 0; return 0;
if (!buffer->loadLyXFile(fname)) { if (buffer->loadLyXFile(fname) != Buffer::ReadSuccess) {
//close the buffer we just opened //close the buffer we just opened
theBufferList().release(buffer); theBufferList().release(buffer);
return 0; return 0;

View File

@ -440,7 +440,7 @@ Buffer * InsetInclude::loadIfNeeded() const
// Buffer creation is not possible. // Buffer creation is not possible.
return 0; return 0;
if (!child->loadLyXFile(included_file)) { if (child->loadLyXFile(included_file) != Buffer::ReadSuccess) {
failedtoload_ = true; failedtoload_ = true;
//close the buffer we just opened //close the buffer we just opened
theBufferList().release(child); theBufferList().release(child);