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.
68 lines
2.4 KiB
68 lines
2.4 KiB
15 years ago
|
/*
|
||
|
* upb - a minimalist implementation of protocol buffers.
|
||
|
*
|
||
|
* Copyright (c) 2009 Joshua Haberman. See LICENSE for details.
|
||
|
*
|
||
|
*/
|
||
|
|
||
15 years ago
|
#include <stdarg.h>
|
||
15 years ago
|
#include <stddef.h>
|
||
15 years ago
|
#include <string.h>
|
||
15 years ago
|
|
||
|
#include "upb.h"
|
||
15 years ago
|
#include "upb_string.h"
|
||
15 years ago
|
|
||
15 years ago
|
#define alignof(t) offsetof(struct { char c; t x; }, x)
|
||
|
#define TYPE_INFO(wire_type, ctype, allows_delimited) \
|
||
|
{alignof(ctype), sizeof(ctype), wire_type, \
|
||
|
(1 << wire_type) | (allows_delimited << UPB_WIRE_TYPE_DELIMITED), \
|
||
|
#ctype},
|
||
|
|
||
|
upb_type_info upb_types[] = {
|
||
15 years ago
|
{0, 0, 0, 0, ""}, // There is no type 0.
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_64BIT, double, 1) // DOUBLE
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_32BIT, float, 1) // FLOAT
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_VARINT, int64_t, 1) // INT64
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_VARINT, uint64_t, 1) // UINT64
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_VARINT, int32_t, 1) // INT32
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_64BIT, uint64_t, 1) // FIXED64
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_32BIT, uint32_t, 1) // FIXED32
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_VARINT, bool, 1) // BOOL
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_DELIMITED, void*, 1) // STRING
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_START_GROUP, void*, 0) // GROUP
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_DELIMITED, void*, 1) // MESSAGE
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_DELIMITED, void*, 1) // BYTES
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_VARINT, uint32_t, 1) // UINT32
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_VARINT, uint32_t, 1) // ENUM
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_32BIT, int32_t, 1) // SFIXED32
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_64BIT, int64_t, 1) // SFIXED64
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_VARINT, int32_t, 1) // SINT32
|
||
|
TYPE_INFO(UPB_WIRE_TYPE_VARINT, int64_t, 1) // SINT64
|
||
15 years ago
|
};
|
||
|
|
||
15 years ago
|
void upb_seterr(upb_status *status, enum upb_status_code code,
|
||
15 years ago
|
const char *msg, ...)
|
||
|
{
|
||
|
if(upb_ok(status)) { // The first error is the most interesting.
|
||
15 years ago
|
status->str = upb_string_new();
|
||
|
char *str = upb_string_getrwbuf(status->str, UPB_ERRORMSG_MAXLEN);
|
||
15 years ago
|
status->code = code;
|
||
|
va_list args;
|
||
|
va_start(args, msg);
|
||
15 years ago
|
vsnprintf(str, UPB_ERRORMSG_MAXLEN, msg, args);
|
||
15 years ago
|
va_end(args);
|
||
|
}
|
||
|
}
|
||
15 years ago
|
|
||
|
void upb_copyerr(upb_status *to, upb_status *from)
|
||
|
{
|
||
|
to->code = from->code;
|
||
15 years ago
|
to->str = upb_string_getref(from->str);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void upb_reset(upb_status *status) {
|
||
|
status->code = UPB_STATUS_OK;
|
||
|
upb_string_unref(status->str);
|
||
|
status->str = NULL;
|
||
|
}
|