]> git.lyx.org Git - lyx.git/blob - src/Mover.cpp
055c293f1615ba144ded1cad5d5f385622e5c5c2
[lyx.git] / src / Mover.cpp
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.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Mover.h"
14
15 #include "support/FileName.h"
16 #include "support/filetools.h"
17 #include "support/lstrings.h"
18 #include "support/Systemcall.h"
19
20 #include <fstream>
21 #include <sstream>
22
23 using namespace std;
24 using namespace lyx::support;
25
26 namespace lyx {
27
28
29 bool Mover::copy(FileName const & from, FileName const & to) const
30 {
31         return from.copyTo(to);
32 }
33
34
35 bool Mover::rename(FileName const & from,
36                    FileName const & to) const
37 {
38         return do_rename(from, to, to.absFilename());
39 }
40
41
42 bool Mover::do_rename(FileName const & from, FileName const & to,
43                       string const &) const
44 {
45         return from.moveTo(to);
46 }
47
48
49 bool SpecialisedMover::do_copy(FileName const & from, FileName const & to,
50                                string const & latex) const
51 {
52         if (command_.empty())
53                 return Mover::copy(from, to);
54
55         string command = libScriptSearch(command_);
56         command = subst(command, "$$i", quoteName(from.toFilesystemEncoding()));
57         command = subst(command, "$$o", quoteName(to.toFilesystemEncoding()));
58         command = subst(command, "$$l", quoteName(latex));
59
60         Systemcall one;
61         return one.startscript(Systemcall::Wait, command) == 0;
62 }
63
64
65 bool SpecialisedMover::do_rename(FileName const & from, FileName const & to,
66                                  string const & latex) const
67 {
68         if (command_.empty())
69                 return Mover::do_rename(from, to, latex);
70
71         if (!do_copy(from, to, latex))
72                 return false;
73         return from.removeFile();
74 }
75
76
77 void Movers::set(string const & fmt, string const & command)
78 {
79         specials_[fmt] = SpecialisedMover(command);
80 }
81
82
83 Mover const & Movers::operator()(string const & fmt) const
84 {
85         SpecialsMap::const_iterator const it = specials_.find(fmt);
86         if (it == specials_.end())
87                 return default_;
88         return  it->second;
89 }
90
91
92 string const Movers::command(string  const & fmt) const
93 {
94         SpecialsMap::const_iterator const it = specials_.find(fmt);
95         return (it == specials_.end()) ? string() : it->second.command();
96 }
97
98
99 } // namespace lyx