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>
|
2006-04-01 13:04:23 +00:00
|
|
|
#include <algorithm>
|
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()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName::~FileName()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2006-11-18 12:49:47 +00:00
|
|
|
FileName::FileName(string const & abs_filename)
|
|
|
|
: name_(abs_filename)
|
2003-09-03 17:23:38 +00:00
|
|
|
{
|
2006-12-04 13:45:08 +00:00
|
|
|
BOOST_ASSERT(empty() || absolutePath(name_));
|
2006-12-03 14:48:00 +00:00
|
|
|
BOOST_ASSERT(!contains(name_, '\\'));
|
2003-09-03 17:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
void FileName::set(string const & name)
|
|
|
|
{
|
|
|
|
name_ = name;
|
|
|
|
BOOST_ASSERT(absolutePath(name_));
|
2006-12-03 14:48:00 +00:00
|
|
|
BOOST_ASSERT(!contains(name_, '\\'));
|
2006-11-26 21:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileName::erase()
|
|
|
|
{
|
|
|
|
name_.erase();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const FileName::toFilesystemEncoding() const
|
|
|
|
{
|
|
|
|
// FIXME UNICODE: correct encoding not implemented yet
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-18 12:49:47 +00:00
|
|
|
bool operator==(FileName const & lhs, FileName const & rhs)
|
|
|
|
{
|
|
|
|
return lhs.absFilename() == rhs.absFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(FileName const & lhs, FileName const & rhs)
|
|
|
|
{
|
|
|
|
return lhs.absFilename() != rhs.absFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
bool operator<(FileName const & lhs, FileName const & rhs)
|
|
|
|
{
|
|
|
|
return lhs.absFilename() < rhs.absFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator>(FileName const & lhs, FileName const & rhs)
|
|
|
|
{
|
|
|
|
return lhs.absFilename() > rhs.absFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::ostream & operator<<(std::ostream & os, FileName const & filename)
|
|
|
|
{
|
|
|
|
return os << filename.absFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-18 12:49:47 +00:00
|
|
|
DocFileName::DocFileName()
|
|
|
|
: save_abs_path_(true)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
DocFileName::DocFileName(string const & abs_filename, bool save_abs)
|
|
|
|
: FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
void DocFileName::set(string const & name, string const & buffer_path)
|
2003-07-22 20:42:40 +00:00
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
save_abs_path_ = absolutePath(name);
|
|
|
|
name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path);
|
2006-07-08 14:16:56 +00:00
|
|
|
zipped_valid_ = false;
|
2003-07-22 20:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-18 12:49:47 +00:00
|
|
|
void DocFileName::erase()
|
2003-07-22 20:42:40 +00:00
|
|
|
{
|
|
|
|
name_.erase();
|
2006-07-08 14:16:56 +00:00
|
|
|
zipped_valid_ = false;
|
2003-07-22 20:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-18 12:49:47 +00:00
|
|
|
string const DocFileName::relFilename(string const & path) const
|
2003-07-22 20:42:40 +00:00
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
return makeRelPath(name_, path);
|
2003-07-22 20:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-18 12:49:47 +00:00
|
|
|
string const DocFileName::outputFilename(string const & path) const
|
2003-07-22 20:42:40 +00:00
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
return save_abs_path_ ? name_ : makeRelPath(name_, path);
|
2003-07-22 20:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-18 12:49:47 +00:00
|
|
|
string const DocFileName::mangledFilename(std::string const & dir) const
|
2003-09-03 17:23:38 +00:00
|
|
|
{
|
2006-11-18 12:49:47 +00:00
|
|
|
// We need to make sure that every DocFileName instance for a given
|
2004-03-11 11:45:08 +00:00
|
|
|
// 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.
|
2006-04-08 22:31:11 +00:00
|
|
|
mname = changeExtension(name_, string());
|
2003-09-03 17:23:38 +00:00
|
|
|
// Replace '/' in the file name with '_'
|
|
|
|
mname = subst(mname, "/", "_");
|
|
|
|
// Replace '.' in the file name with '_'
|
|
|
|
mname = subst(mname, ".", "_");
|
2005-06-24 14:20:51 +00:00
|
|
|
// Replace ' ' in the file name with '_'
|
|
|
|
mname = subst(mname, " ", "_");
|
2005-07-14 12:53:12 +00:00
|
|
|
// Replace ':' in the file name with '_'
|
|
|
|
mname = subst(mname, ":", "_");
|
2003-09-03 17:23:38 +00:00
|
|
|
// Add the extension back on
|
2006-04-08 22:31:11 +00:00
|
|
|
mname = changeExtension(mname, getExtension(name_));
|
2004-12-17 12:30:48 +00:00
|
|
|
|
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;
|
2005-07-14 12:53:12 +00:00
|
|
|
s << counter++ << mname;
|
|
|
|
mname = s.str();
|
|
|
|
|
2006-04-05 23:39:11 +00:00
|
|
|
// MiKTeX's YAP (version 2.4.1803) crashes if the file name
|
2006-04-01 13:04:23 +00:00
|
|
|
// is longer than about 160 characters. MiKTeX's pdflatex
|
|
|
|
// is even pickier. A maximum length of 100 has been proven to work.
|
|
|
|
// If dir.size() > max length, all bets are off for YAP. We truncate
|
|
|
|
// the filename nevertheless, keeping a minimum of 10 chars.
|
|
|
|
|
|
|
|
string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
|
2006-03-15 21:00:15 +00:00
|
|
|
|
|
|
|
// If the mangled file name is too long, hack it to fit.
|
|
|
|
// We know we're guaranteed to have a unique file name because
|
|
|
|
// of the counter.
|
|
|
|
if (mname.size() > max_length) {
|
|
|
|
int const half = (int(max_length) / 2) - 2;
|
|
|
|
if (half > 0) {
|
|
|
|
mname = mname.substr(0, half) + "___" +
|
|
|
|
mname.substr(mname.size() - half);
|
2005-07-14 12:53:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-11 11:45:08 +00:00
|
|
|
mangledNames[name_] = mname;
|
|
|
|
return mname;
|
2003-09-03 17:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-18 12:49:47 +00:00
|
|
|
bool DocFileName::isZipped() const
|
2003-09-03 17:23:38 +00:00
|
|
|
{
|
2006-07-08 14:16:56 +00:00
|
|
|
if (!zipped_valid_) {
|
2006-11-26 21:30:39 +00:00
|
|
|
zipped_ = zippedFile(*this);
|
2006-07-08 14:16:56 +00:00
|
|
|
zipped_valid_ = true;
|
|
|
|
}
|
|
|
|
return zipped_;
|
2003-09-03 17:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-18 12:49:47 +00:00
|
|
|
string const DocFileName::unzippedFilename() const
|
2003-09-03 17:23:38 +00:00
|
|
|
{
|
|
|
|
return unzippedFileName(name_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-18 12:49:47 +00:00
|
|
|
bool operator==(DocFileName const & lhs, DocFileName const & rhs)
|
2003-07-22 20:42:40 +00:00
|
|
|
{
|
|
|
|
return lhs.absFilename() == rhs.absFilename() &&
|
|
|
|
lhs.saveAbsPath() == rhs.saveAbsPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-18 12:49:47 +00:00
|
|
|
bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
|
2003-07-22 20:42:40 +00:00
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2003-09-03 17:23:38 +00:00
|
|
|
} // namespace support
|
2003-07-22 20:42:40 +00:00
|
|
|
} // namespace lyx
|