1BSON_READER_T(3) libbson BSON_READER_T(3)
2
3
4
5Streaming BSON Document Reader
6
8 #include <bson/bson.h>
9
10 typedef struct _bson_reader_t bson_reader_t;
11
12 bson_reader_t *
13 bson_reader_new_from_handle (void *handle,
14 bson_reader_read_func_t rf,
15 bson_reader_destroy_func_t df);
16 bson_reader_t *
17 bson_reader_new_from_fd (int fd, bool close_on_destroy);
18 bson_reader_t *
19 bson_reader_new_from_file (const char *path, bson_error_t *error);
20 bson_reader_t *
21 bson_reader_new_from_data (const uint8_t *data, size_t length);
22
23 void
24 bson_reader_destroy (bson_reader_t *reader);
25
27 bson_reader_t is a structure used for reading a sequence of BSON docu‐
28 ments. The sequence can come from a file-descriptor, memory region, or
29 custom callbacks.
30
32 /*
33 * Copyright 2013 MongoDB, Inc.
34 *
35 * Licensed under the Apache License, Version 2.0 (the "License");
36 * you may not use this file except in compliance with the License.
37 * You may obtain a copy of the License at
38 *
39 * http://www.apache.org/licenses/LICENSE-2.0
40 *
41 * Unless required by applicable law or agreed to in writing, software
42 * distributed under the License is distributed on an "AS IS" BASIS,
43 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44 * See the License for the specific language governing permissions and
45 * limitations under the License.
46 */
47
48
49 /*
50 * This program will print each BSON document contained in the provided files
51 * as a JSON string to STDOUT.
52 */
53
54
55 #include <bson/bson.h>
56 #include <stdio.h>
57
58
59 int
60 main (int argc, char *argv[])
61 {
62 bson_reader_t *reader;
63 const bson_t *b;
64 bson_error_t error;
65 const char *filename;
66 char *str;
67 int i;
68
69 /*
70 * Print program usage if no arguments are provided.
71 */
72 if (argc == 1) {
73 fprintf (stderr, "usage: %s [FILE | -]...\nUse - for STDIN.\n", argv[0]);
74 return 1;
75 }
76
77 /*
78 * Process command line arguments expecting each to be a filename.
79 */
80 for (i = 1; i < argc; i++) {
81 filename = argv[i];
82
83 if (strcmp (filename, "-") == 0) {
84 reader = bson_reader_new_from_fd (STDIN_FILENO, false);
85 } else {
86 if (!(reader = bson_reader_new_from_file (filename, &error))) {
87 fprintf (
88 stderr, "Failed to open \"%s\": %s\n", filename, error.message);
89 continue;
90 }
91 }
92
93 /*
94 * Convert each incoming document to JSON and print to stdout.
95 */
96 while ((b = bson_reader_read (reader, NULL))) {
97 str = bson_as_canonical_extended_json (b, NULL);
98 fprintf (stdout, "%s\n", str);
99 bson_free (str);
100 }
101
102 /*
103 * Cleanup after our reader, which closes the file descriptor.
104 */
105 bson_reader_destroy (reader);
106 }
107
108 return 0;
109 }
110
112 MongoDB, Inc
113
115 2017-present, MongoDB, Inc
116
117
118
119
1201.25.1 Nov 08, 2023 BSON_READER_T(3)