Skip to content

Commit bde8c9c

Browse files
authored
Merge pull request #25 from pedropark99/12-file-op
Add new chapter about File and I/O operations
2 parents 77dc10f + 796e7ed commit bde8c9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+3650
-466
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
./lldb/
1111
lldb
1212

13+
foo.txt
1314

1415
*.o
1516
*.a

Chapters/01-base64.qmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ source("../zig_engine.R")
1010
```
1111

1212

13-
# Building a base64 encoder/decoder {#sec-base64}
13+
# Project 1 - Building a base64 encoder/decoder {#sec-base64}
1414

1515
As our first small project, I want to implement with you a base64 encoder/decoder in Zig.
1616
Base64 is an encoding system which translates binary data to text.

Chapters/04-http-server.qmd

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ knitr::opts_chunk$set(
1515

1616

1717

18-
# Building a HTTP Server from scratch
18+
# Project 2 - Building a HTTP Server from scratch
1919

2020
In this chapter, I want to implement a new
2121
small project with you. This time, we are going
@@ -328,7 +328,7 @@ terminates as soon as the connection is accepted.
328328

329329

330330

331-
### Reading the message from the client
331+
### Reading the message from the client {#sec-read-http-message}
332332

333333
Now that we have a connection established, i.e. the connection
334334
object that we created through the `accept()` function, we can now

Chapters/10-stack-project.qmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ knitr::opts_chunk$set(
1414
```
1515

1616

17-
# Building a stack data structure
17+
# Project 3 - Building a stack data structure
1818

1919
In this chapter we are going to implement a stack data structure as our next small project
2020
in this book. Implementing basic data structures in any language is kind of a

Chapters/12-file-op.qmd

+890
Large diffs are not rendered by default.

Figures/Powerpoint/buffered-io.odp

20.7 KB
Binary file not shown.

Figures/Powerpoint/unbuffered-io.odp

30.7 KB
Binary file not shown.

Figures/buffered-io.png

37.4 KB
Loading

Figures/cwd.png

36.3 KB
Loading

Figures/unbuffered-io.png

38.5 KB
Loading

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ You can find instructions on how to install these pieces of software by clicking
2525
### Install R packages
2626

2727
After you installed the three pieces of software listed above, you should run the `dependencies.R` R script, to install
28-
some R packages that are used across the book. Just run
29-
the command below in your terminal, and you should be fine.
28+
some R packages that are used across the book. Just run the command below in your terminal, and you should be fine.
29+
30+
OBS: If you are on Linux or MacOS, this command will probably take some time to run, because every single dependency get's built from source.
31+
In Windows, this usually doesn't take that long because pre-built binaries are usually available.
3032

3133
```bash
3234
Rscript dependencies.R
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
const cwd = std.fs.cwd();
5+
const file = try cwd.openFile("foo.txt", .{ .mode = .write_only });
6+
defer file.close();
7+
try file.seekFromEnd(0);
8+
var fw = file.writer();
9+
_ = try fw.writeAll("Some random text to write\n");
10+
}

ZigExamples/file-io/buff_io.zig

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const std = @import("std");
2+
const stdout = std.io.getStdOut().writer();
3+
pub fn main() !void {
4+
var file = try std.fs.cwd().openFile("ZigExamples/file-io/lorem.txt", .{});
5+
defer file.close();
6+
var buffered = std.io.bufferedReader(file.reader());
7+
var reader = buffered.reader();
8+
9+
var buffer: [1000]u8 = undefined;
10+
@memset(buffer[0..], 0);
11+
12+
_ = try reader.readUntilDelimiterOrEof(buffer[0..], '\n');
13+
try stdout.print("{s}\n", .{buffer});
14+
}

ZigExamples/file-io/copy_file.zig

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
const cwd = std.fs.cwd();
5+
try cwd.copyFile("foo.txt", cwd, "ZigExamples/file-io/foo.txt", .{});
6+
}

ZigExamples/file-io/create_file.zig

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const std = @import("std");
2+
pub fn main() !void {
3+
const cwd = std.fs.cwd();
4+
const file = try cwd.createFile("foo.txt", .{});
5+
file.close();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const std = @import("std");
2+
const stdout = std.io.getStdOut().writer();
3+
pub fn main() !void {
4+
const cwd = std.fs.cwd();
5+
const file = try cwd.createFile("foo.txt", .{ .read = true });
6+
defer file.close();
7+
8+
var fw = file.writer();
9+
_ = try fw.writeAll("We are going to read this line\n");
10+
11+
var buffer: [300]u8 = undefined;
12+
@memset(buffer[0..], 0);
13+
try file.seekTo(0);
14+
var fr = file.reader();
15+
_ = try fr.readAll(buffer[0..]);
16+
try stdout.print("{s}\n", .{buffer});
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const std = @import("std");
2+
pub fn main() !void {
3+
const cwd = std.fs.cwd();
4+
const file = try cwd.createFile("foo.txt", .{});
5+
defer file.close();
6+
// Do things with the file ...
7+
var fw = file.writer();
8+
_ = try fw.writeAll("Writing this line to the file\n");
9+
}

ZigExamples/file-io/delete-dir.zig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
const cwd = std.fs.cwd();
5+
try cwd.makeDir("src");
6+
try cwd.deleteDir("src");
7+
}

ZigExamples/file-io/delete_file.zig

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
const cwd = std.fs.cwd();
5+
try cwd.deleteFile("foo.txt");
6+
}

ZigExamples/file-io/iterate.zig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const std = @import("std");
2+
const stdout = std.io.getStdOut().writer();
3+
4+
pub fn main() !void {
5+
const cwd = std.fs.cwd();
6+
const dir = try cwd.openDir("ZigExamples/file-io/", .{ .iterate = true });
7+
var it = dir.iterate();
8+
while (try it.next()) |entry| {
9+
try stdout.print("File name: {s}\n", .{entry.name});
10+
}
11+
}

ZigExamples/file-io/lorem.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tincidunt erat sed nulla ornare, nec aliquet ex laoreet. Ut nec rhoncus nunc. Integer magna metus, ultrices eleifend porttitor ut, finibus ut tortor. Maecenas sapien justo, finibus tincidunt dictum ac, semper et lectus. Vivamus molestie egestas orci ac viverra. Pellentesque nec arcu facilisis, euismod eros eu, sodales nisl. Ut egestas sagittis arcu, in accumsan sapien rhoncus sit amet. Aenean neque lectus, imperdiet ac lobortis a, ullamcorper sed massa. Nullam porttitor porttitor erat nec dapibus. Ut vel dui nec nulla vulputate molestie eget non nunc. Ut commodo luctus ipsum, in finibus libero feugiat eget. Etiam vel ante at urna tincidunt posuere sit amet ut felis. Maecenas finibus suscipit tristique. Donec viverra non sapien id suscipit.

ZigExamples/file-io/make-dir.zig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
const cwd = std.fs.cwd();
5+
try cwd.makeDir("src");
6+
try cwd.makePath("src/decoders/jpg/");
7+
}

ZigExamples/file-io/user_input.zig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const std = @import("std");
2+
const stdout = std.io.getStdOut().writer();
3+
const stdin = std.io.getStdIn().reader();
4+
pub fn main() !void {
5+
try stdout.writeAll("Type your name\n");
6+
var buffer: [20]u8 = undefined;
7+
@memset(buffer[0..], 0);
8+
_ = try stdin.readUntilDelimiterOrEof(buffer[0..], '\n');
9+
try stdout.print("Your name is: {s}\n", .{buffer});
10+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const std = @import("std");
2+
const stdout = std.io.getStdOut().writer();
3+
pub fn main() !void {
4+
try stdout.print("Hello World!\n", .{});
5+
}

_freeze/Chapters/01-base64/execute-results/html.json

+5-3
Large diffs are not rendered by default.

_freeze/Chapters/04-http-server/execute-results/html.json

+5-3
Large diffs are not rendered by default.

_freeze/Chapters/10-stack-project/execute-results/html.json

+5-3
Large diffs are not rendered by default.

_freeze/Chapters/12-file-op/execute-results/html.json

+15
Large diffs are not rendered by default.

_quarto.yml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ book:
2121
- Chapters/09-error-handling.qmd
2222
- Chapters/09-data-structures.qmd
2323
- Chapters/10-stack-project.qmd
24+
- Chapters/12-file-op.qmd
2425
- references.qmd
2526

2627
bibliography: references.bib

0 commit comments

Comments
 (0)