summaryrefslogtreecommitdiff
path: root/upb
diff options
context:
space:
mode:
Diffstat (limited to 'upb')
-rw-r--r--upb/bytestream.c4
-rw-r--r--upb/bytestream.h31
-rw-r--r--upb/handlers.h4
-rw-r--r--upb/pb/textprinter.c192
-rw-r--r--upb/upb.h2
5 files changed, 134 insertions, 99 deletions
diff --git a/upb/bytestream.c b/upb/bytestream.c
index 95ba064..09a1fb9 100644
--- a/upb/bytestream.c
+++ b/upb/bytestream.c
@@ -262,7 +262,7 @@ static int32_t upb_stringsink_vprintf(void *_s, const char *fmt, va_list args) {
return ret;
}
-bool upb_stringsink_write(void *_s, const char *buf, size_t len) {
+int upb_stringsink_write(void *_s, const void *buf, int len) {
// TODO: detect realloc() errors.
upb_stringsink *s = _s;
if (s->len + len > s->size) {
@@ -271,7 +271,7 @@ bool upb_stringsink_write(void *_s, const char *buf, size_t len) {
}
memcpy(s->str + s->len, buf, len);
s->len += len;
- return true;
+ return len;
}
void upb_stringsink_init(upb_stringsink *s) {
diff --git a/upb/bytestream.h b/upb/bytestream.h
index 741b7e7..83976a3 100644
--- a/upb/bytestream.h
+++ b/upb/bytestream.h
@@ -145,8 +145,8 @@ INLINE void upb_strref_read(struct _upb_strref *r, char *buf) {
/* upb_bytesink ***************************************************************/
-typedef bool upb_bytesink_write_func(void*, const char*, size_t);
-typedef int32_t upb_bytesink_vprintf_func(void*, const char *fmt, va_list args);
+typedef int upb_bytesink_write_func(void*, const void*, int);
+typedef int upb_bytesink_vprintf_func(void*, const char *fmt, va_list args);
typedef struct {
upb_bytesink_write_func *write;
@@ -156,22 +156,23 @@ typedef struct {
typedef struct {
upb_bytesink_vtbl *vtbl;
upb_status status;
+ uint64_t offset;
} upb_bytesink;
// Should be called by derived classes.
void upb_bytesink_init(upb_bytesink *sink, upb_bytesink_vtbl *vtbl);
void upb_bytesink_uninit(upb_bytesink *sink);
-INLINE bool upb_bytesink_write(upb_bytesink *s, const char *buf, size_t len) {
+INLINE int upb_bytesink_write(upb_bytesink *s, const void *buf, int len) {
return s->vtbl->write(s, buf, len);
}
-INLINE bool upb_bytesink_writestr(upb_bytesink *sink, const char *str) {
+INLINE int upb_bytesink_writestr(upb_bytesink *sink, const char *str) {
return upb_bytesink_write(sink, str, strlen(str));
}
// Returns the number of bytes written or -1 on error.
-INLINE int32_t upb_bytesink_printf(upb_bytesink *sink, const char *fmt, ...) {
+INLINE int upb_bytesink_printf(upb_bytesink *sink, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
uint32_t ret = sink->vtbl->vprintf(sink, fmt, args);
@@ -179,6 +180,26 @@ INLINE int32_t upb_bytesink_printf(upb_bytesink *sink, const char *fmt, ...) {
return ret;
}
+INLINE int upb_bytesink_putc(upb_bytesink *sink, char ch) {
+ return upb_bytesink_write(sink, &ch, 1);
+}
+
+INLINE int upb_bytesink_putrepeated(upb_bytesink *sink, char ch, int len) {
+ char buf[len];
+ memset(buf, ch, len);
+ return upb_bytesink_write(sink, buf, len);
+}
+
+INLINE uint64_t upb_bytesink_getoffset(upb_bytesink *sink) {
+ return sink->offset;
+}
+
+INLINE void upb_bytesink_rewind(upb_bytesink *sink, uint64_t offset) {
+ // TODO
+ (void)sink;
+ (void)offset;
+}
+
// OPT: add getappendbuf()
// OPT: add writefrombytesrc()
// TODO: add flush()
diff --git a/upb/handlers.h b/upb/handlers.h
index 2e44318..a7c1d9d 100644
--- a/upb/handlers.h
+++ b/upb/handlers.h
@@ -89,7 +89,7 @@ typedef enum {
// Halt processing permanently (in a non-resumable way). The endmsg handlers
// for any currently open messages will be called which can supply a more
// specific status message. No further input data will be consumed.
- UPB_BREAK,
+ UPB_BREAK = -1,
// Skips to the end of the current submessage (or if we are at the top
// level, skips to the end of the entire message). In other words, it is
@@ -102,7 +102,7 @@ typedef enum {
//
// If UPB_SKIPSUBMSG is called from the top-level message, no further input
// data will be consumed.
- UPB_SKIPSUBMSG,
+ UPB_SKIPSUBMSG = -2,
// TODO: Add UPB_SUSPEND, for resumable producers/consumers.
} upb_flow_t;
diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c
index 893953c..37f5699 100644
--- a/upb/pb/textprinter.c
+++ b/upb/pb/textprinter.c
@@ -12,7 +12,7 @@
#include "upb/pb/textprinter.h"
struct _upb_textprinter {
- upb_bytesink *bytesink;
+ upb_bytesink *sink;
int indent_depth;
bool single_line;
upb_status status;
@@ -20,11 +20,26 @@ struct _upb_textprinter {
#define CHECK(x) if ((x) < 0) goto err;
+static int upb_textprinter_indent(upb_textprinter *p) {
+ if (!p->single_line)
+ CHECK(upb_bytesink_putrepeated(p->sink, ' ', p->indent_depth*2));
+ return 0;
+err:
+ return -1;
+}
+
+static int upb_textprinter_endfield(upb_textprinter *p) {
+ CHECK(upb_bytesink_putc(p->sink, p->single_line ? ' ' : '\n'));
+ return 0;
+err:
+ return -1;
+}
+
static int upb_textprinter_putescaped(upb_textprinter *p, upb_strref *strref,
bool preserve_utf8) {
// Based on CEscapeInternal() from Google's protobuf release.
- // TODO; we could read directly fraom a bytesrc's buffer instead.
- // TODO; we could write directly into a bytesink's buffer instead.
+ // TODO; we could read directly from a bytesrc's buffer instead.
+ // TODO; we could write strrefs to the sink when possible.
char dstbuf[4096], *dst = dstbuf, *dstend = dstbuf + sizeof(dstbuf);
char buf[strref->len], *src = buf;
char *end = src + strref->len;
@@ -32,12 +47,12 @@ static int upb_textprinter_putescaped(upb_textprinter *p, upb_strref *strref,
// I think hex is prettier and more useful, but proto2 uses octal; should
// investigate whether it can parse hex also.
- bool use_hex = false;
+ const bool use_hex = false;
bool last_hex_escape = false; // true if last output char was \xNN
for (; src < end; src++) {
if (dstend - dst < 4) {
- CHECK(upb_bytesink_write(p->bytesink, dstbuf, dst - dstbuf));
+ CHECK(upb_bytesink_write(p->sink, dstbuf, dst - dstbuf));
dst = dstbuf;
}
@@ -65,108 +80,102 @@ static int upb_textprinter_putescaped(upb_textprinter *p, upb_strref *strref,
last_hex_escape = is_hex_escape;
}
// Flush remaining data.
- CHECK(upb_bytesink_write(p->bytesink, dst, dst - dstbuf));
+ CHECK(upb_bytesink_write(p->sink, dst, dst - dstbuf));
return 0;
err:
return -1;
}
-static int upb_textprinter_indent(upb_textprinter *p) {
- if(!p->single_line)
- for(int i = 0; i < p->indent_depth; i++)
- CHECK(upb_bytesink_writestr(p->bytesink, " "));
- return 0;
-err:
- return -1;
+#define TYPE(member, fmt) \
+ static upb_flow_t upb_textprinter_put ## member(void *_p, upb_value fval, \
+ upb_value val) { \
+ upb_textprinter *p = _p; \
+ upb_fielddef *f = upb_value_getfielddef(fval); \
+ uint64_t start_ofs = upb_bytesink_getoffset(p->sink); \
+ CHECK(upb_textprinter_indent(p)); \
+ CHECK(upb_bytesink_writestr(p->sink, f->name)); \
+ CHECK(upb_bytesink_writestr(p->sink, ": ")); \
+ CHECK(upb_bytesink_printf(p->sink, fmt, upb_value_get ## member(val))); \
+ CHECK(upb_textprinter_endfield(p)); \
+ return UPB_CONTINUE; \
+ err: \
+ upb_bytesink_rewind(p->sink, start_ofs); \
+ return UPB_BREAK; \
}
-static int upb_textprinter_endfield(upb_textprinter *p) {
- if(p->single_line) {
- CHECK(upb_bytesink_writestr(p->bytesink, " "));
+#define STRINGIFY_HELPER(x) #x
+#define STRINGIFY_MACROVAL(x) STRINGIFY_HELPER(x)
+
+TYPE(double, "%." STRINGIFY_MACROVAL(DBL_DIG) "g")
+TYPE(float, "%." STRINGIFY_MACROVAL(FLT_DIG) "g")
+TYPE(int64, "%" PRId64)
+TYPE(uint64, "%" PRIu64)
+TYPE(int32, "%" PRId32)
+TYPE(uint32, "%" PRIu32);
+TYPE(bool, "%hhu");
+
+// Output a symbolic value from the enum if found, else just print as int32.
+static upb_flow_t upb_textprinter_putenum(void *_p, upb_value fval,
+ upb_value val) {
+
+ upb_textprinter *p = _p;
+ uint64_t start_ofs = upb_bytesink_getoffset(p->sink);
+ upb_fielddef *f = upb_value_getfielddef(fval);
+ upb_enumdef *enum_def = upb_downcast_enumdef(f->def);
+ const char *label = upb_enumdef_iton(enum_def, upb_value_getint32(val));
+ if (label) {
+ CHECK(upb_bytesink_writestr(p->sink, label));
} else {
- CHECK(upb_bytesink_writestr(p->bytesink, "\n"));
+ CHECK(upb_textprinter_putint32(_p, fval, val));
}
- return 0;
+ return UPB_CONTINUE;
err:
- return -1;
+ upb_bytesink_rewind(p->sink, start_ofs);
+ return UPB_BREAK;
}
-static upb_flow_t upb_textprinter_value(void *_p, upb_value fval,
- upb_value val) {
+static upb_flow_t upb_textprinter_putstr(void *_p, upb_value fval,
+ upb_value val) {
upb_textprinter *p = _p;
+ uint64_t start_ofs = upb_bytesink_getoffset(p->sink);
upb_fielddef *f = upb_value_getfielddef(fval);
- upb_textprinter_indent(p);
- CHECK(upb_bytesink_printf(p->bytesink, "%s: ", f->name));
-#define CASE(fmtstr, member) \
- CHECK(upb_bytesink_printf(p->bytesink, fmtstr, upb_value_get ## member(val))); break;
- switch(f->type) {
- // TODO: figure out what we should really be doing for these
- // floating-point formats.
- case UPB_TYPE(DOUBLE):
- CHECK(upb_bytesink_printf(p->bytesink, "%.*g", DBL_DIG, upb_value_getdouble(val))); break;
- case UPB_TYPE(FLOAT):
- CHECK(upb_bytesink_printf(p->bytesink, "%.*g", FLT_DIG+2, upb_value_getfloat(val))); break;
- 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): {
- upb_enumdef *enum_def = upb_downcast_enumdef(f->def);
- const char *label = upb_enumdef_iton(enum_def, upb_value_getint32(val));
- if (label) {
- // We found a corresponding string for this enum. Otherwise we fall
- // through to the int32 code path.
- CHECK(upb_bytesink_writestr(p->bytesink, label));
- break;
- }
- }
- case UPB_TYPE(INT32):
- case UPB_TYPE(SFIXED32):
- case UPB_TYPE(SINT32):
- CASE("%" PRId32, int32)
- case UPB_TYPE(BOOL):
- CASE("%hhu", bool);
- case UPB_TYPE(STRING):
- case UPB_TYPE(BYTES): {
- CHECK(upb_bytesink_writestr(p->bytesink, "\""));
- CHECK(upb_textprinter_putescaped(p, upb_value_getstrref(val),
- f->type == UPB_TYPE(STRING)));
- CHECK(upb_bytesink_writestr(p->bytesink, "\""));
- break;
- }
- }
- upb_textprinter_endfield(p);
+ CHECK(upb_bytesink_putc(p->sink, '"'));
+ CHECK(upb_textprinter_putescaped(p, upb_value_getstrref(val),
+ f->type == UPB_TYPE(STRING)));
+ CHECK(upb_bytesink_putc(p->sink, '"'));
return UPB_CONTINUE;
err:
+ upb_bytesink_rewind(p->sink, start_ofs);
return UPB_BREAK;
}
static upb_sflow_t upb_textprinter_startsubmsg(void *_p, upb_value fval) {
upb_textprinter *p = _p;
+ uint64_t start_ofs = upb_bytesink_getoffset(p->sink);
upb_fielddef *f = upb_value_getfielddef(fval);
- upb_textprinter_indent(p);
- bool ret = upb_bytesink_printf(p->bytesink, "%s {", f->name);
- if (!ret) return UPB_SBREAK;
+ CHECK(upb_textprinter_indent(p));
+ CHECK(upb_bytesink_printf(p->sink, "%s {", f->name));
if (!p->single_line)
- upb_bytesink_writestr(p->bytesink, "\n");
+ CHECK(upb_bytesink_putc(p->sink, '\n'));
p->indent_depth++;
return UPB_CONTINUE_WITH(_p);
+err:
+ upb_bytesink_rewind(p->sink, start_ofs);
+ return UPB_SBREAK;
}
static upb_flow_t upb_textprinter_endsubmsg(void *_p, upb_value fval) {
(void)fval;
upb_textprinter *p = _p;
+ uint64_t start_ofs = upb_bytesink_getoffset(p->sink);
p->indent_depth--;
- upb_textprinter_indent(p);
- upb_bytesink_writestr(p->bytesink, "}");
- upb_textprinter_endfield(p);
+ CHECK(upb_textprinter_indent(p));
+ CHECK(upb_bytesink_putc(p->sink, '}'));
+ CHECK(upb_textprinter_endfield(p));
return UPB_CONTINUE;
+err:
+ upb_bytesink_rewind(p->sink, start_ofs);
+ return UPB_BREAK;
}
upb_textprinter *upb_textprinter_new() {
@@ -174,26 +183,31 @@ upb_textprinter *upb_textprinter_new() {
return p;
}
-void upb_textprinter_free(upb_textprinter *p) {
- free(p);
-}
+void upb_textprinter_free(upb_textprinter *p) { free(p); }
void upb_textprinter_reset(upb_textprinter *p, upb_bytesink *sink,
bool single_line) {
- p->bytesink = sink;
+ p->sink = sink;
p->single_line = single_line;
p->indent_depth = 0;
}
+static void upb_textprinter_onfreg(void *c, upb_fhandlers *fh, upb_fielddef *f) {
+ (void)c;
+ upb_fhandlers_setstartsubmsg(fh, &upb_textprinter_startsubmsg);
+ upb_fhandlers_setendsubmsg(fh, &upb_textprinter_endsubmsg);
+#define F(type) &upb_textprinter_put ## type
+ static upb_value_handler *fptrs[] = {NULL, F(double), F(float), F(int64),
+ F(uint64), F(int32), F(uint64), F(uint32), F(bool), F(str),
+ NULL, NULL, F(str), F(uint32), F(enum), F(int32),
+ F(int64), F(int32), F(int64)};
+ upb_fhandlers_setvalue(fh, fptrs[f->type]);
+ upb_value fval;
+ upb_value_setfielddef(&fval, f);
+ upb_fhandlers_setfval(fh, fval);
+}
+
upb_mhandlers *upb_textprinter_reghandlers(upb_handlers *h, upb_msgdef *m) {
- upb_handlerset hset = {
- NULL, // startmsg
- NULL, // endmsg
- upb_textprinter_value,
- upb_textprinter_startsubmsg,
- upb_textprinter_endsubmsg,
- NULL, // startseq
- NULL, // endseq
- };
- return upb_handlers_reghandlerset(h, m, &hset);
+ return upb_handlers_regmsgdef(
+ h, m, NULL, &upb_textprinter_onfreg, NULL);
}
diff --git a/upb/upb.h b/upb/upb.h
index d8ecd9b..708de7c 100644
--- a/upb/upb.h
+++ b/upb/upb.h
@@ -226,7 +226,7 @@ void upb_status_setcode(upb_status *s, upb_errorspace *space, int code);
const char *upb_status_getstr(upb_status *s);
void upb_status_copy(upb_status *to, upb_status *from);
-upb_errorspace upb_posix_errorspace;
+extern upb_errorspace upb_posix_errorspace;
void upb_status_fromerrno(upb_status *status);
// Like vaprintf, but uses *buf (which can be NULL) as a starting point and
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback