]>
Commit | Line | Data |
---|---|---|
01592f9e TM |
1 | /* |
2 | * Copyright (C) 2007 Thiago Macieira <thiago@kde.org> | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | #include <QTextStream> | |
19 | #include <QFile> | |
20 | #include <QDebug> | |
21 | ||
22 | #include "ruleparser.h" | |
23 | ||
24 | Rules::Rules(const QString &fn) | |
25 | : filename(fn) | |
26 | { | |
27 | } | |
28 | ||
12cd84df TM |
29 | Rules::~Rules() |
30 | { | |
31 | } | |
32 | ||
01592f9e TM |
33 | QList<Rules::Repository> Rules::repositories() |
34 | { | |
35 | return m_repositories; | |
36 | } | |
37 | ||
38 | QList<Rules::Match> Rules::matchRules() | |
39 | { | |
40 | return m_matchRules; | |
41 | } | |
42 | ||
43 | void Rules::load() | |
44 | { | |
45 | QFile file(filename); | |
46 | if (!file.open(QIODevice::ReadOnly)) | |
47 | return; | |
48 | ||
49 | // initialize the regexps we will use | |
5d3d11ec | 50 | QRegExp repoLine("create repository\\s+(\\S+)", Qt::CaseInsensitive); |
973fa95b | 51 | QRegExp repoBranchLine("branch\\s+(\\S+)", Qt::CaseInsensitive); |
5b8a1c0c | 52 | |
01592f9e | 53 | QRegExp matchLine("match\\s+(.*)", Qt::CaseInsensitive); |
898704e4 | 54 | QRegExp matchActionLine("action\\s+(\\w+)", Qt::CaseInsensitive); |
5d3d11ec | 55 | QRegExp matchRepoLine("repository\\s+(\\S+)", Qt::CaseInsensitive); |
01592f9e | 56 | QRegExp matchBranchLine("branch\\s+(\\S+)", Qt::CaseInsensitive); |
5b8a1c0c | 57 | QRegExp matchRevLine("(min|max) revision (\\d+)", Qt::CaseInsensitive); |
a5fd311b | 58 | QRegExp matchAnnotateLine("annotated\\s+(\\S+)", Qt::CaseInsensitive); |
01592f9e TM |
59 | |
60 | QTextStream s(&file); | |
61 | enum { ReadingNone, ReadingRepository, ReadingMatch } state = ReadingNone; | |
62 | Repository repo; | |
63 | Match match; | |
1129b425 | 64 | int lineNumber = 0; |
01592f9e | 65 | while (!s.atEnd()) { |
1129b425 | 66 | ++lineNumber; |
01592f9e | 67 | QString origLine = s.readLine(); |
c7d45e66 | 68 | QString line = origLine; |
01592f9e TM |
69 | |
70 | int hash = line.indexOf('#'); | |
c7d45e66 | 71 | if (hash != -1) |
01592f9e | 72 | line.truncate(hash); |
c7d45e66 | 73 | line = line.trimmed(); |
01592f9e TM |
74 | if (line.isEmpty()) |
75 | continue; | |
76 | ||
77 | if (state == ReadingRepository) { | |
78 | if (repoBranchLine.exactMatch(line)) { | |
79 | Repository::Branch branch; | |
c7d45e66 | 80 | branch.name = repoBranchLine.cap(1); |
01592f9e TM |
81 | |
82 | repo.branches += branch; | |
add9aaf4 | 83 | continue; |
c7d45e66 TM |
84 | } else if (line == "end repository") { |
85 | m_repositories += repo; | |
86 | state = ReadingNone; | |
87 | continue; | |
01592f9e TM |
88 | } |
89 | } else if (state == ReadingMatch) { | |
add9aaf4 | 90 | if (matchRepoLine.exactMatch(line)) { |
c7d45e66 | 91 | match.repository = matchRepoLine.cap(1); |
add9aaf4 TM |
92 | continue; |
93 | } else if (matchBranchLine.exactMatch(line)) { | |
c7d45e66 | 94 | match.branch = matchBranchLine.cap(1); |
add9aaf4 | 95 | continue; |
5b8a1c0c TM |
96 | } else if (matchRevLine.exactMatch(line)) { |
97 | if (matchRevLine.cap(1) == "min") | |
98 | match.minRevision = matchRevLine.cap(2).toInt(); | |
99 | else // must be max | |
100 | match.maxRevision = matchRevLine.cap(2).toInt(); | |
101 | continue; | |
898704e4 TM |
102 | } else if (matchActionLine.exactMatch(line)) { |
103 | QString action = matchActionLine.cap(1); | |
104 | if (action == "export") | |
105 | match.action = Match::Export; | |
106 | else if (action == "ignore") | |
107 | match.action = Match::Ignore; | |
b4cb5188 TM |
108 | else if (action == "recurse") |
109 | match.action = Match::Recurse; | |
898704e4 TM |
110 | else |
111 | qFatal("Invalid action \"%s\" on line %d", qPrintable(action), lineNumber); | |
112 | continue; | |
a5fd311b TM |
113 | } else if (matchAnnotateLine.exactMatch(line)) { |
114 | match.annotate = matchAnnotateLine.cap(1) == "true"; | |
115 | continue; | |
c7d45e66 | 116 | } else if (line == "end match") { |
898704e4 TM |
117 | if (!match.repository.isEmpty()) |
118 | match.action = Match::Export; | |
c7d45e66 TM |
119 | m_matchRules += match; |
120 | state = ReadingNone; | |
add9aaf4 TM |
121 | continue; |
122 | } | |
01592f9e TM |
123 | } |
124 | ||
125 | bool isRepositoryRule = repoLine.exactMatch(line); | |
126 | bool isMatchRule = matchLine.exactMatch(line); | |
01592f9e TM |
127 | |
128 | if (isRepositoryRule) { | |
129 | // repository rule | |
130 | state = ReadingRepository; | |
131 | repo = Repository(); // clear | |
c7d45e66 | 132 | repo.name = repoLine.cap(1); |
107fa167 | 133 | repo.lineNumber = lineNumber; |
01592f9e TM |
134 | } else if (isMatchRule) { |
135 | // match rule | |
136 | state = ReadingMatch; | |
137 | match = Match(); | |
c7d45e66 | 138 | match.rx = QRegExp(matchLine.cap(1), Qt::CaseSensitive, QRegExp::RegExp2); |
107fa167 | 139 | match.lineNumber = lineNumber; |
01592f9e | 140 | } else { |
1129b425 TM |
141 | qFatal("Malformed line in rules file: line %d: %s", |
142 | lineNumber, qPrintable(origLine)); | |
01592f9e TM |
143 | } |
144 | } | |
145 | } | |
2ddd1758 TM |
146 | |
147 | #ifndef QT_NO_DEBUG_STREAM | |
148 | QDebug operator<<(QDebug s, const Rules::Match &rule) | |
149 | { | |
150 | s.nospace() << rule.rx.pattern() << " (line " << rule.lineNumber << ")"; | |
b019fed7 | 151 | return s.space(); |
2ddd1758 TM |
152 | } |
153 | ||
154 | #endif |