]> git.lyx.org Git - lyx.git/blob - src/Chktex.cpp
* add PreBabelPreamble to Language definition (fixes #4786).
[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
15 #include "LaTeX.h" // TeXErrors
16
17 #include "support/convert.h"
18 #include "support/docstream.h"
19 #include "support/filetools.h"
20 #include "support/gettext.h"
21 #include "support/lstrings.h"
22 #include "support/Systemcall.h"
23
24 #include <boost/format.hpp>
25
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30
31
32 Chktex::Chktex(string const & chktex, string const & f, string const & p)
33                 : cmd(chktex), file(f), path(p)
34 {
35 }
36
37
38 int Chktex::run(TeXErrors &terr)
39 {
40         // run bibtex
41         string log = onlyFileName(changeExtension(file, ".log"));
42         string tmp = cmd + " -q -v0 -b0 -x " + file + " -o " + log;
43         Systemcall one;
44         int result = one.startscript(Systemcall::Wait, tmp);
45         if (result == 0) {
46                 result = scanLogFile(terr);
47         } else {
48                 result = -1;
49         }
50         return result;
51 }
52
53
54 int Chktex::scanLogFile(TeXErrors & terr)
55 {
56         int retval = 0;
57
58         // FIXME: Find out whether onlyFileName() is really needed,
59         // or whether makeAbsPath(onlyFileName()) is a noop here
60         FileName const tmp(makeAbsPath(onlyFileName(changeExtension(file, ".log"))));
61
62 #if USE_BOOST_FORMAT
63         boost::basic_format<char_type> msg(_("ChkTeX warning id # %1$d"));
64 #else
65         docstring const msg(_("ChkTeX warning id # "));
66 #endif
67         docstring token;
68         // FIXME UNICODE
69         // We have no idea what the encoding of the error file is
70         ifdocstream ifs(tmp.toFilesystemEncoding().c_str());
71         while (getline(ifs, token)) {
72                 docstring srcfile;
73                 docstring line;
74                 docstring pos;
75                 docstring warno;
76                 docstring warning;
77                 token = split(token, srcfile, ':');
78                 token = split(token, line, ':');
79                 token = split(token, pos, ':');
80                 token = split(token, warno, ':');
81                 token = split(token, warning, ':');
82
83                 int const lineno = convert<int>(line);
84
85 #if USE_BOOST_FORMAT
86                 msg % warno;
87                 terr.insertError(lineno, msg.str(), warning);
88                 msg.clear();
89 #else
90                 terr.insertError(lineno, msg + warno, warning);
91 #endif
92
93                 ++retval;
94         }
95         return retval;
96 }
97
98
99 } // namespace lyx