~stub/ubuntu/trusty/avro-c/trunk

« back to all changes in this revision

Viewing changes to src/avrocat.c

  • Committer: Stuart Bishop
  • Date: 2015-05-14 11:53:53 UTC
  • Revision ID: stuart@stuartbishop.net-20150514115353-0cvnrcyohcq5l7yj
Tags: upstream-1.7.7
ImportĀ upstreamĀ versionĀ 1.7.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to you under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 *
 
9
 * http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
14
 * implied.  See the License for the specific language governing
 
15
 * permissions and limitations under the License.
 
16
 */
 
17
 
 
18
#include <errno.h>
 
19
#include <stdio.h>
 
20
#include <stdlib.h>
 
21
#include <string.h>
 
22
 
 
23
#include "avro.h"
 
24
#include "avro_private.h"
 
25
 
 
26
 
 
27
/*-- PROCESSING A FILE --*/
 
28
 
 
29
static void
 
30
process_file(const char *filename)
 
31
{
 
32
        avro_file_reader_t  reader;
 
33
        FILE *fp;
 
34
        int  should_close;
 
35
 
 
36
        if (filename == NULL) {
 
37
                fp = stdin;
 
38
                filename = "<stdin>";
 
39
                should_close = 0;
 
40
        } else {
 
41
                fp = fopen(filename, "rb");
 
42
                should_close = 1;
 
43
 
 
44
                if (fp == NULL) {
 
45
                        fprintf(stderr, "Error opening %s:\n  %s\n",
 
46
                                filename, strerror(errno));
 
47
                        exit(1);
 
48
                }
 
49
        }
 
50
 
 
51
        if (avro_file_reader_fp(fp, filename, 0, &reader)) {
 
52
                fprintf(stderr, "Error opening %s:\n  %s\n",
 
53
                        filename, avro_strerror());
 
54
                if (should_close) {
 
55
                        fclose(fp);
 
56
                }
 
57
                exit(1);
 
58
        }
 
59
 
 
60
        avro_schema_t  wschema;
 
61
        avro_value_iface_t  *iface;
 
62
        avro_value_t  value;
 
63
 
 
64
        wschema = avro_file_reader_get_writer_schema(reader);
 
65
        iface = avro_generic_class_from_schema(wschema);
 
66
        avro_generic_value_new(iface, &value);
 
67
 
 
68
        int rval;
 
69
 
 
70
        while ((rval = avro_file_reader_read_value(reader, &value)) == 0) {
 
71
                char  *json;
 
72
 
 
73
                if (avro_value_to_json(&value, 1, &json)) {
 
74
                        fprintf(stderr, "Error converting value to JSON: %s\n",
 
75
                                avro_strerror());
 
76
                } else {
 
77
                        printf("%s\n", json);
 
78
                        free(json);
 
79
                }
 
80
 
 
81
                avro_value_reset(&value);
 
82
        }
 
83
 
 
84
        // If it was not an EOF that caused it to fail,
 
85
        // print the error.
 
86
        if (rval != EOF) {
 
87
                fprintf(stderr, "Error: %s\n", avro_strerror());
 
88
        }
 
89
 
 
90
        avro_file_reader_close(reader);
 
91
        avro_value_decref(&value);
 
92
        avro_value_iface_decref(iface);
 
93
        avro_schema_decref(wschema);
 
94
 
 
95
        if (should_close) {
 
96
                fclose(fp);
 
97
        }
 
98
}
 
99
 
 
100
 
 
101
/*-- MAIN PROGRAM --*/
 
102
 
 
103
static void usage(void)
 
104
{
 
105
        fprintf(stderr,
 
106
                "Usage: avrocat <avro data file>\n");
 
107
}
 
108
 
 
109
 
 
110
int main(int argc, char **argv)
 
111
{
 
112
        char  *data_filename;
 
113
 
 
114
        if (argc == 2) {
 
115
                data_filename = argv[1];
 
116
        } else if (argc == 1) {
 
117
                data_filename = NULL;
 
118
        } else {
 
119
                fprintf(stderr, "Can't read from multiple input files.\n");
 
120
                usage();
 
121
                exit(1);
 
122
        }
 
123
 
 
124
        /* Process the data file */
 
125
        process_file(data_filename);
 
126
        return 0;
 
127
}