File: fixfz.cpp

package info (click to toggle)
fritzing 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,144 kB
  • sloc: cpp: 98,203; xml: 2,203; python: 803; sh: 274; ansic: 26; makefile: 22
file content (180 lines) | stat: -rw-r--r-- 4,362 bytes parent folder | download | duplicates (2)
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
178
179
180
#include "fixfz.h"
#include <QApplication>
#include <QDir>
#include <QString>
#include <QFile>
#include <iostream>
#include <QtDebug>
#include <QDomDocument>
#include <QDomElement>


QDir globalDir;

void usage() {
	std::cout << "update fz files:" << std::endl << std::endl;
	std::cout << "usage:  fixfz  partsfolder" << std::endl << std::endl;
    std::cout << std::endl;
}


bool findFile(QDir & dir, QFile & resultFile, QString filename) {
	if (!dir.exists()) return false;

	QFileInfoList files = dir.entryInfoList();
	foreach(QFileInfo fileInfo, files) {
		if (fileInfo.isFile()) {
			if (fileInfo.absoluteFilePath().endsWith(filename)) {
				resultFile.setFileName(fileInfo.absoluteFilePath());
				return true;
			}
			continue;
		}

		if (fileInfo.fileName().compare(".") == 0) continue;
		if (fileInfo.fileName().compare("..") == 0) continue;

		if (fileInfo.isDir()) {
			QDir temp(fileInfo.absoluteFilePath());
			if (findFile(temp, resultFile, filename)) {
				return true;
			}
		}
	}

	return false;
}

void handleFile(const QString & filename) {
	std::cout << "\t" << filename.toStdString() << std::endl;
	QFile file(filename);

	QString errorStr;
	int errorLine;
	int errorColumn;
	QDomDocument domDocument;

	if (!domDocument.setContent(&file, true, &errorStr, &errorLine, &errorColumn)) {
		std::cout << "file isn't fritzing file (1)" << std::endl;
		return;
	}

	QDomElement root = domDocument.documentElement();
	if (root.isNull()) {
		std::cout << "file isn't fritzing file (2)" << std::endl;
		return;
	}

    if (root.tagName() != "module") {
		std::cout << "can't find module tag" << std::endl;
        return;
    }


	QDomElement views = root.firstChildElement("views");
	if (views.isNull()) {
		std::cout << "can't find views element" << std::endl;
        return;
	}

	QHash<QString, QString> viewImages;

	QDomElement view = views.firstChildElement();
	while (!view.isNull()) {
		QDomElement layers = view.firstChildElement("layers");
		if (!layers.isNull()) {
			QDomElement layer = layers.firstChildElement("layer");
			while (!layer.isNull()) {
				QString image = layer.attribute("image");
				if (!image.isEmpty()) {
					layers.setAttribute("image", image);
					layer.removeAttribute("image");
				}

				layer = layer.nextSiblingElement("layer");
			}
		}

		viewImages.insert(view.tagName(), layers.attribute("image"));

		view = view.nextSiblingElement();
	}

	QDomElement connectors = root.firstChildElement("connectors");
	if (views.isNull()) {
		std::cout << "can't find connectors element" << std::endl;
        return;
	}

	QDomElement connector = connectors.firstChildElement("connector");
	while (!connector.isNull()) {
		QDomElement views = connector.firstChildElement("views");
		if (views.isNull()) {
			std::cout << "can't find views element" << std::endl;
		}

		QDomElement view = views.firstChildElement();
		while (!view.isNull()) {
			QDomElement pin = view.firstChildElement("pin");
			if (!pin.isNull()) {
				pin.setTagName("p");
				QString id = pin.attribute("svgId");
				QString image = viewImages.value(view.tagName());
				QFile imageFile;
				if (findFile(globalDir, imageFile, image)) {
					if (imageFile.open(QIODevice::ReadOnly)) {
						QString temp = imageFile.readAll();
						imageFile.close();
						if (!temp.contains(id)) {
							std::cout << "--- missing " << id.toStdString() << " in " << view.tagName().toStdString() << " in " << imageFile.fileName().toStdString() << " ---" << std::endl;
						}
					}
				}
			}

			view = view.nextSiblingElement();
		}

		connector = connector.nextSiblingElement("connector");
	}

	QFile data(filename);
	if (data.open(QFile::WriteOnly | QFile::Truncate)) {
		QTextStream out(&data);
		out << domDocument.toString();
		data.close();
	}
}

void runDir(QDir & dir) {
	if (!dir.exists()) return;

	QFileInfoList files = dir.entryInfoList();
	foreach(QFileInfo file, files) {
		if (file.isFile()) {
			if (file.fileName().endsWith(".fz")) {
				handleFile(file.absoluteFilePath());
			}
			continue;
		}

		if (file.fileName().compare(".") == 0) continue;
		if (file.fileName().compare("..") == 0) continue;

		if (file.isDir()) {
			QDir temp(file.absoluteFilePath());
			runDir(temp);
		}
	}
}

int main(int argc, char * argv[])
{
    if ((argc < 2)) {
        usage();
        return 0;
    }

	globalDir.setPath(argv[1]);
	runDir(globalDir);
}