2 * \file GraphicsConverter.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming <a.leeming@ic.ac.uk>
12 #pragma implementation
15 #include "GraphicsConverter.h"
17 #include "converter.h"
21 #include "support/filetools.h"
22 #include "support/forkedcall.h"
23 #include "support/path.h"
25 #include <boost/bind.hpp>
31 string const move_file(string const & from_file, string const & to_file)
33 if (from_file == to_file)
36 ostringstream command;
37 command << "fromfile=" << from_file << "\n"
38 << "tofile=" << to_file << "\n\n"
39 << "'mv' -f ${fromfile} ${tofile}\n"
40 << "if [ $? -ne 0 ]; then\n"
41 << "\t'cp' -f ${fromfile} ${tofile}\n"
42 << "\tif [ $? -ne 0 ]; then\n"
45 << "\t'rm' -f ${fromfile}\n"
48 return command.str().c_str();
56 GConverter & GConverter::get()
58 static GConverter singleton;
63 bool GConverter::isReachable(string const & from_format_name,
64 string const & to_format_name) const
66 return converters.isReachable(from_format_name, to_format_name);
70 void GConverter::convert(string const & from_file, string const & to_file_base,
71 string const & from_format, string const & to_format,
72 SignalTypePtr on_finish)
74 // The conversion commands are stored in a stringstream
76 script << "#!/bin/sh\n";
78 bool const success = build_script(from_file, to_file_base,
79 from_format, to_format, script);
82 lyxerr[Debug::GRAPHICS]
83 << "Unable to build the conversion script" << std::endl;
84 on_finish->operator()(string());
88 lyxerr[Debug::GRAPHICS] << "Conversion script:\n\n"
89 << script.str().c_str() << "\n" << std::endl;
91 // Output the script to file.
92 static int counter = 0;
93 string const script_file = OnlyPath(to_file_base) + "lyxconvert" +
94 tostr(counter++) + ".sh";
96 std::ofstream fs(script_file.c_str());
98 // Unable to output the conversion script to file.
99 on_finish->operator()(string());
103 fs << script.str().c_str();
106 // Create a dummy command for ease of understanding of the
107 // list of forked processes.
108 // Note that 'sh ' is absolutely essential, or execvp will fail.
109 string const script_command =
110 "sh " + script_file + " " +
111 OnlyFilename(from_file) + " " + to_format;
113 string const to_file =
114 ChangeExtension(to_file_base, formats.extension(to_format));
116 // Launch the conversion process.
117 ConvProcessPtr shared_ptr;
118 shared_ptr.reset(new ConvProcess(script_file, script_command,
119 to_file, on_finish));
120 all_processes_.push_back(shared_ptr);
126 typedef boost::shared_ptr<ConvProcess> ConvProcessPtr;
130 Find_Ptr(ConvProcess * ptr) : ptr_(ptr) {}
132 bool operator()(ConvProcessPtr const & ptr)
134 return ptr.get() == ptr_;
144 void GConverter::erase(ConvProcess * process)
146 std::list<ConvProcessPtr>::iterator begin = all_processes_.begin();
147 std::list<ConvProcessPtr>::iterator end = all_processes_.end();
148 std::list<ConvProcessPtr>::iterator it =
149 std::find_if(begin, end, Find_Ptr(process));
154 all_processes_.erase(it);
158 bool GConverter::build_script(string const & from_file,
159 string const & to_file_base,
160 string const & from_format,
161 string const & to_format,
162 ostringstream & script) const
164 typedef Converters::EdgePath EdgePath;
166 string const to_file = ChangeExtension(to_file_base,
167 formats.extension(to_format));
169 if (from_format == to_format) {
170 script << move_file(QuoteName(from_file), QuoteName(to_file));
174 EdgePath edgepath = converters.getPath(from_format, to_format);
176 if (edgepath.empty()) {
180 // Create a temporary base file-name for all intermediate steps.
181 // Remember to remove the temp file because we only want the name...
182 static int counter = 0;
183 string const tmp = "gconvert" + tostr(counter++);
184 string const to_base = lyx::tempName(string(), tmp);
185 lyx::unlink(to_base);
187 string outfile = from_file;
189 // The conversion commands may contain these tokens that need to be
190 // changed to infile, infile_base, outfile respectively.
191 string const token_from("$$i");
192 string const token_base("$$b");
193 string const token_to("$$o");
195 EdgePath::const_iterator it = edgepath.begin();
196 EdgePath::const_iterator end = edgepath.end();
197 for (; it != end; ++it) {
198 Converter const & conv = converters.get(*it);
200 // Build the conversion command
201 string const infile = outfile;
202 string const infile_base = ChangeExtension(infile, string());
203 outfile = ChangeExtension(to_base, conv.To->extension());
205 // Store these names in the shell script
206 script << "infile=" << QuoteName(infile) << '\n'
207 << "infile_base=" << QuoteName(infile_base) << '\n'
208 << "outfile=" << QuoteName(outfile) << '\n';
210 string command = conv.command;
211 command = subst(command, token_from, "${infile}");
212 command = subst(command, token_base, "${infile_base}");
213 command = subst(command, token_to, "${outfile}");
215 // Store in the shell script
216 script << "\n" << command << "\n\n";
218 // Test that this was successful. If not, remove
219 // ${outfile} and exit the shell script
220 script << "if [ $? -ne 0 ]; then\n"
221 << "\t'rm' -f ${outfile}\n"
225 // Test that the outfile exists.
226 // ImageMagick's convert will often create ${outfile}.0,
228 // If this occurs, move ${outfile}.0 to ${outfile}
229 // and delete ${outfile}.?
230 script << "if [ ! -f ${outfile} ]; then\n"
231 << "\tif [ -f ${outfile}.0 ]; then\n"
232 << "\t\t'mv' -f ${outfile}.0 ${outfile}\n"
233 << "\t\t'rm' -f ${outfile}.?\n"
239 // Delete the infile, if it isn't the original, from_file.
240 if (infile != from_file) {
241 script << "'rm' -f ${infile}\n\n";
245 // Move the final outfile to to_file
246 script << move_file("${outfile}", QuoteName(to_file));
252 ConvProcess::ConvProcess(string const & script_file,
253 string const & script_command,
254 string const & to_file, SignalTypePtr on_finish)
255 : script_file_(script_file), to_file_(to_file), on_finish_(on_finish)
257 Forkedcall::SignalTypePtr convert_ptr;
258 convert_ptr.reset(new Forkedcall::SignalType);
260 convert_ptr->connect(boost::bind(&ConvProcess::converted, this, _1, _2, _3));
263 int retval = call.startscript(script_command, convert_ptr);
265 // Unable to even start the script, so clean-up the mess!
266 converted(string(), 0, 1);
271 void ConvProcess::converted(string const &/* cmd */,
272 pid_t /* pid */, int retval)
274 // Clean-up behind ourselves
275 lyx::unlink(script_file_);
278 lyx::unlink(to_file_);
282 if (on_finish_.get()) {
283 on_finish_->operator()(to_file_);
286 grfx::GConverter::get().erase(this);