1 /* 2 * Copyright (c) 2008 Stephan Aßmus <superstippi@gmx.de> 3 * All rights reserved. Distributed under the terms of the MIT license. 4 */ 5 6 #include "ChangesIterator.h" 7 8 #include <new> 9 #include <stdio.h> 10 #include <string.h> 11 12 #include <Directory.h> 13 14 #include "Model.h" 15 16 using std::nothrow; 17 18 19 //#define TRACE_CHANGES_ITERATOR 20 #ifdef TRACE_CHANGES_ITERATOR 21 # define TRACE(x...) printf(x) 22 #else 23 # define TRACE(x...) 24 #endif 25 26 27 ChangesIterator::ChangesIterator(const Model* model) 28 : FileIterator(), 29 fPathMap(), 30 fIteratorIndex(0), 31 32 fRecurseDirs(model->fRecurseDirs), 33 fRecurseLinks(model->fRecurseLinks), 34 fSkipDotDirs(model->fSkipDotDirs), 35 fTextOnly(model->fTextOnly) 36 { 37 } 38 39 40 ChangesIterator::~ChangesIterator() 41 { 42 } 43 44 45 bool 46 ChangesIterator::IsValid() const 47 { 48 return fPathMap.InitCheck() == B_OK; 49 } 50 51 52 bool 53 ChangesIterator::GetNextName(char* buffer) 54 { 55 // TODO: inefficient 56 PathMap::Iterator iterator = fPathMap.GetIterator(); 57 int32 index = 0; 58 while (index < fIteratorIndex && iterator.HasNext()) { 59 iterator.Next(); 60 index++; 61 } 62 63 if (iterator.HasNext()) { 64 const PathMap::Entry& entry = iterator.Next(); 65 sprintf(buffer, "%s", entry.key.GetString()); 66 67 fIteratorIndex++; 68 return true; 69 } 70 71 return false; 72 } 73 74 75 bool 76 ChangesIterator::NotifyNegatives() const 77 { 78 return true; 79 } 80 81 82 // #pragma mark - 83 84 85 void 86 ChangesIterator::EntryAdded(const char* path) 87 { 88 HashString key(path); 89 if (fPathMap.ContainsKey(key)) 90 return; 91 92 TRACE("added: %s\n", path); 93 94 fPathMap.Put(key, ENTRY_ADDED); 95 } 96 97 98 void 99 ChangesIterator::EntryRemoved(const char* path) 100 { 101 HashString key(path); 102 if (fPathMap.ContainsKey(key)) { 103 uint32 mode = fPathMap.Get(key); 104 if (mode == ENTRY_ADDED) { 105 TRACE("ignoring: %s\n", path); 106 fPathMap.Remove(key); 107 return; 108 } else if (mode == ENTRY_REMOVED) 109 return; 110 } 111 112 TRACE("removed: %s\n", path); 113 114 fPathMap.Put(key, ENTRY_REMOVED); 115 } 116 117 118 void 119 ChangesIterator::EntryChanged(const char* path) 120 { 121 HashString key(path); 122 if (fPathMap.ContainsKey(key) && fPathMap.Get(key) == ENTRY_ADDED) 123 return; 124 125 TRACE("changed: %s\n", path); 126 127 fPathMap.Put(key, ENTRY_CHANGED); 128 } 129 130 131 bool 132 ChangesIterator::IsEmpty() const 133 { 134 PathMap::Iterator iterator = fPathMap.GetIterator(); 135 return !iterator.HasNext(); 136 } 137 138 void 139 ChangesIterator::PrintToStream() const 140 { 141 printf("ChangesIterator contents:\n"); 142 PathMap::Iterator iterator = fPathMap.GetIterator(); 143 while (iterator.HasNext()) { 144 const PathMap::Entry& entry = iterator.Next(); 145 const char* value; 146 switch (entry.value) { 147 case ENTRY_ADDED: 148 value = "ADDED"; 149 break; 150 case ENTRY_REMOVED: 151 value = "REMOVED"; 152 break; 153 case ENTRY_CHANGED: 154 value = "CHANGED"; 155 break; 156 default: 157 value = "???"; 158 break; 159 } 160 printf("entry: %s - %s\n", entry.key.GetString(), value); 161 } 162 } 163 164