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