]> git.lyx.org Git - lyx.git/blob - src/mover.C
The Movers patch.
[lyx.git] / src / mover.C
1 /**
2  * \file mover.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include "mover.h"
12
13 #include "support/filetools.h"
14 #include "support/lstrings.h"
15 #include "support/lyxlib.h"
16 #include "support/systemcall.h"
17
18 #include <sstream>
19
20 using std::ostringstream;
21 using std::string;
22
23 namespace support = lyx::support;
24
25 Movers movers;
26 Movers system_movers;
27
28
29 bool Mover::do_copy(string const & from, string const & to) const
30 {
31         return support::copy(from, to);
32 }
33
34
35 bool Mover::do_rename(string const & from, string const & to) const
36 {
37         return support::rename(from, to);
38 }
39
40
41 bool SpecialisedMover::do_copy(string const & from, string const & to) const
42 {
43         if (command_.empty())
44                 return Mover::do_copy(from, to);
45
46         string command = support::LibScriptSearch(command_);
47         command = support::subst(command, "$$i", from);
48         command = support::subst(command, "$$o", to);
49
50         support::Systemcall one;
51         return one.startscript(support::Systemcall::Wait, command) == 0;
52 }
53
54
55 bool SpecialisedMover::do_rename(string const & from, string const & to) const
56 {
57         if (command_.empty())
58                 return Mover::do_rename(from, to);
59
60         if (!do_copy(from, to))
61                 return false;
62         return support::unlink(from) == 0;
63 }
64
65
66 void Movers::set(string const & fmt, string const & command)
67 {
68         specials_[fmt] = SpecialisedMover(command);
69 }
70
71
72 Mover const & Movers::operator()(string const & fmt) const
73 {
74         SpecialsMap::const_iterator const it = specials_.find(fmt);
75         return (it == specials_.end()) ? default_ : it->second;
76 }
77
78
79 string const Movers::command(string  const & fmt) const
80 {
81         SpecialsMap::const_iterator const it = specials_.find(fmt);
82         return (it == specials_.end()) ? string() : it->second.command();
83 }