Skip to content

Commit

Permalink
Merge pull request #25 from pedropark99/12-file-op
Browse files Browse the repository at this point in the history
Add new chapter about File and I/O operations
  • Loading branch information
pedropark99 authored Aug 24, 2024
2 parents 77dc10f + 796e7ed commit bde8c9c
Show file tree
Hide file tree
Showing 55 changed files with 3,650 additions and 466 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
./lldb/
lldb

foo.txt

*.o
*.a
Expand Down
2 changes: 1 addition & 1 deletion Chapters/01-base64.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ source("../zig_engine.R")
```


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

As our first small project, I want to implement with you a base64 encoder/decoder in Zig.
Base64 is an encoding system which translates binary data to text.
Expand Down
4 changes: 2 additions & 2 deletions Chapters/04-http-server.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ knitr::opts_chunk$set(



# Building a HTTP Server from scratch
# Project 2 - Building a HTTP Server from scratch

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



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

Now that we have a connection established, i.e. the connection
object that we created through the `accept()` function, we can now
Expand Down
2 changes: 1 addition & 1 deletion Chapters/10-stack-project.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ knitr::opts_chunk$set(
```


# Building a stack data structure
# Project 3 - Building a stack data structure

In this chapter we are going to implement a stack data structure as our next small project
in this book. Implementing basic data structures in any language is kind of a
Expand Down
890 changes: 890 additions & 0 deletions Chapters/12-file-op.qmd

Large diffs are not rendered by default.

Binary file added Figures/Powerpoint/buffered-io.odp
Binary file not shown.
Binary file added Figures/Powerpoint/unbuffered-io.odp
Binary file not shown.
Binary file added Figures/buffered-io.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Figures/cwd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Figures/unbuffered-io.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ You can find instructions on how to install these pieces of software by clicking
### Install R packages

After you installed the three pieces of software listed above, you should run the `dependencies.R` R script, to install
some R packages that are used across the book. Just run
the command below in your terminal, and you should be fine.
some R packages that are used across the book. Just run the command below in your terminal, and you should be fine.

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.
In Windows, this usually doesn't take that long because pre-built binaries are usually available.

```bash
Rscript dependencies.R
Expand Down
10 changes: 10 additions & 0 deletions ZigExamples/file-io/append_to_file.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const std = @import("std");

pub fn main() !void {
const cwd = std.fs.cwd();
const file = try cwd.openFile("foo.txt", .{ .mode = .write_only });
defer file.close();
try file.seekFromEnd(0);
var fw = file.writer();
_ = try fw.writeAll("Some random text to write\n");
}
14 changes: 14 additions & 0 deletions ZigExamples/file-io/buff_io.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const std = @import("std");
const stdout = std.io.getStdOut().writer();
pub fn main() !void {
var file = try std.fs.cwd().openFile("ZigExamples/file-io/lorem.txt", .{});
defer file.close();
var buffered = std.io.bufferedReader(file.reader());
var reader = buffered.reader();

var buffer: [1000]u8 = undefined;
@memset(buffer[0..], 0);

_ = try reader.readUntilDelimiterOrEof(buffer[0..], '\n');
try stdout.print("{s}\n", .{buffer});
}
6 changes: 6 additions & 0 deletions ZigExamples/file-io/copy_file.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const std = @import("std");

pub fn main() !void {
const cwd = std.fs.cwd();
try cwd.copyFile("foo.txt", cwd, "ZigExamples/file-io/foo.txt", .{});
}
6 changes: 6 additions & 0 deletions ZigExamples/file-io/create_file.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const std = @import("std");
pub fn main() !void {
const cwd = std.fs.cwd();
const file = try cwd.createFile("foo.txt", .{});
file.close();
}
17 changes: 17 additions & 0 deletions ZigExamples/file-io/create_file_and_read.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const std = @import("std");
const stdout = std.io.getStdOut().writer();
pub fn main() !void {
const cwd = std.fs.cwd();
const file = try cwd.createFile("foo.txt", .{ .read = true });
defer file.close();

var fw = file.writer();
_ = try fw.writeAll("We are going to read this line\n");

var buffer: [300]u8 = undefined;
@memset(buffer[0..], 0);
try file.seekTo(0);
var fr = file.reader();
_ = try fr.readAll(buffer[0..]);
try stdout.print("{s}\n", .{buffer});
}
9 changes: 9 additions & 0 deletions ZigExamples/file-io/create_file_and_write_toit.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const std = @import("std");
pub fn main() !void {
const cwd = std.fs.cwd();
const file = try cwd.createFile("foo.txt", .{});
defer file.close();
// Do things with the file ...
var fw = file.writer();
_ = try fw.writeAll("Writing this line to the file\n");
}
7 changes: 7 additions & 0 deletions ZigExamples/file-io/delete-dir.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const std = @import("std");

pub fn main() !void {
const cwd = std.fs.cwd();
try cwd.makeDir("src");
try cwd.deleteDir("src");
}
6 changes: 6 additions & 0 deletions ZigExamples/file-io/delete_file.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const std = @import("std");

pub fn main() !void {
const cwd = std.fs.cwd();
try cwd.deleteFile("foo.txt");
}
11 changes: 11 additions & 0 deletions ZigExamples/file-io/iterate.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const std = @import("std");
const stdout = std.io.getStdOut().writer();

pub fn main() !void {
const cwd = std.fs.cwd();
const dir = try cwd.openDir("ZigExamples/file-io/", .{ .iterate = true });
var it = dir.iterate();
while (try it.next()) |entry| {
try stdout.print("File name: {s}\n", .{entry.name});
}
}
1 change: 1 addition & 0 deletions ZigExamples/file-io/lorem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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.
7 changes: 7 additions & 0 deletions ZigExamples/file-io/make-dir.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const std = @import("std");

pub fn main() !void {
const cwd = std.fs.cwd();
try cwd.makeDir("src");
try cwd.makePath("src/decoders/jpg/");
}
10 changes: 10 additions & 0 deletions ZigExamples/file-io/user_input.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const std = @import("std");
const stdout = std.io.getStdOut().writer();
const stdin = std.io.getStdIn().reader();
pub fn main() !void {
try stdout.writeAll("Type your name\n");
var buffer: [20]u8 = undefined;
@memset(buffer[0..], 0);
_ = try stdin.readUntilDelimiterOrEof(buffer[0..], '\n');
try stdout.print("Your name is: {s}\n", .{buffer});
}
5 changes: 5 additions & 0 deletions ZigExamples/zig-basics/hello_world.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const std = @import("std");
const stdout = std.io.getStdOut().writer();
pub fn main() !void {
try stdout.print("Hello World!\n", .{});
}
8 changes: 5 additions & 3 deletions _freeze/Chapters/01-base64/execute-results/html.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions _freeze/Chapters/04-http-server/execute-results/html.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions _freeze/Chapters/10-stack-project/execute-results/html.json

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions _freeze/Chapters/12-file-op/execute-results/html.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ book:
- Chapters/09-error-handling.qmd
- Chapters/09-data-structures.qmd
- Chapters/10-stack-project.qmd
- Chapters/12-file-op.qmd
- references.qmd

bibliography: references.bib
Expand Down
Loading

0 comments on commit bde8c9c

Please sign in to comment.