static void test_bson_oid_init_from_string (void) { bson_context_t context; bson_oid_t oid; char str[25]; int i; bson_context_init(&context, BSON_CONTEXT_NONE); /* * Test successfully parsed oids. */ for (i = 0; gTestOids[i]; i++) { bson_oid_init_from_string(&oid, gTestOids[i]); bson_oid_to_string(&oid, str); assert(!strcmp(str, gTestOids[i])); } /* * Test the failures. */ for (i = 0; gTestOidsFail[i]; i++) { bson_oid_init_from_string(&oid, gTestOidsFail[i]); bson_oid_to_string(&oid, str); assert(strcmp(str, gTestOidsFail[i])); } }
static void test_bson_oid_init_from_string (void) { bson_context_t *context; bson_oid_t oid; char str[25]; int i; context = bson_context_new (BSON_CONTEXT_NONE); /* * Test successfully parsed oids. */ for (i = 0; gTestOids[i]; i++) { bson_oid_init_from_string (&oid, gTestOids[i]); bson_oid_to_string (&oid, str); BSON_ASSERT (!strcmp (str, gTestOids[i])); } /* * Test successfully parsed oids (case-insensitive). */ for (i = 0; gTestOidsCase[i]; i++) { char oid_lower[25]; int j; bson_oid_init_from_string (&oid, gTestOidsCase[i]); bson_oid_to_string (&oid, str); BSON_ASSERT (!bson_strcasecmp (str, gTestOidsCase[i])); for (j = 0; gTestOidsCase[i][j]; j++) { oid_lower[j] = tolower (gTestOidsCase[i][j]); } oid_lower[24] = '\0'; BSON_ASSERT (!strcmp (str, oid_lower)); } /* * Test that sizeof(str) works (len of 25 with \0 instead of 24). */ BSON_ASSERT (bson_oid_is_valid ("ffffffffffffffffffffffff", 24)); bson_oid_init_from_string (&oid, "ffffffffffffffffffffffff"); bson_oid_to_string (&oid, str); BSON_ASSERT (bson_oid_is_valid (str, sizeof str)); /* * Test the failures. */ for (i = 0; gTestOidsFail[i]; i++) { bson_oid_init_from_string (&oid, gTestOidsFail[i]); bson_oid_to_string (&oid, str); BSON_ASSERT (strcmp (str, gTestOidsFail[i])); } bson_context_destroy (context); }
static void test_bson_oid_copy (void) { bson_oid_t oid; bson_oid_t oid2; bson_oid_init_from_string(&oid, "000000000000000000001234"); bson_oid_init_from_string(&oid2, "000000000000000000004321"); bson_oid_copy(&oid, &oid2); assert(TRUE == bson_oid_equal(&oid, &oid2)); }
static void test_bson_oid_copy (void) { bson_oid_t oid; bson_oid_t oid2; bson_oid_init_from_string (&oid, "000000000000000000001234"); bson_oid_init_from_string (&oid2, "000000000000000000004321"); bson_oid_copy (&oid, &oid2); BSON_ASSERT (true == bson_oid_equal (&oid, &oid2)); }
oid::oid(stdx::string_view str) : _is_valid(bson_oid_is_valid(str.data(), str.length())) { if (_is_valid) { bson_oid_t oid; bson_oid_init_from_string(&oid, str.data()); memcpy(_bytes, oid.bytes, sizeof(_bytes)); } }
static void test_bson_oid_hash (void) { bson_oid_t oid; bson_oid_init_from_string (&oid, "000000000000000000000000"); BSON_ASSERT (bson_oid_hash (&oid) == 1487062149); }
static void context_init (context_t *context) { bson_init (&context->events); context->n_events = 0; bson_oid_init_from_string (&context->topology_id, "000000000000000000000000"); }
static void test_bson_oid_compare (void) { bson_oid_t oid; bson_oid_t oid2; bson_oid_init_from_string (&oid, "000000000000000000001234"); bson_oid_init_from_string (&oid2, "000000000000000000001234"); BSON_ASSERT (0 == bson_oid_compare (&oid, &oid2)); BSON_ASSERT (true == bson_oid_equal (&oid, &oid2)); bson_oid_init_from_string (&oid, "000000000000000000001234"); bson_oid_init_from_string (&oid2, "000000000000000000004321"); BSON_ASSERT (bson_oid_compare (&oid, &oid2) < 0); BSON_ASSERT (bson_oid_compare (&oid2, &oid) > 0); BSON_ASSERT (false == bson_oid_equal (&oid, &oid2)); }
static void test_bson_oid_hash (void) { bson_oid_t oid; bson_oid_init_from_string(&oid, "000000000000000000000000"); assert(bson_oid_hash(&oid) == 1487062149); }
static void test_bson_oid_compare (void) { bson_oid_t oid; bson_oid_t oid2; bson_oid_init_from_string(&oid, "000000000000000000001234"); bson_oid_init_from_string(&oid2, "000000000000000000001234"); assert(0 == bson_oid_compare(&oid, &oid2)); assert(TRUE == bson_oid_equal(&oid, &oid2)); bson_oid_init_from_string(&oid, "000000000000000000001234"); bson_oid_init_from_string(&oid2, "000000000000000000004321"); assert(bson_oid_compare(&oid, &oid2) < 0); assert(bson_oid_compare(&oid2, &oid) > 0); assert(FALSE == bson_oid_equal(&oid, &oid2)); }
static void test_bson_as_json (void) { bson_oid_t oid; bson_decimal128_t decimal128; bson_t *b; bson_t *b2; char *str; size_t len; int i; decimal128.high = 0x3040000000000000ULL; decimal128.low = 0x000000000000000B; bson_oid_init_from_string(&oid, "123412341234abcdabcdabcd"); b = bson_new(); assert(bson_append_utf8(b, "utf8", -1, "bar", -1)); assert(bson_append_int32(b, "int32", -1, 1234)); assert(bson_append_int64(b, "int64", -1, 4321)); assert(bson_append_double(b, "double", -1, 123.4)); assert(bson_append_undefined(b, "undefined", -1)); assert(bson_append_null(b, "null", -1)); assert(bson_append_oid(b, "oid", -1, &oid)); assert(bson_append_bool(b, "true", -1, true)); assert(bson_append_bool(b, "false", -1, false)); assert(bson_append_time_t(b, "date", -1, time(NULL))); assert(bson_append_timestamp(b, "timestamp", -1, (uint32_t)time(NULL), 1234)); assert(bson_append_regex(b, "regex", -1, "^abcd", "xi")); assert(bson_append_dbpointer(b, "dbpointer", -1, "mycollection", &oid)); assert(bson_append_minkey(b, "minkey", -1)); assert(bson_append_maxkey(b, "maxkey", -1)); assert(bson_append_symbol(b, "symbol", -1, "var a = {};", -1)); assert(bson_append_decimal128(b, "decimal128", -1, &decimal128)); b2 = bson_new(); assert(bson_append_int32(b2, "0", -1, 60)); assert(bson_append_document(b, "document", -1, b2)); assert(bson_append_array(b, "array", -1, b2)); { const uint8_t binary[] = { 0, 1, 2, 3, 4 }; assert(bson_append_binary(b, "binary", -1, BSON_SUBTYPE_BINARY, binary, sizeof binary)); } for (i = 0; i < 1000; i++) { str = bson_as_json(b, &len); bson_free(str); } bson_destroy(b); bson_destroy(b2); }
static void test_bson_append_dbpointer (void) { bson_oid_t oid; bson_t *b; bson_t *b2; b = bson_new(); bson_oid_init_from_string(&oid, "0123abcd0123abcd0123abcd"); assert(bson_append_dbpointer(b, "dbpointer", -1, "foo", &oid)); b2 = get_bson("test28.bson"); assert_bson_equal(b, b2); bson_destroy(b); bson_destroy(b2); }
static void test_bson_append_oid (void) { bson_oid_t oid; bson_t *b; bson_t *b2; bson_oid_init_from_string(&oid, "1234567890abcdef1234abcd"); b = bson_new(); assert(bson_append_oid(b, "oid", -1, &oid)); b2 = get_bson("test22.bson"); assert_bson_equal(b, b2); bson_destroy(b); bson_destroy(b2); }
static void topology_opening (const mongoc_apm_topology_opening_t *event) { context_t *ctx; bson_oid_t zeroes; /* new event's topology id is NOT all zeroes */ bson_oid_init_from_string (&zeroes, "000000000000000000000000"); ASSERT (!bson_oid_equal (&event->topology_id, &zeroes)); ctx = (context_t *) mongoc_apm_topology_opening_get_context (event); mongoc_apm_topology_opening_get_topology_id (event, &ctx->topology_id); context_append (ctx, BCON_NEW ("topology_opening_event", "{", "topologyId", BCON_UTF8 ("42"), "}")); }
void HHVM_METHOD(MongoDBBsonObjectID, __construct, const Variant &objectId) { MongoDBBsonObjectIDData* data = Native::data<MongoDBBsonObjectIDData>(this_); const String& str_objectId = objectId.isNull() ? null_string : objectId.toString(); if (!objectId.isNull()) { if (bson_oid_is_valid((HHVM_FN(strtolower)(str_objectId)).c_str(), str_objectId.length())) { bson_oid_init_from_string(&data->m_oid, (HHVM_FN(strtolower)(str_objectId)).c_str()); } else { throw MongoDriver::Utils::throwInvalidArgumentException("Error parsing ObjectID string: " + str_objectId); } } else { bson_oid_init(&data->m_oid, NULL); } this_->o_set(s_MongoBsonObjectID_oid, Variant(oidAsString(data)), s_MongoBsonObjectID_className); }
void updateAlarm(mongoc_collection_t *coll,char* se_id,int interval,int ialarm){ time_t timep; bson_t *query; bson_oid_t se_oid; bson_oid_init_from_string(&se_oid, se_id); time(&timep); bson_error_t error; bson_t reply; query = bson_new (); BSON_APPEND_OID (query, "_id",&se_oid); bson_t *update = BCON_NEW ("$set", "{", "alarmtime",BCON_DOUBLE(timep),"interval",BCON_INT32(interval),"balarm",BCON_INT32(ialarm),"}"); if (!mongoc_collection_find_and_modify (coll, query, NULL, update, NULL, false, false, true, &reply, &error)) { //fprintf (stderr, "find_and_modify() failure: %s\n", error.message); } bson_destroy (query); bson_destroy (update); }
void getPhone(mongoc_collection_t *coll,char * contactId,char *mobile){ mongoc_cursor_t *cursor; bson_t *doc; bson_t *query; char *str; query = bson_new (); bson_oid_t se_oid; bson_oid_init_from_string(&se_oid, contactId); BSON_APPEND_OID (query, "_id",&se_oid); cursor = mongoc_collection_find (coll, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL); double ivalue=0; int buse=0; while (mongoc_cursor_next (cursor, &doc)) { str = bson_as_json (doc, NULL); //printf ("%s\n", str); bson_free (str); bson_iter_t iter; bson_iter_t sub_iter; if (bson_iter_init (&iter, doc)) { while (bson_iter_next (&iter)) { //printf ("Found a field named: %s\n", bson_iter_key (&iter)); bson_value_t *value; value = bson_iter_value (&iter); //printf("type %d\n",value->value_type); if (value->value_type == BSON_TYPE_UTF8) { if(strcmp(bson_iter_key (&iter),"mobile")==0){ //printf("value %d\n", (int)value->value.v_double); strcpy(mobile,value->value.v_utf8.str); } } } } } bson_destroy (query); }
void mongoIdToBSON(const Object& value, const char* key, bson_t* bson) { bson_oid_t oid; bson_oid_init_from_string(&oid, value->o_get("$id").toString().c_str()); bson_append_oid(bson, key, -1, &oid); }
void BsonOidFromString(bson_oid_t *o, char* str) { bson_oid_init_from_string(o, str); }
static void getaddressbyID(char *msg,mongoc_collection_t *coll,char * deviceId,char * device_name){ mongoc_cursor_t *cursor; bson_t *doc; bson_t *query; char *str; query = bson_new (); bson_oid_t de_oid; bson_oid_init_from_string(&de_oid, deviceId); BSON_APPEND_OID (query, "_id",&de_oid); cursor = mongoc_collection_find (coll, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL); double ivalue=0; int buse=0; double lat; double lon; //char lat[256]; //char lon[256]; //memset(lat,'\0',256); //memset(lon,'\0',256); while (mongoc_cursor_next (cursor, &doc)) { str = bson_as_json (doc, NULL); //printf ("%s\n", str); bson_free (str); bson_iter_t iter; bson_iter_t sub_iter; if (bson_iter_init (&iter, doc)) { while (bson_iter_next (&iter)) { //printf ("Found a field named: %s\n", bson_iter_key (&iter)); bson_value_t *value; value = bson_iter_value (&iter); //printf("type %d\n",value->value_type); if (value->value_type == BSON_TYPE_DOUBLE) { if(strcmp(bson_iter_key (&iter),"lat")==0){ (lat=value->value.v_double); } if(strcmp(bson_iter_key (&iter),"lon")==0){ (lon=value->value.v_double); } } if (value->value_type == BSON_TYPE_UTF8) { if(strcmp(bson_iter_key (&iter),"device_name")==0){ strcpy(device_name,value->value.v_utf8.str); } } } } } bson_destroy (query); CURL *curl; CURLcode res; curl = curl_easy_init(); if (curl) { static char str[20480]; memset(str,'\0',20480); char url[1028]; memset(url,'\0',1028); sprintf(url,"http://api.map.baidu.com/geocoder/v2/?ak=IDvNBsejl9oqMbPF316iKsXR&output=json&location=%.8f,%.8f",lat,lon); printf("%s\n",url); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, str); res = curl_easy_perform(curl); printf("%s\n",str); char *s = strstr(str, "formatted_address"); if(NULL!=s){ s=strstr(s, "\""); s=strstr(s+1, "\""); printf("\n%s\n",s); char *p=strstr(s+1, "\""); int len=strlen(s)-strlen(p)-1; strncpy(msg,s+1,len); msg[len]='\0'; printf("msg:%s\n",msg); printf("\n\n"); } curl_easy_cleanup(curl); } }
static void do_alarm(mongoc_collection_t *sensorcoll,char * se_id,char * alarmType,char * upperBoundC,char *lowerBoundC, char *duration,char *target,char * contactId,char* addr,mongoc_collection_t *alarmlogcoll,char* userId,char *device_name,char * de_id){ mongoc_cursor_t *cursor; bson_t *doc; bson_t *query; char *str; query = bson_new (); bson_oid_t se_oid; bson_oid_init_from_string(&se_oid, se_id); BSON_APPEND_OID (query, "_id",&se_oid); cursor = mongoc_collection_find (sensorcoll, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL); double ivalue=0; double dalarmtime=0; int interval=0; int balarm=0; int buse=0; char name[1024]; memset(name,0,1024); while (mongoc_cursor_next (cursor, &doc)) { str = bson_as_json (doc, NULL); printf ("%s\n", str); bson_free (str); bson_iter_t iter; bson_iter_t sub_iter; if (bson_iter_init (&iter, doc)) { while (bson_iter_next (&iter)) { //printf ("Found a field named: %s\n", bson_iter_key (&iter)); bson_value_t *value; value = bson_iter_value (&iter); //printf("type %d\n",value->value_type); if (value->value_type == BSON_TYPE_INT32) { if(strcmp(bson_iter_key (&iter),"value")==0){ printf("value %d\n", value->value.v_int32); ivalue=value->value.v_double; buse =1; }else if(strcmp(bson_iter_key (&iter),"interval")==0){ interval=value->value.v_int32; }else if(strcmp(bson_iter_key (&iter),"balarm")==0){ balarm=value->value.v_int32; } } if(strcmp(bson_iter_key (&iter),"value")==0){ printf("value type %d,%d\n",value->value_type,BSON_TYPE_INT32); } if (value->value_type == BSON_TYPE_DOUBLE) { if(strcmp(bson_iter_key (&iter),"value")==0){ //printf("value %d\n", (int)value->value.v_double); buse =1; ivalue=value->value.v_double; } if(strcmp(bson_iter_key (&iter),"alarmtime")==0){ dalarmtime=value->value.v_double; } } if (value->value_type == BSON_TYPE_UTF8) { if(strcmp(bson_iter_key (&iter),"name")==0){ //printf("value %d\n", (int)value->value.v_double); strcpy(name,value->value.v_utf8.str); } if(strcmp(bson_iter_key (&iter),"value")==0){ //printf("value %d\n", (int)value->value.v_double); ivalue=atof(value->value.v_utf8.str); buse =1; } } } } } bson_destroy (query); printf("se_id:%s,buse:%d\n",se_id,buse); printf("alarmType:%s,%f : %s,%s",alarmType,ivalue,upperBoundC,lowerBoundC); time_t t; t = time(NULL); struct tm *lt; int ii = time(&t); int bdoalarm=1; if(balarm==1){ bdoalarm=1; if(dalarmtime>0 &&interval>0){ if((ii-dalarmtime)<3600){ bdoalarm=0; } } } if(buse ==1){ if(strcmp(alarmType,"val_above")==0){ int iupper=atoi(upperBoundC); if(iupper<ivalue){ printf("val_above %d value\n",iupper,ivalue); char msg[2048]; memset(msg,0,2048); snprintf(msg, sizeof(msg) - 1, "故障报警:%s的%s数值高于%d,位置是%s,请及时处理!", device_name, name, iupper, addr); char buf[1024]; URLEncodeGBK(msg, strlen(msg), buf,sizeof(buf)); printf("%s\n",buf); printf("alarm info: %s\n",msg); if(bdoalarm>0){ int code=sendsms_c(buf,contactId); saveAlarmLog(alarmlogcoll,se_id,code,contactId,msg,userId,device_name,name,de_id); updateAlarm(sensorcoll,se_id,interval+1,1); printf("send sms \n"); } }else { updateAlarm(sensorcoll,se_id,0,0); } }else if(strcmp(alarmType,"val_below")==0){ int iupper=atoi(lowerBoundC); if(iupper>ivalue){ printf("val_below %d value %d\n",iupper,ivalue); char msg[2048]; memset(msg,0,2048); snprintf(msg, sizeof(msg) - 1, "故障报警:%s的%s数值低于%d,位置是%s,请及时处理!", device_name, name, iupper, addr); char buf[1024]; URLEncodeGBK(msg, strlen(msg), buf,sizeof(buf)); printf("%s\n",buf); printf("alarm info: %s\n",msg); if(bdoalarm>0){ int code=sendsms_c(buf,contactId); saveAlarmLog(alarmlogcoll,se_id,code,contactId,msg,userId,device_name,name,de_id); updateAlarm(sensorcoll,se_id,interval+1,1); printf("send sms \n"); } }else { updateAlarm(sensorcoll,se_id,0,0); } }else if(strcmp(alarmType,"switch_on")==0){ if(ivalue==1){ char msg[2048]; memset(msg,0,2048); snprintf(msg, sizeof(msg) - 1, "故障报警:%s,%s,位置是%s,请及时处理!", device_name, name, addr); char buf[1024]; URLEncodeGBK(msg, strlen(msg), buf,sizeof(buf)); printf("%s\n",buf); printf("alarm info: %s\n",msg); if(bdoalarm>0){ int code=sendsms_c(buf,contactId); saveAlarmLog(alarmlogcoll,se_id,code,contactId,msg,userId,device_name,name,de_id); updateAlarm(sensorcoll,se_id,interval+1,1); printf("send sms \n"); } }else { updateAlarm(sensorcoll,se_id,0,0); } }else if(strcmp(alarmType,"offline")==0){ if(ivalue==1){ char msg[2048]; memset(msg,0,2048); snprintf(msg, sizeof(msg) - 1, "故障报警:%s,%s,位置是%s,请及时处理!", device_name, name, addr); char buf[1024]; URLEncodeGBK(msg, strlen(msg), buf,sizeof(buf)); printf("%s\n",buf); printf("alarm info: %s\n",msg); if(bdoalarm>0){ int code=sendsms_c(buf,contactId); saveAlarmLog(alarmlogcoll,se_id,code,contactId,msg,userId,device_name,name,de_id); updateAlarm(sensorcoll,se_id,interval+1,1); printf("send sms \n"); } }else { updateAlarm(sensorcoll,se_id,0,0); } } } }
static bool appendBsonValue(bson_t *bson, const QString &key, const QVariant &value) { const QLatin1String oidkey("_id"); bool ok = true; int type = value.type(); // _id if (key == oidkey) { QByteArray oidVal = value.toByteArray(); if (oidVal.length() == 24) { // ObjectId bson_oid_t oid; bson_oid_init_from_string(&oid, oidVal.data()); BSON_APPEND_OID(bson, oidkey.latin1(), &oid); } else { int id = value.toInt(&ok); if (ok) { BSON_APPEND_INT32(bson, oidkey.latin1(), id); } else { BSON_APPEND_UTF8(bson, oidkey.latin1(), value.toString().toUtf8().data()); } } return true; } switch (type) { case QVariant::Int: BSON_APPEND_INT32(bson, qPrintable(key), value.toInt(&ok)); break; case QVariant::String: BSON_APPEND_UTF8(bson, qPrintable(key), value.toString().toUtf8().data()); break; case QVariant::LongLong: BSON_APPEND_INT64(bson, qPrintable(key), value.toLongLong(&ok)); break; case QVariant::Map: BSON_APPEND_DOCUMENT(bson, qPrintable(key), (const bson_t *)TBson::toBson(value.toMap()).constData()); break; case QVariant::Double: BSON_APPEND_DOUBLE(bson, qPrintable(key), value.toDouble(&ok)); break; case QVariant::Bool: BSON_APPEND_BOOL(bson, qPrintable(key), value.toBool()); break; case QVariant::DateTime: { #if QT_VERSION >= 0x040700 BSON_APPEND_DATE_TIME(bson, qPrintable(key), value.toDateTime().toMSecsSinceEpoch()); #else QDateTime utcDate = value.toDateTime().toUTC(); qint64 ms = utcDate.time().msec(); qint64 tm = utcDate.toTime_t() * 1000LL; if (ms > 0) { tm += ms; } BSON_APPEND_DATE_TIME(bson, qPrintable(key), tm); #endif break; } case QVariant::ByteArray: { QByteArray ba = value.toByteArray(); BSON_APPEND_BINARY(bson, qPrintable(key), BSON_SUBTYPE_BINARY, (uint8_t *)ba.constData(), ba.length()); break; } case QVariant::List: // FALL THROUGH case QVariant::StringList: { bson_t child; BSON_APPEND_ARRAY_BEGIN(bson, qPrintable(key), &child); int i = 0; for (auto &var : value.toList()) { appendBsonValue(&child, QString::number(i++), var); } bson_append_array_end(bson, &child); break; } case QVariant::Invalid: BSON_APPEND_UNDEFINED(bson, qPrintable(key)); break; default: tError("toBson() failed to convert name:%s type:%d", qPrintable(key), type); ok = false; break; } return ok; }
void switch_value(lua_State *L, int index, bson_t* bson, int level, const char* key) { switch(lua_type(L, index)) { case LUA_TTABLE: { int is_a=is_array(L, index); if (is_a) { bson_t child; //start array BSON_APPEND_ARRAY_BEGIN(bson, key, &child); iterate_table(L, index, &child, 0, level+1, NULL); bson_append_array_end(bson, &child); } else { bson_t child; //start map BSON_APPEND_DOCUMENT_BEGIN(bson, key, &child); iterate_table(L, index, &child, 1, level+1, NULL); bson_append_document_end(bson, &child); } break; } case LUA_TNIL: { BSON_APPEND_NULL(bson, key); break; } case LUA_TNUMBER: { BSON_APPEND_DOUBLE(bson, key, lua_tonumber(L, index)); break; } case LUA_TBOOLEAN: { BSON_APPEND_BOOL(bson, key, lua_toboolean(L, index)); break; } case LUA_TSTRING: { BSON_APPEND_UTF8(bson, key, lua_tostring(L, index)); break; } case LUA_TUSERDATA: { // switch userdata type if (luaL_checkudata_ex(L, index, REGEX_METATABLE)) { cbson_regex_t* regex = check_cbson_regex(L, index); BSON_APPEND_REGEX (bson, key, regex->regex, regex->options); } else if (luaL_checkudata_ex(L, index, OID_METATABLE)) { cbson_oid_t* oid = check_cbson_oid(L, index); bson_oid_t boid; bson_oid_init_from_string (&boid, oid->oid); BSON_APPEND_OID (bson, key, &boid); } else if (luaL_checkudata_ex(L, index, BINARY_METATABLE)) { cbson_binary_t* bin = check_cbson_binary(L, index); size_t binary_len = b64_pton (bin->data, NULL, 0); unsigned char* buf=malloc(binary_len+1); b64_pton(bin->data, buf, binary_len+1); BSON_APPEND_BINARY(bson, key, bin->type, buf, binary_len); free(buf); } else if (luaL_checkudata_ex(L, index, SYMBOL_METATABLE)) { cbson_symbol_t* sym = check_cbson_symbol(L, index); BSON_APPEND_SYMBOL(bson, key, sym->symbol); } else if (luaL_checkudata_ex(L, index, REF_METATABLE)) { cbson_ref_t* ref = check_cbson_ref(L, index); bson_oid_t boid; bson_oid_init_from_string (&boid, ref->id); BSON_APPEND_DBPOINTER(bson, key, ref->ref, &boid); } else if (luaL_checkudata_ex(L, index, MINKEY_METATABLE)) { check_cbson_minkey(L, index); BSON_APPEND_MINKEY(bson, key); } else if (luaL_checkudata_ex(L, index, MAXKEY_METATABLE)) { check_cbson_maxkey(L, index); BSON_APPEND_MAXKEY(bson, key); } else if (luaL_checkudata_ex(L, index, TIMESTAMP_METATABLE)) { cbson_timestamp_t* time = check_cbson_timestamp(L, index); BSON_APPEND_TIMESTAMP(bson, key, time->timestamp, time->increment); } else if (luaL_checkudata_ex(L, index, INT64_METATABLE) || luaL_checkudata_ex(L, index, UINT64_METATABLE)) { cbson_int64_t i = cbson_int64_check(L, index); if (i < INT32_MIN || i > INT32_MAX) { BSON_APPEND_INT64(bson, key, i); } else { BSON_APPEND_INT32(bson, key, (int32_t)i); } } else if (luaL_checkudata_ex(L, index, CODE_METATABLE)) { cbson_code_t* code = check_cbson_code(L, index); BSON_APPEND_CODE(bson, key, code->code); } else if (luaL_checkudata_ex(L, index, CODEWSCOPE_METATABLE)) { cbson_codewscope_t* code = check_cbson_codewscope(L, index); BSON_APPEND_CODE_WITH_SCOPE(bson, key, code->code, NULL); } else if (luaL_checkudata_ex(L, index, UNDEFINED_METATABLE)) { check_cbson_undefined(L, index); BSON_APPEND_UNDEFINED(bson, key); } else if (luaL_checkudata_ex(L, index, DATE_METATABLE)) { BSON_APPEND_DATE_TIME(bson, key, cbson_date_check(L, index)); } break; } case LUA_TFUNCTION: case LUA_TTHREAD: case LUA_TLIGHTUSERDATA: default: break; // or bail out? } }
static int mongodb_load_module(int reload) { int res = -1; struct ast_config *cfg = NULL; mongoc_uri_t *uri = NULL; do { const char *tmp; struct ast_variable *var; struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; cfg = ast_config_load(CONFIG_FILE, config_flags); if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { ast_log(LOG_WARNING, "unable to load config file=%s\n", CONFIG_FILE); res = AST_MODULE_LOAD_DECLINE; break; } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) break; var = ast_variable_browse(cfg, CATEGORY); if (!var) { ast_log(LOG_WARNING, "no category specified.\n"); break; } if ((tmp = ast_variable_retrieve(cfg, CATEGORY, URI)) == NULL) { ast_log(LOG_WARNING, "no uri specified.\n"); break; } uri = mongoc_uri_new(tmp); if (uri == NULL) { ast_log(LOG_ERROR, "parsing uri error, %s\n", tmp); break; } if ((tmp = ast_variable_retrieve(cfg, CATEGORY, DATABSE)) == NULL) { ast_log(LOG_WARNING, "no database specified.\n"); break; } if (dbname) ast_free(dbname); dbname = ast_strdup(tmp); if (dbname == NULL) { ast_log(LOG_ERROR, "not enough memory for dbname\n"); break; } if ((tmp = ast_variable_retrieve(cfg, CATEGORY, COLLECTION)) == NULL) { ast_log(LOG_WARNING, "no collection specified.\n"); break; } if (dbcollection) ast_free(dbcollection); dbcollection = ast_strdup(tmp); if (dbcollection == NULL) { ast_log(LOG_ERROR, "not enough memory for dbcollection\n"); break; } if (!ast_test_flag(&config, CONFIG_REGISTERED)) { res = ast_cdr_register(NAME, ast_module_info->description, mongodb_log); if (res) { ast_log(LOG_ERROR, "unable to register CDR handling\n"); break; } ast_set_flag(&config, CONFIG_REGISTERED); } if ((tmp = ast_variable_retrieve(cfg, CATEGORY, SERVERID)) != NULL) { if (!bson_oid_is_valid (tmp, strlen(tmp))) { ast_log(LOG_ERROR, "invalid server id specified.\n"); break; } serverid = ast_malloc(sizeof(bson_oid_t)); if (serverid == NULL) { ast_log(LOG_ERROR, "not enough memory\n"); break; } bson_oid_init_from_string(serverid, tmp); } if (dbpool) mongoc_client_pool_destroy(dbpool); dbpool = mongoc_client_pool_new(uri); if (dbpool == NULL) { ast_log(LOG_ERROR, "cannot make a connection pool for MongoDB\n"); break; } res = 0; // suceess } while (0); if (uri) mongoc_uri_destroy(uri); if (ast_test_flag(&config, CONFIG_REGISTERED) && (!cfg || dbname == NULL || dbcollection == NULL)) { ast_cdr_backend_suspend(NAME); ast_clear_flag(&config, CONFIG_REGISTERED); } else ast_cdr_backend_unsuspend(NAME); if (cfg && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) ast_config_destroy(cfg); return res; }
static int _bson_json_read_string (void *_ctx, /* IN */ const unsigned char *val, /* IN */ size_t vlen) /* IN */ { bson_json_read_state_t rs; bson_json_read_bson_state_t bs; BASIC_YAJL_CB_PREAMBLE; rs = bson->read_state; bs = bson->bson_state; if (rs == BSON_JSON_REGULAR) { bson_append_utf8 (STACK_BSON_CHILD, key, (int)len, (const char *)val, (int)vlen); } else if (rs == BSON_JSON_IN_BSON_TYPE || rs == BSON_JSON_IN_BSON_TYPE_TIMESTAMP_VALUES) { const char *val_w_null; _bson_json_buf_set (&bson->bson_type_buf[2], val, vlen, true); val_w_null = (const char *)bson->bson_type_buf[2].buf; switch (bs) { case BSON_JSON_LF_REGEX: bson->bson_type_data.regex.has_regex = true; _bson_json_buf_set (&bson->bson_type_buf[0], val, vlen, true); break; case BSON_JSON_LF_OPTIONS: bson->bson_type_data.regex.has_options = true; _bson_json_buf_set (&bson->bson_type_buf[1], val, vlen, true); break; case BSON_JSON_LF_OID: if (vlen != 24) { goto BAD_PARSE; } bson->bson_type_data.oid.has_oid = true; bson_oid_init_from_string (&bson->bson_type_data.oid.oid, val_w_null); break; case BSON_JSON_LF_TYPE: bson->bson_type_data.binary.has_subtype = true; #ifdef _WIN32 # define SSCANF sscanf_s #else # define SSCANF sscanf #endif if (SSCANF (val_w_null, "%02x", &bson->bson_type_data.binary.type) != 1) { goto BAD_PARSE; } #undef SSCANF break; case BSON_JSON_LF_BINARY: { /* TODO: error handling for pton */ int binary_len; bson->bson_type_data.binary.has_binary = true; binary_len = b64_pton (val_w_null, NULL, 0); _bson_json_buf_ensure (&bson->bson_type_buf[0], binary_len + 1); b64_pton ((char *)bson->bson_type_buf[2].buf, bson->bson_type_buf[0].buf, binary_len + 1); bson->bson_type_buf[0].len = binary_len; break; } case BSON_JSON_LF_REF: bson->bson_type_data.ref.has_ref = true; _bson_json_buf_set (&bson->bson_type_buf[0], val, vlen, true); break; case BSON_JSON_LF_ID: if (vlen != 24) { goto BAD_PARSE; } bson->bson_type_data.ref.has_id = true; bson_oid_init_from_string (&bson->bson_type_data.ref.id, val_w_null); break; case BSON_JSON_LF_INT64: { int64_t v64; char *endptr = NULL; errno = 0; v64 = bson_ascii_strtoll ((const char *)val, &endptr, 10); if (((v64 == INT64_MIN) || (v64 == INT64_MAX)) && (errno == ERANGE)) { goto BAD_PARSE; } if (endptr != ((const char *)val + vlen)) { goto BAD_PARSE; } bson_append_int64 (STACK_BSON_CHILD, key, (int)len, v64); } break; case BSON_JSON_LF_DATE: case BSON_JSON_LF_TIMESTAMP_T: case BSON_JSON_LF_TIMESTAMP_I: case BSON_JSON_LF_UNDEFINED: case BSON_JSON_LF_MINKEY: case BSON_JSON_LF_MAXKEY: default: goto BAD_PARSE; } return 1; BAD_PARSE: _bson_json_read_set_error (reader, "Invalid input string %s, looking for %d", val_w_null, bs); return 0; } else { _bson_json_read_set_error (reader, "Invalid state to look for string %d", rs); return 0; } return 1; }
oid::oid(stdx::string_view str) : _is_valid(bson_oid_is_valid(str.data(), str.length())) { if (_is_valid) { bson_oid_t oid; bson_oid_init_from_string(&oid, str.data()); } }