Skip to content

Commit ec75f87

Browse files
feat(cli): implement concrete iterate directory function
1 parent 1f8bfb8 commit ec75f87

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

src/cli/cli.zig

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,46 @@ const CLI = struct {
3232
list_of_chapter_support_files: STD.ArrayList([]const u8) = .empty,
3333
};
3434

35+
pub fn iterateDirectory(
36+
allocator: STD.mem.Allocator,
37+
extra_options: struct {
38+
dir_path: []const u8 = "exercises",
39+
},
40+
) !STD.ArrayList([]const u8) {
41+
var list_of_contents: STD.ArrayList([]const u8) = .empty;
42+
43+
const TOP_LEVEL_DIR = try STD.fs.cwd().openDir(extra_options.dir_path, .{ .iterate = true });
44+
var chapters = TOP_LEVEL_DIR.iterate();
45+
46+
while (try chapters.next()) |chapter| {
47+
const CHAPTER_DIR_PATH = try STD.fs.path.join(allocator, &.{ extra_options.dir_path, chapter.name });
48+
49+
const CHAPTER_DIR = try STD.fs.cwd().openDir(CHAPTER_DIR_PATH, .{ .iterate = true });
50+
var contents = CHAPTER_DIR.iterate();
51+
52+
while (try contents.next()) |content| {
53+
const CONTENT_FILE_PATH = try STD.fs.path.join(allocator, &.{ extra_options.dir_path, chapter.name, content.name });
54+
55+
if (STD.mem.containsAtLeast(u8, content.name, 1, ".cpp")) {
56+
try list_of_contents.append(allocator, CONTENT_FILE_PATH);
57+
}
58+
}
59+
}
60+
61+
STD.sort.insertion(
62+
[]const u8,
63+
list_of_contents.items,
64+
{},
65+
struct {
66+
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
67+
return STD.mem.lessThan(u8, a, b);
68+
}
69+
}.lessThan,
70+
);
71+
72+
return list_of_contents;
73+
}
74+
3575
fn iterateExerciseDirectory(
3676
self: *CLI,
3777
exercise_dir: struct { dir_path: []const u8 = "exercises" },

0 commit comments

Comments
 (0)