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动态链接库等文件。运行编译好的可执行文件时需要。

编译:参考 https://www.cnblogs.com/fuland/p/3768705.html

下面简单介绍一下方法,以 vs2010 为例。

第一种方法:

(1) Poco 根目录下有build_vs100.cmd和buildwin.cmd这两个批处理文件, 我们得修改一下它们。
    把build_vs100.cmd 修改为以下内容:
    @echo off
    if defined VS100COMNTOOLS (
       call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\vsvars64.bat")
     buildwin 100 build all both x64 samples

红色的部分是vsvars64.bat的路径,具体要看本机的安装位置。这里因为是要编译X64,所以要修改环境。

蓝色部分是执行编译时候的参数(注意X64的X要小写)。参数的具体说明为:

buildwin VS_VERSION [ACTION] [LINKMODE] [CONFIGURATION] [PLATFORM] [SAMPLES] [TESTS] [TOOL]
    VS_VERSION:    71|80|90|100|110  
    ACTION:        build|rebuild|clean
    LINKMODE:      static_mt|static_md|shared|all
    CONFIGURATION: release|debug|both
    PLATFORM:      Win32|x64|WinCE
    SAMPLES:       samples|nosamples
    TESTS:         tests|notests
    TOOL:          devenv|vcexpress|msbuild

具体的参数选择根据需求。

如果不需要 NetSSL_OpenSSL 和 MySQL ,此时保存后就双击它就行了。 它会自动编译好各种库。
   如果要NetSSL_OpenSSL 这个库则打开buildwin.cmd文件:
   设置 openssl 的路径,如:

set OPENSSL_DIR=c:\OpenSSL-Win64
    set OPENSSL_INCLUDE=%OPENSSL_DIR%\include
    set OPENSSL_LIB=%OPENSSL_DIR%\lib;%OPENSSL_DIR%\lib\VC
    set INCLUDE=%INCLUDE%;%OPENSSL_INCLUDE%
    set LIB=%LIB%;%OPENSSL_LIB%


   注意,如果要 Poco 中的 MySQL 库 还得安装. Mysql
   编辑好后,双击  build_vs100.cmd执行编译。完成后会在Poco根目录下的lib64中看到编译好的库。在bin64中有编译好的dll。

第二种方法(适合熟悉命令行的用户)

按需求修改完buildwin.cmd文件后,使用vs2010的Tools中“Visual Studio x64 Win64 命令提示(2010)”命令,进入命令行状态。转到Poco所在的根目录,直接输入命令:

buildwin 100 build all both x64 samples

 

最后需要注意地方:

上述方法完成编译后,关于Data的MySQL、ODBC、SQLite三个库没有生成(原因不详),需要单独编译,在此以MySQL为例记录一下编译过程。在\Data\MySQL文件夹下,找到MySQL_vs100.sln文件,打开。

打开的工程是win32的,需要改为X64。在C++包含目录中添加本项目的include文件夹,例如:“C:\poco-1.5.1-all\Data\MySQL\include;”,还要添加安装的MySQL的include文件夹,例如“C:\Program Files\MySQL\MySQL Server 5.6\include”,在库目录中添加所需库的路径,例如“C:\Program Files\MySQL\MySQL Server 5.6\lib”。

注意:编译MySQL的Poco库,必须先安装MySQL。

设置好环境之后,就可以选择相应的配置(debug or release)进行编译。

编译完成后,生成的库在Poco根目录下的lib文件夹,如果是编译的动态库,则动态库在Poco根目录下的bin文件夹。

 

用VS2022编译Poco-1.12.4

1. 打开VS2022的开发者命令窗口(Developer Command Prompt)
2. cd 到Poco的目录
3. 执行命令 buildwin 170 build all both Win32 samples

 

源码包和编译过的分享给大家

链接:https://pan.baidu.com/s/14LSszTkkCaU5dnWKvEoYPg
提取码:gzy6

 

 

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; )
若使用静态库(lib里面mt或md结尾的lib),请在 项目属性》C/C++》预处理器中加上 POCO_STATIC

 


以下参考官网文档和例子: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;
}

 

posted @ 2019-08-19 22:20  htj10  阅读(2717)  评论(0编辑  收藏  举报
TOP