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 (NULL),
36 PathMappingList::PathMappingList (ChangedCallback callback,
37 void *callback_baton) :
39 m_callback (callback),
40 m_callback_baton (callback_baton),
46 PathMappingList::PathMappingList (const PathMappingList &rhs) :
47 m_pairs (rhs.m_pairs),
49 m_callback_baton (NULL),
55 const PathMappingList &
56 PathMappingList::operator =(const PathMappingList &rhs)
60 m_pairs = rhs.m_pairs;
62 m_callback_baton = NULL;
63 m_mod_id = rhs.m_mod_id;
69 //----------------------------------------------------------------------
71 //----------------------------------------------------------------------
72 PathMappingList::~PathMappingList ()
77 PathMappingList::Append (const ConstString &path,
78 const ConstString &replacement,
82 m_pairs.push_back(pair(path, replacement));
83 if (notify && m_callback)
84 m_callback (*this, m_callback_baton);
88 PathMappingList::Append (const PathMappingList &rhs, bool notify)
91 if (!rhs.m_pairs.empty())
93 const_iterator pos, end = rhs.m_pairs.end();
94 for (pos = rhs.m_pairs.begin(); pos != end; ++pos)
95 m_pairs.push_back(*pos);
96 if (notify && m_callback)
97 m_callback (*this, m_callback_baton);
102 PathMappingList::Insert (const ConstString &path,
103 const ConstString &replacement,
108 iterator insert_iter;
109 if (index >= m_pairs.size())
110 insert_iter = m_pairs.end();
112 insert_iter = m_pairs.begin() + index;
113 m_pairs.insert(insert_iter, pair(path, replacement));
114 if (notify && m_callback)
115 m_callback (*this, m_callback_baton);
119 PathMappingList::Replace (const ConstString &path,
120 const ConstString &replacement,
124 iterator insert_iter;
125 if (index >= m_pairs.size())
128 m_pairs[index] = pair(path, replacement);
129 if (notify && m_callback)
130 m_callback (*this, m_callback_baton);
135 PathMappingList::Remove (off_t index, bool notify)
137 if (index >= m_pairs.size())
141 iterator iter = m_pairs.begin() + index;
143 if (notify && m_callback)
144 m_callback (*this, m_callback_baton);
148 // For clients which do not need the pair index dumped, pass a pair_index >= 0
149 // to only dump the indicated pair.
151 PathMappingList::Dump (Stream *s, int pair_index)
153 unsigned int numPairs = m_pairs.size();
158 for (index = 0; index < numPairs; ++index)
159 s->Printf("[%d] \"%s\" -> \"%s\"\n",
160 index, m_pairs[index].first.GetCString(), m_pairs[index].second.GetCString());
164 if (pair_index < numPairs)
165 s->Printf("%s -> %s",
166 m_pairs[pair_index].first.GetCString(), m_pairs[pair_index].second.GetCString());
171 PathMappingList::Clear (bool notify)
173 if (!m_pairs.empty())
176 if (notify && m_callback)
177 m_callback (*this, m_callback_baton);
181 PathMappingList::RemapPath (const ConstString &path, ConstString &new_path) const
183 const char *path_cstr = path.GetCString();
188 const_iterator pos, end = m_pairs.end();
189 for (pos = m_pairs.begin(); pos != end; ++pos)
191 const size_t prefixLen = pos->first.GetLength();
193 if (::strncmp (pos->first.GetCString(), path_cstr, prefixLen) == 0)
195 std::string new_path_str (pos->second.GetCString());
196 new_path_str.append(path.GetCString() + prefixLen);
197 new_path.SetCString(new_path_str.c_str());
205 PathMappingList::RemapPath (const char *path, std::string &new_path) const
207 if (m_pairs.empty() || path == NULL || path[0] == '\0')
210 const_iterator pos, end = m_pairs.end();
211 for (pos = m_pairs.begin(); pos != end; ++pos)
213 const size_t prefix_len = pos->first.GetLength();
215 if (::strncmp (pos->first.GetCString(), path, prefix_len) == 0)
217 new_path = pos->second.GetCString();
218 new_path.append(path + prefix_len);
226 PathMappingList::FindFile (const FileSpec &orig_spec, FileSpec &new_spec) const
228 if (!m_pairs.empty())
230 char orig_path[PATH_MAX];
231 char new_path[PATH_MAX];
232 const size_t orig_path_len = orig_spec.GetPath (orig_path, sizeof(orig_path));
233 if (orig_path_len > 0)
235 const_iterator pos, end = m_pairs.end();
236 for (pos = m_pairs.begin(); pos != end; ++pos)
238 const size_t prefix_len = pos->first.GetLength();
240 if (orig_path_len >= prefix_len)
242 if (::strncmp (pos->first.GetCString(), orig_path, prefix_len) == 0)
244 const size_t new_path_len = snprintf(new_path, sizeof(new_path), "%s/%s", pos->second.GetCString(), orig_path + prefix_len);
245 if (new_path_len < sizeof(new_path))
247 new_spec.SetFile (new_path, true);
248 if (new_spec.Exists())
261 PathMappingList::Replace (const ConstString &path, const ConstString &new_path, bool notify)
263 uint32_t idx = FindIndexForPath (path);
264 if (idx < m_pairs.size())
267 m_pairs[idx].second = new_path;
268 if (notify && m_callback)
269 m_callback (*this, m_callback_baton);
276 PathMappingList::Remove (const ConstString &path, bool notify)
278 iterator pos = FindIteratorForPath (path);
279 if (pos != m_pairs.end())
283 if (notify && m_callback)
284 m_callback (*this, m_callback_baton);
290 PathMappingList::const_iterator
291 PathMappingList::FindIteratorForPath (const ConstString &path) const
294 const_iterator begin = m_pairs.begin();
295 const_iterator end = m_pairs.end();
297 for (pos = begin; pos != end; ++pos)
299 if (pos->first == path)
305 PathMappingList::iterator
306 PathMappingList::FindIteratorForPath (const ConstString &path)
309 iterator begin = m_pairs.begin();
310 iterator end = m_pairs.end();
312 for (pos = begin; pos != end; ++pos)
314 if (pos->first == path)
321 PathMappingList::GetPathsAtIndex (uint32_t idx, ConstString &path, ConstString &new_path) const
323 if (idx < m_pairs.size())
325 path = m_pairs[idx].first;
326 new_path = m_pairs[idx].second;
335 PathMappingList::FindIndexForPath (const ConstString &path) const
338 const_iterator begin = m_pairs.begin();
339 const_iterator end = m_pairs.end();
341 for (pos = begin; pos != end; ++pos)
343 if (pos->first == path)
344 return std::distance (begin, pos);