10#include "helpers/formatstring.h"
32 const size_t NAME_PADDING_SIZE = 20;
33 std::string output =
"";
34 std::string spaces(
static_cast<size_t>(depth) * 2,
' ');
36 size_t padding = NAME_PADDING_SIZE - entry->
Name().size() - (
static_cast<size_t>(depth) * 2);
37 if (entry->
FileDirType() == FileDirTypes::Directory)
42 output.append(std::string(padding,
' '));
48 if (!children.empty())
50 for (
size_t index = 0; index < children.size(); ++index)
68 std::cout << output << std::endl;
91 std::cout << std::endl;
92 std::cout <<
"Composite Exercise" << std::endl;
96 std::string filepath =
"root";
98 std::cout <<
" Showing object '" << filepath <<
"'" << std::endl;
101 filepath =
"root/subdir1/FileD.txt";
103 std::cout <<
" Showing object '" << filepath <<
"'" << std::endl;
106 catch (std::filesystem::filesystem_error& e)
108 std::cout <<
"Error! filesystem_error: " << e.what() << std::endl;
111 std::cout <<
" Done." << std::endl;
static void Composite_Exercise_ShowEntry(FileDirEntry *entry)
Recursively display the contents of the hierarchical list of FileDirEntry objects starting with the g...
static void Composite_Exercise_FormatEntry(FileDirEntry *entry, int depth, DynamicString *output)
Format the specified entry for display.
static FileDirEntry * GetEntry(std::string filepath)
Return a FileDirEntry object representing the specified file "path" in an internal list of data entri...
Base class representing a File or Directory entry.
virtual FileDirTypes FileDirType()
The type of this entry as represented by a value from the FileDirTypes enumeration.
virtual std::string Name()
The name of this entry.
virtual DateTime WhenModified()
When this entry was last modified.
virtual long Length()
The length in bytes of this entry. Directory entries are the sum of the length of all children.
virtual FileDirEntryList Children()
The children of this entry. Is empty if the entry can never have any children (that is,...
std::string ToString()
Format the DateTime as a string. The format is "standard" (in this case, preset to 02/22/2023 10:26:1...
Declaration of the Composite_Exercise() function as used in the Composite Pattern.
Declaration of the Composite_FileAccess class used in the Composite Pattern.
Implementation of the FileDirEntry, FileEntry, and DirEntry classes used in the Composite Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
std::vector< std::shared_ptr< FileDirEntry > > FileDirEntryList
Makes it easier to refer to a list of child nodes.
void Composite_Exercise()
Example of using the Composite design pattern.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....