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