LyX 1.5.0 is released
[lyx.git] / src / Mover.cpp
blobcfe203461779474672e4b8893b43c6717496f27e
1 /**
2 * \file Mover.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "Mover.h"
15 #include "support/filetools.h"
16 #include "support/lstrings.h"
17 #include "support/lyxlib.h"
18 #include "support/Systemcall.h"
20 #include <fstream>
21 #include <sstream>
23 using std::ios;
24 using std::string;
26 namespace lyx {
28 using support::quoteName;
30 bool Mover::copy(support::FileName const & from, support::FileName const & to,
31 unsigned long int mode) const
33 return do_copy(from, to, to.absFilename(), mode);
37 bool Mover::do_copy(support::FileName const & from, support::FileName const & to,
38 string const &, unsigned long int mode) const
40 return support::copy(from, to, mode);
44 bool Mover::rename(support::FileName const & from,
45 support::FileName const & to) const
47 return do_rename(from, to, to.absFilename());
51 bool Mover::do_rename(support::FileName const & from, support::FileName const & to,
52 string const &) const
54 return support::rename(from, to);
58 bool SpecialisedMover::do_copy(support::FileName const & from, support::FileName const & to,
59 string const & latex, unsigned long int mode) const
61 if (command_.empty())
62 return Mover::do_copy(from, to, latex, mode);
64 if (mode != (unsigned long int)-1) {
65 std::ofstream ofs(to.toFilesystemEncoding().c_str(), ios::binary | ios::out | ios::trunc);
66 if (!ofs)
67 return false;
68 ofs.close();
69 if (!support::chmod(to, mode))
70 return false;
73 string command = support::libScriptSearch(command_);
74 command = support::subst(command, "$$i", quoteName(from.toFilesystemEncoding()));
75 command = support::subst(command, "$$o", quoteName(to.toFilesystemEncoding()));
76 command = support::subst(command, "$$l", quoteName(latex));
78 support::Systemcall one;
79 return one.startscript(support::Systemcall::Wait, command) == 0;
83 bool SpecialisedMover::do_rename(support::FileName const & from, support::FileName const & to,
84 string const & latex) const
86 if (command_.empty())
87 return Mover::do_rename(from, to, latex);
89 if (!do_copy(from, to, latex, (unsigned long int)-1))
90 return false;
91 return support::unlink(from) == 0;
95 void Movers::set(string const & fmt, string const & command)
97 specials_[fmt] = SpecialisedMover(command);
101 Mover const & Movers::operator()(string const & fmt) const
103 SpecialsMap::const_iterator const it = specials_.find(fmt);
104 if (it == specials_.end())
105 return default_;
106 return it->second;
110 string const Movers::command(string const & fmt) const
112 SpecialsMap::const_iterator const it = specials_.find(fmt);
113 return (it == specials_.end()) ? string() : it->second.command();
117 } // namespace lyx