Get rid of boost usage

This commit is contained in:
Wizzard 2024-04-10 04:49:57 -04:00
parent 6fff2e7525
commit 60a11252cb
2 changed files with 21 additions and 8 deletions

View File

@ -1 +1 @@
g++ -std=c++17 main.cpp -o solprice-cli -lcurl -lncurses -lboost_system g++ -std=c++17 main.cpp -o solprice-cli -lcurl -lncurses

View File

@ -1,8 +1,6 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <curl/curl.h> #include <curl/curl.h>
#include <boost/json.hpp>
#include <boost/json/src.hpp>
#include <ncurses.h> #include <ncurses.h>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *userp) { static size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *userp) {
@ -25,13 +23,28 @@ double getConversionRate() {
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if(res == CURLE_OK) { if(res == CURLE_OK) {
boost::json::error_code ec; std::size_t usdPos = readBuffer.find("\"usd\":");
auto parsed = boost::json::parse(readBuffer, ec); if (usdPos != std::string::npos) {
if(!ec) { std::size_t start = readBuffer.find(':', usdPos) + 1;
auto& obj = parsed.as_object(); std::size_t end = readBuffer.find_first_of("},", start);
rate = obj["solana"].as_object()["usd"].as_double(); if(start < end) {
std::string usdValue = readBuffer.substr(start, end - start);
usdValue.erase(0, usdValue.find_first_not_of(" \n\r\t"));
usdValue.erase(usdValue.find_last_not_of(" \n\r\t") + 1);
try {
rate = std::stod(usdValue);
} catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n';
} catch (const std::out_of_range& oor) {
std::cerr << "Out of Range error: " << oor.what() << '\n';
} }
} }
} else {
std::cerr << "USD rate not found in response." << std::endl;
}
} else {
std::cerr << "CURL failed with error code: " << res << std::endl;
}
} }
return rate; return rate;
} }