int _iterator_getComplex(bson_iterator* iter, struct Rcomplex* z) { bson_iterator sub; if (bson_iterator_type(iter) != BSON_OBJECT) return 0; bson_iterator_subiterator(iter, &sub); if (bson_iterator_next(&sub) != BSON_DOUBLE || strcmp(bson_iterator_key(&sub), "r") != 0) return 0; z->r = bson_iterator_double(&sub); if (bson_iterator_next(&sub) != BSON_DOUBLE || strcmp(bson_iterator_key(&sub), "i") != 0) return 0; z->i = bson_iterator_double(&sub); if (bson_iterator_next(&sub) != BSON_EOO) return 0; return 1; }
void bson_print_raw( const char * data , int depth ){ bson_iterator i; const char * key; int temp; char oidhex[25]; bson_iterator_init( &i , data ); while ( bson_iterator_next( &i ) ){ bson_type t = bson_iterator_type( &i ); if ( t == 0 ) break; key = bson_iterator_key( &i ); for ( temp=0; temp<=depth; temp++ ) printf( "\t" ); printf( "%s : %d \t " , key , t ); switch ( t ){ case bson_int: printf( "%d" , bson_iterator_int( &i ) ); break; case bson_double: printf( "%f" , bson_iterator_double( &i ) ); break; case bson_bool: printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); break; case bson_string: printf( "%s" , bson_iterator_string( &i ) ); break; case bson_null: printf( "null" ); break; case bson_oid: bson_oid_to_string(bson_iterator_oid(&i), oidhex); printf( "%s" , oidhex ); break; case bson_object: case bson_array: printf( "\n" ); bson_print_raw( bson_iterator_value( &i ) , depth + 1 ); break; default: fprintf( stderr , "can't print type : %d\n" , t ); } printf( "\n" ); } }
void MongodbClient::ReadData(KeyedArchive* archive, void* bsonObj) { if((!archive) || (!bsonObj)) return; bson_iterator it; bson_iterator_init(&it, (bson*)bsonObj); while (bson_iterator_next(&it)) { String key = String(bson_iterator_key(&it)); bson_type type = bson_iterator_type(&it); if(key == "_id") continue; // ignore _id switch (type) { case BSON_STRING: archive->SetString(key, String(bson_iterator_string(&it))); break; case BSON_INT: archive->SetInt32(key, bson_iterator_int(&it)); break; case BSON_LONG: archive->SetInt32(key, (int32)bson_iterator_long(&it)); break; case BSON_DOUBLE: archive->SetFloat(key, (float32)bson_iterator_double(&it)); break; case BSON_OBJECT: { bson sub; bson_iterator_subobject(&it, &sub); KeyedArchive* subArchive = new KeyedArchive(); ReadData(subArchive, &sub); archive->SetArchive(key, subArchive); SafeRelease(subArchive); break; } case BSON_OID: //TODO: add 12-bytes array //bson_append_oid(object, key, bson_iterator_oid(&it)); break; default: DVASSERT(false); Logger::Error("[MongodbUpdateObject::ReadData] Not implemented type: %d", type); break; } } }
void json_from_bson_double(yajl_gen *g, bson_iterator *it) { double value = bson_iterator_double( it ); char buffer[100]; sprintf( buffer, "%.9g", value ); yajl_gen_number( *g, buffer, strlen(buffer) ); }
static void bson_decode_tree(lua_State* L, bson_iterator* b,int set_t){ const char *key; while(bson_iterator_next(b)) { bson_type t = bson_iterator_type(b); if(t == 0) { lua_pushnil(L); break; } key = bson_iterator_key(b); if(set_t) { lua_pushstring(L,key); } LOG_MSG("type %d,key %s",t,key); switch(t) { case BSON_DOUBLE: lua_pushnumber(L,bson_iterator_double(b)); break; case BSON_STRING: lua_pushstring(L,bson_iterator_string(b)); break; case BSON_BOOL: lua_pushboolean(L,bson_iterator_bool( b ) ? true : false ); break; case BSON_NULL: lua_pushnil(L); break; case BSON_INT: lua_pushinteger(L,bson_iterator_int(b)); break; case BSON_OBJECT: bson_iterator t; bson_iterator_subiterator(b,&t); bson_decode_tree(L,&t); lua_settable(L,-3); break; case BSON_ARRAY: lua_newtable(L); bson_iterator s; bson_iterator_subiterator(b,&s); bson_decode_tree(L,&s,1); break; default: break; } if(set_t) { lua_settable(L,-3); } } }
//------------------------------------------------------------------------------ int main( int argc, char * argv[] ){ mongo conn; if( mongo_client( &conn , TEST_SERVER, TEST_PORT ) != MONGO_OK ) { std::cout << "failed to connect\n"; return EXIT_FAILURE; } mongo_cursor cursor; mongo_cursor_init( &cursor, &conn, "test.test" ); char hex_oid[25]; while( mongo_cursor_next( &cursor ) == MONGO_OK ) { std::cout << "row:\n"; bson_iterator it; bson_iterator_init( &it, mongo_cursor_bson( &cursor ) ); while( bson_iterator_next( &it ) ) { std::cout << " " << bson_iterator_key( &it ) << " = "; switch( bson_iterator_type( &it ) ) { case BSON_DOUBLE: std::cout << "(double) " << bson_iterator_double( &it ) << std::endl; break; case BSON_INT: std::cout << "(int) " << bson_iterator_int( &it ) << std::endl; break; case BSON_STRING: std::cout << "(string) \"" << bson_iterator_string( &it ) << "\"\n"; break; case BSON_OID: bson_oid_to_string( bson_iterator_oid( &it ), hex_oid ); std::cout << "(oid) \"" << hex_oid << "\"\n"; break; case BSON_OBJECT: std::cout << "(subobject) {...}\n"; break; case BSON_ARRAY: std::cout << "(array) [...]\n"; break; case BSON_TIMESTAMP: std::cout << "(timestamp) [...]\n"; break; default: std::cout << "(type " << bson_iterator_type( &it ) << std::endl; break; } } std::cout << std::endl; } mongo_disconnect( &conn ); return EXIT_SUCCESS; }
void MongodbObject::EnableForEdit() { bson *newObject = new bson(); bson_init(newObject); bson_iterator it; bson_iterator_init(&it, objectData->object); while (bson_iterator_next(&it)) { const char * key = bson_iterator_key(&it); bson_type type = bson_iterator_type(&it); switch (type) { case BSON_STRING: bson_append_string(newObject, key, bson_iterator_string(&it)); break; case BSON_INT: bson_append_int(newObject, key, bson_iterator_int(&it)); break; case BSON_LONG: bson_append_long(newObject, key, bson_iterator_long(&it)); break; case BSON_DOUBLE: bson_append_double(newObject, key, bson_iterator_double(&it)); break; case BSON_OBJECT: bson sub; bson_iterator_subobject(&it, &sub); bson_append_bson(newObject, key, &sub); break; case BSON_OID: bson_append_oid(newObject, key, bson_iterator_oid(&it)); break; default: break; } } bson_destroy(objectData->object); SafeDelete(objectData->object); objectData->object = newObject; }
void testSaveLoad() { CU_ASSERT_PTR_NOT_NULL_FATAL(jb); bson_oid_t oid; EJCOLL *ccoll = ejdbcreatecoll(jb, "contacts", NULL); CU_ASSERT_PTR_NOT_NULL(ccoll); //Save record bson a1; bson_init(&a1); bson_append_string(&a1, "name", "Петров Петр"); bson_append_string(&a1, "phone", "333-222-333"); bson_append_int(&a1, "age", 33); bson_append_long(&a1, "longage", 0xFFFFFFFFFF01LL); bson_append_double(&a1, "doubleage", 0.333333); bson_finish(&a1); ejdbsavebson(ccoll, &a1, &oid); bson_destroy(&a1); bson *lbson = ejdbloadbson(ccoll, &oid); CU_ASSERT_PTR_NOT_NULL(lbson); bson_iterator it1; bson_iterator_init(&it1, lbson); int btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_OID); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_STRING); CU_ASSERT(!strcmp("name", bson_iterator_key(&it1))); CU_ASSERT(!strcmp("Петров Петр", bson_iterator_string(&it1))); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_STRING); CU_ASSERT(!strcmp("phone", bson_iterator_key(&it1))); CU_ASSERT(!strcmp("333-222-333", bson_iterator_string(&it1))); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_INT); CU_ASSERT(!strcmp("age", bson_iterator_key(&it1))); CU_ASSERT(33 == bson_iterator_int(&it1)); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_LONG); CU_ASSERT(!strcmp("longage", bson_iterator_key(&it1))); CU_ASSERT(0xFFFFFFFFFF01LL == bson_iterator_long(&it1)); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_DOUBLE); CU_ASSERT(!strcmp("doubleage", bson_iterator_key(&it1))); CU_ASSERT_DOUBLE_EQUAL(bson_iterator_double(&it1), 0.3, 0.1); btype = bson_iterator_next(&it1); CU_ASSERT(btype == BSON_EOO); bson_del(lbson); }
clsSiteData::clsSiteData(int id, const bson *info) { m_ID = id; bson_iterator it[1]; bson_iterator_init(it, info); while (bson_iterator_next(it)) { if (strcmp("Name", bson_iterator_key(it)) == 0) m_Name = bson_iterator_string(it); //else if(strcmp("AREA", bson_iterator_key(it)) == 0) // m_Area = (float)bson_iterator_double(it); else if (strcmp("LocalX", bson_iterator_key(it)) == 0) m_XPR = (float) bson_iterator_double(it); else if (strcmp("LocalY", bson_iterator_key(it)) == 0) m_YPR = (float) bson_iterator_double(it); else if (strcmp("Lat", bson_iterator_key(it)) == 0) m_Latitude = (float) bson_iterator_double(it); else if (strcmp("Long", bson_iterator_key(it)) == 0) m_Longitude = (float) bson_iterator_double(it); else if (strcmp("Elevation", bson_iterator_key(it)) == 0) m_Elevation = (float) bson_iterator_double(it); } }
void InitWith(bson *obj) { bson_iterator it; bson_iterator_init(&it, obj); while (bson_iterator_next(&it)) { const char * key = bson_iterator_key(&it); bson_type type = bson_iterator_type(&it); switch (type) { case BSON_STRING: bson_append_string(object, key, bson_iterator_string(&it)); break; case BSON_INT: bson_append_int(object, key, bson_iterator_int(&it)); break; case BSON_LONG: bson_append_long(object, key, bson_iterator_long(&it)); break; case BSON_DOUBLE: bson_append_double(object, key, bson_iterator_double(&it)); break; case BSON_OBJECT: { bson sub; bson_iterator_subobject(&it, &sub); bson_append_bson(object, key, &sub); break; } case BSON_OID: bson_append_oid(object, key, bson_iterator_oid(&it)); break; default: DVASSERT(false); Logger::Error("[MongodbObjectInternalData::InitWith] Not implemented type: %d", type); break; } } }
double MongodbObject::GetDouble(const String &fieldname) { double retValue = 0; bson_iterator it; bson_iterator_init(&it, objectData->object); bson_iterator foundIt; bool found = objectData->FindField(&it, &foundIt, fieldname, true); if(found) { retValue = bson_iterator_double(&foundIt); } return retValue; }
MONGO_EXPORT double mongo_count( mongo *conn, const char *db, const char *ns, const bson *query ) { bson cmd; bson out = {NULL, 0}; double count = -1; bson_init( &cmd ); bson_append_string( &cmd, "count", ns ); if ( query && bson_size( query ) > 5 ) /* not empty */ bson_append_bson( &cmd, "query", query ); bson_finish( &cmd ); if( mongo_run_command( conn, db, &cmd, &out ) == MONGO_OK ) { bson_iterator it; if( bson_find( &it, &out, "n" ) ) count = bson_iterator_double( &it ); bson_destroy( &cmd ); bson_destroy( &out ); return count; } else { bson_destroy( &out ); bson_destroy( &cmd ); return MONGO_ERROR; } }
Tcl_Obj * mongotcl_bsontolist_raw (Tcl_Interp *interp, Tcl_Obj *listObj, const char *data , int depth) { bson_iterator i; const char *key; bson_timestamp_t ts; char oidhex[25]; bson scope; if (data == NULL) { return listObj; } bson_iterator_from_buffer(&i, data); while (bson_iterator_next (&i)) { bson_type t = bson_iterator_type (&i); if (t == 0) { break; } key = bson_iterator_key (&i); switch (t) { case BSON_DOUBLE: { append_list_type_object (interp, listObj, "double", key, Tcl_NewDoubleObj (bson_iterator_double (&i))); break; } case BSON_STRING: { append_list_type_object (interp, listObj, "string", key, Tcl_NewStringObj (bson_iterator_string (&i), -1)); break; } case BSON_SYMBOL: { append_list_type_object (interp, listObj, "symbol", key, Tcl_NewStringObj (bson_iterator_string (&i), -1)); break; } case BSON_OID: { bson_oid_to_string( bson_iterator_oid( &i ), oidhex ); append_list_type_object (interp, listObj, "oid", key, Tcl_NewStringObj (oidhex, -1)); break; } case BSON_BOOL: { append_list_type_object (interp, listObj, "bool", key, Tcl_NewBooleanObj (bson_iterator_bool (&i))); break; } case BSON_DATE: { append_list_type_object (interp, listObj, "date", key, Tcl_NewLongObj ((long) bson_iterator_date(&i))); break; } case BSON_BINDATA: { unsigned char *bindata = (unsigned char *)bson_iterator_bin_data (&i); int binlen = bson_iterator_bin_len (&i); append_list_type_object (interp, listObj, "bin", key, Tcl_NewByteArrayObj (bindata, binlen)); break; } case BSON_UNDEFINED: { append_list_type_object (interp, listObj, "undefined", key, Tcl_NewObj ()); break; } case BSON_NULL: { append_list_type_object (interp, listObj, "null", key, Tcl_NewObj ()); break; } case BSON_REGEX: { append_list_type_object (interp, listObj, "regex", key, Tcl_NewStringObj (bson_iterator_regex (&i), -1)); break; } case BSON_CODE: { append_list_type_object (interp, listObj, "code", key, Tcl_NewStringObj (bson_iterator_code (&i), -1)); break; } case BSON_CODEWSCOPE: { bson_printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) ); /* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */ bson_iterator_code_scope( &i, &scope ); bson_printf( "\n\t SCOPE: " ); bson_print( &scope ); /* bson_destroy( &scope ); */ /* review - causes free error */ break; } case BSON_INT: { append_list_type_object (interp, listObj, "int", key, Tcl_NewIntObj (bson_iterator_int (&i))); break; } case BSON_LONG: { append_list_type_object (interp, listObj, "long", key, Tcl_NewLongObj ((uint64_t)bson_iterator_long (&i))); break; } case BSON_TIMESTAMP: { char string[64]; ts = bson_iterator_timestamp (&i); snprintf(string, sizeof(string), "%d:%d", ts.i, ts.t); append_list_type_object (interp, listObj, "timestamp", key, Tcl_NewStringObj (bson_iterator_string (&i), -1)); break; } case BSON_ARRAY: { Tcl_Obj *subList = Tcl_NewObj (); subList = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1); append_list_type_object (interp, listObj, "array", key, subList); break; } case BSON_OBJECT: { Tcl_Obj *subList = Tcl_NewObj (); subList = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1); append_list_type_object (interp, listObj, "object", key, subList); break; } default: { append_list_type_object (interp, listObj, "unknown", key, Tcl_NewIntObj (t)); break; } } } return listObj; }
void to_json(char *buf, const char * data, int depth){ int array_count = 0; int object_count = 0; bson_iterator i; const char * key; char oidhex[25]; bson_iterator_init( &i , data ); sprintf(buf+strlen(buf),"{"); while ( bson_iterator_next( &i ) ){ bson_type t = bson_iterator_type( &i ); if ( t == 0 ) break; key = bson_iterator_key( &i ); if(object_count > 0){sprintf(buf+strlen(buf),",");} else{object_count=1;} sprintf(buf+strlen(buf), "\"%s\":" , key ); switch ( t ){ case bson_int: sprintf(buf+strlen(buf), "%d" , bson_iterator_int( &i ) ); break; case bson_double: sprintf(buf+strlen(buf), "%f" , bson_iterator_double( &i ) ); break; case bson_bool: sprintf(buf+strlen(buf), "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); break; case bson_string: sprintf(buf+strlen(buf), "\"%s\"" , bson_iterator_string( &i ) ); break; case bson_null: sprintf( buf+strlen(buf),"null" ); break; case bson_oid: bson_oid_to_string(bson_iterator_oid(&i), oidhex); sprintf(buf+strlen(buf), "%s" , oidhex ); break; case bson_object: to_json(buf, bson_iterator_value( &i ) , depth + 1 ); break; case bson_array: sprintf(buf+strlen(buf), "[" ); bson_iterator j; bson_iterator_init( &j , bson_iterator_value(&i) ); array_count =0; while( bson_iterator_next(&j)){ if(array_count > 0){sprintf(buf+strlen(buf),",");} else{array_count=1;} to_json(buf, bson_iterator_value( &j ) , depth + 1 ); } sprintf(buf+strlen(buf), "]" ); break; default: fprintf( stderr , "can't print type : %d\n" , t ); } } sprintf(buf+strlen(buf),"}"); }
/** * \brief This function tries to load tag from MongoDB */ static void vs_mongo_tag_load_data(struct VSTag *tag, bson *bson_tag) { bson_iterator tag_iter; if( bson_find(&tag_iter, bson_tag, "data") == BSON_ARRAY) { bson_iterator data_iter; uint8 val_uint8; uint16 val_uint16; uint32 val_uint32; uint64 val_uint64; real32 val_real32; real64 val_real64; const char *str_value; int value_index = 0; bson_iterator_subiterator(&tag_iter, &data_iter); /* Go through all values */ switch(tag->data_type) { case VRS_VALUE_TYPE_UINT8: while( bson_iterator_next(&data_iter) == BSON_INT ) { val_uint8 = (uint8)bson_iterator_int(&data_iter); vs_tag_set_values(tag, 1, value_index, &val_uint8); value_index++; } break; case VRS_VALUE_TYPE_UINT16: while( bson_iterator_next(&data_iter) == BSON_INT ) { val_uint16 = (uint16)bson_iterator_int(&data_iter); vs_tag_set_values(tag, 1, value_index, &val_uint16); value_index++; } break; case VRS_VALUE_TYPE_UINT32: while( bson_iterator_next(&data_iter) == BSON_INT ) { val_uint32 = (uint32)bson_iterator_int(&data_iter); vs_tag_set_values(tag, 1, value_index, &val_uint32); value_index++; } break; case VRS_VALUE_TYPE_UINT64: while( bson_iterator_next(&data_iter) == BSON_LONG ) { val_uint64 = (uint64)bson_iterator_long(&data_iter); vs_tag_set_values(tag, 1, value_index, &val_uint64); value_index++; } break; case VRS_VALUE_TYPE_REAL16: /* TODO: add support for float16 */ break; case VRS_VALUE_TYPE_REAL32: while( bson_iterator_next(&data_iter) == BSON_DOUBLE ) { val_real32 = (real32)bson_iterator_double(&data_iter); vs_tag_set_values(tag, 1, value_index, &val_real32); value_index++; } break; case VRS_VALUE_TYPE_REAL64: while( bson_iterator_next(&data_iter) == BSON_DOUBLE ) { val_real64 = (real64)bson_iterator_double(&data_iter); vs_tag_set_values(tag, 1, value_index, &val_real64); value_index++; } break; case VRS_VALUE_TYPE_STRING8: str_value = bson_iterator_string(&data_iter); strcpy(tag->value, str_value); break; } } }
int test_bson_generic( void ) { bson_iterator it, it2, it3; bson_oid_t oid; bson_timestamp_t ts; bson_timestamp_t ts_result; bson b[1]; bson copy[1]; bson scope[1]; ts.i = 1; ts.t = 2; bson_init( b ); bson_append_double( b, "d", 3.14 ); bson_append_string( b, "s", "hello" ); bson_append_string_n( b, "s_n", "goodbye cruel world", 7 ); { bson_append_start_object( b, "o" ); bson_append_start_array( b, "a" ); bson_append_binary( b, "0", 8, "w\0rld", 5 ); bson_append_finish_object( b ); bson_append_finish_object( b ); } bson_append_undefined( b, "u" ); bson_oid_from_string( &oid, "010203040506070809101112" ); ASSERT( !memcmp( oid.bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12 ) ); bson_append_oid( b, "oid", &oid ); bson_append_bool( b, "b", 1 ); bson_append_date( b, "date", 0x0102030405060708 ); bson_append_null( b, "n" ); bson_append_regex( b, "r", "^asdf", "imx" ); /* no dbref test (deprecated) */ bson_append_code( b, "c", "function(){}" ); bson_append_code_n( b, "c_n", "function(){}garbage", 12 ); bson_append_symbol( b, "symbol", "symbol" ); bson_append_symbol_n( b, "symbol_n", "symbol and garbage", 6 ); { bson_init( scope ); bson_append_int( scope, "i", 123 ); bson_finish( scope ); bson_append_code_w_scope( b, "cws", "function(){return i}", scope ); bson_destroy( scope ); } bson_append_timestamp( b, "timestamp", &ts ); bson_append_long( b, "l", 0x1122334455667788 ); /* Ensure that we can't copy a non-finished object. */ ASSERT( bson_copy( copy, b ) == BSON_ERROR ); bson_finish( b ); ASSERT( b->err == BSON_VALID ); /* Test append after finish. */ ASSERT( bson_append_string( b, "foo", "bar" ) == BSON_ERROR ); ASSERT( b->err & BSON_ALREADY_FINISHED ); ASSERT( bson_copy( copy, b ) == BSON_OK ); ASSERT( 1 == copy->finished ); ASSERT( 0 == copy->err ); bson_destroy( copy ); bson_print( b ); bson_iterator_init( &it, b ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( bson_iterator_type( &it ) == BSON_DOUBLE ); ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) ); ASSERT( bson_iterator_double( &it ) == 3.14 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_STRING ); ASSERT( bson_iterator_type( &it ) == BSON_STRING ); ASSERT( !strcmp( bson_iterator_key( &it ), "s" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "hello" ) ); ASSERT( strcmp( bson_iterator_string( &it ), "" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_STRING ); ASSERT( bson_iterator_type( &it ) == BSON_STRING ); ASSERT( !strcmp( bson_iterator_key( &it ), "s_n" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "goodbye" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_OBJECT ); ASSERT( bson_iterator_type( &it ) == BSON_OBJECT ); ASSERT( !strcmp( bson_iterator_key( &it ), "o" ) ); bson_iterator_subiterator( &it, &it2 ); ASSERT( bson_iterator_more( &it2 ) ); ASSERT( bson_iterator_next( &it2 ) == BSON_ARRAY ); ASSERT( bson_iterator_type( &it2 ) == BSON_ARRAY ); ASSERT( !strcmp( bson_iterator_key( &it2 ), "a" ) ); bson_iterator_subiterator( &it2, &it3 ); ASSERT( bson_iterator_more( &it3 ) ); ASSERT( bson_iterator_next( &it3 ) == BSON_BINDATA ); ASSERT( bson_iterator_type( &it3 ) == BSON_BINDATA ); ASSERT( !strcmp( bson_iterator_key( &it3 ), "0" ) ); ASSERT( bson_iterator_bin_type( &it3 ) == 8 ); ASSERT( bson_iterator_bin_len( &it3 ) == 5 ); ASSERT( !memcmp( bson_iterator_bin_data( &it3 ), "w\0rld", 5 ) ); ASSERT( bson_iterator_more( &it3 ) ); ASSERT( bson_iterator_next( &it3 ) == BSON_EOO ); ASSERT( bson_iterator_type( &it3 ) == BSON_EOO ); ASSERT( !bson_iterator_more( &it3 ) ); ASSERT( bson_iterator_more( &it2 ) ); ASSERT( bson_iterator_next( &it2 ) == BSON_EOO ); ASSERT( bson_iterator_type( &it2 ) == BSON_EOO ); ASSERT( !bson_iterator_more( &it2 ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_UNDEFINED ); ASSERT( bson_iterator_type( &it ) == BSON_UNDEFINED ); ASSERT( !strcmp( bson_iterator_key( &it ), "u" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_OID ); ASSERT( bson_iterator_type( &it ) == BSON_OID ); ASSERT( !strcmp( bson_iterator_key( &it ), "oid" ) ); ASSERT( !memcmp( bson_iterator_oid( &it )->bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12 ) ); ASSERT( bson_iterator_oid( &it )->ints[0] == oid.ints[0] ); ASSERT( bson_iterator_oid( &it )->ints[1] == oid.ints[1] ); ASSERT( bson_iterator_oid( &it )->ints[2] == oid.ints[2] ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_BOOL ); ASSERT( bson_iterator_type( &it ) == BSON_BOOL ); ASSERT( !strcmp( bson_iterator_key( &it ), "b" ) ); ASSERT( bson_iterator_bool( &it ) == 1 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DATE ); ASSERT( bson_iterator_type( &it ) == BSON_DATE ); ASSERT( !strcmp( bson_iterator_key( &it ), "date" ) ); ASSERT( bson_iterator_date( &it ) == 0x0102030405060708 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_NULL ); ASSERT( bson_iterator_type( &it ) == BSON_NULL ); ASSERT( !strcmp( bson_iterator_key( &it ), "n" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_REGEX ); ASSERT( bson_iterator_type( &it ) == BSON_REGEX ); ASSERT( !strcmp( bson_iterator_key( &it ), "r" ) ); ASSERT( !strcmp( bson_iterator_regex( &it ), "^asdf" ) ); ASSERT( !strcmp( bson_iterator_regex_opts( &it ), "imx" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_CODE ); ASSERT( bson_iterator_type( &it ) == BSON_CODE ); ASSERT( !strcmp( bson_iterator_code(&it), "function(){}") ); ASSERT( !strcmp( bson_iterator_key( &it ), "c" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_CODE ); ASSERT( bson_iterator_type( &it ) == BSON_CODE ); ASSERT( !strcmp( bson_iterator_key( &it ), "c_n" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "" ) ); ASSERT( !strcmp( bson_iterator_code( &it ), "function(){}" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_SYMBOL ); ASSERT( bson_iterator_type( &it ) == BSON_SYMBOL ); ASSERT( !strcmp( bson_iterator_key( &it ), "symbol" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "symbol" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_SYMBOL ); ASSERT( bson_iterator_type( &it ) == BSON_SYMBOL ); ASSERT( !strcmp( bson_iterator_key( &it ), "symbol_n" ) ); ASSERT( !strcmp( bson_iterator_string( &it ), "symbol" ) ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_CODEWSCOPE ); ASSERT( bson_iterator_type( &it ) == BSON_CODEWSCOPE ); ASSERT( !strcmp( bson_iterator_key( &it ), "cws" ) ); ASSERT( !strcmp( bson_iterator_code( &it ), "function(){return i}" ) ); { bson scope; bson_iterator_code_scope( &it, &scope ); bson_iterator_init( &it2, &scope ); ASSERT( bson_iterator_more( &it2 ) ); ASSERT( bson_iterator_next( &it2 ) == BSON_INT ); ASSERT( bson_iterator_type( &it2 ) == BSON_INT ); ASSERT( !strcmp( bson_iterator_key( &it2 ), "i" ) ); ASSERT( bson_iterator_int( &it2 ) == 123 ); ASSERT( bson_iterator_more( &it2 ) ); ASSERT( bson_iterator_next( &it2 ) == BSON_EOO ); ASSERT( bson_iterator_type( &it2 ) == BSON_EOO ); ASSERT( !bson_iterator_more( &it2 ) ); } ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_TIMESTAMP ); ASSERT( bson_iterator_type( &it ) == BSON_TIMESTAMP ); ASSERT( !strcmp( bson_iterator_key( &it ), "timestamp" ) ); ts_result = bson_iterator_timestamp( &it ); ASSERT( ts_result.i == 1 ); ASSERT( ts_result.t == 2 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_LONG ); ASSERT( bson_iterator_type( &it ) == BSON_LONG ); ASSERT( !strcmp( bson_iterator_key( &it ), "l" ) ); ASSERT( bson_iterator_long( &it ) == 0x1122334455667788 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_EOO ); ASSERT( bson_iterator_type( &it ) == BSON_EOO ); ASSERT( !bson_iterator_more( &it ) ); bson_destroy( b ); { bson bsrc[1]; bson_init( bsrc ); bson_append_double( bsrc, "d", 3.14 ); bson_finish( bsrc ); ASSERT( bsrc->err == BSON_VALID ); bson_init( b ); bson_append_double( b, "", 3.14 ); /* test empty name (in general) */ bson_iterator_init( &it, bsrc ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( bson_iterator_type( &it ) == BSON_DOUBLE ); bson_append_element( b, "d", &it ); bson_append_element( b, 0, &it ); /* test null */ bson_append_element( b, "", &it ); /* test empty name */ bson_finish( b ); ASSERT( b->err == BSON_VALID ); /* bson_print( b ); */ bson_iterator_init( &it, b ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( !strcmp( bson_iterator_key( &it ), "" ) ); ASSERT( bson_iterator_double( &it ) == 3.14 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) ); ASSERT( bson_iterator_double( &it ) == 3.14 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( !strcmp( bson_iterator_key( &it ), "d" ) ); ASSERT( bson_iterator_double( &it ) == 3.14 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_DOUBLE ); ASSERT( !strcmp( bson_iterator_key( &it ), "" ) ); ASSERT( bson_iterator_double( &it ) == 3.14 ); ASSERT( bson_iterator_more( &it ) ); ASSERT( bson_iterator_next( &it ) == BSON_EOO ); ASSERT( !bson_iterator_more( &it ) ); bson_destroy( bsrc ); bson_destroy( b ); } return 0; }
int main(){ mongo_connection conn[1]; mongo_connection_options opts; bson_buffer bb; bson b; mongo_cursor * cursor; int i; char hex_oid[25]; const char * col = "c.simple"; const char * ns = "test.c.simple"; INIT_SOCKETS_FOR_WINDOWS; strncpy(opts.host, TEST_SERVER, 255); opts.host[254] = '\0'; opts.port = 27017; if (mongo_connect( conn , &opts )){ printf("failed to connect\n"); exit(1); } /* if the collection doesn't exist dropping it will fail */ if (!mongo_cmd_drop_collection(conn, "test", col, NULL) && mongo_find_one(conn, ns, bson_empty(&b), bson_empty(&b), NULL)){ printf("failed to drop collection\n"); exit(1); } for(i=0; i< 5; i++){ bson_buffer_init( & bb ); bson_append_new_oid( &bb, "_id" ); bson_append_double( &bb , "a" , 17 ); bson_append_int( &bb , "b" , 17 ); bson_append_string( &bb , "c" , "17" ); { bson_buffer * sub = bson_append_start_object( &bb , "d" ); bson_append_int( sub, "i", 71 ); bson_append_finish_object(sub); } { bson_buffer * arr = bson_append_start_array( &bb , "e" ); bson_append_int( arr, "0", 71 ); bson_append_string( arr, "1", "71" ); bson_append_finish_object(arr); } bson_from_buffer(&b, &bb); mongo_insert( conn , ns , &b ); bson_destroy(&b); } cursor = mongo_find( conn , ns , bson_empty(&b) , 0 , 0 , 0 , 0 ); while (mongo_cursor_next(cursor)){ bson_iterator it; bson_iterator_init(&it, cursor->current.data); while(bson_iterator_next(&it)){ fprintf(stderr, " %s: ", bson_iterator_key(&it)); switch(bson_iterator_type(&it)){ case bson_double: fprintf(stderr, "(double) %e\n", bson_iterator_double(&it)); break; case bson_int: fprintf(stderr, "(int) %d\n", bson_iterator_int(&it)); break; case bson_string: fprintf(stderr, "(string) \"%s\"\n", bson_iterator_string(&it)); break; case bson_oid: bson_oid_to_string(bson_iterator_oid(&it), hex_oid); fprintf(stderr, "(oid) \"%s\"\n", hex_oid); break; case bson_object: fprintf(stderr, "(subobject) {...}\n"); break; case bson_array: fprintf(stderr, "(array) [...]\n"); break; default: fprintf(stderr, "(type %d)\n", bson_iterator_type(&it)); break; } } fprintf(stderr, "\n"); } mongo_cursor_destroy(cursor); mongo_cmd_drop_db(conn, "test"); mongo_destroy( conn ); return 0; }
void SimpleVector<Scalar>::import_from_file(const char *filename, const char *var_name, MatrixExportFormat fmt) { switch (fmt) { case EXPORT_FORMAT_PLAIN_ASCII: { std::vector<Scalar> data; std::ifstream input(filename); if (input.bad()) throw Exceptions::IOException(Exceptions::IOException::Read, filename); std::string lineData; while (getline(input, lineData)) { Scalar d; std::stringstream lineStream(lineData); lineStream >> d; data.push_back(d); } this->alloc(data.size()); memcpy(this->v, &data[0], sizeof(Scalar)*data.size()); } break; case EXPORT_FORMAT_MATLAB_MATIO: #ifdef WITH_MATIO mat_t *matfp; matvar_t *matvar; matfp = Mat_Open(filename, MAT_ACC_RDONLY); if (!matfp) { throw Exceptions::IOException(Exceptions::IOException::Read, filename); return; } matvar = Mat_VarRead(matfp, var_name); if (matvar) { this->alloc(matvar->dims[0]); if (Hermes::Helpers::TypeIsReal<Scalar>::value) memcpy(this->v, matvar->data, sizeof(Scalar)*this->size); else { std::complex<double>* complex_data = malloc_with_check<SimpleVector<Scalar>, std::complex<double> >(this->size, this); double* real_array = (double*)((mat_complex_split_t*)matvar->data)->Re; double* imag_array = (double*)((mat_complex_split_t*)matvar->data)->Im; for (int i = 0; i < this->size; i++) complex_data[i] = std::complex<double>(real_array[i], imag_array[i]); memcpy(this->v, complex_data, sizeof(Scalar)*this->size); free_with_check(complex_data); } } Mat_Close(matfp); if (!matvar) throw Exceptions::IOException(Exceptions::IOException::Read, filename); #else throw Exceptions::Exception("MATIO not included."); #endif break; case EXPORT_FORMAT_MATRIX_MARKET: throw Hermes::Exceptions::MethodNotImplementedException("SimpleVector<Scalar>::import_from_file - Matrix Market"); break; #ifdef WITH_BSON case EXPORT_FORMAT_BSON: { FILE *fpr; fpr = fopen(filename, "rb"); // file size: fseek(fpr, 0, SEEK_END); int size = ftell(fpr); rewind(fpr); // allocate memory to contain the whole file: char *datar = malloc_with_check<char>(size); fread(datar, size, 1, fpr); fclose(fpr); bson br; bson_init_finished_data(&br, datar, 0); bson_iterator it; bson sub; bson_find(&it, &br, "size"); this->size = bson_iterator_int(&it); this->v = malloc_with_check<SimpleVector<Scalar>, Scalar>(this->size, this); bson_iterator it_coeffs; bson_find(&it_coeffs, &br, "v"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); int index_coeff = 0; while (bson_iterator_next(&it)) this->v[index_coeff++] = bson_iterator_double(&it); if (!Hermes::Helpers::TypeIsReal<Scalar>::value) { bson_find(&it_coeffs, &br, "v-imag"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); index_coeff = 0; while (bson_iterator_next(&it)) ((std::complex<double>)this->v[index_coeff++]).imag(bson_iterator_double(&it)); } bson_destroy(&br); free_with_check(datar); } break; #endif } }
//! Read raster data from MongoDB int clsRasterData::ReadFromMongoDB(gridfs *gfs, const char* remoteFilename) { gridfile gfile[1]; bson b[1]; bson_init(b); bson_append_string(b, "filename", remoteFilename); bson_finish(b); int flag = gridfs_find_query(gfs, b, gfile); if(0 != flag) { throw ModelException("clsRasterData", "ReadFromMongoDB", "The file " + string(remoteFilename) + " does not exist."); } size_t length = (size_t)gridfile_get_contentlength(gfile); char* buf = (char*)malloc(length); gridfile_read (gfile, length, buf); float *data = (float*)buf; bson bmeta[1]; gridfile_get_metadata(gfile, bmeta); bson_iterator iterator[1]; if ( bson_find( iterator, bmeta, "NCOLS" )) m_headers["NCOLS"] = (float)bson_iterator_int(iterator); if ( bson_find( iterator, bmeta, "NROWS" )) m_headers["NROWS"] = (float)bson_iterator_int(iterator); if ( bson_find( iterator, bmeta, "NODATA_VALUE" )) m_headers["NODATA_VALUE"] = (float)bson_iterator_double(iterator); if ( bson_find( iterator, bmeta, "XLLCENTER" )) m_headers["XLLCENTER"] = (float)bson_iterator_double(iterator); if ( bson_find( iterator, bmeta, "YLLCENTER" )) m_headers["YLLCENTER"] = (float)bson_iterator_double(iterator); if ( bson_find( iterator, bmeta, "CELLSIZE" )) { m_headers["CELLSIZE"] = (float)bson_iterator_double(iterator); //m_headers["DY"] = m_headers["DX"]; } int nRows = (int)m_headers["NROWS"]; int nCols = (int)m_headers["NCOLS"]; vector<float> values; vector<int> positionRows; vector<int> positionCols; //get all valid values float nodataFloat = m_headers["NODATA_VALUE"]; for (int i = 0; i < nRows; ++i) { for (int j = 0; j < nCols; ++j) { int index = i*nCols + j; float value = data[index]; if(FloatEqual(nodataFloat, value)) continue; values.push_back(value); positionRows.push_back(i); positionCols.push_back(j); } } //create float array m_nRows = values.size(); m_rasterData = new float[m_nRows]; m_rasterPositionData = new float*[m_nRows]; for (int i = 0; i < m_nRows; ++i) { m_rasterData[i] = values.at(i); m_rasterPositionData[i] = new float[2]; m_rasterPositionData[i][0] = float(positionRows.at(i)); m_rasterPositionData[i][1] = float(positionCols.at(i)); } bson_destroy(b); gridfile_destroy(gfile); free(buf); return 0; }
static char * bson_to_json_recurse(StringInfo buf, const char *data, bool is_object) { bson_iterator i; const char *key; char oidhex[25]; char *str; bool first = true; bson_iterator_from_buffer(&i, data); while (bson_iterator_next(&i)) { bson_type t = bson_iterator_type(&i); if (t == 0) break; if (!first) appendStringInfoChar(buf, ','); first = false; if (is_object) { key = bson_iterator_key(&i); appendStringInfo(buf, "\"%s\"", key); appendStringInfoChar(buf, ':'); } switch (t) { case BSON_DOUBLE: appendStringInfo(buf, "%f", bson_iterator_double(&i)); break; case BSON_STRING: str = quote_string(bson_iterator_string(&i)); appendStringInfoString(buf, str); break; case BSON_OID: bson_oid_to_string(bson_iterator_oid(&i), oidhex); str = quote_string(oidhex); appendStringInfoString(buf, str); break; case BSON_BOOL: appendStringInfoString(buf, bson_iterator_bool(&i) ? "true" : "false"); break; case BSON_INT: appendStringInfo(buf, "%d", bson_iterator_int(&i)); break; case BSON_LONG: appendStringInfo(buf, "%lld", (uint64_t) bson_iterator_long(&i)); break; case BSON_NULL: appendStringInfoString(buf, "null"); break; case BSON_OBJECT: appendStringInfoChar(buf, '{'); bson_to_json_recurse(buf, bson_iterator_value(&i), true); appendStringInfoChar(buf, '}'); break; case BSON_ARRAY: appendStringInfoChar(buf, '['); bson_to_json_recurse(buf, bson_iterator_value(&i), false); appendStringInfoChar(buf, ']'); break; case BSON_DATE: case BSON_TIMESTAMP: case BSON_SYMBOL: case BSON_BINDATA: case BSON_UNDEFINED: case BSON_REGEX: case BSON_CODE: case BSON_CODEWSCOPE: ereport(ERROR, (errmsg("unsupported bson type: %d", t))); break; default: elog(ERROR, "unknown bson type: %d", t); break; } } return buf->data; }
int mongotcl_bsontoarray_raw (Tcl_Interp *interp, char *arrayName, char *typeArrayName, const char *data , int depth) { bson_iterator i; const char *key; bson_timestamp_t ts; char oidhex[25]; Tcl_Obj *obj; char *type; if (data == NULL) { return TCL_OK; } bson_iterator_from_buffer(&i, data); while (bson_iterator_next (&i)) { bson_type t = bson_iterator_type (&i); if (t == 0) { break; } key = bson_iterator_key (&i); switch (t) { case BSON_DOUBLE: { obj = Tcl_NewDoubleObj (bson_iterator_double (&i)); type = "double"; break; } case BSON_SYMBOL: { obj = Tcl_NewStringObj (bson_iterator_string (&i), -1); type = "symbol"; break; } case BSON_STRING: { obj = Tcl_NewStringObj (bson_iterator_string (&i), -1); type = "string"; break; } case BSON_OID: { bson_oid_to_string( bson_iterator_oid( &i ), oidhex ); obj = Tcl_NewStringObj (oidhex, -1); type = "oid"; break; } case BSON_BOOL: { obj = Tcl_NewBooleanObj (bson_iterator_bool (&i)); type = "bool"; break; } case BSON_DATE: { obj = Tcl_NewLongObj ((long) bson_iterator_date(&i)); type = "date"; break; } case BSON_BINDATA: { unsigned char *bindata = (unsigned char *)bson_iterator_bin_data (&i); int binlen = bson_iterator_bin_len (&i); obj = Tcl_NewByteArrayObj (bindata, binlen); type = "bin"; break; } case BSON_UNDEFINED: { obj = Tcl_NewObj (); type = "undefined"; break; } case BSON_NULL: { obj = Tcl_NewObj (); type = "null"; break; } case BSON_REGEX: { obj = Tcl_NewStringObj (bson_iterator_regex (&i), -1); type = "regex"; break; } case BSON_CODE: { obj = Tcl_NewStringObj (bson_iterator_code (&i), -1); type = "code"; break; } case BSON_CODEWSCOPE: { // bson_printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) ); /* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */ // bson_iterator_code_scope( &i, &scope ); // bson_printf( "\n\t SCOPE: " ); // bson_print( &scope ); /* bson_destroy( &scope ); */ /* review - causes free error */ break; } case BSON_INT: { obj = Tcl_NewIntObj (bson_iterator_int (&i)); type = "int"; break; } case BSON_LONG: { obj = Tcl_NewLongObj ((uint64_t)bson_iterator_long (&i)); type = "long"; break; } case BSON_TIMESTAMP: { char string[64]; ts = bson_iterator_timestamp (&i); snprintf(string, sizeof(string), "%d:%d", ts.i, ts.t); obj = Tcl_NewStringObj (bson_iterator_string (&i), -1); type = "timestamp"; break; } case BSON_ARRAY: { obj = Tcl_NewObj(); obj = mongotcl_bsontolist_raw (interp, obj, bson_iterator_value (&i), depth + 1); type = "array"; break; } case BSON_OBJECT: { Tcl_Obj *subList = Tcl_NewObj (); obj = mongotcl_bsontolist_raw (interp, subList, bson_iterator_value (&i), depth + 1); type = "object"; break; } default: { obj = Tcl_NewIntObj (t); type = "unknown"; break; } } if (Tcl_SetVar2Ex (interp, arrayName, key, obj, TCL_LEAVE_ERR_MSG) == NULL) { return TCL_ERROR; } if (typeArrayName != NULL) { if (Tcl_SetVar2Ex (interp, typeArrayName, key, Tcl_NewStringObj (type, -1), TCL_LEAVE_ERR_MSG) == NULL) { return TCL_ERROR; } } } return TCL_OK; }
void bson_print_raw( const char *data , int depth ) { bson_iterator i; const char *key; int temp; bson_timestamp_t ts; char oidhex[25]; bson scope; bson_iterator_from_buffer( &i, data ); while ( bson_iterator_next( &i ) ) { bson_type t = bson_iterator_type( &i ); if ( t == 0 ) break; key = bson_iterator_key( &i ); for ( temp=0; temp<=depth; temp++ ) printf( "\t" ); bson_printf( "%s : %d \t " , key , t ); switch ( t ) { case BSON_DOUBLE: printf( "%f" , bson_iterator_double( &i ) ); break; case BSON_STRING: printf( "%s" , bson_iterator_string( &i ) ); break; case BSON_SYMBOL: printf( "SYMBOL: %s" , bson_iterator_string( &i ) ); break; case BSON_OID: bson_oid_to_string( bson_iterator_oid( &i ), oidhex ); printf( "%s" , oidhex ); break; case BSON_BOOL: printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); break; case BSON_DATE: printf( "%ld" , ( long int )bson_iterator_date( &i ) ); break; case BSON_BINDATA: printf( "BSON_BINDATA" ); break; case BSON_UNDEFINED: printf( "BSON_UNDEFINED" ); break; case BSON_NULL: printf( "BSON_NULL" ); break; case BSON_REGEX: printf( "BSON_REGEX: %s", bson_iterator_regex( &i ) ); break; case BSON_CODE: printf( "BSON_CODE: %s", bson_iterator_code( &i ) ); break; case BSON_CODEWSCOPE: printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) ); bson_init( &scope ); bson_iterator_code_scope( &i, &scope ); printf( "\n\t SCOPE: " ); bson_print( &scope ); break; case BSON_INT: printf( "%d" , bson_iterator_int( &i ) ); break; case BSON_LONG: printf( "%lld" , ( long long int )bson_iterator_long( &i ) ); break; case BSON_TIMESTAMP: ts = bson_iterator_timestamp( &i ); printf( "i: %d, t: %d", ts.i, ts.t ); break; case BSON_OBJECT: case BSON_ARRAY: printf( "\n" ); bson_print_raw( bson_iterator_value( &i ) , depth + 1 ); break; default: bson_errprintf( "can't print type : %d\n" , t ); } printf( "\n" ); } }
EXPORT mxArray* mongo_bson_array_value(struct bson_iterator_* i) { bson_type sub_type, common_type; struct Rcomplex z; bson_iterator sub[MAXDIM+1]; mwSize ndims = 0; mwSize count[MAXDIM+1]; mwSize dim[MAXDIM+1]; mwSize* mdim = dim + 1; mwSize sizes[MAXDIM+1]; mxArray* ret; mwSize depth, j, len, ofs; int isRow = 0; sub[0] = *(bson_iterator*)i; /* count number of dimensions. This is equal to the number of consecutive array markers in the BSON */ do { bson_iterator_subiterator(&sub[ndims], &sub[ndims+1]); if (++ndims > MAXDIM) { mexPrintf("Max dimensions (%d) exceeded. Use an iterator\n", MAXDIM); return 0; } sub_type = bson_iterator_next(&sub[ndims]); } while (sub_type == BSON_ARRAY); /* get the first data value's type */ switch (common_type = sub_type) { case BSON_INT: ; case BSON_LONG: ; case BSON_DOUBLE: ; /* case BSON_STRING: ; */ case BSON_BOOL: ; case BSON_DATE: break; case BSON_OBJECT: if (_iterator_getComplex(&sub[ndims], &z)) break; /* fall thru to default */ default: /* including empty array */ mexPrintf("Unable to convert array - invalid type (%d)", common_type); return 0; } /* initial lowest level count */ for (j = 0; j <= ndims; j++) count[j] = 1; while ((sub_type = bson_iterator_next(&sub[ndims])) != BSON_EOO) { if (sub_type != common_type) { mexPrintf("Unable to convert array - inconsistent types"); return 0; } if (sub_type == BSON_OBJECT && !_iterator_getComplex(&sub[ndims], &z)) { mexPrintf("Unable to convert array - invalid subobject"); return 0; } ++count[ndims]; } /* step through rest of array -- checking common type and dimensions */ memset(dim, 0, sizeof(dim)); depth = ndims; while (depth >= 1) { sub_type = bson_iterator_next(&sub[depth]); switch (sub_type) { case BSON_EOO: if (dim[depth] == 0) dim[depth] = count[depth]; else if (dim[depth] != count[depth]) { mexPrintf("Unable to convert array - inconsistent dimensions"); return 0; } depth--; break; case BSON_ARRAY: count[depth]++; bson_iterator_subiterator(&sub[depth], &sub[depth+1]); if (++depth > ndims) { mexPrintf("Unable to convert array - inconsistent dimensions"); return 0; } count[depth] = 0; break; case BSON_INT: ; case BSON_LONG: ; case BSON_DOUBLE: ; /* case BSON_STRING: ; */ case BSON_BOOL: ; case BSON_DATE: ; GotEl: { if (depth != ndims || sub_type != common_type) { mexPrintf("Unable to convert array - inconsistent dimensions or types"); return 0; } count[depth]++; break; } case BSON_OBJECT: if (_iterator_getComplex(&sub[depth], &z)) goto GotEl; /* fall thru to default */ default: mexPrintf("Unable to convert array - invalid type (%d)", sub_type); return 0; } } if (ndims > 1) { j = dim[ndims]; /* reverse row and column */ dim[ndims] = dim[ndims-1]; dim[ndims-1] = j; } /* calculate offset each dimension multiplies it's index by */ len = 1; for (depth = ndims; depth > 0; depth--) { sizes[depth] = len; len *= dim[depth]; } if (ndims > 1) { reverse(mdim, ndims); /* reverse dimensions for Matlab */ j = sizes[ndims]; sizes[ndims] = sizes[ndims-1]; sizes[ndims-1] = j; } else { isRow = 1; ndims = 2; mdim[1] = mdim[0]; mdim[0] = 1; } /* for (j = 1; j <= ndims; j++) mexPrintf("%d ", dim[j]); mexPrintf("\n"); for (j = 1; j <= ndims; j++) mexPrintf("%d ", sizes[j]); mexPrintf("\n"); */ switch (common_type) { case BSON_INT: ret = mxCreateNumericArray(ndims, mdim, mxINT32_CLASS, mxREAL); break; case BSON_LONG: ret = mxCreateNumericArray(ndims, mdim, mxINT64_CLASS, mxREAL); break; case BSON_DATE: case BSON_DOUBLE: ret = mxCreateNumericArray(ndims, mdim, mxDOUBLE_CLASS, mxREAL); break; /* case BSON_STRING: */ case BSON_BOOL: ret = mxCreateLogicalArray(ndims, mdim); break; case BSON_OBJECT: ret = mxCreateNumericArray(ndims, mdim, mxDOUBLE_CLASS, mxCOMPLEX); break; default: /* never reaches here */ ret = 0; } if (isRow) ndims--; /* step through array(s) again, pulling out values */ bson_iterator_subiterator(&sub[0], &sub[1]); depth = 1; count[depth] = 0; while (depth >= 1) { sub_type = bson_iterator_next(&sub[depth]); count[depth]++; if (sub_type == BSON_EOO) { depth--; } else if (sub_type == BSON_ARRAY) { bson_iterator_subiterator(&sub[depth], &sub[depth+1]); depth++; count[depth] = 0; } else { ofs = 0; for (j = 1; j <= ndims; j++) ofs += sizes[j] * (count[j] - 1); switch (sub_type) { case BSON_INT: ((int*)mxGetData(ret))[ofs] = bson_iterator_int(&sub[depth]); break; case BSON_DATE: mxGetPr(ret)[ofs] = 719529.0 + bson_iterator_date(&sub[depth]) / (1000.0 * 60 * 60 * 24); break; case BSON_DOUBLE: mxGetPr(ret)[ofs] = bson_iterator_double(&sub[depth]); break; case BSON_LONG: ((int64_t*)mxGetData(ret))[ofs] = bson_iterator_long(&sub[depth]); break; case BSON_STRING: break; case BSON_BOOL: ; ((mxLogical*)mxGetData(ret))[ofs] = bson_iterator_bool(&sub[depth]); break; case BSON_OBJECT: _iterator_getComplex(&sub[depth], &z); mxGetPr(ret)[ofs] = z.r; mxGetPi(ret)[ofs] = z.i; break; default: ; /* never reaches here */ } } } return ret; }
int main(){ mongo_connection conn[1]; bson_buffer bb; bson obj; bson cond; int i; bson_oid_t oid; const char* col = "c.update_test"; const char* ns = "test.c.update_test"; INIT_SOCKETS_FOR_WINDOWS; if (mongo_connect( conn , TEST_SERVER, 27017 )){ printf("failed to connect\n"); exit(1); } /* if the collection doesn't exist dropping it will fail */ if ( mongo_cmd_drop_collection(conn, "test", col, NULL) == MONGO_OK && mongo_find_one(conn, ns, bson_empty(&obj), bson_empty(&obj), NULL) != MONGO_OK ){ printf("failed to drop collection\n"); exit(1); } bson_oid_gen(&oid); { /* insert */ bson_buffer_init(&bb); bson_append_oid(&bb, "_id", &oid); bson_append_int(&bb, "a", 3 ); bson_from_buffer(&obj, &bb); mongo_insert(conn, ns, &obj); bson_destroy(&obj); } { /* insert */ bson op; bson_buffer_init(&bb); bson_append_oid(&bb, "_id", &oid); bson_from_buffer(&cond, &bb); bson_buffer_init(&bb); { bson_append_start_object(&bb, "$inc"); bson_append_int(&bb, "a", 2 ); bson_append_finish_object(&bb); } { bson_append_start_object(&bb, "$set"); bson_append_double(&bb, "b", -1.5 ); bson_append_finish_object(&bb); } bson_from_buffer(&op, &bb); for (i=0; i<5; i++) mongo_update(conn, ns, &cond, &op, 0); /* cond is used later */ bson_destroy(&op); } if( mongo_find_one(conn, ns, &cond, 0, &obj) != MONGO_OK ){ printf("Failed to find object\n"); exit(1); } else { int fields = 0; bson_iterator it; bson_iterator_init(&it, obj.data); bson_destroy(&cond); while(bson_iterator_next(&it)){ switch(bson_iterator_key(&it)[0]){ case '_': /* id */ ASSERT(bson_iterator_type(&it) == BSON_OID); ASSERT(!memcmp(bson_iterator_oid(&it)->bytes, oid.bytes, 12)); fields++; break; case 'a': ASSERT(bson_iterator_type(&it) == BSON_INT); ASSERT(bson_iterator_int(&it) == 3 + 5*2); fields++; break; case 'b': ASSERT(bson_iterator_type(&it) == BSON_DOUBLE); ASSERT(bson_iterator_double(&it) == -1.5); fields++; break; } } ASSERT(fields == 3); } bson_destroy(&obj); mongo_cmd_drop_db(conn, "test"); mongo_destroy(conn); return 0; }
int main(int argc, char *argv[]) { bson_buffer bb; bson b; bson_iterator it, it2, it3; bson_oid_t oid; bson_buffer_init(&bb); bson_append_double(&bb, "d", 3.14); bson_append_string(&bb, "s", "hello"); { bson_buffer *obj = bson_append_start_object(&bb, "o"); bson_buffer *arr = bson_append_start_array(obj, "a"); bson_append_binary(arr, "0", 8, "w\0rld", 5); bson_append_finish_object(arr); bson_append_finish_object(obj); } bson_append_undefined(&bb, "u"); bson_oid_from_string(&oid, "010203040506070809101112"); ASSERT(!memcmp(oid.bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12)); bson_append_oid(&bb, "oid", &oid); bson_append_bool(&bb, "b", 1); bson_append_date(&bb, "date", 0x0102030405060708ULL); bson_append_null(&bb, "n"); bson_append_regex(&bb, "r", "^asdf", "imx"); /* no dbref test (deprecated) */ bson_append_code(&bb, "c", "function(){}"); bson_append_symbol(&bb, "symbol", "SYMBOL"); { bson_buffer scope_buf; bson scope; bson_buffer_init(&scope_buf); bson_append_int(&scope_buf, "i", 123); bson_from_buffer(&scope, &scope_buf); bson_append_code_w_scope(&bb, "cws", "function(){return i}", &scope); bson_destroy(&scope); } /* no timestamp test (internal) */ bson_append_long(&bb, "l", 0x1122334455667788ULL); bson_from_buffer(&b, &bb); bson_iterator_init(&it, b.data); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_double); ASSERT(bson_iterator_type(&it) == bson_double); ASSERT(!strcmp(bson_iterator_key(&it), "d")); ASSERT(bson_iterator_double(&it) == 3.14); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_string); ASSERT(bson_iterator_type(&it) == bson_string); ASSERT(!strcmp(bson_iterator_key(&it), "s")); ASSERT(!strcmp(bson_iterator_string(&it), "hello")); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_object); ASSERT(bson_iterator_type(&it) == bson_object); ASSERT(!strcmp(bson_iterator_key(&it), "o")); bson_iterator_subiterator(&it, &it2); ASSERT(bson_iterator_more(&it2)); ASSERT(bson_iterator_next(&it2) == bson_array); ASSERT(bson_iterator_type(&it2) == bson_array); ASSERT(!strcmp(bson_iterator_key(&it2), "a")); bson_iterator_subiterator(&it2, &it3); ASSERT(bson_iterator_more(&it3)); ASSERT(bson_iterator_next(&it3) == bson_bindata); ASSERT(bson_iterator_type(&it3) == bson_bindata); ASSERT(!strcmp(bson_iterator_key(&it3), "0")); ASSERT(bson_iterator_bin_type(&it3) == 8); ASSERT(bson_iterator_bin_len(&it3) == 5); ASSERT(!memcmp(bson_iterator_bin_data(&it3), "w\0rld", 5)); ASSERT(bson_iterator_more(&it3)); ASSERT(bson_iterator_next(&it3) == bson_eoo); ASSERT(bson_iterator_type(&it3) == bson_eoo); ASSERT(!bson_iterator_more(&it3)); ASSERT(bson_iterator_more(&it2)); ASSERT(bson_iterator_next(&it2) == bson_eoo); ASSERT(bson_iterator_type(&it2) == bson_eoo); ASSERT(!bson_iterator_more(&it2)); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_undefined); ASSERT(bson_iterator_type(&it) == bson_undefined); ASSERT(!strcmp(bson_iterator_key(&it), "u")); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_oid); ASSERT(bson_iterator_type(&it) == bson_oid); ASSERT(!strcmp(bson_iterator_key(&it), "oid")); ASSERT(!memcmp(bson_iterator_oid(&it)->bytes, "\x001\x002\x003\x004\x005\x006\x007\x008\x009\x010\x011\x012", 12)); ASSERT(bson_iterator_oid(&it)->ints[0] == oid.ints[0]); ASSERT(bson_iterator_oid(&it)->ints[1] == oid.ints[1]); ASSERT(bson_iterator_oid(&it)->ints[2] == oid.ints[2]); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_bool); ASSERT(bson_iterator_type(&it) == bson_bool); ASSERT(!strcmp(bson_iterator_key(&it), "b")); ASSERT(bson_iterator_bool(&it) == 1); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_date); ASSERT(bson_iterator_type(&it) == bson_date); ASSERT(!strcmp(bson_iterator_key(&it), "date")); ASSERT(bson_iterator_date(&it) == 0x0102030405060708ULL); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_null); ASSERT(bson_iterator_type(&it) == bson_null); ASSERT(!strcmp(bson_iterator_key(&it), "n")); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_regex); ASSERT(bson_iterator_type(&it) == bson_regex); ASSERT(!strcmp(bson_iterator_key(&it), "r")); ASSERT(!strcmp(bson_iterator_regex(&it), "^asdf")); ASSERT(!strcmp(bson_iterator_regex_opts(&it), "imx")); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_code); ASSERT(bson_iterator_type(&it) == bson_code); ASSERT(!strcmp(bson_iterator_key(&it), "c")); ASSERT(!strcmp(bson_iterator_string(&it), "function(){}")); ASSERT(!strcmp(bson_iterator_code(&it), "function(){}")); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_symbol); ASSERT(bson_iterator_type(&it) == bson_symbol); ASSERT(!strcmp(bson_iterator_key(&it), "symbol")); ASSERT(!strcmp(bson_iterator_string(&it), "SYMBOL")); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_codewscope); ASSERT(bson_iterator_type(&it) == bson_codewscope); ASSERT(!strcmp(bson_iterator_key(&it), "cws")); ASSERT(!strcmp(bson_iterator_code(&it), "function(){return i}")); { bson scope; bson_iterator_code_scope(&it, &scope); bson_iterator_init(&it2, scope.data); ASSERT(bson_iterator_more(&it2)); ASSERT(bson_iterator_next(&it2) == bson_int); ASSERT(bson_iterator_type(&it2) == bson_int); ASSERT(!strcmp(bson_iterator_key(&it2), "i")); ASSERT(bson_iterator_int(&it2) == 123); ASSERT(bson_iterator_more(&it2)); ASSERT(bson_iterator_next(&it2) == bson_eoo); ASSERT(bson_iterator_type(&it2) == bson_eoo); ASSERT(!bson_iterator_more(&it2)); } ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_long); ASSERT(bson_iterator_type(&it) == bson_long); ASSERT(!strcmp(bson_iterator_key(&it), "l")); ASSERT(bson_iterator_long(&it) == 0x1122334455667788ULL); ASSERT(bson_iterator_more(&it)); ASSERT(bson_iterator_next(&it) == bson_eoo); ASSERT(bson_iterator_type(&it) == bson_eoo); ASSERT(!bson_iterator_more(&it)); return 0; }
INLINE void bson_to_v P2(bson_iterator *, i, svalue_t *, v){ int temp; const char *cp; char oidhex[25]; bson scope; bson_type t; bson_timestamp_t ts; t = bson_iterator_type( i ); switch ( t ) { case BSON_DOUBLE: v->type = T_REAL; v->u.real = bson_iterator_double( i ); break; case BSON_STRING: cp = bson_iterator_string( i ); v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy(cp,"bson_to_v:1"); break; case BSON_SYMBOL: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("SYMBOL","bson_to_v:2"); break; case BSON_OID: bson_oid_to_string( bson_iterator_oid( i ), oidhex ); v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy(oidhex,"bson_to_v:3"); break; case BSON_BOOL: v->type = T_NUMBER; v->u.number = ( long )bson_iterator_bool( i ); break; case BSON_DATE: v->type = T_NUMBER; v->u.number = ( long )bson_iterator_date( i ); break; case BSON_BINDATA: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_BINDATA","bson_to_v:4"); break; case BSON_UNDEFINED: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_UNDEFINED","bson_to_v:5"); break; case BSON_NULL: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_NULL","bson_to_v:6"); break; case BSON_REGEX: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_REGEX","bson_to_v:7"); break; case BSON_CODE: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_CODE","bson_to_v:8"); break; case BSON_CODEWSCOPE: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("BSON_CODEWSCOPE","bson_to_v:9"); break; case BSON_INT: v->type = T_NUMBER; v->u.number = ( long )bson_iterator_int( i ); break; case BSON_LONG: v->type = T_NUMBER; v->u.number = ( long )bson_iterator_long( i ); break; case BSON_TIMESTAMP: ts = bson_iterator_timestamp( i ); v->type = T_NUMBER; v->u.number = ( long )ts.t; break; case BSON_OBJECT: bson_to_mapping( bson_iterator_value( i ) , v); break; case BSON_ARRAY: bson_to_array( bson_iterator_value( i ) , v); break; default: v->type = T_STRING; v->subtype = STRING_MALLOC; v->u.string = string_copy("UNKOWN_TYPE","bson_to_v:10"); break; } }
static void bson_print_xstr(TCXSTR* xstr, const char *data, int depth) { bson_iterator i; const char *key; int temp; bson_timestamp_t ts; char oidhex[25]; bson scope; bson_iterator_from_buffer(&i, data); while (bson_iterator_next(&i)) { bson_type t = bson_iterator_type(&i); if (t == 0) break; key = bson_iterator_key(&i); for (temp = 0; temp <= depth; temp++) { tcxstrprintf(xstr, "."); } tcxstrprintf(xstr, "%s(%d)=", key, t); switch (t) { case BSON_DOUBLE: tcxstrprintf(xstr, "%f", bson_iterator_double(&i)); break; case BSON_STRING: tcxstrprintf(xstr, "%s", bson_iterator_string(&i)); break; case BSON_SYMBOL: tcxstrprintf(xstr, "SYMBOL: %s", bson_iterator_string(&i)); break; case BSON_OID: bson_oid_to_string(bson_iterator_oid(&i), oidhex); tcxstrprintf(xstr, "%s", oidhex); break; case BSON_BOOL: tcxstrprintf(xstr, "%s", bson_iterator_bool(&i) ? "true" : "false"); break; case BSON_DATE: tcxstrprintf(xstr, "%lld", (uint64_t) bson_iterator_long(&i)); break; case BSON_BINDATA: tcxstrprintf(xstr, "BSON_BINDATA"); break; case BSON_UNDEFINED: tcxstrprintf(xstr, "BSON_UNDEFINED"); break; case BSON_NULL: tcxstrprintf(xstr, "BSON_NULL"); break; case BSON_REGEX: tcxstrprintf(xstr, "BSON_REGEX: %s", bson_iterator_regex(&i)); break; case BSON_CODE: tcxstrprintf(xstr, "BSON_CODE: %s", bson_iterator_code(&i)); break; case BSON_CODEWSCOPE: tcxstrprintf(xstr, "BSON_CODE_W_SCOPE: %s", bson_iterator_code(&i)); /* bson_init( &scope ); */ /* review - stepped on by bson_iterator_code_scope? */ bson_iterator_code_scope(&i, &scope); tcxstrprintf(xstr, "\n SCOPE: "); bson_print_xstr(xstr, scope.data, 0); /* bson_destroy( &scope ); */ /* review - causes free error */ break; case BSON_INT: tcxstrprintf(xstr, "%d", bson_iterator_int(&i)); break; case BSON_LONG: tcxstrprintf(xstr, "%lld", (uint64_t) bson_iterator_long(&i)); break; case BSON_TIMESTAMP: ts = bson_iterator_timestamp(&i); tcxstrprintf(xstr, "i: %d, t: %d", ts.i, ts.t); break; case BSON_OBJECT: case BSON_ARRAY: tcxstrprintf(xstr, "\n"); bson_print_xstr(xstr, bson_iterator_value(&i), depth + 1); break; default: fprintf(stderr, "can't print type : %d\n", t); } tcxstrprintf(xstr, "\n"); } }
EXPORT double mongo_bson_iterator_double(struct bson_iterator_* i) { return bson_iterator_double((bson_iterator*)i); }
void CSMatrix<Scalar>::import_from_file(const char *filename, const char *var_name, MatrixExportFormat fmt, bool invert_storage) { this->free(); switch (fmt) { case EXPORT_FORMAT_MATRIX_MARKET: throw Exceptions::MethodNotOverridenException("CSMatrix<Scalar>::import_from_file - Matrix Market"); break; case EXPORT_FORMAT_MATLAB_MATIO: { #ifdef WITH_MATIO mat_t *matfp; matvar_t *matvar; matfp = Mat_Open(filename, MAT_ACC_RDONLY); if (!matfp) throw Exceptions::IOException(Exceptions::IOException::Read, filename); matvar = Mat_VarRead(matfp, var_name); if (matvar) { mat_sparse_t *sparse = (mat_sparse_t *)matvar->data; this->nnz = sparse->nir; this->Ax = malloc_with_check<CSMatrix<Scalar>, Scalar>(this->nnz, this); this->Ai = malloc_with_check<CSMatrix<Scalar>, int>(this->nnz, this); this->size = sparse->njc; this->Ap = malloc_with_check<CSMatrix<Scalar>, int>(this->size + 1, this); void* data = nullptr; if (Hermes::Helpers::TypeIsReal<Scalar>::value) data = sparse->data; else { std::complex<double>* complex_data = malloc_with_check<CSMatrix<Scalar>, std::complex<double> >(this->nnz, this); double* real_array = (double*)((mat_complex_split_t*)sparse->data)->Re; double* imag_array = (double*)((mat_complex_split_t*)sparse->data)->Im; for (int i = 0; i < this->nnz; i++) complex_data[i] = std::complex<double>(real_array[i], imag_array[i]); data = (void*)complex_data; } memcpy(this->Ax, data, this->nnz * sizeof(Scalar)); if (!Hermes::Helpers::TypeIsReal<Scalar>::value) free_with_check(data); memcpy(this->Ap, sparse->jc, this->size * sizeof(int)); this->Ap[this->size] = this->nnz; memcpy(this->Ai, sparse->ir, this->nnz * sizeof(int)); if (invert_storage) this->switch_orientation(); } Mat_Close(matfp); if (!matvar) throw Exceptions::IOException(Exceptions::IOException::Read, filename); #else throw Exceptions::Exception("MATIO not included."); #endif } break; case EXPORT_FORMAT_PLAIN_ASCII: throw Exceptions::MethodNotOverridenException("CSMatrix<Scalar>::import_from_file - Simple format"); #ifdef WITH_BSON case EXPORT_FORMAT_BSON: { FILE *fpr; fpr = fopen(filename, "rb"); // file size: fseek(fpr, 0, SEEK_END); int size = ftell(fpr); rewind(fpr); // allocate memory to contain the whole file: char *datar = malloc_with_check<char>(size); fread(datar, size, 1, fpr); fclose(fpr); bson br; bson_init_finished_data(&br, datar, 0); bson_iterator it; bson sub; bson_find(&it, &br, "size"); this->size = bson_iterator_int(&it); bson_find(&it, &br, "nnz"); this->nnz = bson_iterator_int(&it); this->Ap = malloc_with_check<CSMatrix<Scalar>, int>(this->size + 1, this); this->Ai = malloc_with_check<CSMatrix<Scalar>, int>(nnz, this); this->Ax = malloc_with_check<CSMatrix<Scalar>, Scalar>(nnz, this); // coeffs bson_iterator it_coeffs; bson_find(&it_coeffs, &br, "Ap"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); int index_coeff = 0; while (bson_iterator_next(&it)) this->Ap[index_coeff++] = bson_iterator_int(&it); this->Ap[this->size] = this->nnz; bson_find(&it_coeffs, &br, "Ai"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); index_coeff = 0; while (bson_iterator_next(&it)) this->Ai[index_coeff++] = bson_iterator_int(&it); bson_find(&it_coeffs, &br, "Ax"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); index_coeff = 0; while (bson_iterator_next(&it)) this->Ax[index_coeff++] = bson_iterator_double(&it); if (!Hermes::Helpers::TypeIsReal<Scalar>::value) { bson_find(&it_coeffs, &br, "Ax-imag"); bson_iterator_subobject_init(&it_coeffs, &sub, 0); bson_iterator_init(&it, &sub); index_coeff = 0; while (bson_iterator_next(&it)) ((std::complex<double>)this->Ax[index_coeff++]).imag(bson_iterator_double(&it)); } if (invert_storage) this->switch_orientation(); bson_destroy(&br); free_with_check(datar); } #endif break; } }
void lua_push_bson_value(lua_State *L, bson_iterator *it) { bson_type bt = bson_iterator_type(it); switch (bt) { case BSON_OID: { char xoid[25]; bson_oid_to_string(bson_iterator_oid(it), xoid); lua_pushstring(L, xoid); break; } case BSON_STRING: case BSON_SYMBOL: lua_pushstring(L, bson_iterator_string(it)); break; case BSON_NULL: case BSON_UNDEFINED: lua_push_bsontype_table(L, bt); break; case BSON_INT: lua_pushinteger(L, bson_iterator_int(it)); break; case BSON_LONG: case BSON_DOUBLE: lua_pushnumber(L, (lua_Number) bson_iterator_double(it)); break; case BSON_BOOL: lua_pushboolean(L, bson_iterator_bool(it)); break; case BSON_OBJECT: case BSON_ARRAY: { bson_iterator nit; bson_iterator_subiterator(it, &nit); if (bt == BSON_OBJECT) { lua_push_bson_table(L, &nit); } else { lua_push_bson_array(L, &nit); } break; } case BSON_DATE: { lua_push_bsontype_table(L, bt); lua_pushnumber(L, bson_iterator_date(it)); lua_rawseti(L, -2, 1); break; } case BSON_BINDATA: { lua_push_bsontype_table(L, bt); lua_pushlstring(L, bson_iterator_bin_data(it), bson_iterator_bin_len(it)); break; } case BSON_REGEX: { const char *re = bson_iterator_regex(it); const char *ro = bson_iterator_regex_opts(it); lua_push_bsontype_table(L, bt); lua_pushstring(L, re); lua_rawseti(L, -2, 1); lua_pushstring(L, ro); lua_rawseti(L, -2, 2); break; } default: break; } }