11const std = @import ("std" );
22const chatbot = @import ("chatbot.zig" );
3- const c = @cImport ({
4- @cInclude ("stdio.h" );
5- @cInclude ("string.h" );
6- @cInclude ("ctype.h" );
7- });
83
94pub fn main () ! void {
105 var gpa = std .heap .GeneralPurposeAllocator (.{}){};
116 defer _ = gpa .deinit ();
127 const allocator = gpa .allocator ();
138
14- _ = c .printf ("$ Chatbot v1.0.0!\n " , );
9+ // Zig 0.15 I/O: explicit buffer management
10+ var stdout_buf : [4096 ]u8 = undefined ;
11+ var stdout = std .fs .File .stdout ().writer (& stdout_buf );
12+
13+ var stdin_buf : [4096 ]u8 = undefined ;
14+ var stdin = std .fs .File .stdin ().reader (& stdin_buf );
15+
16+ try stdout .interface .print ("$ Chatbot v1.0.0!\n " , .{});
17+ try stdout .interface .flush ();
1518
1619 // Create hash table
1720 var ht = try chatbot .HashTable .create (allocator , 65536 );
@@ -25,17 +28,22 @@ pub fn main() !void {
2528 try ht .set ("light" , "I like light" );
2629 try ht .set ("What" , "It is clear, ain't it?" );
2730
28- var buf : [chatbot .LineLength ]u8 = undefined ;
29-
3031 while (true ) {
31- _ = c .printf ("\n $ (user) " , );
32+ try stdout .interface .print ("\n $ (user) " , .{});
33+ try stdout .interface .flush ();
3234
33- const result = c .fgets (& buf , chatbot .LineLength , c .stdin ());
34- if (result == null ) break ;
35-
36- if (@as (usize , c .strlen (& buf )) <= 1 ) break ;
35+ // Read line using Zig 0.15 delimiter API
36+ const line = stdin .interface .takeDelimiterExclusive ('\n ' ) catch | err | {
37+ switch (err ) {
38+ error .EndOfStream = > break ,
39+ error .StreamTooLong = > {
40+ // Line too long, skip it
41+ continue ;
42+ },
43+ else = > return err ,
44+ }
45+ };
3746
38- const line = buf [0.. c .strlen (& buf )];
3947 const trimmed = std .mem .trim (u8 , line , " \t \r \n " );
4048 if (trimmed .len == 0 ) break ;
4149
@@ -45,19 +53,20 @@ pub fn main() !void {
4553 const lower_word = try allocator .alloc (u8 , word .len );
4654 defer allocator .free (lower_word );
4755
48- for (word , lower_word ) | ch , * lch | {
49- lch .* = std .ascii .toLower (ch );
56+ for (word , 0 .. ) | ch , i | {
57+ lower_word [ i ] = std .ascii .toLower (ch );
5058 }
5159
5260 if (std .mem .eql (u8 , lower_word , "exit" )) {
5361 return ;
5462 }
5563
5664 if (ht .get (lower_word )) | response | {
57- _ = c . printf ("\n $ (chatbot) %s \n " , response . ptr );
65+ try stdout . interface . print ("\n $ (chatbot) {s} \n " , .{ response } );
5866 } else {
59- _ = c . printf ("\n $ (chatbot) %s \n " , "Sorry, I don't know what to say about that" );
67+ try stdout . interface . print ("\n $ (chatbot) {s} \n " , .{ "Sorry, I don't know what to say about that" } );
6068 }
69+ try stdout .interface .flush ();
6170 }
6271 }
6372}
0 commit comments