Implementation of the Composite_FileAccess_GetEntry() function along with the static file/directory list used in the example for the Composite Pattern. More...
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "helpers/datetime.h"
#include "helpers/replace.h"
#include "helpers/split.h"
#include "helpers/strdup.h"
#include "Composite_FileDirEntry.h"
#include "Composite_FileAccess.h"
Go to the source code of this file.
Functions | |
FileDirEntry * | _NewFileEntry (const char *name, long length, time_t whenModified) |
Create a new FileEntry object with the specified properties. | |
DirEntry * | _NewDirEntry (const char *name, time_t whenModified) |
Create a new DirEntry object with the specified properties. | |
void | _AddChild (DirEntry *dirEntry, FileDirEntry *child) |
Add a child to the given DirEntry object. | |
static void | _DestroyTree (FileDirEntry *root) |
Destroy a file/directory tree given its root. Each element of the hierarchical tree needs to be freed. After this function returns, the root pointer is no longer valid. | |
static void | _ConstructTree (void) |
Construct a file/directory tree with a predefined set of files and directories. If there are any out of memory conditions, the entire root built to that point is freed, leaving the program in the same state as before this function was called. | |
static FileDirEntry * | _FindEntry (char *path) |
Search the file/directory "tree" for an entry that matches the given file "path". The file path is a slash-separated list of directory names ending in either a directory name or a file name. | |
FileDirEntry * | Composite_FileAccess_GetEntry (const char *path) |
Return a FileDirEntry object representing the specified file "path" from an internal list of data entries that is organized in a file/ directory structure. | |
Variables | |
static FileDirEntry * | _root = NULL |
The root of the constructed file/directory tree. | |
Implementation of the Composite_FileAccess_GetEntry() function along with the static file/directory list used in the example for the Composite Pattern.
Definition in file Composite_FileAccess.c.
void _AddChild | ( | DirEntry * | dirEntry, |
FileDirEntry * | child | ||
) |
Add a child to the given DirEntry object.
dirEntry | The DirEntry to which to add the child. |
child | The FileDirEntry object to add as a child. |
Definition at line 73 of file Composite_FileAccess.c.
References DirEntry::_children, and FileDirEntry::next.
Referenced by _ConstructTree().
|
static |
Construct a file/directory tree with a predefined set of files and directories. If there are any out of memory conditions, the entire root built to that point is freed, leaving the program in the same state as before this function was called.
Definition at line 119 of file Composite_FileAccess.c.
References _AddChild(), _DestroyTree(), _NewDirEntry(), _NewFileEntry(), _root, and datetime_now().
Referenced by _FindEntry().
|
static |
Destroy a file/directory tree given its root. Each element of the hierarchical tree needs to be freed. After this function returns, the root pointer is no longer valid.
root | A FileDirEntry object representing the root of the tree to destroy. |
Definition at line 100 of file Composite_FileAccess.c.
References DirEntry::_children, _DestroyTree(), FileDirEntry::fileDirType, and FileDirType_Directory.
Referenced by _ConstructTree(), and _DestroyTree().
|
static |
Search the file/directory "tree" for an entry that matches the given file "path". The file path is a slash-separated list of directory names ending in either a directory name or a file name.
path | The path to the file or directory to retrieve. This will be altered. |
Definition at line 219 of file Composite_FileAccess.c.
References _ConstructTree(), _root, FileDirEntry_GetChildren(), FileDirEntry_GetName(), FileDirEntry::next, split(), SplitList_Clear(), SplitList::strings, and SplitList::strings_count.
Referenced by Composite_FileAccess_GetEntry(), and Composite_FileAccess::GetEntry().
DirEntry * _NewDirEntry | ( | const char * | name, |
time_t | whenModified | ||
) |
Create a new DirEntry object with the specified properties.
name | Name of the directory entry. |
whenModified | Timestamp for when this directory entry was last modified. |
Definition at line 55 of file Composite_FileAccess.c.
References DirEntry::base, FileDirEntry::fileDirType, FileDirType_Directory, FileDirEntry::name, and FileDirEntry::whenModified.
Referenced by _ConstructTree().
FileDirEntry * _NewFileEntry | ( | const char * | name, |
long | length, | ||
time_t | whenModified | ||
) |
Create a new FileEntry object with the specified properties.
name | Name of the file entry. |
length | Length of the file entry. |
whenModified | Timestamp for when this file entry was last modified. |
Definition at line 34 of file Composite_FileAccess.c.
References FileEntry::base, FileDirEntry::fileDirType, FileDirType_File, FileDirEntry::length, FileDirEntry::name, and FileDirEntry::whenModified.
Referenced by _ConstructTree().
FileDirEntry * Composite_FileAccess_GetEntry | ( | const char * | path | ) |
Return a FileDirEntry object representing the specified file "path" from an internal list of data entries that is organized in a file/ directory structure.
path | A "path" specifying the entry to find, with each component separated by '/'. |
Definition at line 287 of file Composite_FileAccess.c.
References _FindEntry(), replace_chr(), and STRDUP.
Referenced by Composite_Exercise().
|
static |
The root of the constructed file/directory tree.
Definition at line 22 of file Composite_FileAccess.c.
Referenced by _ConstructTree(), and _FindEntry().