// Demonstrates bug: http://pyyaml.org/ticket/156

// Warning: running this program will create a 125 megabyte
// file "foo.yaml" in the current directory!

#include <yaml.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

int main (int argc, char **argv)
{ printf ("libYaml version %s on a %zd-bit machine\n",
          yaml_get_version_string (), 8 * sizeof (void *));

  int i;
  FILE *f = fopen ("foo.yaml", "w");
  for (i = 0 ; i < 5000000 ; i++)
    fprintf (f, "---\nfoo: bar\nbaz: bob\n...\n");
  fclose (f);

  yaml_parser_t parser;
  yaml_event_t event;
  bool done = false;

  yaml_parser_initialize (&parser);
  f = fopen ("foo.yaml", "r");
  yaml_parser_set_input_file (&parser, f);

  while (!done)
    { if (!yaml_parser_parse (&parser, &event))
        goto error;

      done = (event.type == YAML_STREAM_END_EVENT);
      yaml_event_delete (&event);
    }

  yaml_parser_delete (&parser);
  printf ("Success!\n");
  return EXIT_SUCCESS;

 error:
  printf ("parser error %d\n", parser.error);
  if (parser.context)
    printf ("context: %s at line %zd\n",
            parser.context, parser.context_mark.line + 1);
  if (parser.problem)
    printf ("problem: %s at line %zd\n",
            parser.problem, parser.problem_mark.line + 1);

  yaml_parser_delete (&parser);
  printf ("Failure!\n");
  return EXIT_FAILURE;
}
