-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursiveReplaceRead.cpp
More file actions
executable file
·67 lines (51 loc) · 2.27 KB
/
recursiveReplaceRead.cpp
File metadata and controls
executable file
·67 lines (51 loc) · 2.27 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
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QFile>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QTextStream>
#include <QStringList>
#include "recursiveReplaceRead.h"
QString recursiveReplaceRead(QString fileName, QStringList* previousFileNames) {
// Create a file header
QFile fileHeader(fileName);
if (previousFileNames->contains(fileName)) { return ""; }
// Try to open the file, return an empty string upon failure
if (!(fileHeader.open(QIODevice::ReadOnly | QIODevice::Text))) {
return "$AXR_ERROR_START File " +fileName+ " could not be opened $AXR_ERROR_END";
}
previousFileNames->append(fileName);
// Variable to store the read text
QString result = QString();
// Regular expression to check for "read" call
QRegularExpression regexRead("^\\s*read\\s+(.+);$");
// Create a text stream for reading the script file
QTextStream in (&fileHeader);
while (!in.atEnd()) {
QString line = in.readLine().simplified();
QRegularExpressionMatch matchRead = regexRead.match(line);
if (matchRead.hasMatch()) {
// Inferred file name
QString newFile = QFileInfo(fileName).absolutePath() + QDir::separator() + matchRead.captured(1);
if (!matchRead.captured(1).count(".")) {
// if the file has no extention, use the default one (.epi)
newFile += ".epi";
}
// Check if the file has dwo or more dots
if (matchRead.captured(1).count(".") > 1) {
result += "$AXR_ERROR_START Filename " +newFile+ " Contains more than one '.' character. Aborting .AXR generation. $AXR_ERROR_END";
}
// Check if the file is not trying to read itself
if (newFile != fileName && !previousFileNames->contains(newFile)) {
line = recursiveReplaceRead(newFile, previousFileNames);
} else {
line = "$AXR_ERROR_START File " +newFile+ " attempted a circular read. Aborting .AXR generation. $AXR_ERROR_END";
}
}
result += line.trimmed() + "\n";
}
// Close the file
fileHeader.close();
return result;
}