]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.C
Removed all redundant using directives from the source.
[lyx.git] / src / tex2lyx / tex2lyx.C
1 /**
2  * \file tex2lyx.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 // {[(
12
13 #include <config.h>
14
15 #include "tex2lyx.h"
16 #include "context.h"
17
18 #include "debug.h"
19 #include "lyx_main.h"
20 #include "lyxtextclass.h"
21 #include "support/path_defines.h"
22 #include "support/os.h"
23
24 #include <cctype>
25 #include <fstream>
26 #include <iostream>
27 #include <string>
28 #include <sstream>
29 #include <vector>
30
31 using std::endl;
32 using std::cout;
33 using std::cerr;
34 using std::getline;
35
36 using std::ifstream;
37 using std::istringstream;
38 using std::ostringstream;
39 using std::stringstream;
40 using std::string;
41 using std::vector;
42
43 // Hacks to allow the thing to link in the lyxlayout stuff
44 Debug::type const Debug::ANY = Debug::type(0);
45 DebugStream lyxerr;
46
47 void LyX::emergencyCleanup() {}
48
49 void handle_comment(Parser & p)
50 {
51         string s;
52         while (p.good()) {
53                 Token const & t = p.get_token();
54                 if (t.cat() == catNewline)
55                         break;
56                 s += t.asString();
57         }
58         //cerr << "comment: " << s << "\n";
59         p.skip_spaces();
60 }
61
62
63 string const trim(string const & a, char const * p)
64 {
65         // lyx::Assert(p);
66
67         if (a.empty() || !*p)
68                 return a;
69
70         string::size_type r = a.find_last_not_of(p);
71         string::size_type l = a.find_first_not_of(p);
72
73         // Is this the minimal test? (lgb)
74         if (r == string::npos && l == string::npos)
75                 return string();
76
77         return a.substr(l, r - l + 1);
78 }
79
80
81 void split(string const & s, vector<string> & result, char delim)
82 {
83         //cerr << "split 1: '" << s << "'\n";
84         istringstream is(s);
85         string t;
86         while (getline(is, t, delim))
87                 result.push_back(t);
88         //cerr << "split 2\n";
89 }
90
91
92 string join(vector<string> const & input, char const * delim)
93 {
94         ostringstream os;
95         for (size_t i = 0; i < input.size(); ++i) {
96                 if (i)
97                         os << delim;
98                 os << input[i];
99         }
100         return os.str();
101 }
102
103
104 char const ** is_known(string const & str, char const ** what)
105 {
106         for ( ; *what; ++what)
107                 if (str == *what)
108                         return what;
109         return 0;
110 }
111
112
113
114 // current stack of nested environments
115 vector<string> active_environments;
116
117
118 string active_environment()
119 {
120         return active_environments.empty() ? string() : active_environments.back();
121 }
122
123
124 int main(int argc, char * argv[])
125 {
126         if (argc <= 1) {
127                 cerr << "Usage: " << argv[0] << " <infile.tex>" << endl;
128                 return 2;
129         }
130
131         lyx::support::os::init(&argc, &argv);
132         lyx::support::setLyxPaths();
133
134         ifstream is(argv[1]);
135         Parser p(is);
136         //p.dump();
137
138         stringstream ss;
139         LyXTextClass textclass = parse_preamble(p, ss);
140         active_environments.push_back("document");
141         Context context(true, textclass);
142         parse_text(p, ss, FLAG_END, true, context);
143         context.check_end_layout(ss);
144         ss << "\n\\end_document\n";
145
146         ss.seekg(0);
147         cout << ss.str();
148         return 0;
149 }
150
151 // }])