Skip to content

Commit 0d33634

Browse files
committed
first init
0 parents  commit 0d33634

14 files changed

+227
-0
lines changed

Diff for: .gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# https://github.com/github-linguist/linguist/blob/master/docs/overrides.md#documentation
2+
examples/* -linguist-documentation

Diff for: .github/workflows/ci.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: CI
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
paths:
7+
- "**.zig"
8+
- ".github/workflows/**"
9+
push:
10+
branches:
11+
- main
12+
paths:
13+
- "**.zig"
14+
- ".github/workflows/**"
15+
16+
jobs:
17+
examples:
18+
timeout-minutes: 10
19+
runs-on: ${{ matrix.os }}
20+
strategy:
21+
fail-fast: false
22+
matrix:
23+
os: [ubuntu-latest, macos-latest, windows-latest]
24+
steps:
25+
- uses: actions/checkout@v4
26+
- uses: goto-bus-stop/setup-zig@v2
27+
with:
28+
version: 0.11.0
29+
- name: Run examples(Unix)
30+
if: matrix.os != 'windows-latest'
31+
working-directory: examples
32+
run: zig build run-all --summary all
33+
34+
- name: Run examples(Windows)
35+
if: matrix.os == 'windows-latest'
36+
working-directory: examples
37+
run: zig.exe build run-all --summary all

Diff for: .github/workflows/pages.yml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Pages
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- "**.md"
9+
- ".github/workflows/**"
10+
pull_request:
11+
paths:
12+
- "**.md"
13+
- ".github/workflows/**"
14+
15+
jobs:
16+
deploy:
17+
runs-on: ubuntu-latest
18+
concurrency:
19+
group: ${{ github.workflow }}-${{ github.ref }}
20+
steps:
21+
- uses: actions/checkout@v4
22+
- name: Setup mdBook
23+
uses: peaceiris/actions-mdbook@v1
24+
with:
25+
mdbook-version: '0.4.10'
26+
# mdbook-version: 'latest'
27+
- run: mdbook build
28+
- name: Deploy
29+
uses: peaceiris/actions-gh-pages@v3
30+
if: ${{ github.ref == 'refs/heads/main' }}
31+
with:
32+
github_token: ${{ secrets.GITHUB_TOKEN }}
33+
publish_dir: ./book
34+
35+
lint:
36+
timeout-minutes: 10
37+
runs-on: ubuntu-latest
38+
steps:
39+
- uses: actions/checkout@v4
40+
with:
41+
fetch-depth: 0
42+
- uses: actions/setup-node@v3
43+
with:
44+
node-version: "14"
45+
- name: Prettier check
46+
run: |
47+
# if you encounter error, rerun the command below and commit the changes
48+
make lint
49+
git diff --exit-code

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
book
2+
zig-out/
3+
zig-cache/

Diff for: Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
lint:
3+
npx [email protected] --write .
4+
5+
run-all:
6+
cd examples && zig build run-all --summary all

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Zig Cookbook
2+
3+
Zig cookbook is a collection of simple Zig programs that demonstrate good practices to accomplish common programming tasks.

Diff for: book.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[book]
2+
authors = ["ZigCC"]
3+
language = "en"
4+
multilingual = false
5+
src = "src"
6+
title = "Zig cookbook"

Diff for: examples/.tool-versions

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
zig 0.11.0

Diff for: examples/build.zig

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const std = @import("std");
2+
const fs = std.fs;
3+
const allocPrint = std.fmt.allocPrint;
4+
const print = std.debug.print;
5+
6+
pub fn build(b: *std.Build) !void {
7+
var run_all_step = b.step("run-all", "Run all examples");
8+
try addExample(b, run_all_step);
9+
}
10+
11+
fn addExample(b: *std.Build, run_all: *std.build.Step) !void {
12+
const src_dir = try fs.cwd().openIterableDir("src", .{});
13+
14+
var it = src_dir.iterate();
15+
while (try it.next()) |entry| {
16+
switch (entry.kind) {
17+
.file => {
18+
const name = std.mem.trimRight(u8, entry.name, ".zig");
19+
print("Add example {s}...", .{name});
20+
21+
const exe = b.addExecutable(.{
22+
.name = try allocPrint(b.allocator, "examples-{s}", .{name}),
23+
.root_source_file = .{ .path = try allocPrint(b.allocator, "src/{s}.zig", .{name}) },
24+
.target = .{},
25+
.optimize = .Debug,
26+
});
27+
const run_step = &b.addRunArtifact(exe).step;
28+
b.step(try allocPrint(b.allocator, "run-{s}", .{name}), try allocPrint(
29+
b.allocator,
30+
"Run example {s}",
31+
.{name},
32+
)).dependOn(run_step);
33+
run_all.dependOn(run_step);
34+
},
35+
else => {},
36+
}
37+
}
38+
}

Diff for: examples/src/01-01.zig

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const std = @import("std");
2+
const fs = std.fs;
3+
const print = std.debug.print;
4+
5+
pub fn main() !void {
6+
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
7+
defer arena.deinit();
8+
const allocator = arena.allocator();
9+
10+
const file = try fs.cwd().openFile("build.zig", .{});
11+
defer file.close();
12+
13+
const rdr = file.reader();
14+
var line_no: usize = 0;
15+
while (try rdr.readUntilDelimiterOrEofAlloc(allocator, '\n', 4096)) |line| {
16+
line_no += 1;
17+
print("{d}--{s}\n", .{ line_no, line });
18+
}
19+
}

Diff for: examples/src/01-02.zig

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const std = @import("std");
2+
const fs = std.fs;
3+
const print = std.debug.print;
4+
5+
const filename = "/tmp/zig-cookbook-01-02.txt";
6+
const file_size = 4096;
7+
8+
pub fn main() !void {
9+
if (.windows == @import("builtin").os.tag) {
10+
std.debug.print("MMap is not supported in Windows\n", .{});
11+
return;
12+
}
13+
14+
const file = try fs.cwd().createFile(filename, .{
15+
.read = true,
16+
.truncate = true,
17+
.exclusive = false, // Set to true will ensure this file is created by us
18+
});
19+
defer file.close();
20+
21+
// Before mmap, we need to ensure file isn't empty
22+
try file.setEndPos(file_size);
23+
24+
const md = try file.metadata();
25+
print("File size: {d}\n", .{md.size()});
26+
27+
var ptr = try std.os.mmap(
28+
null,
29+
20,
30+
std.os.PROT.READ | std.os.PROT.WRITE,
31+
std.os.MAP.PRIVATE,
32+
file.handle,
33+
0,
34+
);
35+
defer std.os.munmap(ptr);
36+
37+
// Write file via mmap
38+
std.mem.copyForwards(u8, ptr, "hello zig cookbook");
39+
40+
// Read file via mmap
41+
print("File body: {s}", .{ptr});
42+
}

Diff for: src/01-01-read-file-line-by-line.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Read file line by line
2+
3+
There is a Reader type in Zig, which provides various methods to read file.
4+
5+
```zig
6+
const file = try fs.cwd().openFile("build.zig", .{});
7+
defer file.close();
8+
9+
const rdr = file.reader();
10+
var line_no: usize = 0;
11+
while (try rdr.readUntilDelimiterOrEofAlloc(allocator, '\n', 4096)) |line| {
12+
line_no += 1;
13+
print("{d}--{s}\n", .{ line_no, line });
14+
}
15+
```

Diff for: src/01-02-mmap-file.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Mmap file

Diff for: src/SUMMARY.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Summary
2+
3+
- [File System]()
4+
- [Read file line by line](./01-01-read-file-line-by-line.md)
5+
- [Mmap file](./01-02-mmap-file.md)

0 commit comments

Comments
 (0)