authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-18 19:32:27-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-18 19:32:27-07:00
log92dccde2fd7a26ed6b18af4fc4f2dc6b18766a5f
treedc49608ac55b699c178ed19074947a9a607dfe3e
parentea21d2beb6cbdac8f5b6811a0f01aa6d2ee2ddf3

revise plan for cat example


1 files changed, 22 insertions(+), 9 deletions(-)

example/cat/main.zig+22-9
...@@ -2,13 +2,26 @@ export executable "cat";...@@ -2,13 +2,26 @@ export executable "cat";
22
3import "std.zig";3import "std.zig";
44
5pub fn main(args: [][]u8) error => {5// Things to do to make this work:
6// * isize instead of usize for things
7// * var args printing
8// * update std API
9// * !void type
10// * defer
11// * !return
12// * !! operator
13// * make main return !void
14// * how to reference error values (!void).Invalid ? !Invalid ?
15// * ~ is bool not, not !
16// * cast err type to string
17
18pub fn main(args: [][]u8) !void => {
6 const exe = args[0];19 const exe = args[0];
7 var catted_anything = false;20 var catted_anything = false;
8 for (arg in args[1...]) {21 for (arg, args[1...]) {
9 if (arg == "-") {22 if (arg == "-") {
10 catted_anything = true;23 catted_anything = true;
11 cat_stream(stdin) !! (err) => return err;24 !return cat_stream(stdin);
12 } else if (arg[0] == '-') {25 } else if (arg[0] == '-') {
13 return usage(exe);26 return usage(exe);
14 } else {27 } else {
...@@ -20,20 +33,20 @@ pub fn main(args: [][]u8) error => {...@@ -20,20 +33,20 @@ pub fn main(args: [][]u8) error => {
20 defer is.close();33 defer is.close();
2134
22 catted_anything = true;35 catted_anything = true;
23 cat_stream(is) !! (err) => return err;36 !return cat_stream(is);
24 }37 }
25 }38 }
26 if (!catted_anything) {39 if (~catted_anything) {
27 cat_stream(stdin) !! (err) => return err;40 !return cat_stream(stdin)
28 }41 }
29}42}
3043
31fn usage(exe: []u8) error => {44fn usage(exe: []u8) !void => {
32 stderr.print("Usage: {} [FILE]...\n", exe);45 stderr.print("Usage: {} [FILE]...\n", exe);
33 return error.Invalid;46 return !Invalid;
34}47}
3548
36fn cat_stream(is: InputStream) error => {49fn cat_stream(is: InputStream) !void => {
37 var buf: [1024 * 4]u8;50 var buf: [1024 * 4]u8;
3851
39 while (true) {52 while (true) {