From 9722c8f1ea78056ef811ee812cee726da8a7bdbc Mon Sep 17 00:00:00 2001 From: Vincent van Ravesteijn Date: Sat, 24 Oct 2009 14:57:28 +0000 Subject: [PATCH] Add a global accessible function loadIfNeeded(). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@31705 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/buffer_funcs.cpp | 23 +++++++++++++++++++++++ src/buffer_funcs.h | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/src/buffer_funcs.cpp b/src/buffer_funcs.cpp index 3115f4a356..be53275791 100644 --- a/src/buffer_funcs.cpp +++ b/src/buffer_funcs.cpp @@ -249,4 +249,27 @@ int countChars(DocIterator const & from, DocIterator const & to, return chars + blanks; } + +Buffer * loadIfNeeded(FileName const & fname) +{ + Buffer * buffer = theBufferList().getBuffer(fname); + if (!buffer) { + if (!fname.exists()) + return 0; + + buffer = theBufferList().newBuffer(fname.absFilename()); + if (!buffer) + // Buffer creation is not possible. + return 0; + + if (!buffer->loadLyXFile(fname)) { + //close the buffer we just opened + theBufferList().release(buffer); + return 0; + } + } + return buffer; +} + + } // namespace lyx diff --git a/src/buffer_funcs.h b/src/buffer_funcs.h index 1faa1b8f44..f7dd645df3 100644 --- a/src/buffer_funcs.h +++ b/src/buffer_funcs.h @@ -42,6 +42,10 @@ Buffer * newFile(std::string const & filename, std::string const & templatename, Buffer * newUnnamedFile(support::FileName const & path, std::string const & prefix, std::string const & templatename = ""); +/// Load the file with name \c fname, and returns the buffer. If the +/// file was already loaded it just returns the associated buffer. +Buffer * loadIfNeeded(support::FileName const & fname); + /// Count the number of words in the text between these two iterators int countWords(DocIterator const & from, DocIterator const & to);