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
80
|
/*
* upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2009 Joshua Haberman. See LICENSE for details.
*/
#include <inttypes.h>
#include "upb_text.h"
#include "descriptor.h"
void upb_text_printval(upb_field_type_t type, union upb_value val, FILE *file)
{
#define CASE(fmtstr, member) fprintf(file, fmtstr, val.member); break;
switch(type) {
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_DOUBLE:
CASE("%0.f", _double);
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FLOAT:
CASE("%0.f", _float)
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT64:
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED64:
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT64:
CASE("%" PRId64, int64)
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT64:
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED64:
CASE("%" PRIu64, uint64)
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32:
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED32:
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT32:
CASE("%" PRId32, int32)
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32:
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED32:
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ENUM:
CASE("%" PRIu32, uint32);
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BOOL:
CASE("%hhu", _bool);
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_STRING:
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BYTES:
/* TODO: escaping. */
fprintf(file, "\"" UPB_STRFMT "\"", UPB_STRARG(*val.str)); break;
}
}
static void print_indent(struct upb_text_printer *p, FILE *stream)
{
if(!p->single_line)
for(int i = 0; i < p->indent_depth; i++)
fprintf(stream, " ");
}
void upb_text_printfield(struct upb_text_printer *p,
struct upb_string name,
upb_field_type_t valtype, union upb_value val,
FILE *stream)
{
print_indent(p, stream);
fprintf(stream, UPB_STRFMT ":", UPB_STRARG(name));
upb_text_printval(valtype, val, stream);
if(p->single_line)
fputc(' ', stream);
else
fputc('\n', stream);
}
void upb_text_push(struct upb_text_printer *p,
struct upb_string submsg_type,
FILE *stream)
{
print_indent(p, stream);
fprintf(stream, UPB_STRFMT " {", UPB_STRARG(submsg_type));
if(!p->single_line) fputc('\n', stream);
p->indent_depth++;
}
void upb_text_pop(struct upb_text_printer *p,
FILE *stream)
{
p->indent_depth--;
print_indent(p, stream);
fprintf(stream, "}\n");
}
|