1 //===-- FileSpecList.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 #include "lldb/Core/FileSpecList.h"
11
12 #include "lldb/Utility/ConstString.h"
13 #include "lldb/Utility/Stream.h"
14
15 #include <utility>
16
17 #include <stdint.h>
18
19 using namespace lldb_private;
20 using namespace std;
21
FileSpecList()22 FileSpecList::FileSpecList() : m_files() {}
23
24 FileSpecList::FileSpecList(const FileSpecList &rhs) = default;
25
26 FileSpecList::~FileSpecList() = default;
27
28 //------------------------------------------------------------------
29 // Assignment operator
30 //------------------------------------------------------------------
operator =(const FileSpecList & rhs)31 const FileSpecList &FileSpecList::operator=(const FileSpecList &rhs) {
32 if (this != &rhs)
33 m_files = rhs.m_files;
34 return *this;
35 }
36
37 //------------------------------------------------------------------
38 // Append the "file_spec" to the end of the file spec list.
39 //------------------------------------------------------------------
Append(const FileSpec & file_spec)40 void FileSpecList::Append(const FileSpec &file_spec) {
41 m_files.push_back(file_spec);
42 }
43
44 //------------------------------------------------------------------
45 // Only append the "file_spec" if this list doesn't already contain it.
46 //
47 // Returns true if "file_spec" was added, false if this list already contained
48 // a copy of "file_spec".
49 //------------------------------------------------------------------
AppendIfUnique(const FileSpec & file_spec)50 bool FileSpecList::AppendIfUnique(const FileSpec &file_spec) {
51 collection::iterator end = m_files.end();
52 if (find(m_files.begin(), end, file_spec) == end) {
53 m_files.push_back(file_spec);
54 return true;
55 }
56 return false;
57 }
58
59 //------------------------------------------------------------------
60 // Clears the file list.
61 //------------------------------------------------------------------
Clear()62 void FileSpecList::Clear() { m_files.clear(); }
63
64 //------------------------------------------------------------------
65 // Dumps the file list to the supplied stream pointer "s".
66 //------------------------------------------------------------------
Dump(Stream * s,const char * separator_cstr) const67 void FileSpecList::Dump(Stream *s, const char *separator_cstr) const {
68 collection::const_iterator pos, end = m_files.end();
69 for (pos = m_files.begin(); pos != end; ++pos) {
70 pos->Dump(s);
71 if (separator_cstr && ((pos + 1) != end))
72 s->PutCString(separator_cstr);
73 }
74 }
75
76 //------------------------------------------------------------------
77 // Find the index of the file in the file spec list that matches "file_spec"
78 // starting "start_idx" entries into the file spec list.
79 //
80 // Returns the valid index of the file that matches "file_spec" if it is found,
81 // else std::numeric_limits<uint32_t>::max() is returned.
82 //------------------------------------------------------------------
FindFileIndex(size_t start_idx,const FileSpec & file_spec,bool full) const83 size_t FileSpecList::FindFileIndex(size_t start_idx, const FileSpec &file_spec,
84 bool full) const {
85 const size_t num_files = m_files.size();
86
87 // When looking for files, we will compare only the filename if the FILE_SPEC
88 // argument is empty
89 bool compare_filename_only = file_spec.GetDirectory().IsEmpty();
90
91 for (size_t idx = start_idx; idx < num_files; ++idx) {
92 if (compare_filename_only) {
93 if (ConstString::Equals(
94 m_files[idx].GetFilename(), file_spec.GetFilename(),
95 file_spec.IsCaseSensitive() || m_files[idx].IsCaseSensitive()))
96 return idx;
97 } else {
98 if (FileSpec::Equal(m_files[idx], file_spec, full))
99 return idx;
100 }
101 }
102
103 // We didn't find the file, return an invalid index
104 return UINT32_MAX;
105 }
106
107 //------------------------------------------------------------------
108 // Returns the FileSpec object at index "idx". If "idx" is out of range, then
109 // an empty FileSpec object will be returned.
110 //------------------------------------------------------------------
GetFileSpecAtIndex(size_t idx) const111 const FileSpec &FileSpecList::GetFileSpecAtIndex(size_t idx) const {
112 if (idx < m_files.size())
113 return m_files[idx];
114 static FileSpec g_empty_file_spec;
115 return g_empty_file_spec;
116 }
117
GetFileSpecPointerAtIndex(size_t idx) const118 const FileSpec *FileSpecList::GetFileSpecPointerAtIndex(size_t idx) const {
119 if (idx < m_files.size())
120 return &m_files[idx];
121 return nullptr;
122 }
123
124 //------------------------------------------------------------------
125 // Return the size in bytes that this object takes in memory. This returns the
126 // size in bytes of this object's member variables and any FileSpec objects its
127 // member variables contain, the result doesn't not include the string values
128 // for the directories any filenames as those are in shared string pools.
129 //------------------------------------------------------------------
MemorySize() const130 size_t FileSpecList::MemorySize() const {
131 size_t mem_size = sizeof(FileSpecList);
132 collection::const_iterator pos, end = m_files.end();
133 for (pos = m_files.begin(); pos != end; ++pos) {
134 mem_size += pos->MemorySize();
135 }
136
137 return mem_size;
138 }
139
140 //------------------------------------------------------------------
141 // Return the number of files in the file spec list.
142 //------------------------------------------------------------------
GetSize() const143 size_t FileSpecList::GetSize() const { return m_files.size(); }
144
GetFilesMatchingPartialPath(const char * path,bool dir_okay,FileSpecList & matches)145 size_t FileSpecList::GetFilesMatchingPartialPath(const char *path,
146 bool dir_okay,
147 FileSpecList &matches) {
148 return 0;
149 }
150