File: kpathseaParser.cpp

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (177 lines) | stat: -rw-r--r-- 4,495 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "kpathseaParser.h"
#include "utilsSystem.h"

PackageScanner::PackageScanner(QObject *parent) :
	SafeThread(parent)
{
	stopped = false;
}

void PackageScanner::savePackageList(QSet<QString> packages, const QString &filename)
{
	QFile f(filename);
	if (f.open(QFile::WriteOnly | QFile::Text)) {
		QTextStream out(&f);
		out << "% detected .sty and .cls filenames\n";
		foreach (const QString &str, packages) {
			out << str << "\n";
		}
	}
}

QSet<QString> PackageScanner::readPackageList(const QString &filename)
{
	QFile f(filename);
	QSet<QString> result;
	if (f.open(QFile::ReadOnly | QFile::Text)) {
		QTextStream in(&f);
		QString line;
		while (!in.atEnd()) {
			line = in.readLine();
			if (line.isEmpty() || line.startsWith('%')) continue;
			result.insert(line);
		}
	}
	return result;
}

void PackageScanner::stop()
{
	stopped = true;
}

KpathSeaParser::KpathSeaParser(QString kpsecmd, QObject *parent) :
	PackageScanner(parent)
{
	kpseWhichCmd = kpsecmd;
}

void KpathSeaParser::run()
{
	QSet<QString> results;
	QString res = kpsewhich("--show-path ls-R");
	QStringList lstOfFiles = res.split(getPathListSeparator()); // find lcoations of ls-R (file database of tex)
	foreach (QString fn, lstOfFiles) {
		if (stopped)
			return;
		fn = fn.trimmed() + "/ls-R";
		QFile data(fn);
		QString line;
		if (data.open(QIODevice::ReadOnly | QIODevice::Text)) {
			QTextStream stream(&data);
			while (!stream.atEnd()) {
				line = stream.readLine();
				if (line.endsWith(".sty") || line.endsWith(".cls")) {
					line.chop(4);
					results.insert(line);
				}
			}
		}
	}
	emit scanCompleted(results);
}

QString KpathSeaParser::kpsewhich(const QString &arg)
{
	return execCommand(kpseWhichCmd + " " + arg);
}


MiktexPackageScanner::MiktexPackageScanner(QString mpmcmd, QString settingsDir, QObject *parent)
	: PackageScanner(parent)
{
	this->mpmCmd = mpmcmd;
	this->settingsDir = settingsDir;
}

QString MiktexPackageScanner::mpm(const QString &arg)
{
	return execCommand(mpmCmd + " " + arg);
}

void MiktexPackageScanner::savePackageMap(const QHash<QString, QStringList> &map)
{
	QFile f(ensureTrailingDirSeparator(settingsDir) + "miktexPackageNames.dat");
	if (f.open(QFile::WriteOnly | QFile::Text)) {
		QTextStream out(&f);
		out << "% This file maps the MikTeX package names to the .sty and .cls file names.\n";
		out << "% It's used as cache for a fast lookup. It's automatically created and may be deleted without harm.\n";
		foreach (const QString &mpmName, map.keys()) {
			out << mpmName << ":" << map.value(mpmName).join(",") << "\n";
		}
	}
}

QHash<QString, QStringList> MiktexPackageScanner::loadPackageMap()
{
	QFile f(ensureTrailingDirSeparator(settingsDir) + "miktexPackageNames.dat");
	QHash<QString, QStringList> result;
	if (f.open(QFile::ReadOnly | QFile::Text)) {
		QTextStream in(&f);
		QString line;
		QStringList lst;
		while (!in.atEnd()) {
			line = in.readLine();
			if (line.isEmpty() || line.startsWith('%')) continue;
			lst = line.split(":");
			if (lst.length() < 2) continue;
			result.insert(lst[0], lst[1].split(","));
		}
	}
	return result;
}

QStringList MiktexPackageScanner::stysForPackage(const QString &pck)
{
	QStringList result;
	QStringList lines = mpm("--print-package-info " + pck).split("\n");
	bool inRunTimeFilesSection = false;
	foreach (const QString &l, lines) {
		if (!inRunTimeFilesSection) {
			if (l.startsWith("run-time files:"))
				inRunTimeFilesSection = true;
			continue;
		} else {
			QString fn = l.simplified();
			if (fn.endsWith(":")) break; // start of a new section
			fn = QFileInfo(fn).fileName();
			if (fn.endsWith(".sty") || fn.endsWith(".cls")) {
				fn.chop(4);
				result.append(fn);
			}
		}
	}
	return result;
}

void MiktexPackageScanner::run()
{
	QSet<QString> results;

	QHash<QString, QStringList> cachedStys = loadPackageMap();

	QStringList lstOfPackages = mpm("--list").split("\n");
	foreach (QString pck, lstOfPackages) {
		if (stopped)
			return;
		QStringList parts = pck.simplified().split(" "); // output format of "mpm --list": installation status, number of files, size, database name
		if (parts.count() != 4) continue;
		if (parts[0] != "i") continue; // not installed
		pck = parts[3];
		QStringList stys;
		if (cachedStys.contains(pck)) {
			stys = cachedStys.value(pck);
		} else {
			stys = stysForPackage(pck);
			cachedStys.insert(pck, stys);
		}
		foreach (const QString &sty, stys) {
			results.insert(sty);
		}
	}
	savePackageMap(cachedStys);

	emit scanCompleted(results);
}