summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/upb_stream.h3
-rw-r--r--core/upb_string.c18
2 files changed, 20 insertions, 1 deletions
diff --git a/core/upb_stream.h b/core/upb_stream.h
index b7400c5..861bd1c 100644
--- a/core/upb_stream.h
+++ b/core/upb_stream.h
@@ -128,7 +128,8 @@ bool upb_bytesrc_get(upb_bytesrc *src, upb_string *str, upb_strlen_t minlen);
// Appends the next "len" bytes in the stream in-place to "str". This should
// be used when the caller needs to build a contiguous string of the existing
-// data in "str" with more data.
+// data in "str" with more data. The call fails if fewer than len bytes are
+// available in the stream.
bool upb_bytesrc_append(upb_bytesrc *src, upb_string *str, upb_strlen_t len);
// Returns the current error status for the stream.
diff --git a/core/upb_string.c b/core/upb_string.c
index 93686f5..847a3ee 100644
--- a/core/upb_string.c
+++ b/core/upb_string.c
@@ -131,3 +131,21 @@ upb_string *upb_strdup(upb_string *s) {
upb_strcpy(str, s);
return str;
}
+
+upb_string *upb_strreadfile(const char *filename) {
+ FILE *f = fopen(filename, "rb");
+ if(!f) return NULL;
+ if(fseek(f, 0, SEEK_END) != 0) goto error;
+ long size = ftell(f);
+ if(size < 0) goto error;
+ if(fseek(f, 0, SEEK_SET) != 0) goto error;
+ upb_string *s = upb_string_new();
+ char *buf = upb_string_getrwbuf(s, size);
+ if(fread(buf, size, 1, f) != 1) goto error;
+ fclose(f);
+ return s;
+
+error:
+ fclose(f);
+ return NULL;
+}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback