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