Poco库的学习

一、下载和编译Poco库

参考:www.jianshu.com/p/94c27101eee3
blog.csdn.net/walk_and_think/article/details/82432863

  • 下载地址:https://pocoproject.org/releases/poco-1.9.0/     
    或者    https://github.com/pocoproject/poco/releases/tag/poco-1.9.0-release
  • 下载poco-1.9.0-all.zip,其中不带all的是基础版本,只包含基本的poco类库,带all的是全功能版本,包含sqlite,opensll等等。
  • 解压缩zip包后,得到文件。

编译

---- Windows平台下

1、用记事本或UltraEdit打开components文件,添加或删除指定的行即可配置编译哪些模块。不想要openssl和mysql, 删掉openssl和data/mysql即可
例如:
CppUnit
CppUnit/WinTestRunner
Foundation
Encodings
XML
JSON
Util
Net
Zip
=> 只编译基本模块

2、根据Visual Studio版本,在文件夹中直接双击build_vsxxx.cmd文件即可自动编译。
VS2008: build_vs90.cmd
VS2013: build_vs120.cmd
VS2015: build_vs140.cmd
VS2017: build_vs150.cmd

3、本人使用的VS2013因此,双击运行了文件夹中的build_vs120.cmd。之后会自动执行编译脚本,并在当前目录下生成两个文件夹bin和lib。
lib:该文件夹中存放了.lib静态连接库等文件。编译自己写的源代码时需要。
bin:该文件夹中存放了.dll动态链接库等文件。运行编译好的可执行文件时需要。

VS2013下使用Poco库

在Poco1.9.0目录下新建文件夹 include ,把所有的工程下的include的内容合在一起。
设置vs2013的项目属性》C/C++》附加包含目录:D:\poco-1.9.0-release\include (你自己的Poco目录\inlcude )
链接器》附加库目录》D:\poco-1.9.0-release\lib (你自己的Poco目录\lib )
调试》环境:PATH=D:\poco-1.9.0-release\bin;  (你自己的Poco目录\bin; )

 


以下参考官网文档和例子:https://pocoproject.org/slides/
 
#include <vector>
#include "Poco/String.h"
#include "Poco/Format.h"
using Poco::cat;
using Poco::format;
int main(int argc, char** argv)
{
    try{
        std::vector<std::string> colors;
        colors.push_back("red");
        colors.push_back("green");
        colors.push_back("blue");
        std::string str;
        str = cat(std::string(", "), colors.begin(), colors.end());
        // "red, green, blue"

        int n = 42;
        std::string s;
        format(s, "The answer to life, the universe and everything is %d", n);
        s = format("%d + %d = %d", 2, 2, 4); // "2 + 2 = 4"
        s = format("%4d", 42);               // "  42"
        s = format("%-4d", 42);              // "42  "
        format(s, "%d", std::string("foo")); // "[ERRFMT]"
    }
    catch (Poco::Exception& e)
    {
        std::string s = e.what();
        s += " " + e.message();
    }

    return 0;
}

Poco中提供了对 std::string 和 std::wstring 的操作函数。https://pocoproject.org/slides/040-StringsAndFormatting.pdf

例如: 删除空白符(trimming  ) 比较字符串  连接字符串  格式化字符串  字符串大小写转换  字符或字符串替换

std::[w]string trimLeft(const std::[w]string& str) // 删除字符串开头的空白符,返回修剪后的string,原字符串不变
std::[w]string& trimLeftInPlace(std::[w]string& str)// 删除字符串开头的空白符,直接修改原字符串
std::[w]string trimRight(const std::[w]string& str) //删除结尾的空白符,返回修剪后的结果,原字符串不变
std::[w]string& trimRightInPlace(std::[w]string& str)//删除结尾的空白符,直接在原字符串上
std::[w]string trim(const std::[w]string& str)//删除开头和结尾的空白符
std::[w]string& trimInPlace(std::[w]string& str) //直接在原字符串上删除开头和结尾的空白符
//带 -InPlace 的都是在原字符串上直接修改。

//returns a copy of str with all characters converted to upper-/lowercase.
std::[w]string toUpper(const std::[w]string& str)
std::[w]string toLower(const std::[w]string& str)
//直接在原字符串上转换大小写
std::[w]string& toUpperInPlace(std::[w]string& str)
std::[w]string& toLowerInPlace(std::[w]string& str)

Warning: These function do not work with UTF-8 strings. See Poco::UTF8 for an UTF-8 capable replacement.

//Case-insensitive Comparison 不区分大小写的比较
int icompare(const std::[w]string& str1, const std::[w]string& str2)
//compares tr1 to str2, and returns
0 if str1 == str2
-1 if str1 < str2
+1 if str1 > str2


//returns a copy of str with all characters in from replaced with the corresponding (by position) characters in to.If there is no corresponding character in to, the character is removed.
std::[w]string translate(const std::[w]string& str, const std::[w]string& from, const std::[w]string& to)
std::[w]string& translateInPlace(std::[w]string& str, const std::[w]string& from, const std::[w]string& to)
//from and to can also be old-style C strings.


//returns a copy of str with all occurences of the substring given in from replaced with the string given in to.
std::[w]string replace(const std::[w]string& str,const std::[w]string& from, const std::[w]string& to)
std::[w]string& replaceInPlace(std::[w]string& str,const std::[w]string& from, const std::[w]string& to)
//from and to can also be old-style C strings.

//String Concatenation
std::[w]string cat(const std::[w]string& s1, onst std::[w]string& s2 [, const std::[w]string& s3 [,...]])
//concatenates up to six strings and returns the result

template <class S, class It>S cat(const S& delimiter, const It& begin, const It& end)
//concatenates all string in the range [It, end), delimited by delimiter. cat() is more efficient than operator + of std::string

//Typesafe Printf-style Formatting
std::string format(const std::string& format, const Any& value1[, const Any& value2[, ...]])
void format(std::string& result, const std::string& format, const Any& value1[, const Any& value2[, ...]])
//Up to six parameters are supported.

 例子:

#include <vector>
#include <Poco/String.h>
#include <Poco/format.h> //for Poco::format

int main(int argc, char** argv)
{
    std::string hello(" \n \t  Hello, world!  ");
    std::string s1(Poco::trimLeft(hello)); // "Hello, world!  "
    Poco::trimRightInPlace(s1);            // "Hello, world!"
    std::string s2 = Poco::trim(hello);     // "Hello, world!"
    Poco::trimInPlace(hello);        // "Hello, world!"

    s1 = Poco::toUpper(hello); // "HELLO, WORLD!"
    Poco::toLowerInPlace(s2);  // "hello, world!"
    //不分大小写的比较
    int nRet = Poco::icompare(s1, s2);// 0
    nRet = Poco::icompare(hello, "Hello, Universe!");// 1
    nRet = strcmpi(s1.c_str(), s2.c_str());// 0
    //分大小写的比较
    nRet = strcmp(s1.c_str(), s2.c_str());// -1
    
    std::string s("Eiffel Tower");
    //E->3 e->3 l->1 o->0 替换字符
    Poco::translateInPlace(s, "Eelo", "3310"); // "3iff31 T0w3r"
    //替换字符串
    s = "aabbcc";
    std::string r(Poco::replace(s, "aab", "AA")); // "AAbcc"
    r = Poco::replace(s, "bb", "xxx");           // "aaxxxcc"
    r = Poco::replace(s, "bbcc", "");            // "aa"
    r = Poco::replace(s, "aaa", "b");    // "aabbcc" 没有匹配到,不变

    //字符串连接
    std::vector<std::string> colors;
    colors.push_back("red");
    colors.push_back("green");
    colors.push_back("blue");
    std::string st = Poco::cat(colors[0], std::string(" "), colors[1], std::string(" "), colors[2]);// "red green blue"
    st = Poco::cat(std::string(", "), colors.begin(), colors.end());// "red, green, blue"

    //字符串格式化
    int n = 42;
    std::string str;
    Poco::format(str, "The answer to life, the universe and everything is %d", n);
    str = Poco::format("%d + %d = %d", 2, 2, 4); // "2 + 2 = 4"
    str = Poco::format("%4d", 42);               // "  42"
    str = Poco::format("%-4d", 42);              // "42  "
    Poco::format(str, "%d", std::string("foo")); // "[ERRFMT]"

    return 0;
}

 

转载于:https://www.cnblogs.com/htj10/p/11380144.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值