]> git.lyx.org Git - lyx.git/blob - src/Chktex.C
7b728fc9d4ec1262623be06311501e21559c0abb
[lyx.git] / src / Chktex.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *           Copyright 1995 Matthias Ettrich
6  *           Copyright 1995-2001 The LyX Team.
7  *
8  *           This file is Copyright 1997-1998
9  *           Asger Alstrup
10  *
11  * ======================================================
12  */
13
14 #include <config.h>
15
16 #include "Chktex.h"
17 #include "LaTeX.h" // TeXErrors
18 #include "lyxlex.h"
19 #include "debug.h"
20 #include "gettext.h"
21
22 #include "support/FileInfo.h"
23 #include "support/filetools.h"
24 #include "support/systemcall.h"
25 #include "support/path.h"
26 #include "support/lstrings.h"
27
28 #include "support/BoostFormat.h"
29
30 #include <fstream>
31
32 using std::ifstream;
33 using std::getline;
34
35
36 Chktex::Chktex(string const & chktex, string const & f, string const & p)
37                 : cmd(chktex), file(f), path(p)
38 {
39 }
40
41
42 int Chktex::run(TeXErrors &terr)
43 {
44         // run bibtex
45         string log = OnlyFilename(ChangeExtension(file, ".log"));
46         string tmp = cmd + " -q -v0 -b0 -x " + file + " -o " + log;
47         Systemcall one;
48         int result = one.startscript(Systemcall::Wait, tmp);
49         if (result == 0) {
50                 result = scanLogFile(terr);
51         } else {
52                 result = -1;
53         }
54         return result;
55 }
56
57
58 int Chktex::scanLogFile(TeXErrors & terr)
59 {
60         string token;
61         int retval = 0;
62
63         string const tmp = OnlyFilename(ChangeExtension(file, ".log"));
64
65 #if USE_BOOST_FORMAT
66         boost::format msg(STRCONV(_("ChkTeX warning id # %1$d")));
67 #else
68         string const msg(_("ChkTeX warning id # "));
69 #endif
70         ifstream ifs(tmp.c_str());
71         while (getline(ifs, token)) {
72                 string srcfile;
73                 string line;
74                 string pos;
75                 string warno;
76                 string 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 = lyx::atoi(line);
84
85 #if USE_BOOST_FORMAT
86                 msg % warno;
87                 terr.insertError(lineno, STRCONV(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 }