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 TRACE("ignoring: %s\n", path); 104 fPathMap.Remove(key); 105 } 106 } 107 108 109 void 110 ChangesIterator::EntryChanged(const char* path) 111 { 112 HashString key(path); 113 if (fPathMap.ContainsKey(key) && fPathMap.Get(key) == ENTRY_ADDED) 114 return; 115 116 TRACE("changed: %s\n", path); 117 118 fPathMap.Put(key, ENTRY_CHANGED); 119 } 120 121 122 bool 123 ChangesIterator::IsEmpty() const 124 { 125 PathMap::Iterator iterator = fPathMap.GetIterator(); 126 return !iterator.HasNext(); 127 } 128 129 void 130 ChangesIterator::PrintToStream() const 131 { 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; 150 } 151 printf("entry: %s - %s\n", entry.key.GetString(), value); 152 } 153 } 154 155