opti: price and quantity is now store in string

This commit is contained in:
starnakin 2023-10-11 20:06:28 +00:00
parent 9bf46c857c
commit 575979c497
3 changed files with 34 additions and 30 deletions

View File

@ -2,6 +2,8 @@
#include <dpp/unicode_emoji.h> #include <dpp/unicode_emoji.h>
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <string>
#include "config.h" #include "config.h"
#include "utils.hpp" #include "utils.hpp"
@ -29,16 +31,15 @@ void on_message(dpp::cluster& bot)
std::string str = event.msg.content; std::string str = event.msg.content;
unsigned int quantity; std::string quantity, price;
float price;
get_article_data(str, quantity, price); get_article_data(str, quantity, price);
if (quantity != 0) if (!quantity.empty())
embed.add_field("Quantity", std::to_string(quantity)); embed.add_field("Quantity", quantity);
if (price != 0) if (!price.empty())
embed.add_field("Price", std::to_string(price) + ""); embed.add_field("Price", price);
embed.set_title(str); embed.set_title(str);

View File

@ -6,50 +6,53 @@
#include "config.h" #include "config.h"
static float get_price(std::string& str) static std::string get_price(std::string& str)
{ {
std::string price;
float price = 0; size_t i;
const char* str_c = str.c_str();
char* next;
for (size_t i = 0; i != str.size(); i++) for (i = 0; i != str.size(); i++)
{ {
price = std::strtof(str_c + i, &next); if (std::strcmp(str.substr(i, 3).c_str(), "") == 0)
if (next == NULL)
break;
if (std::strncmp(next, "", 3) == 0)
{ {
str.erase(i, next - (str_c + i) + 3); if (price.empty())
return price; {
price = "";
continue;
}
price += "";
break;
} }
else if (std::isdigit(str[i]) || str[i] == '.')
price += str[i];
} }
return 0; str.erase(i - (price.size() - 3), price.size());
return price;
} }
static int get_quantity(std::string& str) static std::string get_quantity(std::string& str)
{ {
unsigned int quantity = 0; size_t i;
size_t len = 0, i; std::string quantity;
for (i = 0; i != str.size(); i++) for (i = 0; i != str.size(); i++)
{ {
if (!std::isdigit(str[i])) if (!std::isdigit(str[i]))
{ {
if (quantity != 0) if (quantity.empty())
break; continue;
continue; break;
} }
quantity = quantity * 10 + str[i] - '0'; quantity += str[i];
len++;
} }
str.erase(i - len, len); str.erase(i - quantity.size(), quantity.size());
return quantity; return quantity;
} }
int get_article_data(std::string& str, unsigned int& quantity, float& price) int get_article_data(std::string& str, std::string& quantity, std::string& price)
{ {
price = get_price(str); price = get_price(str);
std::cout << str << std::endl;
quantity = get_quantity(str); quantity = get_quantity(str);
return 0; return 0;

View File

@ -2,5 +2,5 @@
#include <string> #include <string>
int get_article_data(std::string& str, unsigned int& quantity, float& price); int get_article_data(std::string& str, std::string& quantity, std::string& price);
std::string get_token(char** av, char** env); std::string get_token(char** av, char** env);