-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added another pointer example (#277)
- Loading branch information
1 parent
d043119
commit 6cb8273
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { printDirectoryTree } from './pointer-example-3.zig'; | ||
|
||
printDirectoryTree({ | ||
name: 'root', | ||
entries: [ | ||
{ file: { name: 'README', data: 'Hello world' } }, | ||
{ | ||
dir: { | ||
name: 'images', | ||
entries: [ | ||
{ file: { name: 'cat.jpg', data: new ArrayBuffer(8000) } }, | ||
{ file: { name: 'lobster.jpg', data: new ArrayBuffer(16000) } }, | ||
] | ||
} | ||
}, | ||
{ | ||
dir: { | ||
name: 'src', | ||
entries: [ | ||
{ file: { name: 'index.js', data: 'while (true) alert("You suck!")' } }, | ||
{ dir: { name: 'empty', entries: [] } }, | ||
] | ||
} | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
root/ | ||
README (11) | ||
images/ | ||
cat.jpg (8000) | ||
lobster.jpg (16000) | ||
src/ | ||
index.js (31) | ||
empty/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const std = @import("std"); | ||
|
||
pub const File = struct { | ||
name: []const u8, | ||
data: []const u8, | ||
}; | ||
pub const Directory = struct { | ||
name: []const u8, | ||
entries: []const DirectoryEntry, | ||
}; | ||
pub const DirectoryEntry = union(enum) { | ||
file: File, | ||
dir: Directory, | ||
}; | ||
|
||
fn indent(depth: u32) void { | ||
for (0..depth) |_| { | ||
std.debug.print(" ", .{}); | ||
} | ||
} | ||
|
||
fn printFile(file: File, depth: u32) void { | ||
indent(depth); | ||
std.debug.print("{s} ({d})\n", .{ file.name, file.data.len }); | ||
} | ||
|
||
fn printDirectory(dir: Directory, depth: u32) void { | ||
indent(depth); | ||
std.debug.print("{s}/\n", .{dir.name}); | ||
for (dir.entries) |entry| { | ||
switch (entry) { | ||
.file => |f| printFile(f, depth + 1), | ||
.dir => |d| printDirectory(d, depth + 1), | ||
} | ||
} | ||
} | ||
|
||
pub fn printDirectoryTree(dir: Directory) void { | ||
printDirectory(dir, 0); | ||
} |