12#include "helpers/datetime.h"
14#include "helpers/formatstring.h"
15#include "helpers/makelocaltime.h"
36 size_t NAME_PADDING_SIZE = 20;
38 char indentspaces[20] = {0};
41 size_t nameSize = strlen(entryName);
42 size_t padding = NAME_PADDING_SIZE - nameSize - (depth * 2);
45 for (
int index = 0; index < depth * 2 && index < (int)(
sizeof(indentspaces) - 1); index++)
47 indentspaces[index] =
' ';
50 char* line =
formatstring(
"%s%s%-*s%4d %s\n", indentspaces, entryName, padding, dir_marker, entryLength, timestampAsString);
71 printf(
" Error! Out of memory appending line to the output in Composite_Exercise_FormatEntry()!\n");
76 printf(
" Error! Out of memory formatting line in Composite_Exercise_FormatEntry()!\n");
92 printf(
"%s\n", output.
string);
114 printf(
"\nComposite_Exercise\n");
116 const char* filepath =
"root";
118 if (rootEntry != NULL)
120 printf(
" Showing object '%s'\n", filepath);
123 filepath =
"root/subdir1/FileD.txt";
125 if (rootEntry != NULL)
127 printf(
" Showing object '%s'\n", filepath);
132 printf(
" Error! Unable to get a FileDirEntry for the path \"%s\"!", filepath);
137 printf(
" Error! Unable to get a FileDirEntry for the path \"%s\"!", filepath);
void Composite_Exercise(void)
Example of using the Composite Pattern.
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.
FileDirEntry * Composite_FileAccess_GetEntry(const char *path)
Return a FileDirEntry object representing the specified file "path" from an internal list of data ent...
const char * FileDirEntry_GetName(FileDirEntry *entry)
Retrieve the name of the given FileDirEntry object.
long FileDirEntry_GetLength(FileDirEntry *entry)
Get the length of the given FileDirEntry object. For Files, this is a static value....
FileDirTypes FileDirEntry_GetFileDirType(FileDirEntry *entry)
Get the type of this FileDirEntry object as a value from the FileDirTypes enumeration.
FileDirEntry * FileDirEntry_GetChildren(FileDirEntry *entry)
Retrieve a pointer to the first child of the given FileDirEntry object. If the entry does not support...
time_t FileDirEntry_GetWhenModified(FileDirEntry *entry)
Retrieve the last modified time of the given FileDirEntry object.
@ FileDirType_Directory
Represents a directory entry that can contain other FileDirEntry components.
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.
const char * datetime_to_string(time_t timestamp)
Convert the specified time stamp to a string.
void DynamicString_Clear(DynamicString *string)
Clear a DynamicString object, releasing any allocated memory. Resets to an empty string.
bool DynamicString_Append(DynamicString *string, const char *s)
Append the specified string to the DynamicString object.
Declaration of the DynamicString structure and supporting functions to work with dynamic strings.
Represents a string that can be grown dynamically.
char * string
The string that can grow.
Structure representing a File (FileEntry) or Directory (DirEntry) entry. This is included as the firs...
struct FileDirEntry * next
Points to the next entry in a linked list of FileDirEntry objects. NULL means no more in list.