summaryrefslogtreecommitdiff
path: root/stream/upb_strstream.c
diff options
context:
space:
mode:
Diffstat (limited to 'stream/upb_strstream.c')
-rw-r--r--stream/upb_strstream.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/stream/upb_strstream.c b/stream/upb_strstream.c
new file mode 100644
index 0000000..65f33d9
--- /dev/null
+++ b/stream/upb_strstream.c
@@ -0,0 +1,62 @@
+/*
+ * upb - a minimalist implementation of protocol buffers.
+ *
+ * Copyright (c) 2010 Joshua Haberman. See LICENSE for details.
+ */
+
+#include "upb_strstream.h"
+
+#include <stdlib.h>
+#include "upb_string.h"
+
+struct upb_stringsrc {
+ upb_bytesrc bytesrc;
+ upb_string *str;
+};
+
+void upb_stringsrc_reset(upb_stringsrc *s, upb_string *str) {
+ if (str != s->str) {
+ if (s->str) upb_string_unref(s->str);
+ s->str = upb_string_getref(str);
+ }
+ s->bytesrc.eof = false;
+}
+
+void upb_stringsrc_free(upb_stringsrc *s) {
+ if (s->str) upb_string_unref(s->str);
+ free(s);
+}
+
+static bool upb_stringsrc_get(upb_stringsrc *src, upb_string *str,
+ upb_strlen_t minlen) {
+ // We ignore "minlen" since we always return the entire string.
+ (void)minlen;
+ upb_string_substr(str, src->str, 0, upb_string_len(src->str));
+ src->bytesrc.eof = true;
+ return true;
+}
+
+static bool upb_stringsrc_append(upb_stringsrc *src, upb_string *str,
+ upb_strlen_t len) {
+ // Unimplemented; since we return the string via "get" all in one go,
+ // this method probably isn't very useful.
+ (void)src;
+ (void)str;
+ (void)len;
+ return false;
+}
+
+static upb_bytesrc_vtable upb_stringsrc_vtbl = {
+ (upb_bytesrc_get_fptr)upb_stringsrc_get,
+ (upb_bytesrc_append_fptr)upb_stringsrc_append,
+};
+
+upb_stringsrc *upb_stringsrc_new() {
+ upb_stringsrc *s = malloc(sizeof(*s));
+ upb_bytesrc_init(&s->bytesrc, &upb_stringsrc_vtbl);
+ return s;
+}
+
+upb_bytesrc *upb_stringsrc_bytesrc(upb_stringsrc *s) {
+ return &s->bytesrc;
+}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback