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