update the way we get compounds, make the WolframAlpha::get fucntion safer and restore main.cpp
This commit is contained in:
parent
47366a6d62
commit
537cab18ed
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
#include <print>
|
#include <print>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
@ -54,21 +55,18 @@ public:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T get(const std::string& arg) const {
|
T get(const std::string& arg) const {
|
||||||
std::string value;
|
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;
|
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;
|
value = m_shortOptMap.at(arg)->value;
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error(arg + " has no value");
|
throw std::runtime_error(arg + " has no value");
|
||||||
}
|
}
|
||||||
if constexpr (std::is_same_v<T, int>) {
|
if constexpr (std::is_same_v<T, std::string>) {
|
||||||
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 {
|
|
||||||
return value;
|
return value;
|
||||||
|
} else {
|
||||||
|
std::cout << std::format("the value is: {}", value);
|
||||||
|
return std::stoi(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,33 +16,24 @@ double WolframAlpha::getCalories() {
|
||||||
}
|
}
|
||||||
|
|
||||||
double WolframAlpha::getProtein() {
|
double WolframAlpha::getProtein() {
|
||||||
if (auto match = ctre::search<R"((?<=protein\s)\d+\s*\w+)">(getPod())) {
|
auto m = ctre::search<"(?<=protein\\s)(\\d+)\\s*(\\w+)">(getPod());
|
||||||
const std::string val = std::string(match.to_view().substr(0, match.begin() - match.end()));
|
double d = m.get<1>().to_number();
|
||||||
std::string unit = std::string(ctre::search<R"(\w+$)">(match.to_view()));
|
std::string u = m.get<2>().to_string();
|
||||||
|
return u == "g" ? d : d / 1000;
|
||||||
return unit == "g" ? std::stod(val) * m_amount : std::stod(val) / 1000 * m_amount;
|
|
||||||
}
|
|
||||||
throw std::runtime_error("Protein information not found.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double WolframAlpha::getCarbs() {
|
double WolframAlpha::getCarbs() {
|
||||||
if (auto match = ctre::search<R"((?<=total carbohydrates\s)\d+\s*\w+)">(getPod())) {
|
auto m = ctre::search<"(?<=total carbohydrates\\s)(\\d+)\\s*(\\w+)">(getPod());
|
||||||
const std::string val = std::string(match.to_view().substr(0, match.begin() - match.end()));
|
double d = m.get<1>().to_number();
|
||||||
std::string unit = std::string(ctre::search<R"(\w+$)">(match.to_view()));
|
std::string u = m.get<2>().to_string();
|
||||||
|
return u == "g" ? d : d / 1000;
|
||||||
return unit == "g" ? std::stod(val) * m_amount : std::stod(val) / 1000 * m_amount;
|
|
||||||
}
|
|
||||||
throw std::runtime_error("Carbohydrates information not found.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double WolframAlpha::getFat() {
|
double WolframAlpha::getFat() {
|
||||||
if (auto match = ctre::search<R"((?<=total fat\s)\d+\s*\w+)">(getPod())) {
|
auto m = ctre::search<"(?<=total fat\\s)(\\d+)\\s*(\\w+)">(getPod());
|
||||||
const std::string val = std::string(match.to_view().substr(0, match.begin() - match.end()));
|
double d = m.get<1>().to_number();
|
||||||
std::string unit = std::string(ctre::search<R"(\w+$)">(match.to_view()));
|
std::string u = m.get<2>().to_string();
|
||||||
|
return u == "g" ? d : d / 1000;
|
||||||
return unit == "g" ? std::stod(val) * m_amount : std::stod(val) / 1000 * m_amount;
|
|
||||||
}
|
|
||||||
throw std::runtime_error("Fat information not found.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nlohmann::json WolframAlpha::fetchJson(const std::string& query) {
|
nlohmann::json WolframAlpha::fetchJson(const std::string& query) {
|
||||||
|
|
23
src/main.cpp
23
src/main.cpp
|
@ -1,18 +1,8 @@
|
||||||
#include "ArgParser.hpp"
|
#include "ArgParser.hpp"
|
||||||
#include "Helpers/WolframAlpha.hpp"
|
#include "Helpers/WolframAlpha.hpp"
|
||||||
#include <ctre.hpp>
|
#include <ctre.hpp>
|
||||||
#include <iostream>
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <print>
|
#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[]) {
|
int main(const int argc, char* argv[]) {
|
||||||
try {
|
try {
|
||||||
|
@ -23,14 +13,13 @@ int main(const int argc, char* argv[]) {
|
||||||
args.addArg("-h", "--help", "Print help", false);
|
args.addArg("-h", "--help", "Print help", false);
|
||||||
args.parse(argc, argv);
|
args.parse(argc, argv);
|
||||||
|
|
||||||
// const auto food = args.get<std::string>("--food");
|
const auto food = args.get<std::string>("--food");
|
||||||
// const double amount = args.get<double>("--amount") / 100;
|
const double amount = args.get<double>("--amount") / 100;
|
||||||
|
|
||||||
// if (!args.has("--source") || args.get<std::string>("--source") == "Wolfram") {
|
if (!args.has("--source") || args.get<std::string>("--source") == "Wolfram") {
|
||||||
// WolframAlpha wa(food, amount);
|
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());
|
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");
|
|
||||||
|
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
std::println(stderr, "Error: {}", e.what());
|
std::println(stderr, "Error: {}", e.what());
|
||||||
|
|
Loading…
Reference in New Issue