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
This commit is contained in:
Abdelrazak Younes 2008-07-23 09:23:23 +00:00
parent c8a0b97d2e
commit d79ee740b3
2 changed files with 37 additions and 9 deletions

View File

@ -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()));
}

View File

@ -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();