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+(\\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);
58 enum { ReadingNone, ReadingRepository, ReadingMatch } state = ReadingNone;
62 QString origLine = s.readLine();
63 QString line = origLine.trimmed();
65 int hash = line.indexOf('#');
68 line = line.trimmed();
73 if (state == ReadingRepository) {
74 if (repoBranchLine.exactMatch(line)) {
75 Repository::Branch branch;
76 branch.name = repoBranchLine.cap(0);
77 branch.branchFrom = repoBranchLine.cap(1);
79 repo.branches += branch;
82 } else if (state == ReadingMatch) {
83 if (matchRepoLine.exactMatch(line)) {
84 match.repository = matchRepoLine.cap(0);
86 } else if (matchBranchLine.exactMatch(line)) {
87 match.branch = matchBranchLine.cap(0);
89 } else if (matchPathLine.exactMatch(line)) {
90 match.path = matchPathLine.cap(0);
95 bool isRepositoryRule = repoLine.exactMatch(line);
96 bool isMatchRule = matchLine.exactMatch(line);
97 if (isRepositoryRule || isMatchRule) {
98 // save the current rule
99 if (state == ReadingRepository)
100 m_repositories += repo;
101 else if (state == ReadingMatch)
102 m_matchRules += match;
105 if (isRepositoryRule) {
107 state = ReadingRepository;
108 repo = Repository(); // clear
109 repo.name = repoLine.cap(0);
110 } else if (isMatchRule) {
112 state = ReadingMatch;
114 match.rx = QRegExp(matchLine.cap(0), Qt::CaseSensitive, QRegExp::RegExp2);
116 qWarning() << "Malformed line in configure file:" << origLine;