예제 #1
0
bson_reader_t *
bson_reader_new_from_file (const char   *path,  /* IN */
                           bson_error_t *error) /* OUT */
{
   char errmsg[32];
   int fd;

   bson_return_val_if_fail (path, NULL);

#ifdef BSON_OS_WIN32
   fd = _open (path, (_O_RDONLY | _O_BINARY));
#else
   fd = open (path, O_RDONLY);
#endif

   if (fd == -1) {
      bson_strerror_r (errno, errmsg, sizeof errmsg);
      bson_set_error (error,
                      BSON_ERROR_READER,
                      BSON_ERROR_READER_BADFD,
                      "%s", errmsg);
      return NULL;
   }

   return bson_reader_new_from_fd (fd, true);
}
예제 #2
0
static void
test_reader_tell (void)
{
   bson_reader_t *reader;
   const bson_t *b;
   bson_uint32_t i;
   bson_iter_t iter;
   bson_bool_t eof;
   int fd;

   fd  = open("tests/binary/stream.bson", O_RDONLY);
   assert(fd >= 0);

   reader = bson_reader_new_from_fd(fd, TRUE);

   for (i = 0; i < 1000; i++) {
      if (i) {
         assert_cmpint(5 * i, ==, bson_reader_tell(reader));
      } else {
         assert_cmpint(0, ==, bson_reader_tell(reader));
      }
      eof = FALSE;
      b = bson_reader_read(reader, &eof);
      assert(b);
      assert(bson_iter_init(&iter, b));
      assert(!bson_iter_next(&iter));
   }
예제 #3
0
static void
test_reader_from_fd (void)
{
   bson_reader_t *reader;
   const bson_t *b;
   bson_uint32_t i;
   bson_iter_t iter;
   bson_bool_t eof;
   int fd;

   fd  = bson_open("tests/binary/stream.bson", BSON_O_RDONLY);
   assert(fd >= 0);

   reader = bson_reader_new_from_fd(fd, TRUE);

   for (i = 0; i < 1000; i++) {
      eof = FALSE;
      b = bson_reader_read(reader, &eof);
      assert(b);
      assert(bson_iter_init(&iter, b));
      assert(!bson_iter_next(&iter));
   }

   assert_cmpint(eof, ==, FALSE);
   b = bson_reader_read(reader, &eof);
   assert(!b);
   assert_cmpint(eof, ==, TRUE);
   bson_reader_destroy(reader);
}
예제 #4
0
bson_reader_t *
bson_reader_new_from_file (const char   *path,  /* IN */
                           bson_error_t *error) /* OUT */
{
   char errmsg_buf[BSON_ERROR_BUFFER_SIZE];
   char *errmsg;
   int fd;

   bson_return_val_if_fail (path, NULL);

#ifdef BSON_OS_WIN32
   if (_sopen_s (&fd, path, (_O_RDONLY | _O_BINARY), _SH_DENYNO, 0) != 0) {
      fd = -1;
   }
#else
   fd = open (path, O_RDONLY);
#endif

   if (fd == -1) {
      errmsg = bson_strerror_r (errno, errmsg_buf, sizeof errmsg_buf);
      bson_set_error (error,
                      BSON_ERROR_READER,
                      BSON_ERROR_READER_BADFD,
                      "%s", errmsg);
      return NULL;
   }

   return bson_reader_new_from_fd (fd, true);
}
예제 #5
0
int
main (int   argc,
      char *argv[])
{
   bson_reader_t *reader;
   const bson_t *b;
   const char *filename;
   char *str;
   int fd;
   int i;

   /*
    * Print program usage if no arguments are provided.
    */
   if (argc == 1) {
      fprintf(stderr, "usage: %s FILE...\n", argv[0]);
      return 1;
   }

   /*
    * Process command line arguments expecting each to be a filename.
    */
   for (i = 1; i < argc; i++) {
      filename = argv[i];

      /*
       * Open the filename provided in command line arguments.
       */
      if (0 == strcmp(filename, "-")) {
         fd = STDIN_FILENO;
      } else {
         errno = 0;
         fd = open(filename, O_RDONLY);
         if (fd == -1) {
            fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
            continue;
         }
      }

      /*
       * Initialize a new reader for this file descriptor.
       */
      reader = bson_reader_new_from_fd(fd, TRUE);

      /*
       * Convert each incoming document to JSON and print to stdout.
       */
      while ((b = bson_reader_read(reader, NULL))) {
         str = bson_as_json(b, NULL);
         fprintf(stdout, "%s\n", str);
         bson_free(str);
      }

      /*
       * Cleanup after our reader, which closes the file descriptor.
       */
      bson_reader_destroy(reader);
   }

   return 0;
}
예제 #6
0
/*
 * main --
 *
 *       Connects to a server and reads BSON from it. This program takes the
 *       following command line options:
 *
 *          -h              Print this help and exit.
 *          -s SERVER_NAME  Specify the host name of the server.
 *          -p PORT_NUM     Specify the port number to connect to on the server.
 */
int
main (int   argc,
      char *argv[])
{
   bson_reader_t  *reader;
   char           *hostname = NULL;
   char           *json;
   char           *port = NULL;
   const bson_t   *document;
   int             fd;
   int             opt;

   opterr = 1;

   /*
    * Parse command line arguments.
    */
   while ((opt = getopt (argc, argv, "hs:p:")) != -1) {
      switch (opt) {
      case 'h':
         fprintf (stdout,
                  "Usage: %s [-s SERVER_NAME] [-p PORT_NUM]\n",
                  argv[0]);
         return EXIT_SUCCESS;
      case 'p':
         free (port);
         port = (char *)malloc (strlen (optarg));
         strcpy (port, optarg);
         break;
      case 's':
         free (hostname);
         hostname = (char *)malloc (strlen (optarg));
         strcpy (hostname, optarg);
         break;
      default:
         fprintf (stderr, "Unknown option: %s\n", optarg);
      }
   }

   /*
    * Open a file descriptor on the remote and read in BSON documents, one by
    * one. As an example of processing, this prints the incoming documents as
    * JSON onto STDOUT.
    */
   fd = bson_streaming_remote_open (hostname, port);
   if (fd == -1) {
      return EXIT_FAILURE;
   }

   reader = bson_reader_new_from_fd (fd, true);
   while ((document = bson_reader_read (reader, NULL))) {
      json = bson_as_json (document, NULL);
      fprintf (stdout, "%s\n", json);
      bson_free (json);
   }

   /*
    * Destroy the reader, which performs cleanup. The ``true'' argument passed
    * to bson_reader_new_from_fd tells libbson to close the file descriptor on
    * destruction.
    */
   bson_reader_destroy (reader);
   free (hostname);
   free (port);
   return EXIT_SUCCESS;
}
예제 #7
0
int
main (int   argc,
      char *argv[])
{
   bson_reader_t *reader;
   const bson_t *b;
   const char *filename;
   size_t offset;
   int docnum;
   int fd;
   int i;

   /*
    * Print program usage if no arguments are provided.
    */
   if (argc == 1) {
      fprintf(stderr, "usage: %s FILE...\n", argv[0]);
      return 1;
   }

   /*
    * Process command line arguments expecting each to be a filename.
    */
   for (i = 1; i < argc; i++) {
      filename = argv[i];
      docnum = 0;

      /*
       * Open the filename provided in command line arguments.
       */
      errno = 0;
      fd = open(filename, O_RDONLY);
      if (fd == -1) {
         fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
         continue;
      }

      /*
       * Initialize a new reader for this file descriptor.
       */
      reader = bson_reader_new_from_fd(fd, TRUE);

      /*
       * Convert each incoming document to JSON and print to stdout.
       */
      while ((b = bson_reader_read(reader, NULL))) {
         docnum++;
         if (!bson_validate(b,
                            (BSON_VALIDATE_UTF8 |
                             BSON_VALIDATE_DOLLAR_KEYS |
                             BSON_VALIDATE_DOT_KEYS |
                             BSON_VALIDATE_UTF8_ALLOW_NULL),
                            &offset)) {
            fprintf(stderr,
                    "Document %u in \"%s\" is invalid at offset %u.\n",
                    docnum, filename, (int)offset);
            return 1;
         }
      }

      /*
       * Cleanup after our reader, which closes the file descriptor.
       */
      bson_reader_destroy(reader);
   }

   return 0;
}