]> git.lyx.org Git - lyx.git/blob - src/mover.C
new \lyxline difinition, fixes also bug 1988:
[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 <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 bool Mover::copy(support::FileName const & from, support::FileName const & to,
29                  unsigned long int mode) const
30 {
31         return do_copy(from, to, to.absFilename(), mode);
32 }
33
34
35 bool Mover::do_copy(support::FileName const & from, support::FileName const & to,
36                     string const &, unsigned long int mode) const
37 {
38         return support::copy(from, to, mode);
39 }
40
41
42 bool Mover::rename(support::FileName const & from,
43                    support::FileName const & to) const
44 {
45         return do_rename(from, to, to.absFilename());
46 }
47
48
49 bool Mover::do_rename(support::FileName const & from, support::FileName const & to,
50                       string const &) const
51 {
52         return support::rename(from, to);
53 }
54
55
56 bool SpecialisedMover::do_copy(support::FileName const & from, support::FileName const & to,
57                                string const & latex, unsigned long int mode) const
58 {
59         if (command_.empty())
60                 return Mover::do_copy(from, to, latex, mode);
61
62         if (mode != (unsigned long int)-1) {
63                 std::ofstream ofs(to.toFilesystemEncoding().c_str(), ios::binary | ios::out | ios::trunc);
64                 if (!ofs)
65                         return false;
66                 ofs.close();
67                 if (!support::chmod(to, mode))
68                         return false;
69         }
70
71         string command = support::libScriptSearch(command_);
72         command = support::subst(command, "$$i", from.toFilesystemEncoding());
73         command = support::subst(command, "$$o", to.toFilesystemEncoding());
74         command = support::subst(command, "$$l", latex);
75
76         support::Systemcall one;
77         return one.startscript(support::Systemcall::Wait, command) == 0;
78 }
79
80
81 bool SpecialisedMover::do_rename(support::FileName const & from, support::FileName const & to,
82                                  string const & latex) const
83 {
84         if (command_.empty())
85                 return Mover::do_rename(from, to, latex);
86
87         if (!do_copy(from, to, latex, (unsigned long int)-1))
88                 return false;
89         return support::unlink(from) == 0;
90 }
91
92
93 void Movers::set(string const & fmt, string const & command)
94 {
95         specials_[fmt] = SpecialisedMover(command);
96 }
97
98
99 Mover const & Movers::operator()(string const & fmt) const
100 {
101         SpecialsMap::const_iterator const it = specials_.find(fmt);
102         if (it == specials_.end())
103                 return default_;
104         return  it->second;
105 }
106
107
108 string const Movers::command(string  const & fmt) const
109 {
110         SpecialsMap::const_iterator const it = specials_.find(fmt);
111         return (it == specials_.end()) ? string() : it->second.command();
112 }
113
114
115 } // namespace lyx