1BSON_READER_T(3)                    Libbson                   BSON_READER_T(3)
2
3
4

NAME

6       bson_reader_t - bson_reader_t
7
8       Streaming BSON Document Reader
9

SYNOPSIS

11          #include <bson/bson.h>
12
13          typedef struct _bson_reader_t bson_reader_t;
14
15          bson_reader_t *
16          bson_reader_new_from_handle (void *handle,
17                                       bson_reader_read_func_t rf,
18                                       bson_reader_destroy_func_t df);
19          bson_reader_t *
20          bson_reader_new_from_fd (int fd, bool close_on_destroy);
21          bson_reader_t *
22          bson_reader_new_from_file (const char *path, bson_error_t *error);
23          bson_reader_t *
24          bson_reader_new_from_data (const uint8_t *data, size_t length);
25
26          void
27          bson_reader_destroy (bson_reader_t *reader);
28

DESCRIPTION

30       bson_reader_t  is a structure used for reading a sequence of BSON docu‐
31       ments. The sequence can come from a file-descriptor, memory region,  or
32       custom callbacks.
33

EXAMPLE

35          /*
36           * Copyright 2013 MongoDB, Inc.
37           *
38           * Licensed under the Apache License, Version 2.0 (the "License");
39           * you may not use this file except in compliance with the License.
40           * You may obtain a copy of the License at
41           *
42           *   http://www.apache.org/licenses/LICENSE-2.0
43           *
44           * Unless required by applicable law or agreed to in writing, software
45           * distributed under the License is distributed on an "AS IS" BASIS,
46           * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
47           * See the License for the specific language governing permissions and
48           * limitations under the License.
49           */
50
51
52          /*
53           * This program will print each BSON document contained in the provided files
54           * as a JSON string to STDOUT.
55           */
56
57
58          #include <bson/bson.h>
59          #include <stdio.h>
60
61
62          int
63          main (int argc, char *argv[])
64          {
65             bson_reader_t *reader;
66             const bson_t *b;
67             bson_error_t error;
68             const char *filename;
69             char *str;
70             int i;
71
72             /*
73              * Print program usage if no arguments are provided.
74              */
75             if (argc == 1) {
76                fprintf (stderr, "usage: %s [FILE | -]...\nUse - for STDIN.\n", argv[0]);
77                return 1;
78             }
79
80             /*
81              * Process command line arguments expecting each to be a filename.
82              */
83             for (i = 1; i < argc; i++) {
84                filename = argv[i];
85
86                if (strcmp (filename, "-") == 0) {
87                   reader = bson_reader_new_from_fd (STDIN_FILENO, false);
88                } else {
89                   if (!(reader = bson_reader_new_from_file (filename, &error))) {
90                      fprintf (
91                         stderr, "Failed to open \"%s\": %s\n", filename, error.message);
92                      continue;
93                   }
94                }
95
96                /*
97                 * Convert each incoming document to JSON and print to stdout.
98                 */
99                while ((b = bson_reader_read (reader, NULL))) {
100                   str = bson_as_canonical_extended_json (b, NULL);
101                   fprintf (stdout, "%s\n", str);
102                   bson_free (str);
103                }
104
105                /*
106                 * Cleanup after our reader, which closes the file descriptor.
107                 */
108                bson_reader_destroy (reader);
109             }
110
111             return 0;
112          }
113

AUTHOR

115       MongoDB, Inc
116
118       2017-present, MongoDB, Inc
119
120
121
122
1231.13.1                           Jan 24, 2019                 BSON_READER_T(3)
Impressum