1 //===-- PathMappingList.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
15 // Other libraries and framework includes
17 #include "lldb/Core/Error.h"
18 #include "lldb/Core/Stream.h"
19 #include "lldb/Host/FileSpec.h"
20 #include "lldb/Target/PathMappingList.h"
23 using namespace lldb_private;
25 //----------------------------------------------------------------------
26 // PathMappingList constructor
27 //----------------------------------------------------------------------
28 PathMappingList::PathMappingList () :
31 m_callback_baton(nullptr),
36 PathMappingList::PathMappingList (ChangedCallback callback,
37 void *callback_baton) :
39 m_callback (callback),
40 m_callback_baton (callback_baton),
45 PathMappingList::PathMappingList (const PathMappingList &rhs) :
48 m_callback_baton(nullptr),
53 const PathMappingList &
54 PathMappingList::operator =(const PathMappingList &rhs)
58 m_pairs = rhs.m_pairs;
60 m_callback_baton = nullptr;
61 m_mod_id = rhs.m_mod_id;
66 PathMappingList::~PathMappingList() = default;
69 PathMappingList::Append (const ConstString &path,
70 const ConstString &replacement,
74 m_pairs.push_back(pair(path, replacement));
75 if (notify && m_callback)
76 m_callback (*this, m_callback_baton);
80 PathMappingList::Append (const PathMappingList &rhs, bool notify)
83 if (!rhs.m_pairs.empty())
85 const_iterator pos, end = rhs.m_pairs.end();
86 for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
87 m_pairs.push_back(*pos);
88 if (notify && m_callback)
89 m_callback (*this, m_callback_baton);
94 PathMappingList::Insert (const ConstString &path,
95 const ConstString &replacement,
100 iterator insert_iter;
101 if (index >= m_pairs.size())
102 insert_iter = m_pairs.end();
104 insert_iter = m_pairs.begin() + index;
105 m_pairs.insert(insert_iter, pair(path, replacement));
106 if (notify && m_callback)
107 m_callback (*this, m_callback_baton);
111 PathMappingList::Replace (const ConstString &path,
112 const ConstString &replacement,
116 iterator insert_iter;
117 if (index >= m_pairs.size())
120 m_pairs[index] = pair(path, replacement);
121 if (notify && m_callback)
122 m_callback (*this, m_callback_baton);
127 PathMappingList::Remove (size_t index, bool notify)
129 if (index >= m_pairs.size())
133 iterator iter = m_pairs.begin() + index;
135 if (notify && m_callback)
136 m_callback (*this, m_callback_baton);
140 // For clients which do not need the pair index dumped, pass a pair_index >= 0
141 // to only dump the indicated pair.
143 PathMappingList::Dump (Stream *s, int pair_index)
145 unsigned int numPairs = m_pairs.size();
150 for (index = 0; index < numPairs; ++index)
151 s->Printf("[%d] \"%s\" -> \"%s\"\n",
152 index, m_pairs[index].first.GetCString(), m_pairs[index].second.GetCString());
156 if (static_cast<unsigned int>(pair_index) < numPairs)
157 s->Printf("%s -> %s",
158 m_pairs[pair_index].first.GetCString(), m_pairs[pair_index].second.GetCString());
163 PathMappingList::Clear (bool notify)
165 if (!m_pairs.empty())
168 if (notify && m_callback)
169 m_callback (*this, m_callback_baton);
173 PathMappingList::RemapPath (const ConstString &path, ConstString &new_path) const
175 const char *path_cstr = path.GetCString();
180 const_iterator pos, end = m_pairs.end();
181 for (pos = m_pairs.begin(); pos != end; ++pos)
183 const size_t prefixLen = pos->first.GetLength();
185 if (::strncmp (pos->first.GetCString(), path_cstr, prefixLen) == 0)
187 std::string new_path_str (pos->second.GetCString());
188 new_path_str.append(path.GetCString() + prefixLen);
189 new_path.SetCString(new_path_str.c_str());
197 PathMappingList::RemapPath (const char *path, std::string &new_path) const
199 if (m_pairs.empty() || path == nullptr || path[0] == '\0')
202 const_iterator pos, end = m_pairs.end();
203 for (pos = m_pairs.begin(); pos != end; ++pos)
205 const size_t prefix_len = pos->first.GetLength();
207 if (::strncmp (pos->first.GetCString(), path, prefix_len) == 0)
209 new_path = pos->second.GetCString();
210 new_path.append(path + prefix_len);
218 PathMappingList::ReverseRemapPath (const ConstString &path, ConstString &new_path) const
220 const char *path_cstr = path.GetCString();
224 for (const auto& it : m_pairs)
226 const size_t prefixLen = it.second.GetLength();
227 if (::strncmp (it.second.GetCString(), path_cstr, prefixLen) == 0)
229 std::string new_path_str (it.first.GetCString());
230 new_path_str.append(path.GetCString() + prefixLen);
231 new_path.SetCString(new_path_str.c_str());
239 PathMappingList::FindFile (const FileSpec &orig_spec, FileSpec &new_spec) const
241 if (!m_pairs.empty())
243 char orig_path[PATH_MAX];
244 char new_path[PATH_MAX];
245 const size_t orig_path_len = orig_spec.GetPath (orig_path, sizeof(orig_path));
246 if (orig_path_len > 0)
248 const_iterator pos, end = m_pairs.end();
249 for (pos = m_pairs.begin(); pos != end; ++pos)
251 const size_t prefix_len = pos->first.GetLength();
253 if (orig_path_len >= prefix_len)
255 if (::strncmp (pos->first.GetCString(), orig_path, prefix_len) == 0)
257 const size_t new_path_len = snprintf(new_path, sizeof(new_path), "%s/%s", pos->second.GetCString(), orig_path + prefix_len);
258 if (new_path_len < sizeof(new_path))
260 new_spec.SetFile (new_path, true);
261 if (new_spec.Exists())
274 PathMappingList::Replace (const ConstString &path, const ConstString &new_path, bool notify)
276 uint32_t idx = FindIndexForPath (path);
277 if (idx < m_pairs.size())
280 m_pairs[idx].second = new_path;
281 if (notify && m_callback)
282 m_callback (*this, m_callback_baton);
289 PathMappingList::Remove (const ConstString &path, bool notify)
291 iterator pos = FindIteratorForPath (path);
292 if (pos != m_pairs.end())
296 if (notify && m_callback)
297 m_callback (*this, m_callback_baton);
303 PathMappingList::const_iterator
304 PathMappingList::FindIteratorForPath (const ConstString &path) const
307 const_iterator begin = m_pairs.begin();
308 const_iterator end = m_pairs.end();
310 for (pos = begin; pos != end; ++pos)
312 if (pos->first == path)
318 PathMappingList::iterator
319 PathMappingList::FindIteratorForPath (const ConstString &path)
322 iterator begin = m_pairs.begin();
323 iterator end = m_pairs.end();
325 for (pos = begin; pos != end; ++pos)
327 if (pos->first == path)
334 PathMappingList::GetPathsAtIndex (uint32_t idx, ConstString &path, ConstString &new_path) const
336 if (idx < m_pairs.size())
338 path = m_pairs[idx].first;
339 new_path = m_pairs[idx].second;
346 PathMappingList::FindIndexForPath (const ConstString &path) const
349 const_iterator begin = m_pairs.begin();
350 const_iterator end = m_pairs.end();
352 for (pos = begin; pos != end; ++pos)
354 if (pos->first == path)
355 return std::distance (begin, pos);