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 // Destructor
38 //----------------------------------------------------------------------
39 PathMappingList::~PathMappingList ()
40 {
41 }
42 
43 void
44 PathMappingList::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 
53 void
54 PathMappingList::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 
69 bool
70 PathMappingList::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 
82 void
83 PathMappingList::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 
95 void
96 PathMappingList::Clear (bool notify)
97 {
98     m_pairs.clear();
99     if (notify && m_callback)
100         m_callback (*this, m_callback_baton);
101 }
102 
103 size_t
104 PathMappingList::GetSize ()
105 {
106     return m_pairs.size();
107 }
108 
109 bool
110 PathMappingList::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 }
127