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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
#include "avro_private.h"
28
typedef enum specific_state specific_state;
36
typedef struct specific_ctx specific_ctx;
38
static void indent(specific_ctx * ctx, FILE * fp)
41
for (i = 0; i < ctx->depth; i++) {
46
static int avro_schema_to_source(avro_schema_t schema, specific_ctx * ctx)
48
switch (schema->type) {
55
static int avro_schema_to_header(avro_schema_t schema, specific_ctx * ctx)
58
FILE *fp = ctx->header;
63
if (is_avro_primitive(schema) && !ctx->name) {
67
switch (schema->type) {
69
fprintf(fp, "char *%s;\n", ctx->name);
73
fprintf(fp, "struct %s { size_t %s_len; char *%s_val } %s;\n",
74
ctx->name, ctx->name, ctx->name, ctx->name);
78
fprintf(fp, "int %s;\n", ctx->name);
82
fprintf(fp, "long %s;\n", ctx->name);
86
fprintf(fp, "float %s;\n", ctx->name);
90
fprintf(fp, "double %s;\n", ctx->name);
94
fprintf(fp, "int %s; /* boolean */\n", ctx->name);
102
struct schema_record_t *record_schema =
103
avro_schema_to_record(schema);
104
fprintf(fp, "struct %s {\n", record_schema->name);
105
for (i = 0; i < record_schema->num_fields; i++) {
106
struct record_field_t *field =
107
record_schema->fields[i];
108
ctx->name = field->name;
109
avro_schema_to_header(field->type, ctx);
113
fprintf(fp, "typedef struct %s %s;\n\n",
114
record_schema->name, record_schema->name);
120
struct schema_enum_t *enum_schema =
121
avro_schema_to_enum(schema);
122
fprintf(fp, "enum %s {\n", enum_schema->name);
124
for (i = 0; i < enum_schema->num_symbols; i++) {
126
fprintf(fp, "%s = %ld,\n",
127
enum_schema->symbols[i], i);
131
fprintf(fp, "typedef enum %s %s;\n\n",
132
enum_schema->name, enum_schema->name);
138
struct schema_fixed_t *fixed_schema =
139
avro_schema_to_fixed(schema);
140
fprintf(fp, "char %s[%ld];\n", fixed_schema->name,
153
struct schema_array_t *array_schema =
154
avro_schema_to_array(schema);
158
fprintf(fp, "struct { size_t %s_len; ", ctx->name);
159
if (is_avro_named_type(array_schema->items)) {
161
avro_schema_name(array_schema->items));
162
} else if (is_avro_link(array_schema->items)) {
163
struct schema_link_t *link_schema =
164
avro_schema_to_link(array_schema->items);
165
fprintf(fp, "struct %s",
166
avro_schema_name(link_schema->to));
168
avro_schema_to_header(array_schema->items, ctx);
170
fprintf(fp, " *%s_val;} %s;\n", ctx->name, ctx->name);
175
struct schema_union_t *union_schema =
176
avro_schema_to_array(schema);
180
fprintf(fp, "union {\n");
181
for (i = 0; i < union_schema->num_schemas; i++) {
182
avro_schema_to_header(union_schema->schemas[i],
185
fprintf(fp, "%s_u;\n");
198
int avro_schema_to_specific(avro_schema_t schema, const char *prefix)
208
memset(&ctx, 0, sizeof(ctx));
209
snprintf(buf, sizeof(buf), "%s_avro.h", prefix);
210
ctx.header = fopen(buf, "w");
214
snprintf(buf, sizeof(buf), "%s_avro.c", prefix);
215
ctx.source = fopen(buf, "w");
221
rval = avro_schema_to_header(schema, &ctx);
226
rval = avro_schema_to_source(schema, &ctx);