Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
6.3 KiB
200 lines
6.3 KiB
15 years ago
|
/*
|
||
|
* upb - a minimalist implementation of protocol buffers.
|
||
|
*
|
||
14 years ago
|
* Copyright (c) 2009 Google Inc. See LICENSE for details.
|
||
|
* Author: Josh Haberman <jhaberman@gmail.com>
|
||
15 years ago
|
*/
|
||
|
|
||
14 years ago
|
#include <ctype.h>
|
||
|
#include <float.h>
|
||
15 years ago
|
#include <inttypes.h>
|
||
15 years ago
|
#include <stdlib.h>
|
||
14 years ago
|
#include "upb/pb/textprinter.h"
|
||
15 years ago
|
|
||
|
struct _upb_textprinter {
|
||
|
upb_bytesink *bytesink;
|
||
|
int indent_depth;
|
||
|
bool single_line;
|
||
14 years ago
|
upb_status status;
|
||
15 years ago
|
};
|
||
|
|
||
14 years ago
|
#define CHECK(x) if ((x) < 0) goto err;
|
||
|
|
||
14 years ago
|
static int upb_textprinter_putescaped(upb_textprinter *p, upb_strref *strref,
|
||
14 years ago
|
bool preserve_utf8) {
|
||
|
// Based on CEscapeInternal() from Google's protobuf release.
|
||
14 years ago
|
// TODO; we could read directly fraom a bytesrc's buffer instead.
|
||
14 years ago
|
// TODO; we could write directly into a bytesink's buffer instead.
|
||
|
char dstbuf[4096], *dst = dstbuf, *dstend = dstbuf + sizeof(dstbuf);
|
||
14 years ago
|
char buf[strref->len], *src = buf;
|
||
|
char *end = src + strref->len;
|
||
14 years ago
|
upb_bytesrc_read(strref->bytesrc, strref->stream_offset, strref->len, buf);
|
||
14 years ago
|
|
||
|
// I think hex is prettier and more useful, but proto2 uses octal; should
|
||
|
// investigate whether it can parse hex also.
|
||
|
bool use_hex = false;
|
||
|
bool last_hex_escape = false; // true if last output char was \xNN
|
||
|
|
||
|
for (; src < end; src++) {
|
||
|
if (dstend - dst < 4) {
|
||
14 years ago
|
CHECK(upb_bytesink_write(p->bytesink, dstbuf, dst - dstbuf, &p->status));
|
||
14 years ago
|
dst = dstbuf;
|
||
|
}
|
||
|
|
||
|
bool is_hex_escape = false;
|
||
|
switch (*src) {
|
||
|
case '\n': *(dst++) = '\\'; *(dst++) = 'n'; break;
|
||
|
case '\r': *(dst++) = '\\'; *(dst++) = 'r'; break;
|
||
|
case '\t': *(dst++) = '\\'; *(dst++) = 't'; break;
|
||
|
case '\"': *(dst++) = '\\'; *(dst++) = '\"'; break;
|
||
|
case '\'': *(dst++) = '\\'; *(dst++) = '\''; break;
|
||
|
case '\\': *(dst++) = '\\'; *(dst++) = '\\'; break;
|
||
|
default:
|
||
|
// Note that if we emit \xNN and the src character after that is a hex
|
||
|
// digit then that digit must be escaped too to prevent it being
|
||
|
// interpreted as part of the character code by C.
|
||
|
if ((!preserve_utf8 || (uint8_t)*src < 0x80) &&
|
||
|
(!isprint(*src) || (last_hex_escape && isxdigit(*src)))) {
|
||
|
sprintf(dst, (use_hex ? "\\x%02x" : "\\%03o"), (uint8_t)*src);
|
||
|
is_hex_escape = use_hex;
|
||
|
dst += 4;
|
||
|
} else {
|
||
|
*(dst++) = *src; break;
|
||
|
}
|
||
|
}
|
||
|
last_hex_escape = is_hex_escape;
|
||
|
}
|
||
|
// Flush remaining data.
|
||
14 years ago
|
CHECK(upb_bytesink_write(p->bytesink, dst, dst - dstbuf, &p->status));
|
||
14 years ago
|
return 0;
|
||
|
err:
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
14 years ago
|
static int upb_textprinter_indent(upb_textprinter *p) {
|
||
14 years ago
|
if(!p->single_line)
|
||
|
for(int i = 0; i < p->indent_depth; i++)
|
||
14 years ago
|
CHECK(upb_bytesink_writestr(p->bytesink, " ", &p->status));
|
||
14 years ago
|
return 0;
|
||
|
err:
|
||
|
return -1;
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
static int upb_textprinter_endfield(upb_textprinter *p) {
|
||
|
if(p->single_line) {
|
||
14 years ago
|
CHECK(upb_bytesink_writestr(p->bytesink, " ", &p->status));
|
||
14 years ago
|
} else {
|
||
14 years ago
|
CHECK(upb_bytesink_writestr(p->bytesink, "\n", &p->status));
|
||
14 years ago
|
}
|
||
|
return 0;
|
||
|
err:
|
||
|
return -1;
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
static upb_flow_t upb_textprinter_value(void *_p, upb_value fval,
|
||
14 years ago
|
upb_value val) {
|
||
|
upb_textprinter *p = _p;
|
||
14 years ago
|
upb_fielddef *f = upb_value_getfielddef(fval);
|
||
14 years ago
|
upb_textprinter_indent(p);
|
||
14 years ago
|
CHECK(upb_bytesink_printf(p->bytesink, &p->status, "%s: ", f->name));
|
||
14 years ago
|
#define CASE(fmtstr, member) \
|
||
|
CHECK(upb_bytesink_printf(p->bytesink, &p->status, fmtstr, upb_value_get ## member(val))); break;
|
||
|
switch(f->type) {
|
||
14 years ago
|
// TODO: figure out what we should really be doing for these
|
||
|
// floating-point formats.
|
||
14 years ago
|
case UPB_TYPE(DOUBLE):
|
||
14 years ago
|
CHECK(upb_bytesink_printf(p->bytesink, &p->status, "%.*g", DBL_DIG, upb_value_getdouble(val))); break;
|
||
14 years ago
|
case UPB_TYPE(FLOAT):
|
||
14 years ago
|
CHECK(upb_bytesink_printf(p->bytesink, &p->status, "%.*g", FLT_DIG+2, upb_value_getfloat(val))); break;
|
||
14 years ago
|
case UPB_TYPE(INT64):
|
||
|
case UPB_TYPE(SFIXED64):
|
||
|
case UPB_TYPE(SINT64):
|
||
|
CASE("%" PRId64, int64)
|
||
|
case UPB_TYPE(UINT64):
|
||
|
case UPB_TYPE(FIXED64):
|
||
|
CASE("%" PRIu64, uint64)
|
||
|
case UPB_TYPE(UINT32):
|
||
|
case UPB_TYPE(FIXED32):
|
||
|
CASE("%" PRIu32, uint32);
|
||
|
case UPB_TYPE(ENUM): {
|
||
14 years ago
|
upb_enumdef *enum_def = upb_downcast_enumdef(f->def);
|
||
14 years ago
|
const char *label = upb_enumdef_iton(enum_def, upb_value_getint32(val));
|
||
|
if (label) {
|
||
14 years ago
|
// We found a corresponding string for this enum. Otherwise we fall
|
||
|
// through to the int32 code path.
|
||
14 years ago
|
CHECK(upb_bytesink_writestr(p->bytesink, label, &p->status));
|
||
14 years ago
|
break;
|
||
|
}
|
||
15 years ago
|
}
|
||
14 years ago
|
case UPB_TYPE(INT32):
|
||
|
case UPB_TYPE(SFIXED32):
|
||
|
case UPB_TYPE(SINT32):
|
||
|
CASE("%" PRId32, int32)
|
||
14 years ago
|
case UPB_TYPE(BOOL):
|
||
14 years ago
|
CASE("%hhu", bool);
|
||
14 years ago
|
case UPB_TYPE(STRING):
|
||
14 years ago
|
case UPB_TYPE(BYTES): {
|
||
|
CHECK(upb_bytesink_writestr(p->bytesink, "\"", &p->status));
|
||
|
CHECK(upb_textprinter_putescaped(p, upb_value_getstrref(val),
|
||
14 years ago
|
f->type == UPB_TYPE(STRING)));
|
||
14 years ago
|
CHECK(upb_bytesink_writestr(p->bytesink, "\"", &p->status));
|
||
14 years ago
|
break;
|
||
14 years ago
|
}
|
||
15 years ago
|
}
|
||
|
upb_textprinter_endfield(p);
|
||
14 years ago
|
return UPB_CONTINUE;
|
||
14 years ago
|
err:
|
||
|
return UPB_BREAK;
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
static upb_sflow_t upb_textprinter_startsubmsg(void *_p, upb_value fval) {
|
||
14 years ago
|
upb_textprinter *p = _p;
|
||
14 years ago
|
upb_fielddef *f = upb_value_getfielddef(fval);
|
||
14 years ago
|
upb_textprinter_indent(p);
|
||
14 years ago
|
bool ret = upb_bytesink_printf(p->bytesink, &p->status, "%s {", f->name);
|
||
14 years ago
|
if (!ret) return UPB_SBREAK;
|
||
|
if (!p->single_line)
|
||
14 years ago
|
upb_bytesink_writestr(p->bytesink, "\n", &p->status);
|
||
14 years ago
|
p->indent_depth++;
|
||
14 years ago
|
return UPB_CONTINUE_WITH(_p);
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
static upb_flow_t upb_textprinter_endsubmsg(void *_p, upb_value fval) {
|
||
|
(void)fval;
|
||
14 years ago
|
upb_textprinter *p = _p;
|
||
15 years ago
|
p->indent_depth--;
|
||
|
upb_textprinter_indent(p);
|
||
14 years ago
|
upb_bytesink_writestr(p->bytesink, "}", &p->status);
|
||
15 years ago
|
upb_textprinter_endfield(p);
|
||
14 years ago
|
return UPB_CONTINUE;
|
||
15 years ago
|
}
|
||
|
|
||
|
upb_textprinter *upb_textprinter_new() {
|
||
|
upb_textprinter *p = malloc(sizeof(*p));
|
||
|
return p;
|
||
|
}
|
||
|
|
||
|
void upb_textprinter_free(upb_textprinter *p) {
|
||
|
free(p);
|
||
|
}
|
||
|
|
||
14 years ago
|
void upb_textprinter_reset(upb_textprinter *p, upb_bytesink *sink,
|
||
|
bool single_line) {
|
||
|
p->bytesink = sink;
|
||
|
p->single_line = single_line;
|
||
|
p->indent_depth = 0;
|
||
|
}
|
||
|
|
||
14 years ago
|
upb_mhandlers *upb_textprinter_reghandlers(upb_handlers *h, upb_msgdef *m) {
|
||
|
upb_handlerset hset = {
|
||
14 years ago
|
NULL, // startmsg
|
||
|
NULL, // endmsg
|
||
|
upb_textprinter_value,
|
||
|
upb_textprinter_startsubmsg,
|
||
14 years ago
|
upb_textprinter_endsubmsg,
|
||
|
NULL, // startseq
|
||
|
NULL, // endseq
|
||
14 years ago
|
};
|
||
|
return upb_handlers_reghandlerset(h, m, &hset);
|
||
15 years ago
|
}
|