overload 1
csv_reader :: (input: [] u8, options := Csv_Options.{}) -> Csv_Reader
Creates a reader of records over input, which it borrows; release it with csv_reader_free.
Example:
r := csv_reader(cast([] u8) "one,two\n", .{ header = true });
defer csv_reader_free(*r);overload 2
csv_reader :: (source: Csv_Source, options := Csv_Options.{}) -> Csv_Reader
Creates a reader of records over a chunk source, which uses Json's fill-buffer convention. Example:
#import "Extensions/Csv";
#import "String";
Feed :: struct {
text: string;
}
fill :: (data: *void, buffer: [] u8) -> s64, string {
feed := cast(*Feed) data;
count := min(buffer.count, feed.text.count);
memcpy(buffer.data, feed.text.data, count);
feed.text = slice(feed.text, count, feed.text.count - count);
return count, "";
}
feed := Feed.{ "one,two\n" };
r := csv_reader(Csv_Source.{ *feed, fill });
defer csv_reader_free(*r);
fields, present, error := csv_next(*r);
assert(present && fields[1] == "two");overload 3
csv_reader :: (
$Row: Type,
input: [] u8,
options := Csv_Options.{}
) -> Csv_Reader(Row)
Creates a reader that decodes each record into a Row struct, matching header names (or
declaration order without a header; see @csv(name=...) and @csv(skip)). It maps the columns
once, when the first row is read. Release it with csv_reader_free.
Example:
#import "Extensions/Csv";
User :: struct {
name: string;
age: int;
}
r := csv_reader(User, cast([] u8) "age,name\n36,ada\n", .{ header = true });
defer csv_reader_free(*r);
user, present, error := csv_next(*r);
assert(present && user.name == "ada" && user.age == 36);
csv_row_free(*user);overload 4
csv_reader :: (
$Row: Type,
source: Csv_Source,
options := Csv_Options.{}
) -> Csv_Reader(Row)
Creates a typed reader over a chunk source; options.borrow is rejected, since the chunk is
reused. See the chunk source example of csv_reader.
Example:
#import "Extensions/Csv";
User :: struct {
name: string;
}
fill :: (data: *void, buffer: [] u8) -> s64, string {
return 0, "";
}
r := csv_reader(User, Csv_Source.{ null, fill }, .{ header = true });
defer csv_reader_free(*r);
_, present, error := csv_next(*r);
assert(!present && error.kind == .NONE);