2 * Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
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.
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.
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/>.
18 #include <QTextStream>
22 #include "ruleparser.h"
24 Rules::Rules(const QString &fn)
33 QList<Rules::Repository> Rules::repositories()
35 return m_repositories;
38 QList<Rules::Match> Rules::matchRules()
46 if (!file.open(QIODevice::ReadOnly))
49 // initialize the regexps we will use
50 QRegExp repoLine("create repository\\s+(\\S+)", Qt::CaseInsensitive);
51 QRegExp repoBranchLine("branch\\s+(\\S+)\\s+from\\s+(\\S+)", Qt::CaseInsensitive);
53 QRegExp matchLine("match\\s+(.*)", Qt::CaseInsensitive);
54 QRegExp matchRepoLine("repository\\s+(\\S+)", Qt::CaseInsensitive);
55 QRegExp matchBranchLine("branch\\s+(\\S+)", Qt::CaseInsensitive);
56 QRegExp matchRevLine("(min|max) revision (\\d+)", Qt::CaseInsensitive);
59 enum { ReadingNone, ReadingRepository, ReadingMatch } state = ReadingNone;
65 QString origLine = s.readLine();
66 QString line = origLine;
68 int hash = line.indexOf('#');
71 line = line.trimmed();
75 if (state == ReadingRepository) {
76 if (repoBranchLine.exactMatch(line)) {
77 Repository::Branch branch;
78 branch.name = repoBranchLine.cap(1);
79 branch.branchFrom = repoBranchLine.cap(2);
81 repo.branches += branch;
83 } else if (line == "end repository") {
84 m_repositories += repo;
88 } else if (state == ReadingMatch) {
89 if (matchRepoLine.exactMatch(line)) {
90 match.repository = matchRepoLine.cap(1);
92 } else if (matchBranchLine.exactMatch(line)) {
93 match.branch = matchBranchLine.cap(1);
95 } else if (matchRevLine.exactMatch(line)) {
96 if (matchRevLine.cap(1) == "min")
97 match.minRevision = matchRevLine.cap(2).toInt();
99 match.maxRevision = matchRevLine.cap(2).toInt();
101 } else if (line == "end match") {
102 m_matchRules += match;
108 bool isRepositoryRule = repoLine.exactMatch(line);
109 bool isMatchRule = matchLine.exactMatch(line);
111 if (isRepositoryRule) {
113 state = ReadingRepository;
114 repo = Repository(); // clear
115 repo.name = repoLine.cap(1);
116 repo.lineNumber = lineNumber;
117 } else if (isMatchRule) {
119 state = ReadingMatch;
121 match.rx = QRegExp(matchLine.cap(1), Qt::CaseSensitive, QRegExp::RegExp2);
122 match.lineNumber = lineNumber;
124 qFatal("Malformed line in rules file: line %d: %s",
125 lineNumber, qPrintable(origLine));