summaryrefslogtreecommitdiff
path: root/src/upb_glue.c
blob: b6a027385f3fd60db101f0597e0fc136286810b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * upb - a minimalist implementation of protocol buffers.
 *
 * Copyright (c) 2010 Google Inc.  See LICENSE for details.
 * Author: Josh Haberman <jhaberman@gmail.com>
 */

#include "upb_glue.h"
#include "upb_msg.h"
#include "upb_decoder.h"
#include "upb_strstream.h"
#include "upb_textprinter.h"

void upb_strtomsg(upb_string *str, upb_msg *msg, upb_msgdef *md,
                  upb_status *status) {
  upb_stringsrc strsrc;
  upb_stringsrc_init(&strsrc);
  upb_stringsrc_reset(&strsrc, str);

  upb_handlers h;
  upb_handlers_init(&h, md);
  upb_msg_regdhandlers(&h);

  upb_decoder d;
  upb_decoder_init(&d, &h);
  upb_decoder_reset(&d, upb_stringsrc_bytesrc(&strsrc), msg);

  upb_decoder_decode(&d, status);

  upb_stringsrc_uninit(&strsrc);
  upb_decoder_uninit(&d);
}

void upb_msgtotext(upb_string *str, upb_msg *msg, upb_msgdef *md,
                   bool single_line) {
  upb_stringsink strsink;
  upb_stringsink_init(&strsink);
  upb_stringsink_reset(&strsink, str);

  upb_textprinter *p = upb_textprinter_new();
  upb_handlers h;
  upb_handlers_init(&h, md);
  upb_textprinter_reghandlers(&h);
  upb_textprinter_reset(p, upb_stringsink_bytesink(&strsink), single_line);

  upb_status status = UPB_STATUS_INIT;
  upb_msg_runhandlers(msg, md, &h, p, &status);
  // None of {upb_msg_runhandlers, upb_textprinter, upb_stringsink} should be
  // capable of returning an error.
  assert(upb_ok(&status));
  upb_status_uninit(&status);

  upb_stringsink_uninit(&strsink);
  upb_textprinter_free(p);
}

void upb_parsedesc(upb_symtab *symtab, upb_string *str, upb_status *status) {
  upb_stringsrc strsrc;
  upb_stringsrc_init(&strsrc);
  upb_stringsrc_reset(&strsrc, str);

  upb_handlers h;
  upb_handlers_init(&h, NULL);
  upb_defbuilder_reghandlers(&h);

  upb_decoder d;
  upb_decoder_init(&d, &h);
  upb_defbuilder *b = upb_defbuilder_new(symtab);
  upb_decoder_reset(&d, upb_stringsrc_bytesrc(&strsrc), b);

  upb_decoder_decode(&d, status);

  upb_stringsrc_uninit(&strsrc);
  upb_decoder_uninit(&d);
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback