Design Pattern Examples
Overview of object-oriented design patterns
Composite_FileAccess.c File Reference

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"
Include dependency graph for Composite_FileAccess.c:

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.
 
FileDirEntryComposite_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.
 

Detailed Description

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.

Function Documentation

◆ _AddChild()

void _AddChild ( DirEntry dirEntry,
FileDirEntry child 
)

Add a child to the given DirEntry object.

Parameters
dirEntryThe DirEntry to which to add the child.
childThe 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().

◆ _ConstructTree()

static void _ConstructTree ( void  )
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().

◆ _DestroyTree()

static void _DestroyTree ( FileDirEntry root)
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.

Parameters
rootA 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().

◆ _FindEntry()

static FileDirEntry * _FindEntry ( char *  path)
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.

Parameters
pathThe path to the file or directory to retrieve. This will be altered.
Returns
Returns a FileDirEntry* representing the desired file or directory. Returns NULL if the entry could not be found.

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().

◆ _NewDirEntry()

DirEntry * _NewDirEntry ( const char *  name,
time_t  whenModified 
)

Create a new DirEntry object with the specified properties.

Parameters
nameName of the directory entry.
whenModifiedTimestamp for when this directory entry was last modified.
Returns
Returns a DirEntry object. Returns NULL for out of memory error.

Definition at line 55 of file Composite_FileAccess.c.

References DirEntry::base, FileDirEntry::fileDirType, FileDirType_Directory, FileDirEntry::name, and FileDirEntry::whenModified.

Referenced by _ConstructTree().

◆ _NewFileEntry()

FileDirEntry * _NewFileEntry ( const char *  name,
long  length,
time_t  whenModified 
)

Create a new FileEntry object with the specified properties.

Parameters
nameName of the file entry.
lengthLength of the file entry.
whenModifiedTimestamp for when this file entry was last modified.
Returns
Returns a FileEntry object cast as a FileDirEntry pointer. Returns NULL for out of memory error.

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().

◆ Composite_FileAccess_GetEntry()

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.

Parameters
pathA "path" specifying the entry to find, with each component separated by '/'.
Returns
Returns a FileDirEntry object representing the specified file entry. Returns NULL if no entry could be found.

Definition at line 287 of file Composite_FileAccess.c.

References _FindEntry(), replace_chr(), and STRDUP.

Referenced by Composite_Exercise().

Variable Documentation

◆ _root

FileDirEntry* _root = NULL
static

The root of the constructed file/directory tree.

Definition at line 22 of file Composite_FileAccess.c.

Referenced by _ConstructTree(), and _FindEntry().