]> git.lyx.org Git - lyx.git/blob - src/Mover.cpp
rename MathArray into MathData
[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/filetools.h"
16 #include "support/lstrings.h"
17 #include "support/lyxlib.h"
18 #include "support/Systemcall.h"
19
20 #include <fstream>
21 #include <sstream>
22
23 using std::ios;
24 using std::string;
25
26 namespace lyx {
27
28 using support::quoteName;
29
30 bool Mover::copy(support::FileName const & from, support::FileName const & to,
31                  unsigned long int mode) const
32 {
33         return do_copy(from, to, to.absFilename(), mode);
34 }
35
36
37 bool Mover::do_copy(support::FileName const & from, support::FileName const & to,
38                     string const &, unsigned long int mode) const
39 {
40         return support::copy(from, to, mode);
41 }
42
43
44 bool Mover::rename(support::FileName const & from,
45                    support::FileName const & to) const
46 {
47         return do_rename(from, to, to.absFilename());
48 }
49
50
51 bool Mover::do_rename(support::FileName const & from, support::FileName const & to,
52                       string const &) const
53 {
54         return support::rename(from, to);
55 }
56
57
58 bool SpecialisedMover::do_copy(support::FileName const & from, support::FileName const & to,
59                                string const & latex, unsigned long int mode) const
60 {
61         if (command_.empty())
62                 return Mover::do_copy(from, to, latex, mode);
63
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;
71         }
72
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));
77
78         support::Systemcall one;
79         return one.startscript(support::Systemcall::Wait, command) == 0;
80 }
81
82
83 bool SpecialisedMover::do_rename(support::FileName const & from, support::FileName const & to,
84                                  string const & latex) const
85 {
86         if (command_.empty())
87                 return Mover::do_rename(from, to, latex);
88
89         if (!do_copy(from, to, latex, (unsigned long int)-1))
90                 return false;
91         return support::unlink(from) == 0;
92 }
93
94
95 void Movers::set(string const & fmt, string const & command)
96 {
97         specials_[fmt] = SpecialisedMover(command);
98 }
99
100
101 Mover const & Movers::operator()(string const & fmt) const
102 {
103         SpecialsMap::const_iterator const it = specials_.find(fmt);
104         if (it == specials_.end())
105                 return default_;
106         return  it->second;
107 }
108
109
110 string const Movers::command(string  const & fmt) const
111 {
112         SpecialsMap::const_iterator const it = specials_.find(fmt);
113         return (it == specials_.end()) ? string() : it->second.command();
114 }
115
116
117 } // namespace lyx