]>
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 | |
50 | QRegExp repoLine("create repository\\s+(\\w+)", Qt::CaseInsensitive); | |
51 | QRegExp repoBranchLine("branch\\s+(\\S+)\\s+from\\s+(\\S+)", Qt::CaseInsensitive); | |
52 | QRegExp matchLine("match\\s+(.*)", Qt::CaseInsensitive); | |
53 | QRegExp matchRepoLine("repository\\s+(\\w+)", Qt::CaseInsensitive); | |
54 | QRegExp matchBranchLine("branch\\s+(\\S+)", Qt::CaseInsensitive); | |
55 | QRegExp matchPathLine("path\\s+(.*)", Qt::CaseInsensitive); | |
56 | ||
57 | QTextStream s(&file); | |
58 | enum { ReadingNone, ReadingRepository, ReadingMatch } state = ReadingNone; | |
59 | Repository repo; | |
60 | Match match; | |
61 | while (!s.atEnd()) { | |
62 | QString origLine = s.readLine(); | |
c7d45e66 | 63 | QString line = origLine; |
01592f9e TM |
64 | |
65 | int hash = line.indexOf('#'); | |
c7d45e66 | 66 | if (hash != -1) |
01592f9e | 67 | line.truncate(hash); |
c7d45e66 | 68 | line = line.trimmed(); |
01592f9e TM |
69 | if (line.isEmpty()) |
70 | continue; | |
71 | ||
72 | if (state == ReadingRepository) { | |
73 | if (repoBranchLine.exactMatch(line)) { | |
74 | Repository::Branch branch; | |
c7d45e66 TM |
75 | branch.name = repoBranchLine.cap(1); |
76 | branch.branchFrom = repoBranchLine.cap(2); | |
01592f9e TM |
77 | |
78 | repo.branches += branch; | |
add9aaf4 | 79 | continue; |
c7d45e66 TM |
80 | } else if (line == "end repository") { |
81 | m_repositories += repo; | |
82 | state = ReadingNone; | |
83 | continue; | |
01592f9e TM |
84 | } |
85 | } else if (state == ReadingMatch) { | |
add9aaf4 | 86 | if (matchRepoLine.exactMatch(line)) { |
c7d45e66 | 87 | match.repository = matchRepoLine.cap(1); |
add9aaf4 TM |
88 | continue; |
89 | } else if (matchBranchLine.exactMatch(line)) { | |
c7d45e66 | 90 | match.branch = matchBranchLine.cap(1); |
add9aaf4 TM |
91 | continue; |
92 | } else if (matchPathLine.exactMatch(line)) { | |
c7d45e66 TM |
93 | match.path = matchPathLine.cap(1); |
94 | continue; | |
95 | } else if (line == "end match") { | |
96 | m_matchRules += match; | |
97 | state = ReadingNone; | |
add9aaf4 TM |
98 | continue; |
99 | } | |
01592f9e TM |
100 | } |
101 | ||
102 | bool isRepositoryRule = repoLine.exactMatch(line); | |
103 | bool isMatchRule = matchLine.exactMatch(line); | |
01592f9e TM |
104 | |
105 | if (isRepositoryRule) { | |
106 | // repository rule | |
107 | state = ReadingRepository; | |
108 | repo = Repository(); // clear | |
c7d45e66 | 109 | repo.name = repoLine.cap(1); |
01592f9e TM |
110 | } else if (isMatchRule) { |
111 | // match rule | |
112 | state = ReadingMatch; | |
113 | match = Match(); | |
c7d45e66 | 114 | match.rx = QRegExp(matchLine.cap(1), Qt::CaseSensitive, QRegExp::RegExp2); |
01592f9e TM |
115 | } else { |
116 | qWarning() << "Malformed line in configure file:" << origLine; | |
117 | state = ReadingNone; | |
118 | } | |
119 | } | |
120 | } |