1 //===-- PathMappingList.cpp -------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // C Includes 11 // C++ Includes 12 // Other libraries and framework includes 13 #include "lldb/Core/Error.h" 14 #include "lldb/Core/Stream.h" 15 // Project includes 16 #include "lldb/Target/PathMappingList.h" 17 #include <string.h> 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 //---------------------------------------------------------------------- 23 // PathMappingList constructor 24 //---------------------------------------------------------------------- 25 PathMappingList::PathMappingList 26 ( 27 ChangedCallback callback, 28 void *callback_baton 29 ) : 30 m_pairs (), 31 m_callback (callback), 32 m_callback_baton (callback_baton) 33 { 34 } 35 36 37 PathMappingList::PathMappingList (const PathMappingList &rhs) : 38 m_pairs (rhs.m_pairs), 39 m_callback (NULL), 40 m_callback_baton (NULL) 41 { 42 43 } 44 45 const PathMappingList & 46 PathMappingList::operator =(const PathMappingList &rhs) 47 { 48 if (this != &rhs) 49 { 50 m_pairs = rhs.m_pairs; 51 m_callback = NULL; 52 m_callback_baton = NULL; 53 } 54 return *this; 55 } 56 57 58 //---------------------------------------------------------------------- 59 // Destructor 60 //---------------------------------------------------------------------- 61 PathMappingList::~PathMappingList () 62 { 63 } 64 65 void 66 PathMappingList::Append (const ConstString &path, 67 const ConstString &replacement, 68 bool notify) 69 { 70 m_pairs.push_back(pair(path, replacement)); 71 if (notify && m_callback) 72 m_callback (*this, m_callback_baton); 73 } 74 75 void 76 PathMappingList::Insert (const ConstString &path, 77 const ConstString &replacement, 78 uint32_t index, 79 bool notify) 80 { 81 iterator insert_iter; 82 if (index >= m_pairs.size()) 83 insert_iter = m_pairs.end(); 84 else 85 insert_iter = m_pairs.begin() + index; 86 m_pairs.insert(insert_iter, pair(path, replacement)); 87 if (notify && m_callback) 88 m_callback (*this, m_callback_baton); 89 } 90 91 bool 92 PathMappingList::Remove (off_t index, bool notify) 93 { 94 if (index >= m_pairs.size()) 95 return false; 96 97 iterator iter = m_pairs.begin() + index; 98 m_pairs.erase(iter); 99 if (notify && m_callback) 100 m_callback (*this, m_callback_baton); 101 return true; 102 } 103 104 // For clients which do not need the pair index dumped, pass a pair_index >= 0 105 // to only dump the indicated pair. 106 void 107 PathMappingList::Dump (Stream *s, int pair_index) 108 { 109 unsigned int numPairs = m_pairs.size(); 110 111 if (pair_index < 0) 112 { 113 unsigned int index; 114 for (index = 0; index < numPairs; ++index) 115 s->Printf("[%d] \"%s\" -> \"%s\"\n", 116 index, m_pairs[index].first.GetCString(), m_pairs[index].second.GetCString()); 117 } 118 else 119 { 120 if (pair_index < numPairs) 121 s->Printf("%s -> %s", 122 m_pairs[pair_index].first.GetCString(), m_pairs[pair_index].second.GetCString()); 123 } 124 } 125 126 void 127 PathMappingList::Clear (bool notify) 128 { 129 m_pairs.clear(); 130 if (notify && m_callback) 131 m_callback (*this, m_callback_baton); 132 } 133 134 size_t 135 PathMappingList::GetSize () 136 { 137 return m_pairs.size(); 138 } 139 140 bool 141 PathMappingList::RemapPath (const ConstString &path, ConstString &new_path) 142 { 143 const_iterator pos, end = m_pairs.end(); 144 for (pos = m_pairs.begin(); pos != end; ++pos) 145 { 146 const size_t prefixLen = pos->first.GetLength(); 147 148 if (::strncmp (pos->first.GetCString(), path.GetCString(), prefixLen) == 0) 149 { 150 std::string new_path_str (pos->second.GetCString()); 151 new_path_str.append(path.GetCString() + prefixLen); 152 new_path.SetCString(new_path_str.c_str()); 153 return true; 154 } 155 } 156 return false; 157 } 158 159 bool 160 PathMappingList::Replace (const ConstString &path, const ConstString &new_path, bool notify) 161 { 162 uint32_t idx = FindIndexForPath (path); 163 if (idx < m_pairs.size()) 164 { 165 m_pairs[idx].second = new_path; 166 if (notify && m_callback) 167 m_callback (*this, m_callback_baton); 168 return true; 169 } 170 return false; 171 } 172 173 bool 174 PathMappingList::Remove (const ConstString &path, bool notify) 175 { 176 iterator pos = FindIteratorForPath (path); 177 if (pos != m_pairs.end()) 178 { 179 m_pairs.erase (pos); 180 if (notify && m_callback) 181 m_callback (*this, m_callback_baton); 182 return true; 183 } 184 return false; 185 } 186 187 PathMappingList::const_iterator 188 PathMappingList::FindIteratorForPath (const ConstString &path) const 189 { 190 const_iterator pos; 191 const_iterator begin = m_pairs.begin(); 192 const_iterator end = m_pairs.end(); 193 194 for (pos = begin; pos != end; ++pos) 195 { 196 if (pos->first == path) 197 break; 198 } 199 return pos; 200 } 201 202 PathMappingList::iterator 203 PathMappingList::FindIteratorForPath (const ConstString &path) 204 { 205 iterator pos; 206 iterator begin = m_pairs.begin(); 207 iterator end = m_pairs.end(); 208 209 for (pos = begin; pos != end; ++pos) 210 { 211 if (pos->first == path) 212 break; 213 } 214 return pos; 215 } 216 217 bool 218 PathMappingList::GetPathsAtIndex (uint32_t idx, ConstString &path, ConstString &new_path) const 219 { 220 if (idx < m_pairs.size()) 221 { 222 path = m_pairs[idx].first; 223 new_path = m_pairs[idx].second; 224 return true; 225 } 226 return false; 227 } 228 229 230 231 uint32_t 232 PathMappingList::FindIndexForPath (const ConstString &path) const 233 { 234 const_iterator pos; 235 const_iterator begin = m_pairs.begin(); 236 const_iterator end = m_pairs.end(); 237 238 for (pos = begin; pos != end; ++pos) 239 { 240 if (pos->first == path) 241 return std::distance (begin, pos); 242 } 243 return UINT32_MAX; 244 } 245 246