1#include <iostream>2#include <string>3#include <stdint.h>4#include <map>5#include <fstream>6#include <stdlib.h>7#include <stdio.h>8 9 enumTen { OneEnmmaxconfigwordlen = +, A }; - -typedef STD::MAP<STD::string, std::string>Attrmap; the - - voidGettextfilecontent (Const Char*filename,CharBuf[],Constuint32_t maxbuflen,uint32_t &Readbuflen) - { +Std::ifstream file (Filename,std::ios::inch); - if(!file) + { A return ; at } - CharC; -Readbuflen =0; - while(file.Get(c) && Readbuflen <Maxbuflen) - { - if(c! ='\ n'&& C! =' '&& C! =9) in { -buf[readbuflen++] =C; to } + } -buf[readbuflen]=' /'; the } * $ voidGETALLATTRFROMSTR (Const Char*buf,Constuint32_t Buflen,attrmap &Attrmap)Panax Notoginseng { - CharKey[enmmaxconfigwordlen],value[enmmaxconfigwordlen]; theint32_t flag =0, Len =0;//0->key,1->value + for(Uint32_t i =0; i < Buflen; ++i) A { the Const Char&c =Buf[i]; + Switch(c) - { $ Case '{': flag=0; Break; $ Case '}': -Value[len] =' /'; -Attrmap[key] =value; the Break; - Case ':':WuyiKey[len] =' /'; theLen =0; -Flag =1; Wu Break; - Case ',': AboutValue[len] =' /'; $Len =0; -Flag =0; -Attrmap[key] =value; - Break; A default: + if(Flag = =0 ) the { -key[len++] =C; $ } the Else if(Flag = =1 ) the { thevalue[len++] =C; the } - Break; in } the } the } About the the int32_t Main () the { + Constuint32_t Maxfilebuflen =1024x768; - CharBuf[maxfilebuflen]; the uint32_t Buflen;BayiGettextfilecontent ("dbconfig.cfg", Buf,maxfilebuflen,buflen); the Attrmap Attrmap; the getallattrfromstr (buf,buflen,attrmap); -STD::stringSzhost = attrmap["Host"]; -STD::stringSZUSR = attrmap["User"]; theSTD::stringSZPASSWD = attrmap["passwd"]; theSTD::stringSzdbname = attrmap["dbname"]; theSTD::stringSztablename = attrmap["TableName"]; theuint32_t unport = Atol (attrmap["Port"].c_str ()); -printf"szhost = '%s ' \nszusr = '%s ' \nszpasswd = '%s ' \nszdbname = '%s ' \nsztablename = '%s ' \nunport =%d\n", the szhost.c_str (), Szusr.c_str (), Szpasswd.c_str (), Szdbname.c_str (), Sztablename.c_str (), unport); the return 0; the}
Test file:
{ Host:192.168. 75.134, port:3306, user:sdo, passwd:123456, Dbname:sdo_config, Tablename:sdo_item}
Test results:
' 192.168.75.134 ' 'sdo ' ' 123456 ' Sdo_config ' 'sdo_item'3306
Parsing of the Key_value type configuration file