]> git.lyx.org Git - lyx.git/blob - src/converter.h
several small patches
[lyx.git] / src / converter.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *        
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2000 The LyX Team.
9  *
10  * ====================================================== */
11
12 #ifndef CONVERTER_H
13 #define CONVERTER_H
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include <vector>
20 #include <queue>
21 #include "LString.h"
22 #include "support/lstrings.h"
23
24 class Buffer;
25
26 ///
27 class Format {
28 public:
29         ///
30         Format(string const & n, string const & e, string const & p,
31                string const & s, string const & v) :
32                 name_(n), extension_(e), prettyname_(p), shortcut_(s),
33                 viewer_(v) {};
34         ///
35         bool dummy() const;
36         ///
37         bool IsChildFormat() const;
38         ///
39         string const ParentFormat() const;
40         ///
41         string const & name() const {
42                 return name_;
43         }
44         ///
45         string const & extension() const {
46                 return extension_;
47         }
48         ///
49         string const & prettyname() const {
50                 return prettyname_;
51         }
52         ///
53         string const & shortcut() const {
54                 return shortcut_;
55         }
56         ///
57         string const & viewer() const {
58                 return viewer_;
59         }
60         ///
61         void setViewer(string const & v) {
62                 viewer_ = v;
63         }
64         friend bool operator<(Format const & a, Format const & b) {
65                 return compare_no_case(a.prettyname(),b.prettyname()) < 0;
66         }
67 private:
68         string name_;
69         ///
70         string extension_;
71         ///
72         string prettyname_;
73         ///
74         string shortcut_;
75         ///
76         string viewer_;
77 };
78
79
80 ///
81 class Formats {
82 public:
83         ///
84         typedef std::vector<Format> FormatList;
85         ///
86         typedef FormatList::const_iterator const_iterator;
87         ///
88         Format const & Get(int i) const {
89                 return formatlist[i];
90         }
91         ///
92         Format const * GetFormat(string const & name) const;
93         ///
94         int GetNumber(string const & name) const;
95         ///
96         void Add(string const & name);
97         ///
98         void Add(string const & name, string const & extension, 
99                  string const & prettyname, string const & shortcut);
100         ///
101         void Delete(string const & name);
102         ///
103         void Sort();
104         ///
105         void SetViewer(string const & name, string const & command);
106         ///
107         bool View(Buffer const * buffer, string const & filename,
108                   string const & format_name) const;
109         ///
110         string const PrettyName(string const & name) const;
111         ///
112         string const Extension(string const & name) const;
113         ///
114         const_iterator begin() const {
115                 return formatlist.begin();
116         }
117         ///
118         const_iterator end() const {
119                 return formatlist.end();
120         }
121         ///
122         FormatList::size_type size() const {
123                 return formatlist.size();
124         }
125 private:
126         ///
127         FormatList formatlist;
128 };
129
130 ///////////////////////////////////////////////////////////////////////
131
132 ///
133 class Converter {
134 public:
135         ///
136         Converter(string const & f, string const & t, string const & c,
137                   string const & l)
138                 : from(f), to(t), command(c), flags(l), From(0), To(0),
139                   latex(false), original_dir(false), need_aux(false) {}
140         ///
141         void ReadFlags();
142         ///
143         string from;
144         ///
145         string to;
146         ///
147         string command;
148         ///
149         string flags;
150         ///
151         Format const * From;
152         ///
153         Format const * To;
154
155         /// The converter is latex or its derivatives
156         bool latex;
157         /// Do we need to run the converter in the original directory?
158         bool original_dir;
159         /// This converter needs the .aux files
160         bool need_aux;
161         /// If the converter put the result in a directory, then result_dir
162         /// is the name of the directory
163         string result_dir;
164         /// If the converter put the result in a directory, then result_file
165         /// is the name of the main file in that directory
166         string result_file;
167         /// Command to convert the program output to a LaTeX log file format
168         string parselog;
169 };
170
171
172 ///
173 class Converters {
174 public:
175         typedef std::vector<Converter> ConverterList;
176         ///
177         typedef ConverterList::const_iterator const_iterator;
178         ///
179         typedef std::vector<int> EdgePath;
180         ///
181         Converter const & Get(int i) const {
182                 return converterlist[i];
183         }
184         ///
185         Converter const * GetConverter(string const & from, string const & to);
186         ///
187         int GetNumber(string const & from, string const & to);
188         ///
189         void Add(string const & from, string const & to,
190                  string const & command, string const & flags);
191         //
192         void Delete(string const & from, string const & to);
193         ///
194         void Sort();
195         ///
196         std::vector<Format const *> const
197         GetReachableTo(string const & target, bool clear_visited);
198         ///
199         std::vector<Format const *> const
200         GetReachable(string const & from, bool only_viewable,
201                      bool clear_visited);
202         ///
203         bool IsReachable(string const & from, string const & to);
204         ///
205         EdgePath const GetPath(string const & from, string const & to);
206         ///
207         bool UsePdflatex(EdgePath const & path);
208         ///
209         bool Convert(Buffer const * buffer,
210                      string const & from_file, string const & to_file_base,
211                      string const & from_format, string const & to_format,
212                      string & to_file);
213         ///
214         bool Convert(Buffer const * buffer,
215                      string const & from_file, string const & to_file_base,
216                      string const & from_format, string const & to_format);
217         ///
218         string const dvi_papersize(Buffer const * buffer);
219         ///
220         string const dvips_options(Buffer const * buffer);
221         ///
222         void Update(Formats const & formats);
223         ///
224         void UpdateLast(Formats const & formats);
225         ///
226         void BuildGraph();
227         ///
228         bool FormatIsUsed(string const & format);
229         ///
230         const_iterator begin() const {
231                 return converterlist.begin();
232         }
233         const_iterator end() const {
234                 return converterlist.end();
235         }
236 private:
237         ///
238         bool scanLog(Buffer const * buffer, string const & command, 
239                      string const & filename);
240         ///
241         bool runLaTeX(Buffer const * buffer, string const & command);
242         ///
243         ConverterList converterlist;
244         ///
245         string latex_command;
246         ///
247         struct Vertex {
248                 std::vector<int> in_vertices;
249                 std::vector<int> out_vertices;
250                 std::vector<int> out_edges;
251         };
252         ///
253         static
254         std::vector<Vertex> vertices;
255         ///
256         std::vector<bool> visited;
257         ///
258         std::queue<int> Q;
259         ///
260         int BFS_init(string const & start, bool clear_visited = true);
261         ///
262         bool Move(string const & from, string const & to, bool copy);
263 };
264
265 extern Formats formats;
266 extern Converters converters;
267
268 extern Formats system_formats;
269 extern Converters system_converters;
270
271 #endif