2003-07-22 20:42:40 +00:00
|
|
|
/**
|
|
|
|
* \file filename.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
2003-09-08 09:51:40 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-07-22 20:42:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2004-11-07 13:22:51 +00:00
|
|
|
#include "support/filename.h"
|
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include "support/os.h"
|
2003-07-22 20:42:40 +00:00
|
|
|
|
2003-09-09 17:25:35 +00:00
|
|
|
#include <boost/assert.hpp>
|
|
|
|
|
2004-03-11 11:45:08 +00:00
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
2003-07-22 20:42:40 +00:00
|
|
|
|
2004-03-11 11:45:08 +00:00
|
|
|
|
|
|
|
using std::map;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
|
|
|
|
|
|
|
|
2003-07-22 20:42:40 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace support {
|
|
|
|
|
|
|
|
|
|
|
|
FileName::FileName()
|
|
|
|
: save_abs_path_(true)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2003-09-03 17:23:38 +00:00
|
|
|
FileName::FileName(string const & abs_filename, bool save_abs)
|
|
|
|
: name_(abs_filename), save_abs_path_(save_abs)
|
|
|
|
{
|
2003-09-09 17:25:35 +00:00
|
|
|
BOOST_ASSERT(AbsolutePath(name_));
|
2003-09-03 17:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-22 20:42:40 +00:00
|
|
|
void FileName::set(string const & name, string const & buffer_path)
|
|
|
|
{
|
|
|
|
save_abs_path_ = AbsolutePath(name);
|
|
|
|
name_ = save_abs_path_ ? name : MakeAbsPath(name, buffer_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileName::erase()
|
|
|
|
{
|
|
|
|
name_.erase();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const FileName::relFilename(string const & path) const
|
|
|
|
{
|
|
|
|
return MakeRelPath(name_, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-29 10:56:55 +00:00
|
|
|
string const FileName::outputFilename(string const & path) const
|
2003-07-22 20:42:40 +00:00
|
|
|
{
|
2003-07-29 10:56:55 +00:00
|
|
|
return save_abs_path_ ? name_ : MakeRelPath(name_, path);
|
2003-07-22 20:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-03 17:23:38 +00:00
|
|
|
string const FileName::mangledFilename() const
|
|
|
|
{
|
2004-03-11 11:45:08 +00:00
|
|
|
// We need to make sure that every FileName instance for a given
|
|
|
|
// filename returns the same mangled name.
|
2004-10-05 10:11:42 +00:00
|
|
|
typedef map<string, string> MangledMap;
|
2004-03-11 11:45:08 +00:00
|
|
|
static MangledMap mangledNames;
|
|
|
|
MangledMap::const_iterator const it = mangledNames.find(name_);
|
|
|
|
if (it != mangledNames.end())
|
|
|
|
return (*it).second;
|
|
|
|
|
|
|
|
// Now the real work
|
2004-12-20 16:59:33 +00:00
|
|
|
string mname = os::internal_path(name_);
|
2003-09-03 17:23:38 +00:00
|
|
|
// Remove the extension.
|
|
|
|
mname = ChangeExtension(name_, string());
|
|
|
|
// Replace '/' in the file name with '_'
|
|
|
|
mname = subst(mname, "/", "_");
|
|
|
|
// Replace '.' in the file name with '_'
|
|
|
|
mname = subst(mname, ".", "_");
|
|
|
|
// Add the extension back on
|
2004-03-11 11:45:08 +00:00
|
|
|
mname = ChangeExtension(mname, GetExtension(name_));
|
2004-12-17 12:30:48 +00:00
|
|
|
|
|
|
|
#if defined(__CYGWIN__) || defined(__CYGWIN32__) || defined(_WIN32)
|
|
|
|
// Mangle the drive letter in a Windows-style path.
|
|
|
|
if (mname.size() >= 2 && mname[1] == ':')
|
|
|
|
mname[1] = '_';
|
|
|
|
#endif
|
|
|
|
|
2004-03-11 11:45:08 +00:00
|
|
|
// Prepend a counter to the filename. This is necessary to make
|
|
|
|
// the mangled name unique.
|
|
|
|
static int counter = 0;
|
|
|
|
std::ostringstream s;
|
|
|
|
s << counter++;
|
|
|
|
mname = s.str() + mname;
|
|
|
|
mangledNames[name_] = mname;
|
|
|
|
return mname;
|
2003-09-03 17:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileName::isZipped() const
|
|
|
|
{
|
|
|
|
return zippedFile(name_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const FileName::unzippedFilename() const
|
|
|
|
{
|
|
|
|
return unzippedFileName(name_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-22 20:42:40 +00:00
|
|
|
bool operator==(FileName const & lhs, FileName const & rhs)
|
|
|
|
{
|
|
|
|
return lhs.absFilename() == rhs.absFilename() &&
|
|
|
|
lhs.saveAbsPath() == rhs.saveAbsPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(FileName const & lhs, FileName const & rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2003-09-03 17:23:38 +00:00
|
|
|
} // namespace support
|
2003-07-22 20:42:40 +00:00
|
|
|
} // namespace lyx
|