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 // C Includes
13 // C++ Includes
14 #include <algorithm>
15 
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/Utility/Stream.h"
19 
20 using namespace lldb_private;
21 using namespace std;
22 
23 FileSpecList::FileSpecList() : m_files() {}
24 
25 FileSpecList::FileSpecList(const FileSpecList &rhs) = default;
26 
27 FileSpecList::~FileSpecList() = default;
28 
29 //------------------------------------------------------------------
30 // Assignment operator
31 //------------------------------------------------------------------
32 const FileSpecList &FileSpecList::operator=(const FileSpecList &rhs) {
33   if (this != &rhs)
34     m_files = rhs.m_files;
35   return *this;
36 }
37 
38 //------------------------------------------------------------------
39 // Append the "file_spec" to the end of the file spec list.
40 //------------------------------------------------------------------
41 void FileSpecList::Append(const FileSpec &file_spec) {
42   m_files.push_back(file_spec);
43 }
44 
45 //------------------------------------------------------------------
46 // Only append the "file_spec" if this list doesn't already contain
47 // it.
48 //
49 // Returns true if "file_spec" was added, false if this list already
50 // contained a copy of "file_spec".
51 //------------------------------------------------------------------
52 bool FileSpecList::AppendIfUnique(const FileSpec &file_spec) {
53   collection::iterator pos, end = m_files.end();
54   if (find(m_files.begin(), end, file_spec) == end) {
55     m_files.push_back(file_spec);
56     return true;
57   }
58   return false;
59 }
60 
61 //------------------------------------------------------------------
62 // Clears the file list.
63 //------------------------------------------------------------------
64 void FileSpecList::Clear() { m_files.clear(); }
65 
66 //------------------------------------------------------------------
67 // Dumps the file list to the supplied stream pointer "s".
68 //------------------------------------------------------------------
69 void FileSpecList::Dump(Stream *s, const char *separator_cstr) const {
70   collection::const_iterator pos, end = m_files.end();
71   for (pos = m_files.begin(); pos != end; ++pos) {
72     pos->Dump(s);
73     if (separator_cstr && ((pos + 1) != end))
74       s->PutCString(separator_cstr);
75   }
76 }
77 
78 //------------------------------------------------------------------
79 // Find the index of the file in the file spec list that matches
80 // "file_spec" starting "start_idx" entries into the file spec list.
81 //
82 // Returns the valid index of the file that matches "file_spec" if
83 // it is found, else std::numeric_limits<uint32_t>::max() is returned.
84 //------------------------------------------------------------------
85 size_t FileSpecList::FindFileIndex(size_t start_idx, const FileSpec &file_spec,
86                                    bool full, bool remove_dots) const {
87   const size_t num_files = m_files.size();
88 
89   // When looking for files, we will compare only the filename if the
90   // FILE_SPEC argument is empty
91   bool compare_filename_only = file_spec.GetDirectory().IsEmpty();
92 
93   for (size_t idx = start_idx; idx < num_files; ++idx) {
94     if (compare_filename_only) {
95       if (ConstString::Equals(
96               m_files[idx].GetFilename(), file_spec.GetFilename(),
97               file_spec.IsCaseSensitive() || m_files[idx].IsCaseSensitive()))
98         return idx;
99     } else {
100       if (FileSpec::Equal(m_files[idx], file_spec, full, remove_dots))
101         return idx;
102     }
103   }
104 
105   // We didn't find the file, return an invalid index
106   return UINT32_MAX;
107 }
108 
109 //------------------------------------------------------------------
110 // Returns the FileSpec object at index "idx". If "idx" is out of
111 // range, then an empty FileSpec object will be returned.
112 //------------------------------------------------------------------
113 const FileSpec &FileSpecList::GetFileSpecAtIndex(size_t idx) const {
114   if (idx < m_files.size())
115     return m_files[idx];
116   static FileSpec g_empty_file_spec;
117   return g_empty_file_spec;
118 }
119 
120 const FileSpec *FileSpecList::GetFileSpecPointerAtIndex(size_t idx) const {
121   if (idx < m_files.size())
122     return &m_files[idx];
123   return nullptr;
124 }
125 
126 //------------------------------------------------------------------
127 // Return the size in bytes that this object takes in memory. This
128 // returns the size in bytes of this object's member variables and
129 // any FileSpec objects its member variables contain, the result
130 // doesn't not include the string values for the directories any
131 // filenames as those are in shared string pools.
132 //------------------------------------------------------------------
133 size_t FileSpecList::MemorySize() const {
134   size_t mem_size = sizeof(FileSpecList);
135   collection::const_iterator pos, end = m_files.end();
136   for (pos = m_files.begin(); pos != end; ++pos) {
137     mem_size += pos->MemorySize();
138   }
139 
140   return mem_size;
141 }
142 
143 //------------------------------------------------------------------
144 // Return the number of files in the file spec list.
145 //------------------------------------------------------------------
146 size_t FileSpecList::GetSize() const { return m_files.size(); }
147 
148 size_t FileSpecList::GetFilesMatchingPartialPath(const char *path,
149                                                  bool dir_okay,
150                                                  FileSpecList &matches) {
151 #if 0 // FIXME: Just sketching...
152     matches.Clear();
153     FileSpec path_spec = FileSpec (path);
154     if (path_spec.Exists ())
155     {
156         FileSpec::FileType type = path_spec.GetFileType();
157         if (type == FileSpec::eFileTypeSymbolicLink)
158             // Shouldn't there be a Resolve on a file spec that real-path's it?
159         {
160         }
161 
162         if (type == FileSpec::eFileTypeRegular
163             || (type == FileSpec::eFileTypeDirectory && dir_okay))
164         {
165             matches.Append (path_spec);
166             return 1;
167         }
168         else if (type == FileSpec::eFileTypeDirectory)
169         {
170             // Fill the match list with all the files in the directory:
171         }
172         else
173         {
174             return 0;
175         }
176     }
177     else
178     {
179         ConstString dir_name = path_spec.GetDirectory();
180         ConstString file_name = GetFilename();
181         if (dir_name == nullptr)
182         {
183             // Match files in the CWD.
184         }
185         else
186         {
187             // Match files in the given directory:
188         }
189     }
190 #endif
191   return 0;
192 }
193