Skip to content

Instantly share code, notes, and snippets.

Created August 25, 2016 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e62ee2c340d105edb020c3e1147803d8 to your computer and use it in GitHub Desktop.
Save anonymous/e62ee2c340d105edb020c3e1147803d8 to your computer and use it in GitHub Desktop.
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