QT中对BSON的处理

QT551中对BSON的处理:
http://api.mongodb.org/libbson/current/
http://api.mongodb.org/libbson/

BSON C 学习笔记:http://blog.csdn.net/pk124729136/article/details/41555659

http://blog.csdn.net/mafuli007/article/details/7317258
bson-compat.h 中要更改一句: 
//# include //更改成下面一句  因为我的环境是VS2010的,它和VS2010冲突了,编译不过,所以用下面的一句,就可以编译过了
不然会出现类似 1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\winsock2.h(2370): error C2375: “WSAAsyncGetServByPort”: 重定义;不同的链接
1>          C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\winsock.h(901) : 参见“WSAAsyncGetServByPort”的声明 的编译错误
# include

libbson 下载: http://api.mongodb.org/libbson/1.2.0/installing.html#build-yourself  里面有下载页面或者 从 CSDN的资源页:http://download.csdn.net/detail/ljt350740378/9457703
下载下来后,先解压到当前文件夹,然后用CMAKE来生成VS2010的工程 (CMAKE:下载地址 http://cmake.org/   或者从 CSDN的资源页:http://download.csdn.net/detail/ljt350740378/9457715)
用CMAKE 源码路径 : D:/softexe/libbson-1.2.2   目标路径:  D:/softexe/libbson-1.2.2/vs2010
1. bson_shared这个工程生成了  libbson-1.0.dll 把这个DLL放到EXE同级目录下面。
2. bson_static 这个工程生成了  libbson-1.0.lib 把这个lib链接 到你的工程里面。 --- 这2个工程还有 test-libbson 就是我们关心的内容,其它 的不用关心,也没有什么用。
3. 把 D:\softexe\libbson-1.2.2\src\bson 里面的H文件还有 D:\softexe\libbson-1.2.2\vs2010\src\bson 下面的 H文件添加到你的工程里面
4. 在工程源码中  #include "includeFiles/BSON/include/bson.h" 
5. 使用的时候的示例代码:
 

// 读取 .xml 同级目录下面的其它 文件,路径为 m_resPath,并把值存放到MAP中
void readAllFile(string &filename)
{
 
  
#if 1
    m_fileMap.clear();
    QDir dir(filename.c_str());
    //    QStringList filters;
    //    filters << "*.xml";
    //    dir.setNameFilters(filters);
    QStringList files=dir.entryList(QDir::Filter::Files,
                                    QDir::SortFlag::NoSort);
    int fileCnt = files.size();
    //qDebug()<<"fileCnt"<
    foreach(QString str,files)
    {
        QString sFilePath = dir.absoluteFilePath(str);
        //qDebug()<<"fileCnt"<
        QByteArray qba=sFilePath.toLocal8Bit();
        string _Filename=qba.toStdString();
        FILE *pFile = fopen(_Filename.c_str(),"r");
        if(pFile==NULL)
        {
            //fclose(pFile);
            continue;
        }
        fseek(pFile,0,SEEK_END);
        int len = ftell(pFile);
        rewind(pFile);
        char* ch = (char*)malloc(sizeof(char)* (len));    //分配内存,多一个字节
        memset(ch,0,(len));           //清0
        fread(ch,1,len,pFile);          //读取文件
        fclose(pFile);
        ByteArray bades;
        for(int i=0;i<len;i++)
        {
            bades.push_back((::Ice::Byte)ch[i]);
        }
        free(ch);
        m_fileMap.insert(str, bades);
    }
#endif
}
 
  

void convertAllFileToBsonArray(QMap<QString, ByteArray> &infileByte,ByteArray &outba)
{
#if 1
    QList<QString> ks = infileByte.keys();
    QList<ByteArray> kv=infileByte.values();
    int mapLen=infileByte.size();
    bson_t * AllBson = bson_new ();
    bson_init (AllBson);
	bool bTest;
    for(int i=0;i<mapLen;i++)
    {
        QString strKey=ks[i];
        int valueLen=kv[i].size();
        bson_subtype_t  subtype=bson_subtype_t::BSON_SUBTYPE_BINARY;
        BSON_APPEND_BINARY(AllBson,(const char*)(strKey.toStdString().c_str())
                           ,subtype,(const uint8_t*)(kv[i].data()),valueLen);
    }
    int len=AllBson->len;//12761703  12761590
    const uint8_t * pbsnData=bson_get_data (AllBson);//获取 到所有的数据
    outba.clear();
    for(int i=0;i<len;i++)
    {
        outba.push_back((::Ice::Byte)pbsnData[i]);
    }
	bson_destroy(AllBson);
#endif
}
 
  

逆向解析:

void convertByteArrayToBson(ByteArray& inba,int &result, QString &strMsg)
{
    int indata_len = inba.size();
    qDebug() <<" after  indata_len ="<<indata_len;
    uint8_t * psrcData;
    psrcData=( uint8_t *)malloc(indata_len);
    for(int i=0;i<indata_len;i++)
    {psrcData[i]=(uint8_t)(inba.at(i));}
 
  
 
  
    bson_t * AllBson = bson_new ();
    bson_init (AllBson);
 
  
    bool binit =bson_init_static (AllBson,psrcData,indata_len);
    //AllBson =  bson_new_from_data (psrcData,indata_len);
    if(binit==true)
    {
        //QString sstest = "icebox.bat";
        //const char   *key=sstest.toStdString().c_str();
        //bool bin = bson_has_field (AllBson,key);
        uint32_t allbsonSize =bson_count_keys(AllBson);
        qDebug() <<" after  allbsonSize ="<<allbsonSize;
        // for test b
        bson_iter_t iter;
        BSON_ASSERT (AllBson);
        if (bson_iter_init (&iter, AllBson))
        {
            //iter.key
           while (bson_iter_next (&iter))
           {
               qDebug() <<" hello now begin =";
               const char * ppkey = bson_iter_key (&iter);
               //(const char *)(iter->raw + iter->key)
               QString skeyStr(ppkey);
               qDebug() <<" ppkey ="<<skeyStr<<"skeyStr.length()"<<skeyStr.length();
               //bool bfind1 = bson_iter_find (iter,ppkey); ---  此句一有,会改变ITER的位置,所以不能有这句
               //qDebug() <<" ppkey bfind1 ="<
 
  
               //const char * ppkey1 = "ConfigItem.xml";
               //bool bfind2 = bson_iter_find (iter,ppkey1);
               //qDebug() <<" ppkey bfind2 ="<
 
  
               //qDebug() <<" iter.len ="<len;
               //qDebug() <<" iter.type ="<type;
               //qDebug() <<" iter.key ="<key;
               //qDebug() <<" iter.key ="<
 
  
               const bson_value_t * pv = bson_iter_value(&iter);
               if(pv->value_type == BSON_TYPE_BINARY)
               {
                   int dataLen = pv->value.v_binary.data_len;
                   uint8_t *pdata = pv->value.v_binary.data;// unsigned char
 
  
                   char *cData=new char[dataLen];
                   memset(cData,0,sizeof(cData));
                   for(int u=0;u<dataLen;u++)
                   {
                       cData[u]=(char)(pdata[u]);
                   }
                   //memcpy(cData,pdata,sizeof(cData));
                   QString strData(cData);
                   delete cData;
                   qDebug() <<" ppvalue="<<strData<<"ppvalue.length()"<<dataLen;
               }
           }
        }
        // for test e
 
  
        int tt;
    }
    free(psrcData);
    bson_destroy(AllBson);
}
 
  
 
  
void convertByteArrayToBson(ByteArray& inba,int &result, QString &strMsg)
{
    result = -1;
    strMsg="";
 
  
    int indata_len = inba.size();
    //qDebug() <<" after  indata_len ="<
    uint8_t * psrcData;
    psrcData=( uint8_t *)malloc(indata_len);
    for(int i=0;i<indata_len;i++)
    {psrcData[i]=(uint8_t)(inba.at(i));}
 
  
 
  
    bson_t * AllBson = bson_new ();
    bson_init (AllBson);
 
  
    bool binit =bson_init_static (AllBson,psrcData,indata_len);
    //AllBson =  bson_new_from_data (psrcData,indata_len);
    if(binit==true)
    {
        uint32_t allbsonSize =bson_count_keys(AllBson);
        //qDebug() <<" after  allbsonSize ="<
 
  
        bson_iter_t iter;
        BSON_ASSERT (AllBson);
        if (bson_iter_init (&iter, AllBson))
        {
 
  
           while (bson_iter_next (&iter))
           {
               //qDebug() <<" hello now begin =";
               const char * ppkey = bson_iter_key (&iter);
 
  
               QString skeyStr(ppkey);
               //qDebug() <<" ppkey ="<
 
  
               const bson_value_t * pv = bson_iter_value(&iter);
               if(pv->value_type == BSON_TYPE_UTF8)
               {
				   int dataLen = pv->value.v_utf8.len;
                   uint32_t outDataLen=-1;
                   const char * ppv = (char *)bson_iter_utf8(&iter,&outDataLen);
				   string sppv(ppv);
                   QString smyValue(sppv.c_str());
                   if(skeyStr=="result")
                   {
                       bool ok;
                       uint ree = smyValue.toUInt(&ok, 10);
                       if(ok==true)
                           result=ree;
                   }
                   if(skeyStr=="message")
                   {
                       strMsg=smyValue;
                   }
                   //qDebug() <<" skeyStr="<
               }
           }
        }
        // for test e
 
  
        int tt;
    }
    free(psrcData);
    bson_destroy(AllBson);
}
 
  
 
  

你可能感兴趣的:(QT中对BSON的处理)