GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Parsing YAML tree to a glib "N-ary tree" (C)

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
caracal
GTK+ Guru


Joined: 21 Jun 2007
Posts: 207
Location: Wilkes Barre Pa

PostPosted: Fri Oct 02, 2009 10:54 am    Post subject: Parsing YAML tree to a glib "N-ary tree" (C) Reply with quote

Parsing YAML tree to a glib "N-ary tree":

main.c
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

#include <yaml.h>
#include <stdio.h>
#include <glib.h>

/* gcc -Wall -g -o main main.c `pkg-config --cflags --libs glib-2.0` -lyaml */

void process_layer(yaml_parser_t *parser, GNode *data);
gboolean dump(GNode *n, gpointer data);



int main (int argc, char **argv) {
    char *file_path = "test.yaml";
    GNode *cfg = g_node_new(file_path);
    yaml_parser_t parser;

    FILE *source = fopen(file_path, "rb");
    yaml_parser_initialize(&parser);
    yaml_parser_set_input_file(&parser, source);
    process_layer(&parser, cfg); // Recursive parsing
   
yaml_parser_delete(&parser);
    fclose(source);

    printf("Results iteration:\n");
    g_node_traverse(cfg, G_PRE_ORDER, G_TRAVERSE_ALL, -1, dump, NULL);
    g_node_destroy(cfg);

    return(0);
}



enum storage_flags { VAR, VAL, SEQ }; // "Store as" switch

void process_layer(yaml_parser_t *parser, GNode *data) {
    GNode *last_leaf = data;
    yaml_event_t event;
    int storage = VAR; // mapping cannot start with VAL definition w/o VAR key

   
while (1) {
        yaml_parser_parse(parser, &event);

        // Parse value either as a new leaf in the mapping
        //  or as a leaf value (one of them, in case it's a sequence)
       
if (event.type == YAML_SCALAR_EVENT) {
                if (storage) g_node_append_data(last_leaf, g_strdup((gchar*) event.data.scalar.value));
                else last_leaf = g_node_append(data, g_node_new(g_strdup((gchar*) event.data.scalar.value)));
                storage ^= VAL; // Flip VAR/VAL switch for the next event
       
}

        // Sequence - all the following scalars will be appended to the last_leaf
       
else if (event.type == YAML_SEQUENCE_START_EVENT) storage = SEQ;
        else if (event.type == YAML_SEQUENCE_END_EVENT) storage = VAR;

        // depth += 1
       
else if (event.type == YAML_MAPPING_START_EVENT) {
                process_layer(parser, last_leaf);
                storage ^= VAL; // Flip VAR/VAL, w/o touching SEQ
       
}

        // depth -= 1
       
else if (
                event.type == YAML_MAPPING_END_EVENT
                || event.type == YAML_STREAM_END_EVENT
        ) break;

        yaml_event_delete(&event);
    }
}


gboolean dump(GNode *node, gpointer data) {
    int i = g_node_depth(node);
    while (--i) printf(" ");
    printf("%s\n", (char*) node->data);
    return(FALSE);
}


http://pyyaml.org/wiki/LibYAML

test.yaml
Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

package:
# General Package Settings
  - &Info
    # Package Name
    name: binutils

    # Package Version
    version: 2.19.1
   
    # Package Release
    release: 1
   
    # Package Description
    description: "The Binutils package contains a
    linker, an assembler, and other tools for handling
    object files."

    # SBU
    sbu: 1
   
    # Required disk space
    space: 248MB
   
    # Package Home Location
    home: http://sources.redhat.com/binutils/

    # Package Download Location
    download: ["http://ftp.gnu.org/gnu/binutils/binutils-2.19.1.tar.bz2"]

    # Package Checksum
    chksum: 09a8c5821a2dfdbb20665bc0bd680791

    # Dependances
    depends: [bash, coreutils, diffutils, gcc, gettext, glibc, grep, make, perl, sed, texinfo]

# Toolchain Build Instructions
  - &Toolchain
    # Pre Configure
    pre_configure: "
    mkdir -v ../binutils-build
    cd ../binutils-build
    "
    # Configure
    configure: "
    ../binutils-2.19.1/configure --target=$ZFS_TGT --prefix=/tools \
    --disable-nls --disable-werror
    "

    # Build
    build: "
    make
    "

    # Install
    install: "
    make install
    "

# 32Bit Build Instructions
  - &32Bit
    # Configure
    configure: "
    "

    # Build
    build: "
    "

    # Install
    install: "
    "
   
default: *info
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP