summaryrefslogtreecommitdiff
path: root/upbc.c
diff options
context:
space:
mode:
authorJoshua Haberman <joshua@reverberate.org>2009-07-05 17:54:49 -0700
committerJoshua Haberman <joshua@reverberate.org>2009-07-05 17:54:49 -0700
commit7f871401c77bcda18e8f41e457ee55388773d183 (patch)
tree2996509ed66b4056789a09fb7b81c418e7a3af57 /upbc.c
parent421f276086dfc4d1df7411820f9d56f3ae78472e (diff)
More work on upbc.
Diffstat (limited to 'upbc.c')
-rw-r--r--upbc.c68
1 files changed, 42 insertions, 26 deletions
diff --git a/upbc.c b/upbc.c
index b49d24b..2cda5e4 100644
--- a/upbc.c
+++ b/upbc.c
@@ -10,6 +10,7 @@
#include <inttypes.h>
#include "descriptor.h"
#include "upb_context.h"
+#include "upb_enum.h"
/* These are in-place string transformations that do not change the length of
* the string (and thus never need to re-allocate). */
@@ -31,12 +32,11 @@ static void to_preproc(struct upb_string str)
* also defines constants for the enum values.
*
* Assumes that d has been validated. */
-static void write_header(google_protobuf_FileDescriptorProto *d, FILE *stream)
+static void write_header(struct upb_symtab_entry entries[], int num_entries,
+ struct upb_string outfile_name, FILE *stream)
{
/* Header file prologue. */
- fprintf(stream, "/* Auto-generated from " UPB_STRFMT ". Do not edit. */\n\n",
- UPB_STRARG(*d->name));
- struct upb_string include_guard_name = upb_strdup(*d->name);
+ struct upb_string include_guard_name = upb_strdup(outfile_name);
to_preproc(include_guard_name);
fprintf(stream, "#ifndef " UPB_STRFMT "\n", UPB_STRARG(include_guard_name));
fprintf(stream, "#define " UPB_STRFMT "\n\n", UPB_STRARG(include_guard_name));
@@ -46,29 +46,31 @@ static void write_header(google_protobuf_FileDescriptorProto *d, FILE *stream)
fputs("#endif\n\n", stream);
/* Enums. */
- if(d->set_flags.has.enum_type) {
- fprintf(stream, "/* Enums. */\n\n");
- for(uint32_t i = 0; i < d->enum_type->len; i++) { /* Foreach enum */
- google_protobuf_EnumDescriptorProto *e = d->enum_type->elements[i];
- struct upb_string enum_name = upb_strdup(*e->name);
- to_cident(enum_name);
- fprintf(stream, "typedef enum " UPB_STRFMT " {\n", UPB_STRARG(enum_name));
- if(e->set_flags.has.value) {
- for(uint32_t j = 0; j < e->value->len; j++) { /* Foreach enum value. */
- google_protobuf_EnumValueDescriptorProto *v = e->value->elements[i];
- struct upb_string value_name = upb_strdup(*v->name);
- to_preproc(value_name);
- /* " GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13," */
- fprintf(stream, " " UPB_STRFMT " = %" PRIu32,
- UPB_STRARG(value_name), v->number);
- if(j != e->value->len-1) fputc(',', stream);
- fputc('\n', stream);
- upb_strfree(value_name);
- }
+ fprintf(stream, "/* Enums. */\n\n");
+ for(int i = 0; i < num_entries; i++) { /* Foreach enum */
+ if(entries[i].type != UPB_SYM_ENUM) continue;
+ struct upb_symtab_entry *entry = &entries[i];
+ struct upb_enum *e = entry->ref._enum;
+ google_protobuf_EnumDescriptorProto *ed = e->descriptor;
+ /* We use entry->e.key (the fully qualified name) instead of ed->name. */
+ struct upb_string enum_name = upb_strdup(entry->e.key);
+ to_cident(enum_name);
+ fprintf(stream, "typedef enum " UPB_STRFMT " {\n", UPB_STRARG(enum_name));
+ if(ed->set_flags.has.value) {
+ for(uint32_t j = 0; j < ed->value->len; j++) { /* Foreach enum value. */
+ google_protobuf_EnumValueDescriptorProto *v = ed->value->elements[i];
+ struct upb_string value_name = upb_strdup(*v->name);
+ to_preproc(value_name);
+ /* " GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13," */
+ fprintf(stream, " " UPB_STRFMT " = %" PRIu32,
+ UPB_STRARG(value_name), v->number);
+ if(j != ed->value->len-1) fputc(',', stream);
+ fputc('\n', stream);
+ upb_strfree(value_name);
}
- fprintf(stream, "} " UPB_STRFMT ";\n\n", UPB_STRARG(enum_name));
- upb_strfree(enum_name);
}
+ fprintf(stream, "} " UPB_STRFMT ";\n\n", UPB_STRARG(enum_name));
+ upb_strfree(enum_name);
}
/* Epilogue. */
@@ -81,6 +83,20 @@ static void write_header(google_protobuf_FileDescriptorProto *d, FILE *stream)
int main()
{
- write_header(&google_protobuf_filedescriptor, stdout);
+ struct upb_context c;
+ upb_context_init(&c);
+ struct upb_string fds;
+ assert(upb_strreadfile("/tmp/descriptor.proto.bin", &fds));
+ assert(upb_context_parsefds(&c, &fds));
+ struct upb_strtable *t = &c.symtab;
+ int symcount = t->t.count;
+ struct upb_symtab_entry entries[symcount];
+ struct upb_symtab_entry *e = upb_strtable_begin(t);
+ int i = 0;
+ for(; e && i < symcount; e = upb_strtable_next(t, &e->e), i++)
+ entries[i] = *e;
+ assert(e == NULL && i == symcount);
+ struct upb_string name = UPB_STRLIT("descriptor.proto");
+ write_header(entries, symcount, name, stdout);
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback