blob: 23eea225900e09a5af4be1c624ed23249815871d [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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
Eli Friedmanb1ed5162010-06-09 09:32:42 +000016#include "lldb/Target/PathMappingList.h"
17#include <string.h>
Chris Lattner24943d22010-06-08 16:52:24 +000018
19using namespace lldb;
20using namespace lldb_private;
21
22//----------------------------------------------------------------------
23// PathMappingList constructor
24//----------------------------------------------------------------------
25PathMappingList::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// Destructor
38//----------------------------------------------------------------------
39PathMappingList::~PathMappingList ()
40{
41}
42
43void
44PathMappingList::Append (const ConstString &path,
45 const ConstString &replacement,
46 bool notify)
47{
48 m_pairs.push_back(pair(path, replacement));
49 if (notify && m_callback)
50 m_callback (*this, m_callback_baton);
51}
52
53void
54PathMappingList::Insert (const ConstString &path,
55 const ConstString &replacement,
56 uint32_t index,
57 bool notify)
58{
59 iterator insert_iter;
60 if (index >= m_pairs.size())
61 insert_iter = m_pairs.end();
62 else
63 insert_iter = m_pairs.begin() + index;
64 m_pairs.insert(insert_iter, pair(path, replacement));
65 if (notify && m_callback)
66 m_callback (*this, m_callback_baton);
67}
68
69bool
70PathMappingList::Remove (off_t index, bool notify)
71{
72 if (index >= m_pairs.size())
73 return false;
74
75 iterator iter = m_pairs.begin() + index;
76 m_pairs.erase(iter);
77 if (notify && m_callback)
78 m_callback (*this, m_callback_baton);
79 return true;
80}
81
82void
83PathMappingList::Dump (Stream *s)
84{
85 unsigned int numPairs = m_pairs.size();
86 unsigned int index;
87
88 for (index = 0; index < numPairs; ++index)
89 {
90 s->Printf("[%d] \"%s\" -> \"%s\"\n",
91 index, m_pairs[index].first.GetCString(), m_pairs[index].second.GetCString());
92 }
93}
94
95void
96PathMappingList::Clear (bool notify)
97{
98 m_pairs.clear();
99 if (notify && m_callback)
100 m_callback (*this, m_callback_baton);
101}
102
103size_t
104PathMappingList::GetSize ()
105{
106 return m_pairs.size();
107}
108
109bool
110PathMappingList::RemapPath (const ConstString &path, ConstString &new_path)
111{
112 const_iterator pos, end = m_pairs.end();
113 for (pos = m_pairs.begin(); pos != end; ++pos)
114 {
115 const size_t prefixLen = pos->first.GetLength();
116
117 if (::strncmp (pos->first.GetCString(), path.GetCString(), prefixLen) == 0)
118 {
119 std::string new_path_str (pos->second.GetCString());
120 new_path_str.append(path.GetCString() + prefixLen);
121 new_path.SetCString(new_path_str.c_str());
122 return true;
123 }
124 }
125 return false;
126}