tags:

views:

150

answers:

2

Hi,

I would like to read a thin YAML file using a simple C program. I beleve the best way for this is to use the follow library: Pyyaml.org.

After reading the wiki page, I have try to use some examples here: http://pyyaml.org/browser/libyaml/branches/stable/tests/ But for a noob like me, it is not very simple to understand.

If for example I get this more simple YAML file (named test.yml):

test:
  - {conf: hi}
  - {conf: test}
  - {conf: abc}

How can I do to do somethig like this:

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

int main(int argc, char **argv)
{
  /* I do not really see how to load the file here... */

  char *conf[3] = {"hi", "test", "abc"}; /* configuration of the YAML file */

  int i;
  for (i = 0; i < 3; i++)
  {
    printf("content: %s\n", conf[i]);
  }

  return 0;
}

...but from the given YAML file?

Many thanks for any suggestions!

+1  A: 

The second answer to this question has a little bit better example of using the library to parse yaml file into something more useful.

Paul Wicks
A: 

libyaml seems to be event based, which usually means it's tuned for speed and less for easy usability. And there aren't many examples around (at least for the original C code, the scripting language bindings are more popular). The source distribution has some sample code in the tests directory. The most comprehensive seems to be the deconstructor example. It's long but very well commented, so it's easy to follow.

Generally, I'm not sure how useful YAML is for C code. YAML's greatest strength is that it maps naturally to the data types used by scripting languages like Python and Perl - lists and dictionaries, nested to arbitrary levels. The ability to trivially serialize such nested structures into YAML makes it attractive. I'm curious why you need YAML for C code.

Eli Bendersky