]> andersk Git - svn-all-fast-export.git/blob - src/ruleparser.cpp
c59b44b0f64997a3202917fc450a4fbdf8e0c22a
[svn-all-fast-export.git] / src / ruleparser.cpp
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
29 Rules::~Rules()
30 {
31 }
32
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+(\\S+)", Qt::CaseInsensitive);
51     QRegExp repoBranchLine("branch\\s+(\\S+)\\s+from\\s+(\\S+)", Qt::CaseInsensitive);
52
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 matchPathLine("path\\s+(.*)", Qt::CaseInsensitive);
57     QRegExp matchRevLine("(min|max) revision (\\d+)", Qt::CaseInsensitive);
58
59     QTextStream s(&file);
60     enum { ReadingNone, ReadingRepository, ReadingMatch } state = ReadingNone;
61     Repository repo;
62     Match match;
63     while (!s.atEnd()) {
64         QString origLine = s.readLine();
65         QString line = origLine;
66
67         int hash = line.indexOf('#');
68         if (hash != -1)
69             line.truncate(hash);
70         line = line.trimmed();
71         if (line.isEmpty())
72             continue;
73
74         if (state == ReadingRepository) {
75             if (repoBranchLine.exactMatch(line)) {
76                 Repository::Branch branch;
77                 branch.name = repoBranchLine.cap(1);
78                 branch.branchFrom = repoBranchLine.cap(2);
79
80                 repo.branches += branch;
81                 continue;
82             } else if (line == "end repository") {
83                 m_repositories += repo;
84                 state = ReadingNone;
85                 continue;
86             }
87         } else if (state == ReadingMatch) {
88             if (matchRepoLine.exactMatch(line)) {
89                 match.repository = matchRepoLine.cap(1);
90                 continue;
91             } else if (matchBranchLine.exactMatch(line)) {
92                 match.branch = matchBranchLine.cap(1);
93                 continue;
94             } else if (matchPathLine.exactMatch(line)) {
95                 match.path = matchPathLine.cap(1);
96                 continue;
97             } else if (matchRevLine.exactMatch(line)) {
98                 if (matchRevLine.cap(1) == "min")
99                     match.minRevision = matchRevLine.cap(2).toInt();
100                 else            // must be max
101                     match.maxRevision = matchRevLine.cap(2).toInt();
102                 continue;
103             } else if (line == "end match") {
104                 m_matchRules += match;
105                 state = ReadingNone;
106                 continue;
107             }
108         }
109
110         bool isRepositoryRule = repoLine.exactMatch(line);
111         bool isMatchRule = matchLine.exactMatch(line);
112
113         if (isRepositoryRule) {
114             // repository rule
115             state = ReadingRepository;
116             repo = Repository(); // clear
117             repo.name = repoLine.cap(1);
118         } else if (isMatchRule) {
119             // match rule
120             state = ReadingMatch;
121             match = Match();
122             match.rx = QRegExp(matchLine.cap(1), Qt::CaseSensitive, QRegExp::RegExp2);
123         } else {
124             qWarning() << "Malformed line in configure file:" << origLine;
125             state = ReadingNone;
126         }
127     }
128 }
This page took 0.065868 seconds and 3 git commands to generate.