Includes are now via upb/foo.h. Files specific to the protobuf format are now in upb/pb (the core library is concerned with message definitions, handlers, and byte streams, but knows nothing about any particular serializationf format).pull/13171/head
parent
6a1f3a6693
commit
10265aa56b
42 changed files with 325 additions and 393 deletions
@ -1,71 +0,0 @@ |
|||||||
/*
|
|
||||||
* upb - a minimalist implementation of protocol buffers. |
|
||||||
* |
|
||||||
* Copyright (c) 2010 Google Inc. See LICENSE for details. |
|
||||||
* Author: Josh Haberman <jhaberman@gmail.com> |
|
||||||
* |
|
||||||
* This file provides upb_bytesrc and upb_bytesink implementations for |
|
||||||
* ANSI C stdio, which is less efficient than posixfd, but more portable. |
|
||||||
* |
|
||||||
* Specifically, stdio functions acquire locks on every operation (unless you |
|
||||||
* use the f{read,write,...}_unlocked variants, which are not standard) and |
|
||||||
* performs redundant buffering (unless you disable it with setvbuf(), but we |
|
||||||
* can only do this on newly-opened filehandles). |
|
||||||
*/ |
|
||||||
|
|
||||||
#include <stdio.h> |
|
||||||
#include "upb_bytestream.h" |
|
||||||
|
|
||||||
#ifndef UPB_STDIO_H_ |
|
||||||
#define UPB_STDIO_H_ |
|
||||||
|
|
||||||
#ifdef __cplusplus |
|
||||||
extern "C" { |
|
||||||
#endif |
|
||||||
|
|
||||||
typedef struct { |
|
||||||
uint64_t ofs; |
|
||||||
uint32_t refcount; |
|
||||||
char data[]; |
|
||||||
} upb_stdio_buf; |
|
||||||
|
|
||||||
// We use a single object for both bytesrc and bytesink for simplicity.
|
|
||||||
// The object is still not thread-safe, and may only be used by one reader
|
|
||||||
// and one writer at a time.
|
|
||||||
typedef struct { |
|
||||||
upb_bytesrc src; |
|
||||||
upb_bytesink sink; |
|
||||||
FILE *file; |
|
||||||
bool should_close; |
|
||||||
upb_stdio_buf **bufs; |
|
||||||
uint32_t nbuf, szbuf; |
|
||||||
} upb_stdio; |
|
||||||
|
|
||||||
void upb_stdio_init(upb_stdio *stdio); |
|
||||||
// Caller should call upb_stdio_flush prior to calling this to ensure that
|
|
||||||
// all data is flushed, otherwise data can be silently dropped if an error
|
|
||||||
// occurs flushing the remaining buffers.
|
|
||||||
void upb_stdio_uninit(upb_stdio *stdio); |
|
||||||
|
|
||||||
// Resets the object to read/write to the given "file." The caller is
|
|
||||||
// responsible for closing the file, which must outlive this object.
|
|
||||||
void upb_stdio_reset(upb_stdio *stdio, FILE *file); |
|
||||||
|
|
||||||
// As an alternative to upb_stdio_reset(), initializes the object by opening a
|
|
||||||
// file, and will handle closing it. This may result in more efficient I/O
|
|
||||||
// than the previous since we can call setvbuf() to disable buffering.
|
|
||||||
void upb_stdio_open(upb_stdio *stdio, const char *filename, const char *mode, |
|
||||||
upb_status *s); |
|
||||||
|
|
||||||
// Must be called to cleanup after the object, including closing the file if
|
|
||||||
// it was opened with upb_stdio_open() (which can fail, hence the status).
|
|
||||||
//
|
|
||||||
|
|
||||||
upb_bytesrc *upb_stdio_bytesrc(upb_stdio *stdio); |
|
||||||
upb_bytesink *upb_stdio_bytesink(upb_stdio *stdio); |
|
||||||
|
|
||||||
#ifdef __cplusplus |
|
||||||
} /* extern "C" */ |
|
||||||
#endif |
|
||||||
|
|
||||||
#endif |
|
@ -1,106 +0,0 @@ |
|||||||
/*
|
|
||||||
* upb - a minimalist implementation of protocol buffers. |
|
||||||
* |
|
||||||
* Copyright (c) 2010 Google Inc. See LICENSE for details. |
|
||||||
* Author: Josh Haberman <jhaberman@gmail.com> |
|
||||||
*/ |
|
||||||
|
|
||||||
#include "upb_strstream.h" |
|
||||||
|
|
||||||
#include <stdlib.h> |
|
||||||
|
|
||||||
|
|
||||||
/* upb_stringsrc **************************************************************/ |
|
||||||
|
|
||||||
size_t upb_stringsrc_fetch(void *_src, uint64_t ofs, upb_status *s) { |
|
||||||
upb_stringsrc *src = _src; |
|
||||||
size_t bytes = src->len - ofs; |
|
||||||
if (bytes == 0) s->code = UPB_EOF; |
|
||||||
return bytes; |
|
||||||
} |
|
||||||
|
|
||||||
void upb_stringsrc_read(void *_src, uint64_t src_ofs, size_t len, char *dst) { |
|
||||||
upb_stringsrc *src = _src; |
|
||||||
memcpy(dst, src->str + src_ofs, len); |
|
||||||
} |
|
||||||
|
|
||||||
const char *upb_stringsrc_getptr(void *_src, uint64_t ofs, size_t *len) { |
|
||||||
upb_stringsrc *src = _src; |
|
||||||
*len = src->len - ofs; |
|
||||||
return src->str + ofs; |
|
||||||
} |
|
||||||
|
|
||||||
void upb_stringsrc_init(upb_stringsrc *s) { |
|
||||||
static upb_bytesrc_vtbl vtbl = { |
|
||||||
&upb_stringsrc_fetch, |
|
||||||
&upb_stringsrc_read, |
|
||||||
&upb_stringsrc_getptr, |
|
||||||
NULL, NULL, NULL, NULL |
|
||||||
}; |
|
||||||
upb_bytesrc_init(&s->bytesrc, &vtbl); |
|
||||||
s->str = NULL; |
|
||||||
} |
|
||||||
|
|
||||||
void upb_stringsrc_reset(upb_stringsrc *s, const char *str, size_t len) { |
|
||||||
s->str = str; |
|
||||||
s->len = len; |
|
||||||
} |
|
||||||
|
|
||||||
void upb_stringsrc_uninit(upb_stringsrc *s) { (void)s; } |
|
||||||
|
|
||||||
upb_bytesrc *upb_stringsrc_bytesrc(upb_stringsrc *s) { |
|
||||||
return &s->bytesrc; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
/* upb_stringsink *************************************************************/ |
|
||||||
|
|
||||||
void upb_stringsink_uninit(upb_stringsink *s) { |
|
||||||
free(s->str); |
|
||||||
} |
|
||||||
|
|
||||||
// Resets the stringsink to a state where it will append to the given string.
|
|
||||||
// The string must be newly created or recycled. The stringsink will take a
|
|
||||||
// reference on the string, so the caller need not ensure that it outlives the
|
|
||||||
// stringsink. A stringsink can be reset multiple times.
|
|
||||||
void upb_stringsink_reset(upb_stringsink *s, char *str, size_t size) { |
|
||||||
free(s->str); |
|
||||||
s->str = str; |
|
||||||
s->len = 0; |
|
||||||
s->size = size; |
|
||||||
} |
|
||||||
|
|
||||||
upb_bytesink *upb_stringsink_bytesink(upb_stringsink *s) { |
|
||||||
return &s->bytesink; |
|
||||||
} |
|
||||||
|
|
||||||
static int32_t upb_stringsink_vprintf(void *_s, upb_status *status, |
|
||||||
const char *fmt, va_list args) { |
|
||||||
(void)status; // TODO: report realloc() errors.
|
|
||||||
upb_stringsink *s = _s; |
|
||||||
int ret = upb_vrprintf(&s->str, &s->size, s->len, fmt, args); |
|
||||||
if (ret >= 0) s->len += ret; |
|
||||||
return ret; |
|
||||||
} |
|
||||||
|
|
||||||
bool upb_stringsink_write(void *_s, const char *buf, size_t len, |
|
||||||
upb_status *status) { |
|
||||||
(void)status; // TODO: report realloc() errors.
|
|
||||||
upb_stringsink *s = _s; |
|
||||||
if (s->len + len > s->size) { |
|
||||||
while(s->len + len > s->size) s->size *= 2; |
|
||||||
s->str = realloc(s->str, s->size); |
|
||||||
} |
|
||||||
memcpy(s->str + s->len, buf, len); |
|
||||||
s->len += len; |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
void upb_stringsink_init(upb_stringsink *s) { |
|
||||||
static upb_bytesink_vtbl vtbl = { |
|
||||||
upb_stringsink_write, |
|
||||||
upb_stringsink_vprintf |
|
||||||
}; |
|
||||||
upb_bytesink_init(&s->bytesink, &vtbl); |
|
||||||
s->str = NULL; |
|
||||||
} |
|
@ -1,72 +0,0 @@ |
|||||||
/*
|
|
||||||
* upb - a minimalist implementation of protocol buffers. |
|
||||||
* |
|
||||||
* Copyright (c) 2009-2010 Google Inc. See LICENSE for details. |
|
||||||
* Author: Josh Haberman <jhaberman@gmail.com> |
|
||||||
* |
|
||||||
* This file contains upb_bytesrc and upb_bytesink implementations for |
|
||||||
* upb_string. |
|
||||||
*/ |
|
||||||
|
|
||||||
#ifndef UPB_STRSTREAM_H |
|
||||||
#define UPB_STRSTREAM_H |
|
||||||
|
|
||||||
#include "upb_bytestream.h" |
|
||||||
|
|
||||||
#ifdef __cplusplus |
|
||||||
extern "C" { |
|
||||||
#endif |
|
||||||
|
|
||||||
/* upb_stringsrc **************************************************************/ |
|
||||||
|
|
||||||
struct _upb_stringsrc { |
|
||||||
upb_bytesrc bytesrc; |
|
||||||
const char *str; |
|
||||||
size_t len; |
|
||||||
}; |
|
||||||
typedef struct _upb_stringsrc upb_stringsrc; |
|
||||||
|
|
||||||
// Create/free a stringsrc.
|
|
||||||
void upb_stringsrc_init(upb_stringsrc *s); |
|
||||||
void upb_stringsrc_uninit(upb_stringsrc *s); |
|
||||||
|
|
||||||
// Resets the stringsrc to a state where it will vend the given string. The
|
|
||||||
// stringsrc will take a reference on the string, so the caller need not ensure
|
|
||||||
// that it outlives the stringsrc. A stringsrc can be reset multiple times.
|
|
||||||
void upb_stringsrc_reset(upb_stringsrc *s, const char *str, size_t len); |
|
||||||
|
|
||||||
// Returns the upb_bytesrc* for this stringsrc.
|
|
||||||
upb_bytesrc *upb_stringsrc_bytesrc(upb_stringsrc *s); |
|
||||||
|
|
||||||
|
|
||||||
/* upb_stringsink *************************************************************/ |
|
||||||
|
|
||||||
struct _upb_stringsink { |
|
||||||
upb_bytesink bytesink; |
|
||||||
char *str; |
|
||||||
size_t len, size; |
|
||||||
}; |
|
||||||
typedef struct _upb_stringsink upb_stringsink; |
|
||||||
|
|
||||||
// Create/free a stringsrc.
|
|
||||||
void upb_stringsink_init(upb_stringsink *s); |
|
||||||
void upb_stringsink_uninit(upb_stringsink *s); |
|
||||||
|
|
||||||
// Resets the sink's string to "str", which the sink takes ownership of.
|
|
||||||
// "str" may be NULL, which will make the sink allocate a new string.
|
|
||||||
void upb_stringsink_reset(upb_stringsink *s, char *str, size_t size); |
|
||||||
|
|
||||||
// Releases ownership of the returned string (which is "len" bytes long) and
|
|
||||||
// resets the internal string to be empty again (as if reset were called with
|
|
||||||
// NULL).
|
|
||||||
const char *upb_stringsink_release(upb_stringsink *s, size_t *len); |
|
||||||
|
|
||||||
// Returns the upb_bytesink* for this stringsrc. Invalidated by reset above.
|
|
||||||
upb_bytesink *upb_stringsink_bytesink(); |
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus |
|
||||||
} /* extern "C" */ |
|
||||||
#endif |
|
||||||
|
|
||||||
#endif |
|
Loading…
Reference in new issue