C++ REST SDK で websocketのレスポンスでjson扱う

うまくいかにゃい

※追記:
BOM付きJSONだとうまくいかないことが判明!

そんな時はこんな感じでBOM削除して改めてjson::value作らないといけない。

std::wstring json_bom = res.extract_string().get().c_str();
std::wstring json_nobom = L"{" + json_bom.substr(2, json_bom.length() - 2);
json::value json = json::value::parse(json_nobom);

std::stringとstd::wstringの相互変換

内部コードにUTF8を利用する場合 かつ日本語使わない

#include <iostream>
#include <locale> 
#include <codecvt> 
#include <cstdio>

int main(){
    std::string message = "ABCDEFG";
    std::wstring_convert<std::codecvt_utf8<wchar_t>,wchar_t> cv;
    //string→wstring
    std::wstring wsmessage = cv.from_bytes(message);
    std::wcout << wsmessage << std::endl;
    //wchar_t型を取得 
    wprintf("%S",wsmessage.c_str());

    //wstring→string
    std::string reconvert = cv.to_bytes(wsmessage);
    std::cout << reconvert << std::endl;
    return 0;
}

C++で非同期プログラミング Taskを起動

#include <ppltasks.h>
#include <string>
#include <windows.h>

using namespace concurrency;
using namespace std;

int wmain()
{
    auto t = create_task([]()
    {
        DWORD   ms;
        wchar_t str[40];
        for (int i = 0; i < 3; i++)
        {
            ms = GetTickCount();        // Win32 API()
            swprintf(str, 40, L"A: Time %d ミリ秒\n", ms);
            OutputDebugString(str);
            Sleep(rand() % 100);
        }
    });
    t.then([]()
    {   OutputDebugString(L"Run Task\n");
    }).wait();
}

auto t = create_task(・・・) でタスクを生成。

t.then([]() でタスクが起動され、.wait(); で終了を待ち合せます。
L"Run Task\n" はタスクが終了したときに印字されます。

C++ REST SDK サンプル Bing検索

#include <cpprest/http_client.h>

...

// Create http_client to send the request.
http_client client(U("http://www.bing.com/"));

// Build request URI and start the request.
uri_builder builder(U("/search"));
builder.append_query(U("q"), U("cpprestsdk github"));

auto t1{client.request(methods::GET, builder.to_string()); };

t1.then([](http_response res) {
	std::wstring s1{ res.extract_string().get().c_str() };
	wprintf(s1.c_str());
}).wait();

C++ による base64 エンコード/デコード

元サイト
http://yano.hatenadiary.jp/entry/20100908/1283945820


消えたら困るのでメモ

base64.hpp

#ifndef BASE64_HPP_20100908_
#define BASE64_HPP_20100908_

#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif


#include <string>
#include <vector>


namespace algorithm {
    bool encode_base64(const std::vector<unsigned char>& src, std::string& dst);
    bool decode_base64(const std::string& src, std::vector<unsigned char>& dst);
}


#endif

base64.cpp

#include "base64.hpp"


// base64 エンコード
bool algorithm::encode_base64(const std::vector<unsigned char>& src, std::string& dst)
{
    const std::string table("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
    std::string       cdst;

    for (std::size_t i = 0; i < src.size(); ++i) {
        switch (i % 3) {
        case 0:
            cdst.push_back(table[(src[i] & 0xFC) >> 2]);
            if (i + 1 == src.size()) {
                cdst.push_back(table[(src[i] & 0x03) << 4]);
                cdst.push_back('=');
                cdst.push_back('=');
            }

            break;
        case 1:
            cdst.push_back(table[((src[i - 1] & 0x03) << 4) | ((src[i + 0] & 0xF0) >> 4)]);
            if (i + 1 == src.size()) {
                cdst.push_back(table[(src[i] & 0x0F) << 2]);
                cdst.push_back('=');
            }

            break;
        case 2:
            cdst.push_back(table[((src[i - 1] & 0x0F) << 2) | ((src[i + 0] & 0xC0) >> 6)]);
            cdst.push_back(table[src[i] & 0x3F]);

            break;
        }
    }

    dst.swap(cdst);

    return true;
}


// base64 デコード
bool algorithm::decode_base64(const std::string& src, std::vector<unsigned char>& dst)
{
    if (src.size() & 0x00000003) {
        return false;
    }
    else {
        const std::string          table("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
        std::vector<unsigned char> cdst;

        for (std::size_t i = 0; i < src.size(); i += 4) {
            if (src[i + 0] == '=') {
                return false;
            }
            else if (src[i + 1] == '=') {
                return false;
            }
            else if (src[i + 2] == '=') {
                const std::string::size_type s1 = table.find(src[i + 0]);
                const std::string::size_type s2 = table.find(src[i + 1]);

                if (s1 == std::string::npos || s2 == std::string::npos) {
                    return false;
                }

                cdst.push_back(static_cast<unsigned char>(((s1 & 0x3F) << 2) | ((s2 & 0x30) >> 4)));

                break;
            }
            else if (src[i + 3] == '=') {
                const std::string::size_type s1 = table.find(src[i + 0]);
                const std::string::size_type s2 = table.find(src[i + 1]);
                const std::string::size_type s3 = table.find(src[i + 2]);

                if (s1 == std::string::npos || s2 == std::string::npos || s3 == std::string::npos) {
                    return false;
                }

                cdst.push_back(static_cast<unsigned char>(((s1 & 0x3F) << 2) | ((s2 & 0x30) >> 4)));
                cdst.push_back(static_cast<unsigned char>(((s2 & 0x0F) << 4) | ((s3 & 0x3C) >> 2)));

                break;
            }
            else {
                const std::string::size_type s1 = table.find(src[i + 0]);
                const std::string::size_type s2 = table.find(src[i + 1]);
                const std::string::size_type s3 = table.find(src[i + 2]);
                const std::string::size_type s4 = table.find(src[i + 3]);

                if (s1 == std::string::npos || s2 == std::string::npos || s3 == std::string::npos || s4 == std::string::npos) {
                    return false;
                }

                cdst.push_back(static_cast<unsigned char>(((s1 & 0x3F) << 2) | ((s2 & 0x30) >> 4)));
                cdst.push_back(static_cast<unsigned char>(((s2 & 0x0F) << 4) | ((s3 & 0x3C) >> 2)));
                cdst.push_back(static_cast<unsigned char>(((s3 & 0x03) << 6) | ((s4 & 0x3F) >> 0)));
            }
        }

        dst.swap(cdst);

        return true;
    }
}

test.cpp

#include <iostream>
#include <iomanip>
#include <vector>
#include "base64.hpp"


int main()
{
    std::vector<unsigned char> v1, v2;

    v1.push_back(0xde);
    v1.push_back(0xad);
    v1.push_back(0xbe);
    v1.push_back(0xef);

    std::string s;

    algorithm::encode_base64(v1, s); // base64 エンコード
    std::cout << s << std::endl; // 3q2+7w==

    algorithm::decode_base64(s, v2); // base64 デコード
    for (std::vector<unsigned char>::const_iterator it = v2.begin(); it != v2.end(); ++it) {
        std::cout << std::hex << static_cast<int>(*it);
    } // deadbeef

    return 0;
}