1 /*
2 * Copyright 2014 MongoDB, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 #include <bcon.h>
19 #include <bson.h>
20 #include <mongoc.h>
21 #include <stdio.h>
22
23
24 /*
25 * This is an example that reads BSON documents from STDIN and prints them
26 * to standard output as JSON if they match {'hello': 'world'}.
27 */
28
29
30 int
main(int argc,char * argv[])31 main (int argc, char *argv[])
32 {
33 mongoc_matcher_t *matcher;
34 bson_reader_t *reader;
35 const bson_t *bson;
36 bson_t *spec;
37 char *str;
38 int fd;
39
40 mongoc_init ();
41
42 #ifdef _WIN32
43 fd = fileno (stdin);
44 #else
45 fd = STDIN_FILENO;
46 #endif
47
48 reader = bson_reader_new_from_fd (fd, false);
49
50 spec = BCON_NEW ("hello", "world");
51 matcher = mongoc_matcher_new (spec, NULL);
52
53 while ((bson = bson_reader_read (reader, NULL))) {
54 if (mongoc_matcher_match (matcher, bson)) {
55 str = bson_as_canonical_extended_json (bson, NULL);
56 printf ("%s\n", str);
57 bson_free (str);
58 }
59 }
60
61 bson_reader_destroy (reader);
62 bson_destroy (spec);
63
64 return 0;
65 }
66