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
92 {
93     for_each (m_files.begin(), m_files.end(), bind2nd(mem_fun_ref(&FileSpec::Dump),s));
94 }
95 
96 //------------------------------------------------------------------
97 // Find the index of the file in the file spec list that matches
98 // "file_spec" starting "start_idx" entries into the file spec list.
99 //
100 // Returns the valid index of the file that matches "file_spec" if
101 // it is found, else UINT32_MAX is returned.
102 //------------------------------------------------------------------
103 uint32_t
104 FileSpecList::FindFileIndex (uint32_t start_idx, const FileSpec &file_spec) const
105 {
106     const uint32_t num_files = m_files.size();
107     uint32_t idx;
108 
109     // When looking for files, we will compare only the filename if the
110     // FILE_SPEC argument is empty
111     bool compare_filename_only = file_spec.GetDirectory().IsEmpty();
112 
113     for (idx = start_idx; idx < num_files; ++idx)
114     {
115         if (compare_filename_only)
116         {
117             if (m_files[idx].GetFilename() == file_spec.GetFilename())
118                 return idx;
119         }
120         else
121         {
122             if (m_files[idx] == file_spec)
123                 return idx;
124         }
125     }
126 
127     // We didn't find the file, return an invalid index
128     return UINT32_MAX;
129 }
130 
131 //------------------------------------------------------------------
132 // Returns the FileSpec object at index "idx". If "idx" is out of
133 // range, then an empty FileSpec object will be returned.
134 //------------------------------------------------------------------
135 const FileSpec &
136 FileSpecList::GetFileSpecAtIndex(uint32_t idx) const
137 {
138 
139     if (idx < m_files.size())
140         return m_files[idx];
141     static FileSpec g_empty_file_spec;
142     return g_empty_file_spec;
143 }
144 
145 const FileSpec *
146 FileSpecList::GetFileSpecPointerAtIndex(uint32_t idx) const
147 {
148     if (idx < m_files.size())
149         return &m_files[idx];
150     return NULL;
151 }
152 
153 //------------------------------------------------------------------
154 // Return the size in bytes that this object takes in memory. This
155 // returns the size in bytes of this object's member variables and
156 // any FileSpec objects its member variables contain, the result
157 // doesn't not include the string values for the directories any
158 // filenames as those are in shared string pools.
159 //------------------------------------------------------------------
160 size_t
161 FileSpecList::MemorySize () const
162 {
163     size_t mem_size = sizeof(FileSpecList);
164     collection::const_iterator pos, end = m_files.end();
165     for (pos = m_files.begin(); pos != end; ++pos)
166     {
167         mem_size += pos->MemorySize();
168     }
169 
170     return mem_size;
171 }
172 
173 //------------------------------------------------------------------
174 // Return the number of files in the file spec list.
175 //------------------------------------------------------------------
176 uint32_t
177 FileSpecList::GetSize() const
178 {
179     return m_files.size();
180 }
181 
182 size_t
183 FileSpecList::GetFilesMatchingPartialPath (const char *path, bool dir_okay, FileSpecList &matches)
184 {
185 #if 0  // FIXME: Just sketching...
186     matches.Clear();
187     FileSpec path_spec = FileSpec (path);
188     if (path_spec.Exists ())
189     {
190         FileSpec::FileType type = path_spec.GetFileType();
191         if (type == FileSpec::eFileTypeSymbolicLink)
192             // Shouldn't there be a Resolve on a file spec that real-path's it?
193         {
194         }
195 
196         if (type == FileSpec::eFileTypeRegular
197             || (type == FileSpec::eFileTypeDirectory && dir_okay))
198         {
199             matches.Append (path_spec);
200             return 1;
201         }
202         else if (type == FileSpec::eFileTypeDirectory)
203         {
204             // Fill the match list with all the files in the directory:
205 
206         }
207         else
208         {
209             return 0;
210         }
211 
212     }
213     else
214     {
215         ConstString dir_name = path_spec.GetDirectory();
216         Constring file_name = GetFilename();
217         if (dir_name == NULL)
218         {
219             // Match files in the CWD.
220         }
221         else
222         {
223             // Match files in the given directory:
224 
225         }
226     }
227 #endif
228     return 0;
229 }
230