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

View File

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

View File

@ -2,5 +2,5 @@
#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);