-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
130 lines (106 loc) · 3.35 KB
/
main.cpp
File metadata and controls
130 lines (106 loc) · 3.35 KB
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
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QStringList>
#include <QCoreApplication>
#include <QDateTime>
#define NUM_OF_FILES 4
int num_of_input(const QVector<QString> &vector)
{
for(int i = 0; i < vector.size(); ++i) {
if (vector[i].contains("DEPENDPATH")) {
return i;
}
}
return 0;
}
void cut_vector(int begin, int end, QVector<QString> &vector)
{
vector.remove(begin, end);
}
void full_vector(QTextStream &in, QVector<QString> &vector)
{
while (!(in.atEnd())) {
vector.push_back(in.readLine());
}
}
void close_files(QFile *file, bool qRemove)
{
for (int i = 0; i < NUM_OF_FILES; ++i) {
file[i].close();
if (qRemove) {
file[NUM_OF_FILES - 1].remove();
}
}
}
int main(int argc, char *argv[])
{
if (argc != 3) {
qDebug() << "Please use:";
qDebug() << "\t./magxheader [*.pro file] [appname]";
return 1;
}
QDateTime dt = QDateTime::currentDateTime();
QVector<QString> head_vector;
QVector<QString> mid_vector;
QVector<QString> end_vector;
QString blank = "######################################################################";
QString appname = QString("APPNAME = %1").arg(QString(argv[2]));
QString dtstring = "# Generated by magxheader " + dt.toString("dd.MM.yyyy HH:mm:ss");
QString pro_file = QString(argv[1]);
if (!pro_file.contains(".pro")) {
qDebug() << "This is not *.pro file!";
return 2;
}
QStringList dest = pro_file.split(".");
pro_file = dest[0] + "_magx." + dest[1];
QFile file[NUM_OF_FILES];
file[0].setFileName(QString(argv[1]));
file[1].setFileName("://header_p1");
file[2].setFileName("://header_p2");
file[3].setFileName(pro_file);
for (int i = 0; i < NUM_OF_FILES; ++i) {
if (i < 3) {
if (!file[i].open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Error openning" << file[i].fileName() << "for reading!";
return 3;
}
} else {
if (!file[i].open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Error openning" << file[i].fileName() << "for writing!";
return 4;
}
}
}
QTextStream textStream[NUM_OF_FILES];
for (int i = 0; i < NUM_OF_FILES; ++i) {
textStream[i].setCodec("UTF-8");
textStream[i].setDevice(&file[i]);
}
full_vector(textStream[0], end_vector);
int num_cut = num_of_input(end_vector);
if (num_cut <= 0) {
qDebug() << "\"DEPENDPATH\" string not found!\nCheck you *.pro file!";
close_files(file, true);
return 5;
}
cut_vector(0, num_cut, end_vector);
full_vector(textStream[1], head_vector);
full_vector(textStream[2], mid_vector);
textStream[3] << blank << '\n';
textStream[3] << dtstring << '\n';
for (int i = 0; i < head_vector.size(); ++i) {
textStream[3] << head_vector[i] << '\n';
}
textStream[3] << appname << '\n';
for (int i = 0; i < mid_vector.size(); ++i) {
textStream[3] << mid_vector[i] << '\n';
}
textStream[3] << '\n';
for (int i = 0; i < end_vector.size(); ++i) {
textStream[3] << end_vector[i] << '\n';
}
close_files(file, false);
qDebug() << "Writing " << pro_file << "...\nAll done!";
return 0;
}