1 //===-- FileSpecList.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "lldb/Core/FileSpecList.h"
10 #include "lldb/Core/Stream.h"
13 using namespace lldb_private;
16 //------------------------------------------------------------------
17 // Default constructor
18 //------------------------------------------------------------------
19 FileSpecList::FileSpecList() :
24 //------------------------------------------------------------------
26 //------------------------------------------------------------------
27 FileSpecList::FileSpecList(const FileSpecList& rhs) :
32 //------------------------------------------------------------------
34 //------------------------------------------------------------------
35 FileSpecList::~FileSpecList()
39 //------------------------------------------------------------------
40 // Assignment operator
41 //------------------------------------------------------------------
43 FileSpecList::operator= (const FileSpecList& rhs)
46 m_files = rhs.m_files;
50 //------------------------------------------------------------------
51 // Append the "file_spec" to the end of the file spec list.
52 //------------------------------------------------------------------
54 FileSpecList::Append(const FileSpec &file_spec)
56 m_files.push_back(file_spec);
59 //------------------------------------------------------------------
60 // Only append the "file_spec" if this list doesn't already contain
63 // Returns true if "file_spec" was added, false if this list already
64 // contained a copy of "file_spec".
65 //------------------------------------------------------------------
67 FileSpecList::AppendIfUnique(const FileSpec &file_spec)
69 collection::iterator pos, end = m_files.end();
70 if (find(m_files.begin(), end, file_spec) == end)
72 m_files.push_back(file_spec);
78 //------------------------------------------------------------------
79 // Clears the file list.
80 //------------------------------------------------------------------
87 //------------------------------------------------------------------
88 // Dumps the file list to the supplied stream pointer "s".
89 //------------------------------------------------------------------
91 FileSpecList::Dump(Stream *s, const char *separator_cstr) const
93 collection::const_iterator pos, end = m_files.end();
94 for (pos = m_files.begin(); pos != end; ++pos)
97 if (separator_cstr && ((pos + 1) != end))
98 s->PutCString(separator_cstr);
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.
106 // Returns the valid index of the file that matches "file_spec" if
107 // it is found, else UINT32_MAX is returned.
108 //------------------------------------------------------------------
110 FileSpecList::FindFileIndex (size_t start_idx, const FileSpec &file_spec, bool full) const
112 const size_t num_files = m_files.size();
114 // When looking for files, we will compare only the filename if the
115 // FILE_SPEC argument is empty
116 bool compare_filename_only = file_spec.GetDirectory().IsEmpty();
118 for (size_t idx = start_idx; idx < num_files; ++idx)
120 if (compare_filename_only)
122 if (m_files[idx].GetFilename() == file_spec.GetFilename())
127 if (FileSpec::Equal (m_files[idx], file_spec, full))
132 // We didn't find the file, return an invalid index
136 //------------------------------------------------------------------
137 // Returns the FileSpec object at index "idx". If "idx" is out of
138 // range, then an empty FileSpec object will be returned.
139 //------------------------------------------------------------------
141 FileSpecList::GetFileSpecAtIndex(size_t idx) const
144 if (idx < m_files.size())
146 static FileSpec g_empty_file_spec;
147 return g_empty_file_spec;
151 FileSpecList::GetFileSpecPointerAtIndex(size_t idx) const
153 if (idx < m_files.size())
154 return &m_files[idx];
158 //------------------------------------------------------------------
159 // Return the size in bytes that this object takes in memory. This
160 // returns the size in bytes of this object's member variables and
161 // any FileSpec objects its member variables contain, the result
162 // doesn't not include the string values for the directories any
163 // filenames as those are in shared string pools.
164 //------------------------------------------------------------------
166 FileSpecList::MemorySize () const
168 size_t mem_size = sizeof(FileSpecList);
169 collection::const_iterator pos, end = m_files.end();
170 for (pos = m_files.begin(); pos != end; ++pos)
172 mem_size += pos->MemorySize();
178 //------------------------------------------------------------------
179 // Return the number of files in the file spec list.
180 //------------------------------------------------------------------
182 FileSpecList::GetSize() const
184 return m_files.size();
188 FileSpecList::GetFilesMatchingPartialPath (const char *path, bool dir_okay, FileSpecList &matches)
190 #if 0 // FIXME: Just sketching...
192 FileSpec path_spec = FileSpec (path);
193 if (path_spec.Exists ())
195 FileSpec::FileType type = path_spec.GetFileType();
196 if (type == FileSpec::eFileTypeSymbolicLink)
197 // Shouldn't there be a Resolve on a file spec that real-path's it?
201 if (type == FileSpec::eFileTypeRegular
202 || (type == FileSpec::eFileTypeDirectory && dir_okay))
204 matches.Append (path_spec);
207 else if (type == FileSpec::eFileTypeDirectory)
209 // Fill the match list with all the files in the directory:
220 ConstString dir_name = path_spec.GetDirectory();
221 Constring file_name = GetFilename();
222 if (dir_name == NULL)
224 // Match files in the CWD.
228 // Match files in the given directory: