File: test-parser.c

package info (click to toggle)
vnlog 1.40-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 952 kB
  • sloc: perl: 4,496; ansic: 727; python: 462; sh: 116; makefile: 7
file content (50 lines) | stat: -rw-r--r-- 1,228 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../vnlog-parser.h"

#define MSG(fmt, ...) \
    fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        fprintf(stderr, "Usage: %s input.vnl query-key\n", argv[0]);
        return 1;
    }

    const char* filename = argv[1];
    FILE* fp = (0 == strcmp(filename,"-")) ?
        stdin : fopen(filename, "r");
    if(fp == NULL)
    {
        MSG("Couldn't open '%s'", filename);
        return 1;
    }

    const char* querykey = argv[2];


    vnlog_parser_t ctx;
    if(VNL_OK != vnlog_parser_init(&ctx, fp))
        return 1;

    const char*const* queryvalue = vnlog_parser_record_from_key(&ctx, querykey);

    vnlog_parser_result_t result;
    while(VNL_OK == (result = vnlog_parser_read_record(&ctx, fp)))
    {
        printf("======\n");
        for(int i=0; i<ctx.Ncolumns; i++)
        {
            printf("%s = %s\n", ctx.record[i].key, ctx.record[i].value);
        }
        printf("query: %s = %s\n",
               querykey,
               queryvalue != NULL ? *queryvalue : "NOT FOUND");
    }

    return (result == VNL_OK || result == VNL_EOF) ? 0 : 1;
}