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