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