Hi all, the code below compiles and runs perfectly under VS 2013 but when I try and compile it using g++ on a Linux box I get a slew of errors regarding std::wstring, I know this is the problem as if I replace all wstring and wostringstream with string and ostringstream respectively all is good. This is only a personal project and thus very rough at the moment.
#include <vector>
#include <string>
#include <time.h>
#include <iostream>
#include <sstream>
#include <map>
#include "squeezemessage.h"
#include <random>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>
using namespace utility; using namespace web; using namespace web::http; using namespace web::http::client; using namespace concurrency::streams;
int ResultCount = 0;
std::vector<std::pair<std::wstring, std::wstring>> Artists;
void GetJsonResponse(json::value &jsonValue, http_response &response)
{
jsonValue = response.extract_json().get().at(U("result"));
ResultCount = jsonValue.size();
}
void SetRequest(http_request &requester, std::wstring &SqueezeMsg)
{
requester.set_method(methods::POST);
requester.headers().set_content_type(L"application/json");
requester.set_body(SqueezeMsg);
}
std::wstring GetClient()
{
return L"http://10.10.1.11:9000/jsonrpc.js";
}
pplx::task<void> Post(std::wstring SqueezeMsg)
{
http_request requester;
SetRequest(requester, SqueezeMsg);
Artists.clear();
return pplx::create_task([requester]
{
std::wstring URL(GetClient());
http_client client(URL);
return client.request(requester);
}).then([](http_response response)
{
int code = response.status_code();
if (response.status_code() == status_codes::OK)
{
json::value jsonValue;
GetJsonResponse(jsonValue, response);
std::map<std::wstring, std::wstring> Entities;
Entities.insert(std::pair<std::wstring, std::wstring>(U("contributors_loop"),U("contributor")));
Entities.insert(std::pair<std::wstring, std::wstring>(U("albums_loop"),U("album")));
Entities.insert(std::pair<std::wstring, std::wstring>(U("tracks_loop"),U("track")));
std::wostringstream ss;
if (ResultCount > 0)
{
ResultCount = 0;
std::map<std::wstring, std::wstring>::iterator it;
for (it = Entities.begin(); it != Entities.end(); ++it)
{
if (jsonValue.has_field(it->first))
{
json::value EntityArray = jsonValue.at(it->first);
std::wstring FieldName(it->second);
for (size_t i = 0; i < EntityArray.size(); i++)
{
std::wostringstream ss;
ss << FieldName << ": " << (i + 1);
std::wstring whatever(EntityArray[i].at(FieldName).as_string());
Artists.push_back(std::pair<std::wstring, std::wstring>(ss.str(), whatever));
ResultCount++;
}
}
}
}
}
});
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::wcout << L"\n\nUsage: " << argv[0] << L" search term\n";
return -1;
}
SqueezeMessage Squeezer;
std::wostringstream ss;
ss << L"term:" << argv[1];
std::wstring SearchTerm(ss.str());
Squeezer.BuildMessage(L"search", L"0", L"10000", SearchTerm);
std::wstring SqueezeMsg = Squeezer.SqueezeRequestW;
std::wcout << L"\n\n*** Running query " << SqueezeMsg << L"\n";
Post(SqueezeMsg).get();
for (std::vector<std::pair<std::wstring, std::wstring>>::iterator it = Artists.begin(); it != Artists.end(); ++it)
std::wcout << it->first << ":" << it->second << '\n';
std::wcout << L"\n\n*** " << Artists.size() << L" row" << (Artists.size() == 1 ? L"" : L"s") << L" returned ***\n";
}
What I have tried:
Googling, drinking heavily, long walks ( to the pub and back ) and finally posting here.