Created
August 25, 2016 07:52
-
-
Save anonymous/e62ee2c340d105edb020c3e1147803d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use NativeCall; | |
my class CSV-Parser is repr("CStruct") { | |
... | |
}; | |
# This works ok | |
# int csv_init (struct csv_parser *p, unsigned char options); | |
sub csv_init (CSV-Parser, uint8) returns int32 is native("csv3") { * } | |
# So does this | |
# void csv_free(struct csv_parser *p); | |
sub csv_free (CSV-Parser) is native("csv3") { * } | |
# This causes trouble | |
# size_t csv_parse (struct csv_parser *p, const void *s, size_t len, | |
# void (*cb1)(void *, size_t, void *), void (*cb2)(int, void *), | |
# void *data); | |
sub csv_parse (CSV-Parser, Str, int64, | |
&cb1 (Str, int64, int64 is rw), | |
&cb2 (int64, int64 is rw), | |
int64 is rw) returns int64 is native("csv3") { * } | |
# const int READ_SZ = 1024 * 1024; | |
my $READ-SZ = 1024 * 1024; | |
my $i = 0; | |
# void field_count (void* str, size_t str_len, void* data) { | |
sub field-count (Str $buf, int64 $str-len, int64 $data is rw) { | |
# int* count = (int*)data; | |
# *count += 1; | |
say $i++; | |
} | |
# int main (int argc, char* argv[]) { | |
# struct csv_parser parser = {0}; | |
my $parser = CSV-Parser.new; | |
# csv_init (&parser, CSV_APPEND_NULL); | |
csv_init ($parser, 0); | |
# char *buf = (char*)malloc (READ_SZ); | |
# size_t buflen = READ_SZ; | |
# int count = 0; | |
my int64 $count = 0; | |
# while ((buflen = read (0, buf, READ_SZ)) > 0) { | |
while (my $blob = $*IN.read ($READ-SZ)) { | |
# THIS FAILS | |
# csv_parse (&parser, buf, buflen, field_count, 0, &count); | |
csv_parse ($parser, $blob.decode, $READ-SZ, &field-count, 0, $count); | |
} | |
# printf ("%d\n", count); | |
say $count; | |
# free (buf); | |
# csv_free (&parser); | |
csv_free ($parser); | |
# return EXIT_SUCCESS; | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment