diff --git a/hello-wuffs-c/README.md b/hello-wuffs-c/README.md index 60b2996a..d07bfd3d 100644 --- a/hello-wuffs-c/README.md +++ b/hello-wuffs-c/README.md @@ -20,13 +20,14 @@ pointer. Anyway, here's what the output should look like: ``` $ ./run.sh +--- C Implementation prints --- 0 12 56789 4294967295 0 3197704724 --------- +------ Wuffs Impl Prints ------ 0 12 56789 diff --git a/hello-wuffs-c/main.c b/hello-wuffs-c/main.c index 7704d6a0..afa0b51d 100644 --- a/hello-wuffs-c/main.c +++ b/hello-wuffs-c/main.c @@ -16,63 +16,10 @@ #include #include -#include #include -#ifndef USE_WUFFS -uint32_t parse(char* p, size_t n) { - uint32_t ret = 0; - for (size_t i = 0; (i < n) && p[i]; i++) { - ret = (10 * ret) + (p[i] - '0'); - } - return ret; -} -#else // #ifndef USE_WUFFS - -// TODO: this 'rough edge' shouldn't be necessary. See -// https://github.com/google/wuffs/issues/24 -#define WUFFS_CONFIG__MODULE__BASE - -#define WUFFS_IMPLEMENTATION -#include "./parse.c" - -uint32_t parse(char* p, size_t n) { - wuffs_base__status status; - wuffs_demo__parser* parser = - (wuffs_demo__parser*)(malloc(sizeof__wuffs_demo__parser())); - if (!parser) { - printf("malloc failed\n"); - return 0; - } - - status = wuffs_demo__parser__initialize(parser, sizeof__wuffs_demo__parser(), - WUFFS_VERSION, 0); - if (!wuffs_base__status__is_ok(&status)) { - printf("initialize: %s\n", wuffs_base__status__message(&status)); - free(parser); - return 0; - } - - wuffs_base__io_buffer iobuf; - iobuf.data.ptr = (uint8_t*)p; - iobuf.data.len = n; - iobuf.meta.wi = n; - iobuf.meta.ri = 0; - iobuf.meta.pos = 0; - iobuf.meta.closed = true; - - status = wuffs_demo__parser__parse(parser, &iobuf); - if (!wuffs_base__status__is_ok(&status)) { - printf("parse: %s\n", wuffs_base__status__message(&status)); - free(parser); - return 0; - } - - uint32_t ret = wuffs_demo__parser__value(parser); - free(parser); - return ret; -} -#endif // #ifndef USE_WUFFS +// See naive-parse.c and wuffs-parse.c for implementations of this function +uint32_t parse(char *, size_t); void run(char* p) { size_t n = strlen(p) + 1; // +1 for the trailing NUL that ends a C string. diff --git a/hello-wuffs-c/naive-parse.c b/hello-wuffs-c/naive-parse.c new file mode 100644 index 00000000..a184739c --- /dev/null +++ b/hello-wuffs-c/naive-parse.c @@ -0,0 +1,10 @@ +#include +#include + +uint32_t parse(char* p, size_t n) { + uint32_t ret = 0; + for (size_t i = 0; (i < n) && p[i]; i++) { + ret = (10 * ret) + (p[i] - '0'); + } + return ret; +} diff --git a/hello-wuffs-c/run.sh b/hello-wuffs-c/run.sh index aa0ff8b3..a2eb53c7 100755 --- a/hello-wuffs-c/run.sh +++ b/hello-wuffs-c/run.sh @@ -22,12 +22,10 @@ CC=${CC:-gcc} # beforehand, to install the wuffs-c compiler. wuffs-c gen -package_name demo < parse.wuffs > parse.c -$CC main.c +echo --- C Implementation prints --- +$CC main.c naive-parse.c ./a.out -echo -------- - -$CC main.c -DUSE_WUFFS +echo ------ Wuffs Impl Prints ------ +$CC main.c wuffs-parse.c ./a.out - -rm a.out parse.c diff --git a/hello-wuffs-c/wuffs-parse.c b/hello-wuffs-c/wuffs-parse.c new file mode 100644 index 00000000..d66cac25 --- /dev/null +++ b/hello-wuffs-c/wuffs-parse.c @@ -0,0 +1,41 @@ +#include + +// TODO: this 'rough edge' shouldn't be necessary. See +// https://github.com/google/wuffs/issues/24 +#define WUFFS_CONFIG__MODULE__BASE + +#define WUFFS_IMPLEMENTATION +#include "./parse.c" + +uint32_t parse(char* p, size_t n) { + wuffs_base__status status; + // If you want this on the heap instead, use + // wuffs_demo__parser *parser = wuffs_demo__parser__alloc(); + // and don't run __initialize(); + wuffs_demo__parser parser; + status = wuffs_demo__parser__initialize(&parser, sizeof__wuffs_demo__parser(), + WUFFS_VERSION, 0); + // This happens when bad arguments are passed to __initialize() + if (!wuffs_base__status__is_ok(&status)) { + printf("initialize: %s\n", wuffs_base__status__message(&status)); + return 0; + } + + wuffs_base__io_buffer iobuf; + iobuf.data.ptr = (uint8_t*)p; + iobuf.data.len = n; + iobuf.meta.wi = n; + iobuf.meta.ri = 0; + iobuf.meta.pos = 0; + iobuf.meta.closed = true; + + // This happens when wuffs code returns "# Some status" + status = wuffs_demo__parser__parse(&parser, &iobuf); + if (!wuffs_base__status__is_ok(&status)) { + printf("parse: %s\n", wuffs_base__status__message(&status)); + return 0; + } + + uint32_t ret = wuffs_demo__parser__value(&parser); + return ret; +}