11const std = @import ("std" );
22const ini = @import ("ini.zig" );
3-
4- const c = @cImport ({
5- @cInclude ("ini.h" );
6- });
3+ const c = @import ("c" );
74
85const Record = extern struct {
96 type : Type ,
@@ -29,12 +26,12 @@ const Record = extern struct {
2926};
3027
3128const BufferParser = struct {
32- stream : std.io .Reader ,
29+ stream : std.Io .Reader ,
3330 parser : ini.Parser ,
3431};
3532
3633const FileParser = struct {
37- old_reader_adapter : CReader.Adapter ,
34+ reader : CReader ,
3835 parser : ini.Parser ,
3936};
4037
@@ -70,7 +67,7 @@ comptime {
7067export fn ini_create_buffer (parser : * IniParser , data : [* ]const u8 , data_length : usize , comment_characters : [* ]const u8 , comment_characters_length : usize ) void {
7168 parser .* = IniParser {
7269 .buffer = .{
73- .stream = std .io .Reader .fixed (data [0.. data_length ]),
70+ .stream = std .Io .Reader .fixed (data [0.. data_length ]),
7471 .parser = undefined ,
7572 },
7673 };
@@ -81,12 +78,12 @@ export fn ini_create_buffer(parser: *IniParser, data: [*]const u8, data_length:
8178export fn ini_create_file (parser : * IniParser , read_buffer : [* ]u8 , read_buffer_length : usize , file : * std.c.FILE , comment_characters : [* ]const u8 , comment_characters_length : usize ) void {
8279 parser .* = IniParser {
8380 .file = .{
84- .old_reader_adapter = cReader ( file ). adaptToNewApi ( read_buffer [0.. read_buffer_length ]),
81+ .reader = CReader . init ( file , read_buffer [0.. read_buffer_length ]),
8582 .parser = undefined ,
8683 },
8784 };
8885
89- parser .file .parser = ini .parse (std .heap .c_allocator , & parser .file .old_reader_adapter . new_interface , comment_characters [0.. comment_characters_length ]);
86+ parser .file .parser = ini .parse (std .heap .c_allocator , & parser .file .reader . interface , comment_characters [0.. comment_characters_length ]);
9087}
9188
9289export fn ini_destroy (parser : * IniParser ) void {
@@ -97,7 +94,7 @@ export fn ini_destroy(parser: *IniParser) void {
9794 parser .* = undefined ;
9895}
9996
100- const ParseError = error { OutOfMemory , StreamTooLong } || std .io .Reader .Error || std .io .Writer .Error ;
97+ const ParseError = error { OutOfMemory , StreamTooLong } || std .Io .Reader .Error || std .Io .Writer .Error ;
10198
10299fn mapError (err : ParseError ) IniError {
103100 return switch (err ) {
@@ -141,28 +138,37 @@ export fn ini_next(parser: *IniParser, record: *Record) IniError {
141138 return .success ;
142139}
143140
144- const CReader = std . Io . GenericReader ( * std .c .FILE , std . fs . File . ReadError , cReaderRead ) ;
141+ extern "c" fn feof ( stream : * std.c.FILE ) c_int ;
145142
146- fn cReader ( c_file : * std.c.FILE ) CReader {
147- return .{ . context = c_file };
148- }
143+ const CReader = struct {
144+ file : * std.c.FILE ,
145+ interface : std.Io.Reader ,
149146
150- fn cReaderRead (c_file : * std.c.FILE , bytes : []u8 ) std.fs.File.ReadError ! usize {
151- const amt_read = std .c .fread (bytes .ptr , 1 , bytes .len , c_file );
152- if (amt_read >= 0 ) return amt_read ;
153- switch (@as (std .os .E , @enumFromInt (std .c ._errno ().* ))) {
154- .SUCCESS = > unreachable ,
155- .INVAL = > unreachable ,
156- .FAULT = > unreachable ,
157- .AGAIN = > unreachable , // this is a blocking API
158- .BADF = > unreachable , // always a race condition
159- .DESTADDRREQ = > unreachable , // connect was never called
160- .DQUOT = > return error .DiskQuota ,
161- .FBIG = > return error .FileTooBig ,
162- .IO = > return error .InputOutput ,
163- .NOSPC = > return error .NoSpaceLeft ,
164- .PERM = > return error .AccessDenied ,
165- .PIPE = > return error .BrokenPipe ,
166- else = > | err | return std .os .unexpectedErrno (err ),
147+ fn init (file : * std.c.FILE , buffer : []u8 ) CReader {
148+ return .{
149+ .file = file ,
150+ .interface = .{
151+ .vtable = &.{ .stream = stream },
152+ .buffer = buffer ,
153+ .seek = 0 ,
154+ .end = 0 ,
155+ },
156+ };
167157 }
168- }
158+
159+ fn stream (r : * std.Io.Reader , w : * std.Io.Writer , limit : std.Io.Limit ) std.Io.Reader.StreamError ! usize {
160+ const creader : * CReader = @alignCast (@fieldParentPtr ("interface" , r ));
161+
162+ if (limit == .nothing ) return 0 ;
163+ const dest = limit .slice (try w .writableSliceGreedy (1 ));
164+
165+ const n = std .c .fread (dest .ptr , 1 , dest .len , creader .file );
166+ if (n > 0 ) {
167+ w .advance (n );
168+ return n ;
169+ }
170+
171+ if (feof (creader .file ) != 0 ) return error .EndOfStream ;
172+ return error .ReadFailed ;
173+ }
174+ };
0 commit comments