comparison filestates.cpp @ 92:06f4fffd5287

* Rename StatParser to FileStates; start thinking about selections
author Chris Cannam
date Wed, 24 Nov 2010 13:23:30 +0000
parents statparser.cpp@89f793fbedda
children 44ed7766d55a
comparison
equal deleted inserted replaced
91:879af4608c5e 92:06f4fffd5287
1 #include "filestates.h"
2
3 #include "debug.h"
4
5 #include <QMap>
6
7 FileStates::FileStates(QString text)
8 {
9 text.replace("\r\n", "\n");
10
11 QMap<QChar, QStringList *> buckets;
12 buckets['M'] = &modified;
13 buckets['A'] = &added;
14 buckets['R'] = &removed;
15 buckets['!'] = &missing;
16 buckets['?'] = &unknown;
17
18 QStringList lines = text.split("\n", QString::SkipEmptyParts);
19 foreach (QString line, lines) {
20 if (line.length() < 3 || line[1] != ' ') {
21 continue;
22 }
23 QChar tag = line[0];
24 QString file = line.right(line.length() - 2);
25 if (buckets.contains(tag)) {
26 buckets[tag]->push_back(file);
27 }
28 }
29
30 DEBUG << "FileStates: " << modified.size() << " modified, " << added.size()
31 << " added, " << removed.size() << " removed, " << missing.size()
32 << " missing, " << unknown.size() << " unknown" << endl;
33 }
34
35 QStringList FileStates::getFilesInState(State s)
36 {
37 switch (s) {
38
39 case Modified: return modified;
40 case Added: return added;
41 case Unknown: return unknown;
42 case Removed: return removed;
43 case Missing: return missing;
44
45 case UpToDate: // not supported yet
46 default:
47 return QStringList();
48 }
49 }
50
51 FileStates::State FileStates::getStateOfFile(QString file)
52 {
53 // slow, but let's worry about that if it becomes a problem
54 for (int is = int(FirstState); is <= int(LastState); ++is) {
55 QStringList fl = getFilesInState(State(is));
56 foreach (QString f, fl) if (f == file) return State(is);
57 }
58 DEBUG << "FileStates: WARNING: getStateOfFile: file "
59 << file << " is unknown to us: returning Unknown state, "
60 << "but unknown to us is not supposed to be the same "
61 << "thing as unknown state..."
62 << endl;
63 return Unknown;
64 }