1BSON_RESERVE_BUFFER(3)              libbson             BSON_RESERVE_BUFFER(3)
2
3
4

SYNOPSIS

6          uint8_t *
7          bson_reserve_buffer (bson_t *bson, uint32_t size);
8

PARAMETERS

10bson: An initialized bson_t.
11
12size: The length in bytes of the new buffer.
13

DESCRIPTION

15       Grow the internal buffer of bson to size and set the document length to
16       size. Useful for eliminating copies when  reading  BSON  bytes  from  a
17       stream.
18
19       First,  initialize  bson with bson_init() or bson_new(), then call this
20       function. After it returns, the length of bson is set to size  but  its
21       contents  are  uninitialized  memory: you must fill the contents with a
22       BSON document of the correct length before any other operations.
23
24       The document must be freed with bson_destroy().
25

RETURNS

27       A pointer to the internal buffer, which is at least size bytes, or NULL
28       if the space could not be allocated.
29

EXAMPLE

31       Use bson_reserve_buffer to write a function that takes a bson_t pointer
32       and reads a file into it directly:
33
34          #include <stdio.h>
35          #include <bson/bson.h>
36
37          bool
38          read_into (bson_t *bson, FILE *fp)
39          {
40             uint8_t *buffer;
41             long size;
42
43             if (fseek (fp, 0L, SEEK_END) < 0) {
44                perror ("Couldn't get file size");
45                return 1;
46             }
47
48             size = ftell (fp);
49             if (size == EOF) {
50                perror ("Couldn't get file size");
51                return 1;
52             }
53
54             if (size > INT32_MAX) {
55                fprintf (stderr, "File too large\n");
56                return 1;
57             }
58
59             /* reserve buffer space - bson is temporarily invalid */
60             buffer = bson_reserve_buffer (bson, (uint32_t) size);
61             if (!buffer) {
62                fprintf (stderr, "Couldn't reserve %ld bytes", size);
63                return false;
64             }
65
66             /* read file directly into the buffer */
67             rewind (fp);
68             if (fread ((void *) buffer, 1, (size_t) size, fp) < (size_t) size) {
69                perror ("Couldn't read file");
70                return false;
71             }
72
73             return true;
74          }
75
76          int
77          main ()
78          {
79             FILE *fp;
80             char *json;
81
82             /* stack-allocated, initialized bson_t */
83             bson_t bson = BSON_INITIALIZER;
84
85             if (!(fp = fopen ("document.bson", "rb"))) {
86                perror ("Couldn't read file");
87                return 1;
88             }
89
90             read_into (&bson, fp);
91             fclose (fp);
92
93             json = bson_as_canonical_extended_json (&bson, NULL);
94             printf ("%s\n", json);
95
96             bson_free (json);
97             bson_destroy (&bson);
98
99             return 0;
100          }
101

AUTHOR

103       MongoDB, Inc
104
106       2017-present, MongoDB, Inc
107
108
109
110
1111.25.1                           Nov 08, 2023           BSON_RESERVE_BUFFER(3)
Impressum