[C++11] Correction / Optimisation - Extraction de donn�e d'un string
Bonjour � tous, j'ai cr�� une petite fonction nomm�e extractStrInfo qui permet de r�cup�rer une donn�e venant d'une cha�ne de caract�re en renvoyant cette donn�e avec le bon type associ�. Le probl�me est que je ne sais pas r�cup�rer ma donn�e :/ d�ailleurs je ne sais pas non plus si je m'y prends de la bonne fa�on, je n'utilise presque jamais les templates.
Voici tout le code que j'ai fait :
function.hpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#ifndef Function_included
#define Function_included
#include <string>
#include <sstream>
#include <vector>
#include <ctype.h>
#include <algorithm>
#include "Item.hpp"
namespace xy
{
void split( std::string const &s, char delim, std::vector<std::string> &elems );
template <class T>
T extractStrInfo( std::string const &str, char delim, unsigned int pos );
bool isNumber( std::string const &str );
bool isBool( std::string const &str );
std::string toLower( std::string const &str );
std::string toUpper( std::string const &str );
bool toBool( std::string const &str );
};
#endif // Function_included |
function.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include "function.hpp"
void xy::split( std::string const &s, char delim, std::vector<std::string> &elems )
{
std::stringstream ss;
ss.str(s);
std::string str;
while(std::getline(ss, str, delim))
{
elems.push_back(str);
}
}
template <class T>
T xy::extractStrInfo( std::string const &str, char delim, unsigned int pos )
{
std::vector<std::string> elems;
xy::split( str, delim, elems );
if( pos > elems.size() - 1 )
{
throw std::string("[Extractor] Element out of reach " + static_cast<std::string>(pos) + '/' + static_cast<std::string>(elems.size() - 1) + '.');
}
else
{
if( xy::isNumber(elems[pos]) )
return std::stoi(elems[pos]);
else if( xy::isBool(elems[pos]));
return xy::toBool(elems[pos]);
return elems[pos];
}
}
bool xy::isNumber( std::string const &str )
{
for( size_t i{0}; i < str.size(); ++i )
{
if(!isdigit(str[i]) && str[i] != '-')
return false;
}
return true;
}
bool xy::isBool( std::string const &str )
{
if( xy::toLower(str) == "true" || xy::toLower(str) == "false" )
return true;
return false;
}
std::string xy::toLower( std::string const &str )
{
std::string n{str};
std::transform(n.begin(), n.end(), n.begin(), tolower);
return n;
}
std::string xy::toUpper( std::string const &str )
{
std::string n{str};
std::transform(n.begin(), n.end(), n.begin(), toupper);
return n;
}
bool xy::toBool( std::string const &str )
{
if( !xy::isBool(str) )
throw std::string("[ToBool] " + str + " is not convertible.");
else
{
if( xy::toLower(str) == "true" )
return true;
return false;
}
} |
En faite j'ai une BDD contenant des items, elle ressemble � ceci :
Code:
1 2 3 4 5
|
645;50;Amulet;Amulette Royale du Bouftou;true;0;0;0;0;30;30;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
646;50;Amulet;Amuronce;true;40;0;0;-15;20;0;20;0;0;1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0
647;50;Amulet;Amublop Reinette;true;40;0;0;15;25;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;5;0;0
.... |
J'ai une classe Item et je souhaite charger depuis cette BDD toutes ses caract�ristiques.
Bref. En l�occurrence la fonction extract me servira juste pour r�cup�rer des infos pour debug, je ferais une autre fonction presque similaire pour charger directement un Item mais encore faudrait-il que je comprenne comment faire la conversion x).
Voil� voil� donc si vous pouvez m'expliquer comment faire ce serait sympa ^^". En soit le code fonctionne mais, il ne fait peut-�tre pas ce que je souhaite r�ellement ou bien je ne sais juste pas me servir de ce que j'ai cr�� ( il faut le faire quand m�me :mouarf: ).
Je vous remercie de bien vouloir m'aider.
Disixlis
Ps : Je ne souhaitais pas utiliser d'api comme mysql connector, car je veux apprendre par moi m�me et cela fait un bon exercice. || Je ne veux pas d'un bloc de code en guise de solution, par contre un bloc d'explications serait sympa :)