1 //===-- SBFileSpecList.cpp --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <limits.h>
10 
11 #include "Utils.h"
12 #include "lldb/API/SBFileSpec.h"
13 #include "lldb/API/SBFileSpecList.h"
14 #include "lldb/API/SBStream.h"
15 #include "lldb/Core/FileSpecList.h"
16 #include "lldb/Host/PosixApi.h"
17 #include "lldb/Utility/FileSpec.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/Utility/Stream.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 SBFileSpecList::SBFileSpecList() : m_opaque_up(new FileSpecList()) {}
25 
26 SBFileSpecList::SBFileSpecList(const SBFileSpecList &rhs) : m_opaque_up() {
27   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
28 
29   m_opaque_up = clone(rhs.m_opaque_up);
30 
31   if (log) {
32     log->Printf("SBFileSpecList::SBFileSpecList (const SBFileSpecList "
33                 "rhs.ap=%p) => SBFileSpecList(%p)",
34                 static_cast<void *>(rhs.m_opaque_up.get()),
35                 static_cast<void *>(m_opaque_up.get()));
36   }
37 }
38 
39 SBFileSpecList::~SBFileSpecList() {}
40 
41 const SBFileSpecList &SBFileSpecList::operator=(const SBFileSpecList &rhs) {
42   if (this != &rhs)
43     m_opaque_up = clone(rhs.m_opaque_up);
44   return *this;
45 }
46 
47 uint32_t SBFileSpecList::GetSize() const { return m_opaque_up->GetSize(); }
48 
49 void SBFileSpecList::Append(const SBFileSpec &sb_file) {
50   m_opaque_up->Append(sb_file.ref());
51 }
52 
53 bool SBFileSpecList::AppendIfUnique(const SBFileSpec &sb_file) {
54   return m_opaque_up->AppendIfUnique(sb_file.ref());
55 }
56 
57 void SBFileSpecList::Clear() { m_opaque_up->Clear(); }
58 
59 uint32_t SBFileSpecList::FindFileIndex(uint32_t idx, const SBFileSpec &sb_file,
60                                        bool full) {
61   return m_opaque_up->FindFileIndex(idx, sb_file.ref(), full);
62 }
63 
64 const SBFileSpec SBFileSpecList::GetFileSpecAtIndex(uint32_t idx) const {
65   SBFileSpec new_spec;
66   new_spec.SetFileSpec(m_opaque_up->GetFileSpecAtIndex(idx));
67   return new_spec;
68 }
69 
70 const lldb_private::FileSpecList *SBFileSpecList::operator->() const {
71   return m_opaque_up.get();
72 }
73 
74 const lldb_private::FileSpecList *SBFileSpecList::get() const {
75   return m_opaque_up.get();
76 }
77 
78 const lldb_private::FileSpecList &SBFileSpecList::operator*() const {
79   return *m_opaque_up;
80 }
81 
82 const lldb_private::FileSpecList &SBFileSpecList::ref() const {
83   return *m_opaque_up;
84 }
85 
86 bool SBFileSpecList::GetDescription(SBStream &description) const {
87   Stream &strm = description.ref();
88 
89   if (m_opaque_up) {
90     uint32_t num_files = m_opaque_up->GetSize();
91     strm.Printf("%d files: ", num_files);
92     for (uint32_t i = 0; i < num_files; i++) {
93       char path[PATH_MAX];
94       if (m_opaque_up->GetFileSpecAtIndex(i).GetPath(path, sizeof(path)))
95         strm.Printf("\n    %s", path);
96     }
97   } else
98     strm.PutCString("No value");
99 
100   return true;
101 }
102