#include <fcntl.h>
#include <unistd.h>

#include "callbacks.h"
#include "parser3.h"
#include "weaseljson.h"

int main(int argc, char **argv) {
  if (argc < 2) {
    printf("Usage: %s <path>\n", argv[0]);
    return 1;
  }
  int fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  auto c = noopCallbacks();
  parser3::Parser3 parser(&c, nullptr);
  for (;;) {
    char buf[1024];
    int l = read(fd, buf, sizeof(buf));
    if (l == -1) {
      perror("read");
      return 1;
    }
    switch (parser.parse(buf, l)) {
    case WeaselJson_OK:
      return 0;
    case WeaselJson_AGAIN:
      continue;
    case WeaselJson_REJECT:
    case WeaselJson_OVERFLOW:
      return 1;
    }
    if (l == 0) {
      return 1;
    }
  }
}
