BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / text_search / ChangesIterator.cpp
blob6a34ffee3c4373cbf0c3bdfe5e0812777707293a
1 /*
2 * Copyright (c) 2008 Stephan Aßmus <superstippi@gmx.de>
3 * All rights reserved. Distributed under the terms of the MIT license.
4 */
6 #include "ChangesIterator.h"
8 #include <new>
9 #include <stdio.h>
10 #include <string.h>
12 #include <Directory.h>
14 #include "Model.h"
16 using std::nothrow;
19 //#define TRACE_CHANGES_ITERATOR
20 #ifdef TRACE_CHANGES_ITERATOR
21 # define TRACE(x...) printf(x)
22 #else
23 # define TRACE(x...)
24 #endif
27 ChangesIterator::ChangesIterator(const Model* model)
28 : FileIterator(),
29 fPathMap(),
30 fIteratorIndex(0),
32 fRecurseDirs(model->fRecurseDirs),
33 fRecurseLinks(model->fRecurseLinks),
34 fSkipDotDirs(model->fSkipDotDirs),
35 fTextOnly(model->fTextOnly)
40 ChangesIterator::~ChangesIterator()
45 bool
46 ChangesIterator::IsValid() const
48 return fPathMap.InitCheck() == B_OK;
52 bool
53 ChangesIterator::GetNextName(char* buffer)
55 // TODO: inefficient
56 PathMap::Iterator iterator = fPathMap.GetIterator();
57 int32 index = 0;
58 while (index < fIteratorIndex && iterator.HasNext()) {
59 iterator.Next();
60 index++;
63 if (iterator.HasNext()) {
64 const PathMap::Entry& entry = iterator.Next();
65 sprintf(buffer, "%s", entry.key.GetString());
67 fIteratorIndex++;
68 return true;
71 return false;
75 bool
76 ChangesIterator::NotifyNegatives() const
78 return true;
82 // #pragma mark -
85 void
86 ChangesIterator::EntryAdded(const char* path)
88 HashString key(path);
89 if (fPathMap.ContainsKey(key))
90 return;
92 TRACE("added: %s\n", path);
94 fPathMap.Put(key, ENTRY_ADDED);
98 void
99 ChangesIterator::EntryRemoved(const char* path)
101 HashString key(path);
102 if (fPathMap.ContainsKey(key)) {
103 TRACE("ignoring: %s\n", path);
104 fPathMap.Remove(key);
109 void
110 ChangesIterator::EntryChanged(const char* path)
112 HashString key(path);
113 if (fPathMap.ContainsKey(key) && fPathMap.Get(key) == ENTRY_ADDED)
114 return;
116 TRACE("changed: %s\n", path);
118 fPathMap.Put(key, ENTRY_CHANGED);
122 bool
123 ChangesIterator::IsEmpty() const
125 PathMap::Iterator iterator = fPathMap.GetIterator();
126 return !iterator.HasNext();
129 void
130 ChangesIterator::PrintToStream() const
132 printf("ChangesIterator contents:\n");
133 PathMap::Iterator iterator = fPathMap.GetIterator();
134 while (iterator.HasNext()) {
135 const PathMap::Entry& entry = iterator.Next();
136 const char* value;
137 switch (entry.value) {
138 case ENTRY_ADDED:
139 value = "ADDED";
140 break;
141 case ENTRY_REMOVED:
142 value = "REMOVED";
143 break;
144 case ENTRY_CHANGED:
145 value = "CHANGED";
146 break;
147 default:
148 value = "???";
149 break;
151 printf("entry: %s - %s\n", entry.key.GetString(), value);