]> git.lyx.org Git - lyx.git/blob - src/Chktex.C
4ad44c2fca4447f674d3bed647f6f585718989f4
[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/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/lyxlib.h"
21 #include "support/systemcall.h"
22
23 #include <boost/format.hpp>
24
25 #include <fstream>
26
27 using lyx::support::atoi;
28 using lyx::support::ChangeExtension;
29 using lyx::support::OnlyFilename;
30 using lyx::support::split;
31 using lyx::support::Systemcall;
32
33 using std::getline;
34 using std::string;
35 using std::ifstream;
36
37
38 Chktex::Chktex(string const & chktex, string const & f, string const & p)
39                 : cmd(chktex), file(f), path(p)
40 {
41 }
42
43
44 int Chktex::run(TeXErrors &terr)
45 {
46         // run bibtex
47         string log = OnlyFilename(ChangeExtension(file, ".log"));
48         string tmp = cmd + " -q -v0 -b0 -x " + file + " -o " + log;
49         Systemcall one;
50         int result = one.startscript(Systemcall::Wait, tmp);
51         if (result == 0) {
52                 result = scanLogFile(terr);
53         } else {
54                 result = -1;
55         }
56         return result;
57 }
58
59
60 int Chktex::scanLogFile(TeXErrors & terr)
61 {
62         string token;
63         int retval = 0;
64
65         string const tmp = OnlyFilename(ChangeExtension(file, ".log"));
66
67 #if USE_BOOST_FORMAT
68         boost::format msg(_("ChkTeX warning id # %1$d"));
69 #else
70         string const msg(_("ChkTeX warning id # "));
71 #endif
72         ifstream ifs(tmp.c_str());
73         while (getline(ifs, token)) {
74                 string srcfile;
75                 string line;
76                 string pos;
77                 string warno;
78                 string warning;
79                 token = split(token, srcfile, ':');
80                 token = split(token, line, ':');
81                 token = split(token, pos, ':');
82                 token = split(token, warno, ':');
83                 token = split(token, warning, ':');
84
85                 int const lineno = atoi(line);
86
87 #if USE_BOOST_FORMAT
88                 msg % warno;
89                 terr.insertError(lineno, msg.str(), warning);
90                 msg.clear();
91 #else
92                 terr.insertError(lineno, msg + warno, warning);
93 #endif
94
95                 ++retval;
96         }
97         return retval;
98 }