hipack-parse.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * hipack-parse.c
  3. * Copyright (C) 2015 Adrian Perez <aperez@igalia.com>
  4. *
  5. * Distributed under terms of the MIT license.
  6. */
  7. #include "../hipack.h"
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. int
  12. main (int argc, const char *argv[])
  13. {
  14. if (argc != 2) {
  15. fprintf (stderr, "Usage: %s PATH\n", argv[0]);
  16. return EXIT_FAILURE;
  17. }
  18. FILE *fp = fopen (argv[1], "rb");
  19. if (!fp) {
  20. fprintf (stderr, "%s: Cannot open '%s' (%s)\n",
  21. argv[0], argv[1], strerror (errno));
  22. return EXIT_FAILURE;
  23. }
  24. int retcode = EXIT_SUCCESS;
  25. hipack_reader_t reader = {
  26. .getchar = hipack_stdio_getchar,
  27. .getchar_data = fp,
  28. };
  29. hipack_dict_t *message = hipack_read (&reader);
  30. if (!message) {
  31. assert (reader.error);
  32. fprintf (stderr, "line %u, column %u: %s\n",
  33. reader.error_line, reader.error_column,
  34. (reader.error == HIPACK_READ_ERROR)
  35. ? strerror (errno) : reader.error);
  36. retcode = EXIT_FAILURE;
  37. }
  38. hipack_dict_free (message);
  39. fclose (fp);
  40. return retcode;
  41. }