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