]> git.lyx.org Git - lyx.git/blob - src/Converter.cpp
avoid float-conversion warning and simplify size computation
[lyx.git] / src / Converter.cpp
1 /**
2  * \file Converter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Converter.h"
14
15 #include "Buffer.h"
16 #include "buffer_funcs.h"
17 #include "BufferParams.h"
18 #include "ConverterCache.h"
19 #include "Encoding.h"
20 #include "ErrorList.h"
21 #include "Format.h"
22 #include "Language.h"
23 #include "LaTeX.h"
24 #include "Mover.h"
25
26 #include "frontends/alert.h"
27
28 #include "support/debug.h"
29 #include "support/FileNameList.h"
30 #include "support/filetools.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
33 #include "support/os.h"
34 #include "support/Package.h"
35 #include "support/PathChanger.h"
36 #include "support/Systemcall.h"
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43 namespace Alert = lyx::frontend::Alert;
44
45
46 namespace {
47
48 string const token_from("$$i");
49 string const token_base("$$b");
50 string const token_to("$$o");
51 string const token_path("$$p");
52 string const token_orig_path("$$r");
53 string const token_orig_from("$$f");
54 string const token_encoding("$$e");
55 string const token_latex_encoding("$$E");
56
57
58 string const add_options(string const & command, string const & options)
59 {
60         string head;
61         string const tail = split(command, head, ' ');
62         return head + ' ' + options + ' ' + tail;
63 }
64
65
66 string const dvipdfm_options(BufferParams const & bp)
67 {
68         string result;
69
70         if (bp.papersize != PAPER_CUSTOM) {
71                 string const paper_size = bp.paperSizeName(BufferParams::DVIPDFM);
72                 if (!paper_size.empty())
73                         result = "-p "+ paper_size;
74
75                 if (bp.orientation == ORIENTATION_LANDSCAPE)
76                         result += " -l";
77         }
78
79         return result;
80 }
81
82
83 class ConverterEqual {
84 public:
85         ConverterEqual(string const & from, string const & to)
86                 : from_(from), to_(to) {}
87         bool operator()(Converter const & c) const {
88                 return c.from() == from_ && c.to() == to_;
89         }
90 private:
91         string const from_;
92         string const to_;
93 };
94
95 } // namespace anon
96
97
98 Converter::Converter(string const & f, string const & t,
99                      string const & c, string const & l)
100         : from_(f), to_(t), command_(c), flags_(l),
101           From_(0), To_(0), latex_(false), xml_(false),
102           need_aux_(false), nice_(false)
103 {}
104
105
106 void Converter::readFlags()
107 {
108         string flag_list(flags_);
109         while (!flag_list.empty()) {
110                 string flag_name, flag_value;
111                 flag_list = split(flag_list, flag_value, ',');
112                 flag_value = split(flag_value, flag_name, '=');
113                 if (flag_name == "latex") {
114                         latex_ = true;
115                         latex_flavor_ = flag_value.empty() ?
116                                 "latex" : flag_value;
117                 } else if (flag_name == "xml")
118                         xml_ = true;
119                 else if (flag_name == "needaux")
120                         need_aux_ = true;
121                 else if (flag_name == "resultdir")
122                         result_dir_ = (flag_value.empty())
123                                 ? token_base : flag_value;
124                 else if (flag_name == "resultfile")
125                         result_file_ = flag_value;
126                 else if (flag_name == "parselog")
127                         parselog_ = flag_value;
128                 else if (flag_name == "nice")
129                         nice_ = true;
130         }
131         if (!result_dir_.empty() && result_file_.empty())
132                 result_file_ = "index." + formats.extension(to_);
133         //if (!contains(command, token_from))
134         //      latex = true;
135 }
136
137
138 Converter const * Converters::getConverter(string const & from,
139                                             string const & to) const
140 {
141         ConverterList::const_iterator const cit =
142                 find_if(converterlist_.begin(), converterlist_.end(),
143                         ConverterEqual(from, to));
144         if (cit != converterlist_.end())
145                 return &(*cit);
146         else
147                 return 0;
148 }
149
150
151 int Converters::getNumber(string const & from, string const & to) const
152 {
153         ConverterList::const_iterator const cit =
154                 find_if(converterlist_.begin(), converterlist_.end(),
155                         ConverterEqual(from, to));
156         if (cit != converterlist_.end())
157                 return distance(converterlist_.begin(), cit);
158         else
159                 return -1;
160 }
161
162
163 void Converters::add(string const & from, string const & to,
164                      string const & command, string const & flags)
165 {
166         formats.add(from);
167         formats.add(to);
168         ConverterList::iterator it = find_if(converterlist_.begin(),
169                                              converterlist_.end(),
170                                              ConverterEqual(from , to));
171
172         Converter converter(from, to, command, flags);
173         if (it != converterlist_.end() && !flags.empty() && flags[0] == '*') {
174                 converter = *it;
175                 converter.setCommand(command);
176                 converter.setFlags(flags);
177         }
178         converter.readFlags();
179
180         // The latex_command is used to update the .aux file when running
181         // a converter that uses it.
182         if (converter.latex()) {
183                 if (latex_command_.empty() ||
184                     converter.latex_flavor() == "latex")
185                         latex_command_ = subst(command, token_from, "");
186                 if (dvilualatex_command_.empty() ||
187                     converter.latex_flavor() == "dvilualatex")
188                         dvilualatex_command_ = subst(command, token_from, "");
189                 if (lualatex_command_.empty() ||
190                     converter.latex_flavor() == "lualatex")
191                         lualatex_command_ = subst(command, token_from, "");
192                 if (pdflatex_command_.empty() ||
193                     converter.latex_flavor() == "pdflatex")
194                         pdflatex_command_ = subst(command, token_from, "");
195                 if (xelatex_command_.empty() ||
196                     converter.latex_flavor() == "xelatex")
197                         xelatex_command_ = subst(command, token_from, "");
198         }
199
200         if (it == converterlist_.end()) {
201                 converterlist_.push_back(converter);
202         } else {
203                 converter.setFrom(it->From());
204                 converter.setTo(it->To());
205                 *it = converter;
206         }
207 }
208
209
210 void Converters::erase(string const & from, string const & to)
211 {
212         ConverterList::iterator const it =
213                 find_if(converterlist_.begin(),
214                         converterlist_.end(),
215                         ConverterEqual(from, to));
216         if (it != converterlist_.end())
217                 converterlist_.erase(it);
218 }
219
220
221 // This method updates the pointers From and To in all the converters.
222 // The code is not very efficient, but it doesn't matter as the number
223 // of formats and converters is small.
224 // Furthermore, this method is called only on startup, or after
225 // adding/deleting a format in FormPreferences (the latter calls can be
226 // eliminated if the formats in the Formats class are stored using a map or
227 // a list (instead of a vector), but this will cause other problems).
228 void Converters::update(Formats const & formats)
229 {
230         ConverterList::iterator it = converterlist_.begin();
231         ConverterList::iterator end = converterlist_.end();
232         for (; it != end; ++it) {
233                 it->setFrom(formats.getFormat(it->from()));
234                 it->setTo(formats.getFormat(it->to()));
235         }
236 }
237
238
239 // This method updates the pointers From and To in the last converter.
240 // It is called when adding a new converter in FormPreferences
241 void Converters::updateLast(Formats const & formats)
242 {
243         if (converterlist_.begin() != converterlist_.end()) {
244                 ConverterList::iterator it = converterlist_.end() - 1;
245                 it->setFrom(formats.getFormat(it->from()));
246                 it->setTo(formats.getFormat(it->to()));
247         }
248 }
249
250
251 OutputParams::FLAVOR Converters::getFlavor(Graph::EdgePath const & path,
252                                            Buffer const * buffer)
253 {
254         for (Graph::EdgePath::const_iterator cit = path.begin();
255              cit != path.end(); ++cit) {
256                 Converter const & conv = converterlist_[*cit];
257                 if (conv.latex()) {
258                         if (conv.latex_flavor() == "latex")
259                                 return OutputParams::LATEX;
260                         if (conv.latex_flavor() == "xelatex")
261                                 return OutputParams::XETEX;
262                         if (conv.latex_flavor() == "lualatex")
263                                 return OutputParams::LUATEX;
264                         if (conv.latex_flavor() == "dvilualatex")
265                                 return OutputParams::DVILUATEX;
266                         if (conv.latex_flavor() == "pdflatex")
267                                 return OutputParams::PDFLATEX;
268                 }
269                 if (conv.xml())
270                         return OutputParams::XML;
271         }
272         return buffer ? buffer->params().getOutputFlavor()
273                       : OutputParams::LATEX;
274 }
275
276
277 bool Converters::convert(Buffer const * buffer,
278                          FileName const & from_file, FileName const & to_file,
279                          FileName const & orig_from,
280                          string const & from_format, string const & to_format,
281                          ErrorList & errorList, int conversionflags)
282 {
283         if (from_format == to_format)
284                 return move(from_format, from_file, to_file, false);
285
286         if ((conversionflags & try_cache) &&
287             ConverterCache::get().inCache(orig_from, to_format))
288                 return ConverterCache::get().copy(orig_from, to_format, to_file);
289
290         Graph::EdgePath edgepath = getPath(from_format, to_format);
291         if (edgepath.empty()) {
292                 if (conversionflags & try_default) {
293                         // if no special converter defined, then we take the
294                         // default one from ImageMagic.
295                         string const from_ext = from_format.empty() ?
296                                 getExtension(from_file.absFileName()) :
297                                 formats.extension(from_format);
298                         string const to_ext = formats.extension(to_format);
299                         string const command =
300                                 os::python() + ' ' +
301                                 quoteName(libFileSearch("scripts", "convertDefault.py").toFilesystemEncoding()) +
302                                 ' ' + from_ext + ' ' +
303                                 quoteName(from_file.toFilesystemEncoding()) +
304                                 ' ' + to_ext + ' ' +
305                                 quoteName(to_file.toFilesystemEncoding());
306                         LYXERR(Debug::FILES, "No converter defined! "
307                                    "I use convertDefault.py:\n\t" << command);
308                         Systemcall one;
309                         one.startscript(Systemcall::Wait, command,
310                                         buffer ? buffer->filePath() : string(),
311                                         buffer ? buffer->layoutPos() : string());
312                         if (to_file.isReadableFile()) {
313                                 if (conversionflags & try_cache)
314                                         ConverterCache::get().add(orig_from,
315                                                         to_format, to_file);
316                                 return true;
317                         }
318                 }
319
320                 // only warn once per session and per file type
321                 static std::map<string, string> warned;
322                 if (warned.find(from_format) != warned.end() && warned.find(from_format)->second == to_format) {
323                         return false;
324                 }
325                 warned.insert(make_pair(from_format, to_format));
326
327                 Alert::error(_("Cannot convert file"),
328                              bformat(_("No information for converting %1$s "
329                                                     "format files to %2$s.\n"
330                                                     "Define a converter in the preferences."),
331                                                         from_ascii(from_format), from_ascii(to_format)));
332                 return false;
333         }
334
335         // buffer is only invalid for importing, and then runparams is not
336         // used anyway.
337         OutputParams runparams(buffer ? &buffer->params().encoding() : 0);
338         runparams.flavor = getFlavor(edgepath, buffer);
339
340         if (buffer) {
341                 runparams.use_japanese = buffer->params().bufferFormat() == "platex";
342                 runparams.use_indices = buffer->params().use_indices;
343                 runparams.bibtex_command = (buffer->params().bibtex_command == "default") ?
344                         string() : buffer->params().bibtex_command;
345                 runparams.index_command = (buffer->params().index_command == "default") ?
346                         string() : buffer->params().index_command;
347                 runparams.document_language = buffer->params().language->babel();
348         }
349
350         // Some converters (e.g. lilypond) can only output files to the
351         // current directory, so we need to change the current directory.
352         // This has the added benefit that all other files that may be
353         // generated by the converter are deleted when LyX closes and do not
354         // clutter the real working directory.
355         // FIXME: This does not work if path is an UNC path on windows
356         //        (bug 6127).
357         string const path(onlyPath(from_file.absFileName()));
358         // Prevent the compiler from optimizing away p
359         FileName pp(path);
360         PathChanger p(pp);
361
362         // empty the error list before any new conversion takes place.
363         errorList.clear();
364
365         bool run_latex = false;
366         string from_base = changeExtension(from_file.absFileName(), "");
367         string to_base = changeExtension(to_file.absFileName(), "");
368         FileName infile;
369         FileName outfile = from_file;
370         for (Graph::EdgePath::const_iterator cit = edgepath.begin();
371              cit != edgepath.end(); ++cit) {
372                 Converter const & conv = converterlist_[*cit];
373                 bool dummy = conv.To()->dummy() && conv.to() != "program";
374                 if (!dummy) {
375                         LYXERR(Debug::FILES, "Converting from  "
376                                << conv.from() << " to " << conv.to());
377                 }
378                 infile = outfile;
379                 outfile = FileName(conv.result_file().empty()
380                         ? changeExtension(from_file.absFileName(), conv.To()->extension())
381                         : addName(subst(conv.result_dir(),
382                                         token_base, from_base),
383                                   subst(conv.result_file(),
384                                         token_base, onlyFileName(from_base))));
385
386                 // if input and output files are equal, we use a
387                 // temporary file as intermediary (JMarc)
388                 FileName real_outfile;
389                 if (!conv.result_file().empty())
390                         real_outfile = FileName(changeExtension(from_file.absFileName(),
391                                 conv.To()->extension()));
392                 if (outfile == infile) {
393                         real_outfile = infile;
394                         // when importing, a buffer does not necessarily exist
395                         if (buffer)
396                                 outfile = FileName(addName(buffer->temppath(), "tmpfile.out"));
397                         else
398                                 outfile = FileName(addName(package().temp_dir().absFileName(),
399                                                    "tmpfile.out"));
400                 }
401
402                 if (conv.latex()) {
403                         run_latex = true;
404                         string command = conv.command();
405                         command = subst(command, token_from, "");
406                         command = subst(command, token_latex_encoding, buffer ?
407                                 buffer->params().encoding().latexName() : string());
408                         LYXERR(Debug::FILES, "Running " << command);
409                         if (!runLaTeX(*buffer, command, runparams, errorList))
410                                 return false;
411                 } else {
412                         if (conv.need_aux() && !run_latex) {
413                                 string command;
414                                 switch (runparams.flavor) {
415                                 case OutputParams::DVILUATEX:
416                                         command = dvilualatex_command_;
417                                         break;
418                                 case OutputParams::LUATEX:
419                                         command = lualatex_command_;
420                                         break;
421                                 case OutputParams::PDFLATEX:
422                                         command = pdflatex_command_;
423                                         break;
424                                 case OutputParams::XETEX:
425                                         command = xelatex_command_;
426                                         break;
427                                 default:
428                                         command = latex_command_;
429                                         break;
430                                 }
431                                 if (!command.empty()) {
432                                         LYXERR(Debug::FILES, "Running "
433                                                 << command
434                                                 << " to update aux file");
435                                         if (!runLaTeX(*buffer, command,
436                                                       runparams, errorList))
437                                                 return false;
438                                 }
439                         }
440
441                         // FIXME UNICODE
442                         string const infile2 =
443                                 to_utf8(makeRelPath(from_utf8(infile.absFileName()), from_utf8(path)));
444                         string const outfile2 =
445                                 to_utf8(makeRelPath(from_utf8(outfile.absFileName()), from_utf8(path)));
446
447                         string command = conv.command();
448                         command = subst(command, token_from, quoteName(infile2));
449                         command = subst(command, token_base, quoteName(from_base));
450                         command = subst(command, token_to, quoteName(outfile2));
451                         command = subst(command, token_path, quoteName(onlyPath(infile.absFileName())));
452                         command = subst(command, token_orig_path, quoteName(onlyPath(orig_from.absFileName())));
453                         command = subst(command, token_orig_from, quoteName(onlyFileName(orig_from.absFileName())));
454                         command = subst(command, token_encoding, buffer ? buffer->params().encoding().iconvName() : string());
455
456                         if (!conv.parselog().empty())
457                                 command += " 2> " + quoteName(infile2 + ".out");
458
459                         // it is not actually not necessary to test for buffer here,
460                         // but it pleases coverity.
461                         if (buffer && conv.from() == "dvi" && conv.to() == "ps")
462                                 command = add_options(command,
463                                                       buffer->params().dvips_options());
464                         else if (buffer && conv.from() == "dvi" && prefixIs(conv.to(), "pdf"))
465                                 command = add_options(command,
466                                                       dvipdfm_options(buffer->params()));
467
468                         LYXERR(Debug::FILES, "Calling " << command);
469                         if (buffer)
470                                 buffer->message(_("Executing command: ")
471                                 + from_utf8(command));
472
473                         Systemcall one;
474                         int res;
475                         if (dummy) {
476                                 res = one.startscript(Systemcall::DontWait,
477                                         to_filesystem8bit(from_utf8(command)),
478                                         buffer ? buffer->filePath() : string(),
479                                         buffer ? buffer->layoutPos() : string());
480                                 // We're not waiting for the result, so we can't do anything
481                                 // else here.
482                         } else {
483                                 res = one.startscript(Systemcall::Wait,
484                                                 to_filesystem8bit(from_utf8(command)),
485                                                 buffer ? buffer->filePath()
486                                                        : string(),
487                                                 buffer ? buffer->layoutPos()
488                                                        : string());
489                                 if (!real_outfile.empty()) {
490                                         Mover const & mover = getMover(conv.to());
491                                         if (!mover.rename(outfile, real_outfile))
492                                                 res = -1;
493                                         else
494                                                 LYXERR(Debug::FILES, "renaming file " << outfile
495                                                         << " to " << real_outfile);
496                                         // Finally, don't forget to tell any future
497                                         // converters to use the renamed file...
498                                         outfile = real_outfile;
499                                 }
500
501                                 if (!conv.parselog().empty()) {
502                                         string const logfile =  infile2 + ".log";
503                                         string const command2 = conv.parselog() +
504                                                 " < " + quoteName(infile2 + ".out") +
505                                                 " > " + quoteName(logfile);
506                                         one.startscript(Systemcall::Wait,
507                                                 to_filesystem8bit(from_utf8(command2)),
508                                                 buffer->filePath(),
509                                                 buffer->layoutPos());
510                                         if (!scanLog(*buffer, command, makeAbsPath(logfile, path), errorList))
511                                                 return false;
512                                 }
513                         }
514
515                         if (res) {
516                                 if (conv.to() == "program") {
517                                         Alert::error(_("Build errors"),
518                                                 _("There were errors during the build process."));
519                                 } else {
520 // FIXME: this should go out of here. For example, here we cannot say if
521 // it is a document (.lyx) or something else. Same goes for elsewhere.
522                                         Alert::error(_("Cannot convert file"),
523                                                 bformat(_("An error occurred while running:\n%1$s"),
524                                                 wrapParas(from_utf8(command))));
525                                 }
526                                 return false;
527                         }
528                 }
529         }
530
531         Converter const & conv = converterlist_[edgepath.back()];
532         if (conv.To()->dummy())
533                 return true;
534
535         if (!conv.result_dir().empty()) {
536                 // The converter has put the file(s) in a directory.
537                 // In this case we ignore the given to_file.
538                 if (from_base != to_base) {
539                         string const from = subst(conv.result_dir(),
540                                             token_base, from_base);
541                         string const to = subst(conv.result_dir(),
542                                           token_base, to_base);
543                         Mover const & mover = getMover(conv.from());
544                         if (!mover.rename(FileName(from), FileName(to))) {
545                                 Alert::error(_("Cannot convert file"),
546                                         bformat(_("Could not move a temporary directory from %1$s to %2$s."),
547                                                 from_utf8(from), from_utf8(to)));
548                                 return false;
549                         }
550                 }
551                 return true;
552         } else {
553                 if (conversionflags & try_cache)
554                         ConverterCache::get().add(orig_from, to_format, outfile);
555                 return move(conv.to(), outfile, to_file, conv.latex());
556         }
557 }
558
559
560 bool Converters::move(string const & fmt,
561                       FileName const & from, FileName const & to, bool copy)
562 {
563         if (from == to)
564                 return true;
565
566         bool no_errors = true;
567         string const path = onlyPath(from.absFileName());
568         string const base = onlyFileName(removeExtension(from.absFileName()));
569         string const to_base = removeExtension(to.absFileName());
570         string const to_extension = getExtension(to.absFileName());
571
572         support::FileNameList const files = FileName(path).dirList(getExtension(from.absFileName()));
573         for (support::FileNameList::const_iterator it = files.begin();
574              it != files.end(); ++it) {
575                 string const from2 = it->absFileName();
576                 string const file2 = onlyFileName(from2);
577                 if (prefixIs(file2, base)) {
578                         string const to2 = changeExtension(
579                                 to_base + file2.substr(base.length()),
580                                 to_extension);
581                         LYXERR(Debug::FILES, "moving " << from2 << " to " << to2);
582
583                         Mover const & mover = getMover(fmt);
584                         bool const moved = copy
585                                 ? mover.copy(*it, FileName(to2))
586                                 : mover.rename(*it, FileName(to2));
587                         if (!moved && no_errors) {
588                                 Alert::error(_("Cannot convert file"),
589                                         bformat(copy ?
590                                                 _("Could not copy a temporary file from %1$s to %2$s.") :
591                                                 _("Could not move a temporary file from %1$s to %2$s."),
592                                                 from_utf8(from2), from_utf8(to2)));
593                                 no_errors = false;
594                         }
595                 }
596         }
597         return no_errors;
598 }
599
600
601 bool Converters::formatIsUsed(string const & format)
602 {
603         ConverterList::const_iterator cit = converterlist_.begin();
604         ConverterList::const_iterator end = converterlist_.end();
605         for (; cit != end; ++cit) {
606                 if (cit->from() == format || cit->to() == format)
607                         return true;
608         }
609         return false;
610 }
611
612
613 bool Converters::scanLog(Buffer const & buffer, string const & /*command*/,
614                          FileName const & filename, ErrorList & errorList)
615 {
616         OutputParams runparams(0);
617         runparams.flavor = OutputParams::LATEX;
618         LaTeX latex("", runparams, filename);
619         TeXErrors terr;
620         int const result = latex.scanLogFile(terr);
621
622         if (result & LaTeX::ERRORS)
623                 buffer.bufferErrors(terr, errorList);
624
625         return true;
626 }
627
628
629 namespace {
630
631 class ShowMessage
632         : public boost::signals::trackable {
633 public:
634         ShowMessage(Buffer const & b) : buffer_(b) {}
635         void operator()(docstring const & msg) const { buffer_.message(msg); }
636 private:
637         Buffer const & buffer_;
638 };
639
640 }
641
642
643 bool Converters::runLaTeX(Buffer const & buffer, string const & command,
644                           OutputParams const & runparams, ErrorList & errorList)
645 {
646         buffer.setBusy(true);
647         buffer.message(_("Running LaTeX..."));
648
649         // do the LaTeX run(s)
650         string const name = buffer.latexName();
651         LaTeX latex(command, runparams, FileName(makeAbsPath(name)),
652                     buffer.filePath(), buffer.layoutPos(),
653                     buffer.lastPreviewError());
654         TeXErrors terr;
655         ShowMessage show(buffer);
656         latex.message.connect(show);
657         int const result = latex.run(terr);
658
659         if (result & LaTeX::ERRORS)
660                 buffer.bufferErrors(terr, errorList);
661
662         if (!errorList.empty()) {
663           // We will show the LaTeX Errors GUI later which contains
664           // specific error messages so it would be repetitive to give
665           // e.g. the "finished with an error" dialog in addition.
666         }
667         else if (result & LaTeX::NO_LOGFILE) {
668                 docstring const str =
669                         bformat(_("LaTeX did not run successfully. "
670                                                "Additionally, LyX could not locate "
671                                                "the LaTeX log %1$s."), from_utf8(name));
672                 Alert::error(_("LaTeX failed"), str);
673         } else if (result & LaTeX::NONZERO_ERROR) {
674                 docstring const str =
675                         bformat(_( "The external program\n%1$s\n"
676                               "finished with an error. "
677                               "It is recommended you fix the cause of the external "
678                               "program's error (check the logs). "), from_utf8(command));
679                 Alert::error(_("LaTeX failed"), str);
680         } else if (result & LaTeX::NO_OUTPUT) {
681                 Alert::warning(_("Output is empty"),
682                                _("No output file was generated."));
683         }
684
685
686         buffer.setBusy(false);
687
688         int const ERROR_MASK =
689                         LaTeX::NO_LOGFILE |
690                         LaTeX::ERRORS |
691                         LaTeX::NO_OUTPUT;
692
693         return (result & ERROR_MASK) == 0;
694 }
695
696
697
698 void Converters::buildGraph()
699 {
700         // clear graph's data structures
701         G_.init(formats.size());
702         // each of the converters knows how to convert one format to another
703         // so, for each of them, we create an arrow on the graph, going from
704         // the one to the other
705         ConverterList::iterator it = converterlist_.begin();
706         ConverterList::iterator const end = converterlist_.end();
707         for (; it != end ; ++it) {
708                 int const from = formats.getNumber(it->from());
709                 int const to   = formats.getNumber(it->to());
710                 G_.addEdge(from, to);
711         }
712 }
713
714
715 vector<Format const *> const
716 Converters::intToFormat(vector<int> const & input)
717 {
718         vector<Format const *> result(input.size());
719
720         vector<int>::const_iterator it = input.begin();
721         vector<int>::const_iterator const end = input.end();
722         vector<Format const *>::iterator rit = result.begin();
723         for ( ; it != end; ++it, ++rit) {
724                 *rit = &formats.get(*it);
725         }
726         return result;
727 }
728
729
730 vector<Format const *> const
731 Converters::getReachableTo(string const & target, bool const clear_visited)
732 {
733         vector<int> const & reachablesto =
734                 G_.getReachableTo(formats.getNumber(target), clear_visited);
735
736         return intToFormat(reachablesto);
737 }
738
739
740 vector<Format const *> const
741 Converters::getReachable(string const & from, bool const only_viewable,
742                          bool const clear_visited, set<string> const & excludes)
743 {
744         set<int> excluded_numbers;
745
746         set<string>::const_iterator sit = excludes.begin();
747         set<string>::const_iterator const end = excludes.end();
748         for (; sit != end; ++sit)
749                 excluded_numbers.insert(formats.getNumber(*sit));
750
751         vector<int> const & reachables =
752                 G_.getReachable(formats.getNumber(from),
753                                 only_viewable,
754                                 clear_visited,
755                                 excluded_numbers);
756
757         return intToFormat(reachables);
758 }
759
760
761 bool Converters::isReachable(string const & from, string const & to)
762 {
763         return G_.isReachable(formats.getNumber(from),
764                               formats.getNumber(to));
765 }
766
767
768 Graph::EdgePath Converters::getPath(string const & from, string const & to)
769 {
770         return G_.getPath(formats.getNumber(from),
771                           formats.getNumber(to));
772 }
773
774
775 vector<Format const *> Converters::importableFormats()
776 {
777         vector<string> l = loaders();
778         vector<Format const *> result = getReachableTo(l[0], true);
779         vector<string>::const_iterator it = l.begin() + 1;
780         vector<string>::const_iterator en = l.end();
781         for (; it != en; ++it) {
782                 vector<Format const *> r = getReachableTo(*it, false);
783                 result.insert(result.end(), r.begin(), r.end());
784         }
785         return result;
786 }
787
788
789 vector<Format const *> Converters::exportableFormats(bool only_viewable)
790 {
791         vector<string> s = savers();
792         vector<Format const *> result = getReachable(s[0], only_viewable, true);
793         vector<string>::const_iterator it = s.begin() + 1;
794         vector<string>::const_iterator en = s.end();
795         for (; it != en; ++it) {
796                 vector<Format const *> r =
797                         getReachable(*it, only_viewable, false);
798                 result.insert(result.end(), r.begin(), r.end());
799         }
800         return result;
801 }
802
803
804 vector<string> Converters::loaders() const
805 {
806         vector<string> v;
807         v.push_back("lyx");
808         v.push_back("text");
809         v.push_back("textparagraph");
810         return v;
811 }
812
813
814 vector<string> Converters::savers() const
815 {
816         vector<string> v;
817         v.push_back("docbook");
818         v.push_back("latex");
819         v.push_back("literate");
820         v.push_back("luatex");
821         v.push_back("dviluatex");
822         v.push_back("lyx");
823         v.push_back("xhtml");
824         v.push_back("pdflatex");
825         v.push_back("platex");
826         v.push_back("text");
827         v.push_back("xetex");
828         return v;
829 }
830
831
832 } // namespace lyx