YAML教程

1、簡介html

YAML是一種人們能夠輕鬆閱讀的數據序列化格式,而且它很是適合對動態編程語言中使用的數據類型進行編碼。YAML是YAML Ain't Markup Language簡寫,和GNU("GNU's Not Unix!")同樣,YAML是一個遞歸着說「不」的名字。不一樣的是,GNU對UNIX說不,YAML說不的對象是XML。YAML不是XML。它能夠用做數據序列,配置文件,log文件,Internat信息和過濾。java

參考:http://jyaml.sourceforge.net/download.htmlnode

        http://justjavac.iteye.com/blog/694498編程

 

 

2、安裝配置maven

1)java:下載jar包並導入編程語言

 

2)c:libyaml編碼

方式1:yum方式spa

yum install libyaml-devel libyaml

image

 

方式2:下載libyaml並編譯安裝.net

wget http://pyyaml.org/download/libyaml/yaml-0.1.5.tar.gz

tar -zxvf yaml-0.1.5.tar.gz
$ ./configure
$ make
# make install

 

3、編程實例3d

參考:http://pyyaml.org/wiki/LibYAML

        http://yaml.org/spec/1.1/

 

程度1:獲取yaml版本信息

#include <yaml.h>

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

#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>

int main(void)
{
    int major = -1;
    int minor = -1;
    int patch = -1;
    char buf[64];

    yaml_get_version(&major, &minor, &patch);
    sprintf(buf, "%d.%d.%d", major, minor, patch);
    assert(strcmp(buf, yaml_get_version_string()) == 0);

    /* Print structure sizes. */
    printf("sizeof(token) = %d\n", sizeof(yaml_token_t));
    printf("sizeof(event) = %d\n", sizeof(yaml_event_t));
    printf("sizeof(parser) = %d\n", sizeof(yaml_parser_t));

    return 0;
}

編譯

gcc -o example1 example1.c -lyaml

運行

image

 

 

程度2:

#include <yaml.h>

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

#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>

int main(int argc, char *argv[])
{
    int number;

    if (argc < 2) {
        printf("Usage: %s file1.yaml ...\n", argv[0]);
        return 0;
    }

    for (number = 1; number < argc; number ++)
    {
        FILE *file;
        yaml_parser_t parser;
        yaml_document_t document;
        int done = 0;
        int count = 0;
        int error = 0;

        printf("[%d] Loading '%s': ", number, argv[number]);
        fflush(stdout);

        file = fopen(argv[number], "rb");
        assert(file);

        assert(yaml_parser_initialize(&parser));

        yaml_parser_set_input_file(&parser, file);

        while (!done)
        {
            if (!yaml_parser_load(&parser, &document)) {
                error = 1;
                break;
            }

            done = (!yaml_document_get_root_node(&document));

            yaml_document_delete(&document);

            if (!done) count ++;
        }

        yaml_parser_delete(&parser);

        assert(!fclose(file));

        printf("%s (%d documents)\n", (error ? "FAILURE" : "SUCCESS"), count);
    }

    return 0;
}

編譯

gcc -o example2 example2.c -lyaml

運行

image

相關文章
相關標籤/搜索