Design Pattern Examples
Overview of object-oriented design patterns
Composite_FileAccess.cs
Go to the documentation of this file.
1
14
15using System;
16using System.IO;
17
19{
24 public static class Composite_FileAccess
25 {
26 // The hardcoded hierarchy representing a file/directory structure.
27 // Note: This is the Composite pattern in action.
29 new DirEntry("root", DateTime.Now, new FileDirEntry[] {
30 new FileEntry("FileA.txt", 101, DateTime.Now),
31 new FileEntry("FileB.txt", 102, DateTime.Now),
32 new FileEntry("FileC.txt", 103, DateTime.Now),
33 new DirEntry("subdir1", DateTime.Now, new FileDirEntry[] {
34 new FileEntry("FileD.txt", 104, DateTime.Now),
35 new FileEntry("FileE.txt", 105, DateTime.Now),
36 new DirEntry("subdir2", DateTime.Now, new FileDirEntry[] {
37 new FileEntry("FileF.txt", 106, DateTime.Now),
38 new FileEntry("FileG.txt", 107, DateTime.Now)
39 }),
40 }),
41 });
42
43
52 private static FileDirEntry? _FindEntry(string filepath)
53 {
54 FileDirEntry? root = rootEntry;
55
56 string[] pathComponents = filepath.Split('/');
57 int numComponents = pathComponents.Length;
58 for (int index = 0; index < numComponents; ++index)
59 {
60 if (root.Name != pathComponents[index])
61 {
62 // Mismatch in path to this entry, bad path
63 root = null;
64 break;
65 }
66 if (index + 1 >= numComponents)
67 {
68 // Reached end of path so we found what was asked for.
69 break;
70 }
71
72 // Still haven't reached end of specified path, look at
73 // the current root for children.
74
75 FileDirEntry[]? children = root.Children;
76 if (children == null)
77 {
78 // Path included leaf in the middle, bad path
79 root = null;
80 break;
81 }
82
83 root = null; // Assume we won't find matching child
84 // Look ahead in the path for a matching child.
85 string childComponent = pathComponents[index + 1];
86 foreach (FileDirEntry child in children)
87 {
88 if (childComponent == child.Name)
89 {
90 root = child;
91 break;
92 }
93 }
94 if (root == null)
95 {
96 // Couldn't find matching child, bad path
97 break;
98 }
99 }
100
101 // Root is either a valid file/directory entry that matches the
102 // requested path or it is null, indicating the entry could not be
103 // found.
104 return root;
105 }
106
107
118 public static FileDirEntry GetEntry(string filepath)
119 {
120 filepath = filepath.Replace('\\', '/');
121 FileDirEntry? filedirEntry = _FindEntry(filepath);
122
123 if (filedirEntry == null)
124 {
125 string msg = String.Format("Unable to find '{0}'", filepath);
126 throw new FileNotFoundException(msg);
127 }
128
129 return filedirEntry;
130 }
131 }
132}
static FileDirEntry * _FindEntry(char *path)
Search the file/directory "tree" for an entry that matches the given file "path". The file path is a ...
Static class containing functions for accessing a hardcoded "file" and "directory" hierarchy.
static FileDirEntry GetEntry(string filepath)
Return a FileDirEntry object representing the specified file "path" in an internal list of data entri...
static ? FileDirEntry _FindEntry(string filepath)
Helper method to search the static data list for the specified file/dir entry.
Represents a Directory entry.
Base class representing a File or Directory entry.
virtual long Length
The length in bytes of this entry. Directory entries are the sum of the length of all children.
virtual ? FileDirEntry[] Children
The children of this entry. Is null if the entry can never have any children (that is,...
virtual string Name
The name of this entry.
The namespace containing all Design Pattern Examples implemented in C#.