From 537cab18ed8f7b09ad7e6195ede4f90fb2f87203 Mon Sep 17 00:00:00 2001 From: etenie Date: Wed, 16 Oct 2024 17:30:37 +0200 Subject: [PATCH] update the way we get compounds, make the WolframAlpha::get fucntion safer and restore main.cpp --- src/ArgParser.hpp | 16 +++++++--------- src/Helpers/WolframAlpha.cpp | 33 ++++++++++++--------------------- src/main.cpp | 23 ++++++----------------- 3 files changed, 25 insertions(+), 47 deletions(-) diff --git a/src/ArgParser.hpp b/src/ArgParser.hpp index 23325c8..9ea27c3 100644 --- a/src/ArgParser.hpp +++ b/src/ArgParser.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -54,21 +55,18 @@ public: template 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) { - return std::stoi(value); - } else if constexpr (std::is_same_v) { - return std::stof(value); - } else if constexpr (std::is_same_v) { - return std::stod(value); - } else { + if constexpr (std::is_same_v) { return value; + } else { + std::cout << std::format("the value is: {}", value); + return std::stoi(value); } } diff --git a/src/Helpers/WolframAlpha.cpp b/src/Helpers/WolframAlpha.cpp index d3872be..b69a482 100644 --- a/src/Helpers/WolframAlpha.cpp +++ b/src/Helpers/WolframAlpha.cpp @@ -16,33 +16,24 @@ double WolframAlpha::getCalories() { } double WolframAlpha::getProtein() { - if (auto match = ctre::search(getPod())) { - const std::string val = std::string(match.to_view().substr(0, match.begin() - match.end())); - std::string unit = std::string(ctre::search(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(getPod())) { - const std::string val = std::string(match.to_view().substr(0, match.begin() - match.end())); - std::string unit = std::string(ctre::search(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(getPod())) { - const std::string val = std::string(match.to_view().substr(0, match.begin() - match.end())); - std::string unit = std::string(ctre::search(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) { diff --git a/src/main.cpp b/src/main.cpp index 3a57645..37f3844 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,8 @@ #include "ArgParser.hpp" #include "Helpers/WolframAlpha.hpp" #include -#include #include #include -#include - -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("--food"); - // const double amount = args.get("--amount") / 100; + const auto food = args.get("--food"); + const double amount = args.get("--amount") / 100; - // if (!args.has("--source") || args.get("--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("--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());