aboutsummaryrefslogtreecommitdiff
path: root/src/vdf/tests/index.c
blob: c3f96b1a282ac1a4e39b4f8c8a250190f1e6f901 (plain)
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
/**
 * a command line utility that parses a given
 * vdf file and indexes requested values by key
 *
 * Returns 0 if value is a string otherwise 1
 *
 * Used for testing to ensure indexing works reliably
 * on a known file
 */
#include <stdio.h>

#include "vdf.h"

int main(int argc, char** argv)
{
    if (argc < 2)
    {
        puts("vdf_index file index [index...]");
        return 1;
    }

    struct vdf_object* o = vdf_parse_file(argv[1]);

    if (!o)
    {
        puts("Invalid File");
        return 1;
    }

    struct vdf_object* k = o;
    for (int i = 2; i < argc; ++i)
    {
        k = vdf_object_index_array_str(k, argv[i]);
        if (!k)
        {
            printf("Invalid index '%s'\n", argv[i]);
            break;
        }
    }

    int retval = k->type != VDF_TYPE_STRING;

    if (retval)
        vdf_print_object(k);
    else
        puts(k->data.data_string.str);

    vdf_free_object(o);

    return retval;
}