update the way we get compounds, make the WolframAlpha::get fucntion safer and restore main.cpp

This commit is contained in:
:^) 2024-10-16 17:30:37 +02:00
parent 47366a6d62
commit 537cab18ed
3 changed files with 25 additions and 47 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <iostream>
#include <print>
#include <string>
#include <unordered_map>
@ -54,21 +55,18 @@ public:
template <typename T>
T get(const std::string& arg) const {
std::string value;
if (m_longOptMap.contains(arg)) {
if (m_longOptMap.contains(arg) && !m_longOptMap.at(arg)->value.empty()) {
value = m_longOptMap.at(arg)->value;
} else if (m_shortOptMap.contains(arg)) {
} else if (m_shortOptMap.contains(arg) && !m_shortOptMap.at(arg)->value.empty()) {
value = m_shortOptMap.at(arg)->value;
} else {
throw std::runtime_error(arg + " has no value");
}
if constexpr (std::is_same_v<T, int>) {
return std::stoi(value);
} else if constexpr (std::is_same_v<T, float>) {
return std::stof(value);
} else if constexpr (std::is_same_v<T, double>) {
return std::stod(value);
} else {
if constexpr (std::is_same_v<T, std::string>) {
return value;
} else {
std::cout << std::format("the value is: {}", value);
return std::stoi(value);
}
}

View File

@ -16,33 +16,24 @@ double WolframAlpha::getCalories() {
}
double WolframAlpha::getProtein() {
if (auto match = ctre::search<R"((?<=protein\s)\d+\s*\w+)">(getPod())) {
const std::string val = std::string(match.to_view().substr(0, match.begin() - match.end()));
std::string unit = std::string(ctre::search<R"(\w+$)">(match.to_view()));
return unit == "g" ? std::stod(val) * m_amount : std::stod(val) / 1000 * m_amount;
}
throw std::runtime_error("Protein information not found.");
auto m = ctre::search<"(?<=protein\\s)(\\d+)\\s*(\\w+)">(getPod());
double d = m.get<1>().to_number();
std::string u = m.get<2>().to_string();
return u == "g" ? d : d / 1000;
}
double WolframAlpha::getCarbs() {
if (auto match = ctre::search<R"((?<=total carbohydrates\s)\d+\s*\w+)">(getPod())) {
const std::string val = std::string(match.to_view().substr(0, match.begin() - match.end()));
std::string unit = std::string(ctre::search<R"(\w+$)">(match.to_view()));
return unit == "g" ? std::stod(val) * m_amount : std::stod(val) / 1000 * m_amount;
}
throw std::runtime_error("Carbohydrates information not found.");
auto m = ctre::search<"(?<=total carbohydrates\\s)(\\d+)\\s*(\\w+)">(getPod());
double d = m.get<1>().to_number();
std::string u = m.get<2>().to_string();
return u == "g" ? d : d / 1000;
}
double WolframAlpha::getFat() {
if (auto match = ctre::search<R"((?<=total fat\s)\d+\s*\w+)">(getPod())) {
const std::string val = std::string(match.to_view().substr(0, match.begin() - match.end()));
std::string unit = std::string(ctre::search<R"(\w+$)">(match.to_view()));
return unit == "g" ? std::stod(val) * m_amount : std::stod(val) / 1000 * m_amount;
}
throw std::runtime_error("Fat information not found.");
auto m = ctre::search<"(?<=total fat\\s)(\\d+)\\s*(\\w+)">(getPod());
double d = m.get<1>().to_number();
std::string u = m.get<2>().to_string();
return u == "g" ? d : d / 1000;
}
nlohmann::json WolframAlpha::fetchJson(const std::string& query) {

View File

@ -1,18 +1,8 @@
#include "ArgParser.hpp"
#include "Helpers/WolframAlpha.hpp"
#include <ctre.hpp>
#include <iostream>
#include <nlohmann/json.hpp>
#include <print>
#include <string_view>
void getVal(std::string_view sv) {
auto m = ctre::search<"(?<=protein\\s)(\\d+)\\s*(\\w+)">(sv);
double d = m.get<1>().to_number();
std::string u = m.get<2>().to_string();
std::println("1: {}\n2: {}", d, u);
}
int main(const int argc, char* argv[]) {
try {
@ -23,14 +13,13 @@ int main(const int argc, char* argv[]) {
args.addArg("-h", "--help", "Print help", false);
args.parse(argc, argv);
// const auto food = args.get<std::string>("--food");
// const double amount = args.get<double>("--amount") / 100;
const auto food = args.get<std::string>("--food");
const double amount = args.get<double>("--amount") / 100;
// if (!args.has("--source") || args.get<std::string>("--source") == "Wolfram") {
// WolframAlpha wa(food, amount);
// std::println("Calories: {:.2f} kcal\nProtein: {:.2f} g\nCarbs: {:.2f} g\nFat: {:.2f} g", wa.getCalories(), wa.getProtein(), wa.getCarbs(), wa.getFat());
// }
getVal("carbs 14 g | protein 20 mg | fat 11 g");
if (!args.has("--source") || args.get<std::string>("--source") == "Wolfram") {
WolframAlpha wa(food, amount);
std::println("Calories: {:.2f} kcal\nProtein: {:.2f} g\nCarbs: {:.2f} g\nFat: {:.2f} g", wa.getCalories(), wa.getProtein(), wa.getCarbs(), wa.getFat());
}
} catch (const std::exception& e) {
std::println(stderr, "Error: {}", e.what());