]> andersk Git - svn-all-fast-export.git/blob - src/ruleparser.cpp
c4c36afe694a265823093ddbcb8bd1aea3265127
[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 QList<Rules::Repository> Rules::repositories()
30 {
31     return m_repositories;
32 }
33
34 QList<Rules::Match> Rules::matchRules()
35 {
36     return m_matchRules;
37 }
38
39 void Rules::load()
40 {
41     QFile file(filename);
42     if (!file.open(QIODevice::ReadOnly))
43         return;
44
45     // initialize the regexps we will use
46     QRegExp repoLine("create repository\\s+(\\w+)", Qt::CaseInsensitive);
47     QRegExp repoBranchLine("branch\\s+(\\S+)\\s+from\\s+(\\S+)", Qt::CaseInsensitive);
48     QRegExp matchLine("match\\s+(.*)", Qt::CaseInsensitive);
49     QRegExp matchRepoLine("repository\\s+(\\w+)", Qt::CaseInsensitive);
50     QRegExp matchBranchLine("branch\\s+(\\S+)", Qt::CaseInsensitive);
51     QRegExp matchPathLine("path\\s+(.*)", Qt::CaseInsensitive);
52
53     QTextStream s(&file);
54     enum { ReadingNone, ReadingRepository, ReadingMatch } state = ReadingNone;
55     Repository repo;
56     Match match;
57     while (!s.atEnd()) {
58         QString origLine = s.readLine();
59         QString line = origLine.trimmed();
60
61         int hash = line.indexOf('#');
62         if (hash != -1) {
63             line.truncate(hash);
64             line = line.trimmed();
65         }
66         if (line.isEmpty())
67             continue;
68
69         if (state == ReadingRepository) {
70             if (repoBranchLine.exactMatch(line)) {
71                 Repository::Branch branch;
72                 branch.name = repoBranchLine.cap(0);
73                 branch.branchFrom = repoBranchLine.cap(1);
74
75                 repo.branches += branch;
76             }
77         } else if (state == ReadingMatch) {
78             if (matchRepoLine.exactMatch(line))
79                 match.repository = matchRepoLine.cap(0);
80             else if (matchBranchLine.exactMatch(line))
81                 match.branch = matchBranchLine.cap(0);
82             else if (matchPathLine.exactMatch(line))
83                 match.path = matchPathLine.cap(0);
84         }
85
86         bool isRepositoryRule = repoLine.exactMatch(line);
87         bool isMatchRule = matchLine.exactMatch(line);
88         if (isRepositoryRule || isMatchRule) {
89             // save the current rule
90             if (state == ReadingRepository)
91                 m_repositories += repo;
92             else if (state == ReadingMatch)
93                 m_matchRules += match;
94         }
95
96         if (isRepositoryRule) {
97             // repository rule
98             state = ReadingRepository;
99             repo = Repository(); // clear
100             repo.name = repoLine.cap(0);
101         } else if (isMatchRule) {
102             // match rule
103             state = ReadingMatch;
104             match = Match();
105             match.rx = QRegExp(matchLine.cap(0), Qt::CaseSensitive, QRegExp::RegExp2);
106         } else {
107             qWarning() << "Malformed line in configure file:" << origLine;
108             state = ReadingNone;
109         }
110     }
111 }
This page took 0.068517 seconds and 3 git commands to generate.