From: Abdelrazak Younes Date: Wed, 23 Jul 2008 09:23:23 +0000 (+0000) Subject: Introduce a new tempName() method with base directory. X-Git-Tag: 1.6.10~3915 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=d79ee740b3aaeb603cd7e0b01fe9269ea3914eb4;p=features.git Introduce a new tempName() method with base directory. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25830 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index 70c7b8ba87..8e01df4410 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -17,6 +17,7 @@ #include "support/debug.h" #include "support/filetools.h" #include "support/lstrings.h" +#include "support/qstring_helpers.h" #include "support/os.h" #include "support/Package.h" #include "support/qstring_helpers.h" @@ -141,6 +142,15 @@ FileName::FileName(FileName const & rhs) : d(new Private) } +FileName::FileName(FileName const & rhs, string const & suffix) : d(new Private) +{ + if (!rhs.d->fi.isDir()) + d->fi.setFile(rhs.d->fi.filePath() + toqstr(suffix)); + else + d->fi.setFile(rhs.d->fi.absoluteDir(), toqstr(suffix)); +} + + FileName & FileName::operator=(FileName const & rhs) { d->fi = rhs.d->fi; @@ -353,20 +363,33 @@ FileNameList FileName::dirList(string const & ext) const } +static string createTempFile(QString const & mask) +{ + QTemporaryFile qt_tmp(mask); + if (qt_tmp.open()) { + string const temp_file = fromqstr(qt_tmp.fileName()); + LYXERR(Debug::FILES, "Temporary file `" << temp_file << "' created."); + return temp_file; + } + LYXERR(Debug::FILES, "Unable to create temporary file with following template: " + << qt_tmp.fileTemplate()); + return string(); +} + + +FileName FileName::tempName(FileName const & temp_dir, string const & mask) +{ + QFileInfo tmp_fi(temp_dir.d->fi.absoluteDir(), toqstr(mask)); + return FileName(createTempFile(tmp_fi.absoluteFilePath())); +} + + FileName FileName::tempName(string const & mask) { QFileInfo tmp_fi(toqstr(mask)); if (!tmp_fi.isAbsolute()) tmp_fi.setFile(package().temp_dir().d->fi.absoluteDir(), toqstr(mask)); - - QTemporaryFile qt_tmp(tmp_fi.absoluteFilePath()); - if (qt_tmp.open()) { - FileName tmp_name(fromqstr(qt_tmp.fileName())); - LYXERR(Debug::FILES, "Temporary file `" << tmp_name << "' created."); - return tmp_name; - } - LYXERR(Debug::FILES, "LyX Error: Unable to create temporary file."); - return FileName(); + return FileName(createTempFile(tmp_fi.absoluteFilePath())); } diff --git a/src/support/FileName.h b/src/support/FileName.h index bb635f3ce6..9a8659054f 100644 --- a/src/support/FileName.h +++ b/src/support/FileName.h @@ -43,6 +43,9 @@ public: /// copy constructor. FileName(FileName const &); + /// constructor with base name and suffix. + FileName(FileName const & fn, std::string const & suffix); + /// FileName & operator=(FileName const &); @@ -156,6 +159,8 @@ public: /// relative path, the template file will be created in the global /// temporary directory as given by 'package().temp_dir()'. static FileName tempName(std::string const & mask = empty_string()); + static FileName tempName(FileName const & temp_dir, + std::string const & mask); /// get the current working directory static FileName getcwd();