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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
//FileConfig.h
#ifndef FILECONFIG_H_
#define FILECONFIG_H_
#include "File.h"
#include "configFileMacros.h"
#include <fstream>
#include <vector>
using namespace std;
enum dataType {
INT,
FLOAT,
DATE,
SET,
STRING
};
struct instructions {
int lineNumber;
dataType type;
};
class FileConfig: public File {
public:
FileConfig(string _path);
virtual ~FileConfig();
using File::read;
int keyToInstructions(int key);
void setInstructions(int _lineNumber, dataType _type) {
m_instructions.lineNumber = _lineNumber;
m_instructions.type = _type;
}
vector<string> stringSplit(string str, char separateur = ';');
string removeSpaces( string stringIn );
template<typename T>
bool fromString( const string & str, T & Dest ) //transforme la string vers un autre type
{
istringstream iss( str ); // créer un flux à partir de la chaîne donnée
return iss >> Dest != 0; // tenter la conversion vers Dest
}
template<typename T>
int read(int key, T & sortie) {
keyToInstructions(key);
ifstream file(path.c_str());
vector<string> linesFull;
string stringTmp;
while (getline(file, stringTmp))
linesFull.push_back(stringTmp);
file.close();
for(unsigned int i = 0; i < linesFull.size(); i++) {
removeSpaces(linesFull.at(i));
if(linesFull[i].at(0) == ';') linesFull.at(i).erase();
}
vector<string> linesPart = stringSplit(linesFull[m_instructions.lineNumber]);
vector<string> dateTmp = stringSplit(linesPart.front(), ',');
vector<int> date;
switch(m_instructions.type) {
case INT:
int result_int;
sortie = fromString(linesPart.front(), result_int);
return (1);
case FLOAT:
float result_float;
sortie = fromString(linesPart.front(), result_float);
return (1);
case STRING:
//sortie = linesPart.front();
return (1);
case DATE:
for(int i = 0; i < (int) dateTmp.size(); i++)
int test = atoi(dateTmp[i].c_str());
//date.push_back(atoi(dateTmp[i].c_str()));
sortie = date;
return (1);
case SET:
sortie = stringSplit(linesPart.front(), ',');
return (1);
default: return (0);
}
}
protected:
struct instructions m_instructions;
string path;
};
#endif /* FILECONFIG_H_ */ |
Partager