]> git.lyx.org Git - lyx.git/blob - src/Chktex.cpp
d50390764685f0b6daefe2911450e7735f5e1efa
[lyx.git] / src / Chktex.cpp
1 /**
2  * \file Chktex.cpp
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/docstream.h"
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
22 #include "support/systemcall.h"
23
24 #include <boost/format.hpp>
25
26
27 namespace lyx {
28
29 using support::changeExtension;
30 using support::FileName;
31 using support::makeAbsPath;
32 using support::onlyFilename;
33 using support::split;
34 using support::Systemcall;
35
36 using std::getline;
37 using std::string;
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         int retval = 0;
65
66         // FIXME: Find out whether onlyFilename() is really needed,
67         // or whether makeAbsPath(onlyFilename()) is a noop here
68         FileName const tmp(makeAbsPath(onlyFilename(changeExtension(file, ".log"))));
69
70 #if USE_BOOST_FORMAT
71         boost::basic_format<char_type> msg(_("ChkTeX warning id # %1$d"));
72 #else
73         docstring const msg(_("ChkTeX warning id # "));
74 #endif
75         docstring token;
76         // FIXME UNICODE
77         // We have no idea what the encoding of the error file is
78         idocfstream ifs(tmp.toFilesystemEncoding().c_str());
79         while (getline(ifs, token)) {
80                 docstring srcfile;
81                 docstring line;
82                 docstring pos;
83                 docstring warno;
84                 docstring warning;
85                 token = split(token, srcfile, ':');
86                 token = split(token, line, ':');
87                 token = split(token, pos, ':');
88                 token = split(token, warno, ':');
89                 token = split(token, warning, ':');
90
91                 int const lineno = convert<int>(line);
92
93 #if USE_BOOST_FORMAT
94                 msg % warno;
95                 terr.insertError(lineno, msg.str(), warning);
96                 msg.clear();
97 #else
98                 terr.insertError(lineno, msg + warno, warning);
99 #endif
100
101                 ++retval;
102         }
103         return retval;
104 }
105
106
107 } // namespace lyx