• Main Page
  • Classes
  • Files
  • File List
  • File Members

bson.h

Go to the documentation of this file.
00001 
00006 /*    Copyright 2009, 2010, 2011 10gen Inc.
00007  *
00008  *    Licensed under the Apache License, Version 2.0 (the "License");
00009  *    you may not use this file except in compliance with the License.
00010  *    You may obtain a copy of the License at
00011  *
00012  *    http://www.apache.org/licenses/LICENSE-2.0
00013  *
00014  *    Unless required by applicable law or agreed to in writing, software
00015  *    distributed under the License is distributed on an "AS IS" BASIS,
00016  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017  *    See the License for the specific language governing permissions and
00018  *    limitations under the License.
00019  */
00020 
00021 #ifndef _BSON_H_
00022 #define _BSON_H_
00023 
00024 #include "platform_hacks.h"
00025 #include <time.h>
00026 
00027 MONGO_EXTERN_C_START
00028 
00029 typedef enum {
00030     bson_eoo=0 ,
00031     bson_double=1,
00032     bson_string=2,
00033     bson_object=3,
00034     bson_array=4,
00035     bson_bindata=5,
00036     bson_undefined=6,
00037     bson_oid=7,
00038     bson_bool=8,
00039     bson_date=9,
00040     bson_null=10,
00041     bson_regex=11,
00042     bson_dbref=12, /* deprecated */
00043     bson_code=13,
00044     bson_symbol=14,
00045     bson_codewscope=15,
00046     bson_int = 16,
00047     bson_timestamp = 17,
00048     bson_long = 18
00049 } bson_type;
00050 
00051 typedef int bson_bool_t;
00052 
00053 typedef struct {
00054     char * data;
00055     bson_bool_t owned;
00056 } bson;
00057 
00058 typedef struct {
00059     const char * cur;
00060     bson_bool_t first;
00061 } bson_iterator;
00062 
00063 typedef struct {
00064     char * buf;
00065     char * cur;
00066     int bufSize;
00067     bson_bool_t finished;
00068     int stack[32];
00069     int stackPos;
00070 } bson_buffer;
00071 
00072 #pragma pack(1)
00073 typedef union{
00074     char bytes[12];
00075     int ints[3];
00076 } bson_oid_t;
00077 #pragma pack()
00078 
00079 typedef int64_t bson_date_t; /* milliseconds since epoch UTC */
00080 
00081 typedef struct {
00082   int i; /* increment */
00083   int t; /* time in seconds */
00084 } bson_timestamp_t;
00085 
00086 /* ----------------------------
00087    READING
00088    ------------------------------ */
00096 bson * bson_empty(bson * obj); /* returns pointer to static empty bson object */
00097 
00104 void bson_copy(bson* out, const bson* in); /* puts data in new buffer. NOOP if out==NULL */
00105 
00114 bson * bson_from_buffer(bson * b, bson_buffer * buf);
00115 
00125 bson * bson_init( bson * b , char * data , bson_bool_t mine );
00126 
00134 int bson_size(const bson * b );
00135 
00141 void bson_destroy( bson * b );
00142 
00148 void bson_print( bson * b );
00149 
00156 void bson_print_raw( const char * bson , int depth );
00157 
00158 /* advances iterator to named field */
00159 /* returns bson_eoo (which is false) if field not found */
00169 bson_type bson_find(bson_iterator* it, const bson* obj, const char* name);
00170 
00177 void bson_iterator_init( bson_iterator * i , const char * bson );
00178 
00179 /* more returns true for eoo. best to loop with bson_iterator_next(&it) */
00187 bson_bool_t bson_iterator_more( const bson_iterator * i );
00188 
00196 bson_type bson_iterator_next( bson_iterator * i );
00197 
00205 bson_type bson_iterator_type( const bson_iterator * i );
00206 
00214 const char * bson_iterator_key( const bson_iterator * i );
00215 
00223 const char * bson_iterator_value( const bson_iterator * i );
00224 
00225 /* these convert to the right type (return 0 if non-numeric) */
00234 double bson_iterator_double( const bson_iterator * i );
00235 
00243 int bson_iterator_int( const bson_iterator * i );
00244 
00252 int64_t bson_iterator_long( const bson_iterator * i );
00253 
00254 /* return the bson timestamp as a whole or in parts */
00263 bson_timestamp_t bson_iterator_timestamp( const bson_iterator * i );
00264 
00273 /* false: boolean false, 0 in any type, or null */
00274 /* true: anything else (even empty strings and objects) */
00275 bson_bool_t bson_iterator_bool( const bson_iterator * i );
00276 
00285 /* these assume you are using the right type */
00286 double bson_iterator_double_raw( const bson_iterator * i );
00287 
00296 int bson_iterator_int_raw( const bson_iterator * i );
00297 
00306 int64_t bson_iterator_long_raw( const bson_iterator * i );
00307 
00316 bson_bool_t bson_iterator_bool_raw( const bson_iterator * i );
00317 
00326 bson_oid_t* bson_iterator_oid( const bson_iterator * i );
00327 
00336 /* these can also be used with bson_code and bson_symbol*/
00337 const char * bson_iterator_string( const bson_iterator * i );
00338 
00347 int bson_iterator_string_len( const bson_iterator * i );
00348 
00358 /* works with bson_code, bson_codewscope, and bson_string */
00359 /* returns NULL for everything else */
00360 const char * bson_iterator_code(const bson_iterator * i);
00361 
00368 /* calls bson_empty on scope if not a bson_codewscope */
00369 void bson_iterator_code_scope(const bson_iterator * i, bson * scope);
00370 
00379 /* both of these only work with bson_date */
00380 bson_date_t bson_iterator_date(const bson_iterator * i);
00381 
00390 time_t bson_iterator_time_t(const bson_iterator * i);
00391 
00400 int bson_iterator_bin_len( const bson_iterator * i );
00401 
00410 char bson_iterator_bin_type( const bson_iterator * i );
00411 
00420 const char * bson_iterator_bin_data( const bson_iterator * i );
00421 
00430 const char * bson_iterator_regex( const bson_iterator * i );
00431 
00440 const char * bson_iterator_regex_opts( const bson_iterator * i );
00441 
00442 /* these work with bson_object and bson_array */
00450 void bson_iterator_subobject(const bson_iterator * i, bson * sub);
00451 
00458 void bson_iterator_subiterator(const bson_iterator * i, bson_iterator * sub);
00459 
00460 /* str must be at least 24 hex chars + null byte */
00467 void bson_oid_from_string(bson_oid_t* oid, const char* str);
00468 
00475 void bson_oid_to_string(const bson_oid_t* oid, char* str);
00476 
00482 void bson_oid_gen(bson_oid_t* oid);
00483 
00489 time_t bson_oid_generated_time(bson_oid_t* oid); /* Gives the time the OID was created */
00490 
00491 /* ----------------------------
00492    BUILDING
00493    ------------------------------ */
00501 bson_buffer * bson_buffer_init( bson_buffer * b );
00502 
00511 bson_buffer * bson_ensure_space( bson_buffer * b , const int bytesNeeded );
00512 
00521 char * bson_buffer_finish( bson_buffer * b );
00522 
00529 void bson_buffer_destroy( bson_buffer * b );
00530 
00540 bson_buffer * bson_append_oid( bson_buffer * b , const char * name , const bson_oid_t* oid );
00541 
00550 bson_buffer * bson_append_new_oid( bson_buffer * b , const char * name );
00551 
00561 bson_buffer * bson_append_int( bson_buffer * b , const char * name , const int i );
00562 
00572 bson_buffer * bson_append_long( bson_buffer * b , const char * name , const int64_t i );
00573 
00583 bson_buffer * bson_append_double( bson_buffer * b , const char * name , const double d );
00584 
00594 bson_buffer * bson_append_string( bson_buffer * b , const char * name , const char * str );
00595 
00606 bson_buffer * bson_append_string_n( bson_buffer * b, const char * name, const char * str , int len);
00607 
00617 bson_buffer * bson_append_symbol( bson_buffer * b , const char * name , const char * str );
00628 bson_buffer * bson_append_symbol_n( bson_buffer * b , const char * name , const char * str , int len );
00629 
00640 bson_buffer * bson_append_code( bson_buffer * b , const char * name , const char * str );
00641 
00652 bson_buffer * bson_append_code_n( bson_buffer * b , const char * name , const char * str , int len );
00653 
00664 bson_buffer * bson_append_code_w_scope( bson_buffer * b , const char * name , const char * code , const bson * scope);
00665 
00677 bson_buffer * bson_append_code_w_scope_n( bson_buffer * b , const char * name , const char * code , int size , const bson * scope);
00678 
00690 bson_buffer * bson_append_binary( bson_buffer * b, const char * name, char type, const char * str, int len );
00691 
00701 bson_buffer * bson_append_bool( bson_buffer * b , const char * name , const bson_bool_t v );
00702 
00711 bson_buffer * bson_append_null( bson_buffer * b , const char * name );
00712 
00721 bson_buffer * bson_append_undefined( bson_buffer * b , const char * name );
00722 
00733 bson_buffer * bson_append_regex( bson_buffer * b , const char * name , const char * pattern, const char * opts );
00734 
00744 bson_buffer * bson_append_bson( bson_buffer * b , const char * name , const bson* bson);
00745 
00755 bson_buffer * bson_append_element( bson_buffer * b, const char * name_or_null, const bson_iterator* elem);
00756 
00766 bson_buffer * bson_append_timestamp( bson_buffer * b, const char * name, bson_timestamp_t * ts );
00767 
00768 /* these both append a bson_date */
00778 bson_buffer * bson_append_date(bson_buffer * b, const char * name, bson_date_t millis);
00779 
00789 bson_buffer * bson_append_time_t(bson_buffer * b, const char * name, time_t secs);
00790 
00799 bson_buffer * bson_append_start_object( bson_buffer * b , const char * name );
00800 
00809 bson_buffer * bson_append_start_array( bson_buffer * b , const char * name );
00810 
00818 bson_buffer * bson_append_finish_object( bson_buffer * b );
00819 
00820 void bson_numstr(char* str, int i);
00821 void bson_incnumstr(char* str);
00822 
00823 
00824 /* ------------------------------
00825    ERROR HANDLING - also used in mongo code
00826    ------------------------------ */
00836 void * bson_malloc(int size); /* checks return value */
00837 
00847 void * bson_realloc(void * ptr, int size); /* checks return value */
00848 
00849 /* bson_err_handlers shouldn't return!!! */
00850 typedef void(*bson_err_handler)(const char* errmsg);
00851 
00852 /* returns old handler or NULL */
00853 /* default handler prints error then exits with failure*/
00861 bson_err_handler set_bson_err_handler(bson_err_handler func);
00862 
00863 /* does nothing if ok != 0 */
00869 void bson_fatal( int ok );
00870 
00877 void bson_fatal_msg( int ok, const char* msg );
00878 
00879 MONGO_EXTERN_C_END
00880 #endif

Generated on Wed May 18 2011 11:22:49 for MongoDBCDriver by  doxygen 1.7.1