Design Pattern Examples
Overview of object-oriented design patterns
Composite_FileDirEntry.c
Go to the documentation of this file.
1
6
8
9
11// FileDirEntry_GetFileDirType()
14{
16
17 if (entry != NULL)
18 {
19 type = entry->fileDirType;
20 }
21
22 return type;
23}
24
26// FileDirEntry_GetLength()
29{
30 long length = 0;
31
32 if (entry->fileDirType == FileDirType_File)
33 {
34 length = entry->length;
35 }
36 else if (entry->fileDirType == FileDirType_Directory)
37 {
38 DirEntry* dirEntry = (DirEntry*)entry;
39 if (dirEntry->_lengthSet)
40 {
41 length = dirEntry->base.length;
42 }
43 else
44 {
45 FileDirEntry* child = dirEntry->_children;
46 while (child != NULL)
47 {
48 length += FileDirEntry_GetLength(child);
49 child = child->next;
50 }
51 dirEntry->base.length = length;
52 dirEntry->_lengthSet = true;
53 }
54 }
55
56 return length;
57}
58
60// FileDirEntry_GetChildren()
63{
64 FileDirEntry* firstChild = NULL;
65
66 if (entry != NULL)
67 {
69 {
70 DirEntry* dirEntry = (DirEntry*)entry;
71 firstChild = dirEntry->_children;
72 }
73 }
74
75 return firstChild;
76}
77
79// FileDirEntry_GetName()
82{
83 const char* name = NULL;
84
85 if (entry != NULL)
86 {
87 name = entry->name;
88 }
89
90 return name;
91}
92
94// FileDirEntry_GetWhenModified()
97{
98 time_t whenModified = 0;
99
100 if (entry != NULL)
101 {
102 whenModified = entry->whenModified;
103 }
104
105 return whenModified;
106}
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.
FileDirTypes
Represents the type of entries allowed in the hierarchy for the Composite design pattern example.
@ FileDirType_Directory
Represents a directory entry that can contain other FileDirEntry components.
@ FileDirType_File
Represents a file entry.
@ FileDirType_Unknown
Represents an unknown type and is considered an error condition.
Implementation of the FileDirEntry, FileEntry, and DirEntry classes used in the Composite Pattern.
Represents a Directory entry.
FileDirEntry * _children
Linked list of possible children. NULL if empty.
bool _lengthSet
FileDirEntry base
Base FileDirEntry identifying this entry.
Structure representing a File (FileEntry) or Directory (DirEntry) entry. This is included as the firs...
FileDirTypes fileDirType
Value from the FileDirTypes enumeration indicating what type of entry this is a part of....
const char * name
Name of this entry. Use the FileDirEntry_GetName() function to get this value.
struct FileDirEntry * next
Points to the next entry in a linked list of FileDirEntry objects. NULL means no more in list.
long length
Length of this entry. For DirEntry objects, this is computed when getting the length by calling the F...
time_t whenModified
Timestamp of when this entry was last "modified". Use the FileDirEntry_GetWhenModified() function to ...