* FileName:

- introduce changeExtension(): the goal is to replace progressively the one in filetools.cpp
- get rid of internal_path() checking for Win32 as the path is always normalized with slashes with QFileInfo.

* BufferParams: get rid of filepath member. This was used only once and was fixing a path in BufferParam was wrong anyway.

* Buffer::filePath(): always regenerate instead of caching it in BufferParams.

 

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21847 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2007-11-28 15:25:07 +00:00
parent 25a4523860
commit c886bc68c2
10 changed files with 54 additions and 29 deletions

View File

@ -233,7 +233,7 @@ Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_)
inset.setAutoBreakRows(true);
lyxvc.setBuffer(&parent);
temppath = createBufferTmpDir();
params.filepath = onlyPath(file.absFilename());
// FIXME: And now do something if temppath == string(), because we
// assume from now on that temppath points to a valid temp dir.
// See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg67406.html
@ -445,7 +445,6 @@ void Buffer::setReadonly(bool const flag)
void Buffer::setFileName(string const & newfile)
{
pimpl_->filename = makeAbsPath(newfile);
params().filepath = onlyPath(pimpl_->filename.absFilename());
setReadonly(pimpl_->filename.isReadOnly());
updateTitles();
}
@ -501,7 +500,7 @@ int Buffer::readHeader(Lexer & lex)
LYXERR(Debug::PARSER, "Handling document header token: `"
<< token << '\'');
string unknown = params().readToken(lex, token);
string unknown = params().readToken(lex, token, pimpl_->filename.onlyPath());
if (!unknown.empty()) {
if (unknown[0] != '\\' && token == "\\textclass") {
Alert::warning(_("Unknown document class"),
@ -848,8 +847,7 @@ bool Buffer::save() const
backupName = FileName(absFileName() + '~');
if (!lyxrc.backupdir_path.empty()) {
string const mangledName =
subst(subst(os::internal_path(
backupName.absFilename()), '/', '!'), ':', '!');
subst(subst(backupName.absFilename(), '/', '!'), ':', '!');
backupName = FileName(addName(lyxrc.backupdir_path,
mangledName));
}
@ -1701,9 +1699,9 @@ string Buffer::absFileName() const
}
string const & Buffer::filePath() const
string Buffer::filePath() const
{
return params().filepath;
return pimpl_->filename.onlyPath().absFilename();
}

View File

@ -244,7 +244,7 @@ public:
/// Returns the the path where the buffer lives.
/// It is always an absolute path.
std::string const & filePath() const;
std::string filePath() const;
/** A transformed version of the file name, adequate for LaTeX.
\param no_path optional if \c true then the path is stripped.

View File

@ -44,6 +44,7 @@
#include "support/convert.h"
#include "support/docstream.h"
#include "support/FileName.h"
#include "support/filetools.h"
#include "support/Translator.h"
#include "support/lstrings.h"
@ -464,14 +465,16 @@ void BufferParams::setDefSkip(VSpace const & vs)
}
string const BufferParams::readToken(Lexer & lex, string const & token)
string const BufferParams::readToken(Lexer & lex, string const & token,
FileName const & filepath)
{
if (token == "\\textclass") {
lex.next();
string const classname = lex.getString();
// if there exists a local layout file, ignore the system one
// NOTE: in this case, the textclass (.cls file) is assumed to be available.
pair<bool, lyx::textclass_type> pp = textclasslist.addTextClass(classname, filepath);
pair<bool, lyx::textclass_type> pp = textclasslist.addTextClass(
classname, filepath.absFilename());
if (pp.first)
setBaseClass(pp.second);
else {

View File

@ -30,6 +30,10 @@
namespace lyx {
namespace support {
class FileName;
}
class AuthorList;
class BranchList;
class Bullet;
@ -65,7 +69,9 @@ public:
docstring const B_(std::string const & l10n) const;
/// read a header token, if unrecognised, return it or an unknown class name
std::string const readToken(Lexer & lex, std::string const & token);
std::string const readToken(Lexer & lex,
std::string const & token, ///< token to read.
support::FileName const & filepath); ///< where to look for local layout file.
///
void writeFile(std::ostream &) const;
@ -284,8 +290,7 @@ public:
std::string const & sf, std::string const & tt,
bool const & sc, bool const & osf,
int const & sfscale, int const & ttscale) const;
/// path of the current buffer
std::string filepath;
/// get the appropriate cite engine (natbib handling)
biblio::CiteEngine getEngine() const;

View File

@ -650,8 +650,7 @@ void GuiExternal::updateTemplate()
void GuiExternal::applyView()
{
params_.filename.set(internal_path(fromqstr(fileED->text())),
bufferFilepath());
params_.filename.set(fromqstr(fileED->text()), bufferFilepath());
params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);

View File

@ -614,8 +614,7 @@ void GuiGraphics::applyView()
{
InsetGraphicsParams & igp = params_;
igp.filename.set(internal_path(fromqstr(filename->text())),
bufferFilepath());
igp.filename.set(fromqstr(filename->text()), bufferFilepath());
igp.filename.setEmbed(embedCB->checkState() == Qt::Checked);
// the bb section

View File

@ -79,12 +79,10 @@ FileName::FileName() : d(new Private)
{
}
FileName::FileName(string const & abs_filename)
: d(abs_filename.empty() ? new Private : new Private(abs_filename))
{
#if defined(_WIN32)
BOOST_ASSERT(!contains(abs_filename, '\\'));
#endif
}
@ -117,9 +115,6 @@ void FileName::set(string const & name)
{
d->fi.setFile(toqstr(name));
BOOST_ASSERT(d->fi.isAbsolute());
#if defined(_WIN32)
BOOST_ASSERT(!contains(name, '\\'));
#endif
}
@ -360,6 +355,26 @@ string FileName::fileContents() const
}
void FileName::changeExtension(std::string const & extension)
{
// FIXME: use Qt native methods...
string const oldname = absFilename();
string::size_type const last_slash = oldname.rfind('/');
string::size_type last_dot = oldname.rfind('.');
if (last_dot < last_slash && last_slash != string::npos)
last_dot = string::npos;
string ext;
// Make sure the extension starts with a dot
if (!extension.empty() && extension[0] != '.')
ext= '.' + extension;
else
ext = extension;
set(oldname.substr(0, last_dot) + ext);
}
string FileName::guessFormatFromContents() const
{
// the different filetypes and what they contain in one of the first lines
@ -636,7 +651,7 @@ string const DocFileName::mangledFilename(std::string const & dir) const
// Now the real work
string mname = os::internal_path(name);
// Remove the extension.
mname = changeExtension(name, string());
mname = support::changeExtension(name, string());
// The mangled name must be a valid LaTeX name.
// The list of characters to keep is probably over-restrictive,
// but it is not really a problem.
@ -651,7 +666,7 @@ string const DocFileName::mangledFilename(std::string const & dir) const
while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
mname[pos++] = '_';
// Add the extension back on
mname = changeExtension(mname, getExtension(name));
mname = support::changeExtension(mname, getExtension(name));
// Prepend a counter to the filename. This is necessary to make
// the mangled name unique.

View File

@ -112,6 +112,13 @@ public:
* \p name must have an absolute path.
*/
/// Change extension.
/**
* If oldname does not have an extension, it is appended.
* If the extension is empty, any extension is removed from the name.
*/
void changeExtension(std::string const & extension);
/** Guess the file format name (as in Format::name()) from contents.
Normally you don't want to use this directly, but rather
Formats::getFormatFromFile().

View File

@ -367,7 +367,7 @@ FileName const get_temp_dir()
// Typical example: C:/TEMP/.
char path[MAX_PATH];
GetTempPath(MAX_PATH, path);
return FileName(os::internal_path(to_utf8(from_local8bit(path))));
return FileName(to_utf8(from_local8bit(path)));
#else // Posix-like.
return FileName("/tmp");
#endif

View File

@ -448,7 +448,7 @@ FileName const makeAbsPath(string const & relPath, string const & basePath)
}
// returns absolute path
return FileName(os::internal_path(tempBase));
return FileName(tempBase);
}
@ -840,8 +840,7 @@ FileName const findtexfile(string const & fil, string const & /*format*/)
LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n'
<< "kpse result = `" << rtrim(c.second, "\n\r") << '\'');
if (c.first != -1)
return FileName(os::internal_path(rtrim(to_utf8(from_filesystem8bit(c.second)),
"\n\r")));
return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r"));
else
return FileName();
}