aboutsummaryrefslogtreecommitdiff
path: root/core/encoding/csv/reader.odin
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2024-05-24 13:26:12 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2024-05-24 13:26:12 +0200
commit3b739dc5ccf1ddb29b53a86155e5671b5be00c54 (patch)
treec53e7ac13d7a42d261c532dbb7741982e683cb36 /core/encoding/csv/reader.odin
parent7dc1f114b96b9e25dfc2537ab153fe655e65affc (diff)
Add iterator_next(&r) to CSV.
Diffstat (limited to 'core/encoding/csv/reader.odin')
-rw-r--r--core/encoding/csv/reader.odin25
1 files changed, 23 insertions, 2 deletions
diff --git a/core/encoding/csv/reader.odin b/core/encoding/csv/reader.odin
index f8c72c423..200bf43ea 100644
--- a/core/encoding/csv/reader.odin
+++ b/core/encoding/csv/reader.odin
@@ -57,6 +57,9 @@ Reader :: struct {
field_indices: [dynamic]int,
last_record: [dynamic]string,
sr: strings.Reader, // used by reader_init_with_string
+
+ // Set and used by the iterator. Query using `iterator_last_error`
+ last_iterator_error: Error,
}
@@ -121,6 +124,25 @@ reader_destroy :: proc(r: ^Reader) {
bufio.reader_destroy(&r.r)
}
+/*
+ Returns a record at a time.
+
+ for record, row_idx in csv.iterator_next(&r) { ... }
+
+ TIP: If you process the results within the loop and don't need to own the results,
+ you can set the Reader's `reuse_record` and `reuse_record_reuse_record_buffer` to true;
+ you won't need to delete the record or its fields.
+*/
+iterator_next :: proc(r: ^Reader) -> (record: []string, idx: int, err: Error, more: bool) {
+ record, r.last_iterator_error = read(r)
+ return record, r.line_count - 1, r.last_iterator_error, r.last_iterator_error == nil
+}
+
+// Get last error if we the iterator
+iterator_last_error :: proc(r: Reader) -> (err: Error) {
+ return r.last_iterator_error
+}
+
// read reads a single record (a slice of fields) from r
//
// All \r\n sequences are normalized to \n, including multi-line field
@@ -460,5 +482,4 @@ _read_record :: proc(r: ^Reader, dst: ^[dynamic]string, allocator := context.all
r.fields_per_record = len(dst)
}
return dst[:], err
-
-}
+} \ No newline at end of file