天天看点

POCO库的使用

最近在找网络库的时候发现了POCO这个库,里面的配置文件接口用着挺舒服的。TCP没有使用因为windows下面是select模式,效率不够。数据库用的是ODBC效率也不够,日志系统没有glog好,所以日志用的glog,别的还没有研究,有空的时候再补上。

wstring s2w(const string& str)

{

size_t num = 0;

vector<wchar_t>buf( str.size()*2 + 2 );

_locale_t local = _create_locale(LC_ALL,"chs");

//if failed set buf empty string and num = 0

_mbstowcs_s_l(&num, &buf[0], buf.size(),str.c_str(),_TRUNCATE,local);

_free_locale(local);

return &buf[0];

}

中文路径的支持

str =   "D:\\我的文档\\config.xml";

wstring wstr = s2w(str);

Poco::UnicodeConverter::toUTF8(wstr, str);

AutoPtr<Poco::Util::XMLConfiguration> cfg(new Poco::Util::XMLConfiguration(str.c_str()));

m_dbIp = cfg->getString("database.ip");

m_dbPort = cfg->getString("database.port");

m_dbUser = cfg->getString("database.userName");

m_dbPwd = cfg->getString("database.password");

配置文件格式如下;

<config>

    <ftp>

    <ip>192.168.18.131</ip>

    <port>21</port>

    <userName>ANONYMOUS</userName>

    <password></password>

    </ftp>

    <database>

       <ip>192.168.18.246</ip>

       <port>1433</port>

       <userName>sa</userName>

       <password>`123qwer</password>

    </database>

</config>