]> git.lyx.org Git - lyx.git/blob - src/Chktex.C
* Painter.h:
[lyx.git] / src / Chktex.C
1 /**
2  * \file Chktex.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Chktex.h"
14 #include "gettext.h"
15
16 #include "LaTeX.h" // TeXErrors
17
18 #include "support/convert.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/systemcall.h"
22
23 #include <boost/format.hpp>
24
25 #include <fstream>
26
27
28 namespace lyx {
29
30 using support::changeExtension;
31 using support::onlyFilename;
32 using support::split;
33 using support::Systemcall;
34
35 using std::getline;
36 using std::string;
37 using std::ifstream;
38
39
40 Chktex::Chktex(string const & chktex, string const & f, string const & p)
41                 : cmd(chktex), file(f), path(p)
42 {
43 }
44
45
46 int Chktex::run(TeXErrors &terr)
47 {
48         // run bibtex
49         string log = onlyFilename(changeExtension(file, ".log"));
50         string tmp = cmd + " -q -v0 -b0 -x " + file + " -o " + log;
51         Systemcall one;
52         int result = one.startscript(Systemcall::Wait, tmp);
53         if (result == 0) {
54                 result = scanLogFile(terr);
55         } else {
56                 result = -1;
57         }
58         return result;
59 }
60
61
62 int Chktex::scanLogFile(TeXErrors & terr)
63 {
64         string token;
65         int retval = 0;
66
67         string const tmp = onlyFilename(changeExtension(file, ".log"));
68
69 #if USE_BOOST_FORMAT
70         boost::format msg(to_utf8(_("ChkTeX warning id # %1$d")));
71 #else
72         string const msg(to_utf8(_("ChkTeX warning id # ")));
73 #endif
74         ifstream ifs(tmp.c_str());
75         while (getline(ifs, token)) {
76                 string srcfile;
77                 string line;
78                 string pos;
79                 string warno;
80                 string warning;
81                 token = split(token, srcfile, ':');
82                 token = split(token, line, ':');
83                 token = split(token, pos, ':');
84                 token = split(token, warno, ':');
85                 token = split(token, warning, ':');
86
87                 int const lineno = convert<int>(line);
88
89 #if USE_BOOST_FORMAT
90                 msg % warno;
91                 terr.insertError(lineno, msg.str(), warning);
92                 msg.clear();
93 #else
94                 terr.insertError(lineno, msg + warno, warning);
95 #endif
96
97                 ++retval;
98         }
99         return retval;
100 }
101
102
103 } // namespace lyx