2004-10-26 18:39:13 +00:00
|
|
|
/**
|
|
|
|
* \file mover.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
2005-04-26 10:30:24 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2004-10-26 18:39:13 +00:00
|
|
|
#include "mover.h"
|
|
|
|
|
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
#include "support/systemcall.h"
|
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
#include <fstream>
|
2004-10-26 18:39:13 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
using std::ios;
|
2006-10-21 00:16:43 +00:00
|
|
|
using std::string;
|
2004-10-26 18:39:13 +00:00
|
|
|
|
2007-01-18 08:42:53 +00:00
|
|
|
namespace lyx {
|
2004-10-26 18:39:13 +00:00
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
bool Mover::copy(support::FileName const & from, support::FileName const & to,
|
|
|
|
unsigned long int mode) const
|
|
|
|
{
|
|
|
|
return do_copy(from, to, to.absFilename(), mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Mover::do_copy(support::FileName const & from, support::FileName const & to,
|
2006-11-13 10:27:57 +00:00
|
|
|
string const &, unsigned long int mode) const
|
2004-10-26 18:39:13 +00:00
|
|
|
{
|
2006-11-13 10:27:57 +00:00
|
|
|
return support::copy(from, to, mode);
|
2004-10-26 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
bool Mover::rename(support::FileName const & from,
|
|
|
|
support::FileName const & to) const
|
|
|
|
{
|
|
|
|
return do_rename(from, to, to.absFilename());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Mover::do_rename(support::FileName const & from, support::FileName const & to,
|
2006-04-05 23:56:29 +00:00
|
|
|
string const &) const
|
2004-10-26 18:39:13 +00:00
|
|
|
{
|
|
|
|
return support::rename(from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
bool SpecialisedMover::do_copy(support::FileName const & from, support::FileName const & to,
|
2006-11-13 10:27:57 +00:00
|
|
|
string const & latex, unsigned long int mode) const
|
2004-10-26 18:39:13 +00:00
|
|
|
{
|
|
|
|
if (command_.empty())
|
2006-11-13 10:27:57 +00:00
|
|
|
return Mover::do_copy(from, to, latex, mode);
|
|
|
|
|
|
|
|
if (mode != (unsigned long int)-1) {
|
2006-11-26 21:30:39 +00:00
|
|
|
std::ofstream ofs(to.toFilesystemEncoding().c_str(), ios::binary | ios::out | ios::trunc);
|
2006-11-13 10:27:57 +00:00
|
|
|
if (!ofs)
|
|
|
|
return false;
|
|
|
|
ofs.close();
|
2006-11-14 16:28:44 +00:00
|
|
|
if (!support::chmod(to, mode))
|
2006-11-13 10:27:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2004-10-26 18:39:13 +00:00
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
string command = support::libScriptSearch(command_);
|
2006-11-26 21:30:39 +00:00
|
|
|
command = support::subst(command, "$$i", from.toFilesystemEncoding());
|
|
|
|
command = support::subst(command, "$$o", to.toFilesystemEncoding());
|
2004-11-01 08:50:42 +00:00
|
|
|
command = support::subst(command, "$$l", latex);
|
2004-10-26 18:39:13 +00:00
|
|
|
|
|
|
|
support::Systemcall one;
|
|
|
|
return one.startscript(support::Systemcall::Wait, command) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
bool SpecialisedMover::do_rename(support::FileName const & from, support::FileName const & to,
|
2006-04-05 23:56:29 +00:00
|
|
|
string const & latex) const
|
2004-10-26 18:39:13 +00:00
|
|
|
{
|
|
|
|
if (command_.empty())
|
2004-11-01 08:50:42 +00:00
|
|
|
return Mover::do_rename(from, to, latex);
|
2004-10-26 18:39:13 +00:00
|
|
|
|
2006-11-13 10:27:57 +00:00
|
|
|
if (!do_copy(from, to, latex, (unsigned long int)-1))
|
2004-10-26 18:39:13 +00:00
|
|
|
return false;
|
|
|
|
return support::unlink(from) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Movers::set(string const & fmt, string const & command)
|
|
|
|
{
|
|
|
|
specials_[fmt] = SpecialisedMover(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Mover const & Movers::operator()(string const & fmt) const
|
|
|
|
{
|
|
|
|
SpecialsMap::const_iterator const it = specials_.find(fmt);
|
2007-01-18 08:42:53 +00:00
|
|
|
if (it == specials_.end())
|
|
|
|
return default_;
|
|
|
|
return it->second;
|
2004-10-26 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const Movers::command(string const & fmt) const
|
|
|
|
{
|
|
|
|
SpecialsMap::const_iterator const it = specials_.find(fmt);
|
|
|
|
return (it == specials_.end()) ? string() : it->second.command();
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|