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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
#include "timFile.h"
timFile::timFile() {
m_s_path_file.clear();
}
timFile::timFile(std::string path_file) {
m_s_path_file = path_file;
}
timFile::~timFile() {
if(m_file.is_open()) {
m_file.close();
}
}
bool timFile::open() {
// On regarde si l'utilisateur a spécifié un fichier à ouvrir
if(m_s_path_file.empty()) {
return false;
}
// On regarde si le fichier est déjà ouvert en lecture
if(m_file.is_open()) {
return false;
}
// Sinon, on ouvre le fichier
else {
m_file.open(m_s_path_file.c_str(),
std::ios::in | std::ios::out | std::ios::ate);
}
// On teste si le fichier a bien été ouvert
if(m_file == NULL) {
return false;
}
return true;
}
void timFile::close() {
m_file.close();
}
bool timFile::is_bloc(std::string name) {
char carac;
std::string s_string;
// Remise à 0 du curseur
m_file.seekg(0, std::ios::beg);
// On teste si le fichier a d'abord été ouvert
if(!m_file.is_open()) {
return false;
}
while(m_file.get(carac)) {
// On regarde si la ligne est un commentaire
if(carac == '#') {
// Si oui, alors on la saute
while(m_file.get(carac)) {
if(carac == '\n') {
break;
}
}
}
if(carac == '[') {
s_string.clear();
while(m_file.get(carac)) {
if(carac != ']' && carac != EOF && carac != '\n') {
s_string += carac;
}
else {
break;
}
}
}
// On compare ensuite la chaine obtenue avec le nom de bloc à trouver
if(name.compare(s_string) == 0) {
return true;
}
}
return false;
}
bool timFile::is_value(std::string bloc, std::string value) {
char carac;
std::string s_string;
// On teste si le fichier a d'abord été ouvert
if(!m_file.is_open()) {
return false;
}
// On teste si le bloc existe
if(!is_bloc(bloc)) {
return false;
}
// On cherche dans le bloc jusqu'à ce qu'on trouve la valeur sélectionnée
while(m_file.get(carac)) {
// Tant qu'on ne rencontre pas l'opérateur d'assignation
if(carac != '=' && carac != ' ' && carac != '\n') {
s_string += carac;
}
// Sinon, on vérifie si on a trouvé la bonne valeur puis on saute la ligne
// si ce n'est pas elle
else if(carac == '=') {
if(s_string.compare(value) == 0) {
return true;
}
s_string.clear();
while(m_file.get(carac)) {
if(carac == ';') {
break;
}
}
}
}
return false;
}
std::string timFile::get_value(std::string bloc_name, std::string value_name) {
char carac;
std::string s_string;
if(!is_value(bloc_name, value_name)) {
return "-1";
}
s_string.clear();
while(m_file.get(carac)) {
if(carac != ';') {
s_string += carac;
}
else {
break;
}
}
return s_string;
}
std::string* timFile::get_avalue(std::string bloc_name, std::string value_name) {
int pos_cursor = 0;
int nb_value = 0;
int i = 0;
char carac;
std::string* a_s_string;
// On vérifie que la valeur existe
if(!is_value(bloc_name, value_name)) {
return NULL;
}
// On enregsitre la position du curseur
pos_cursor = m_file.tellg();
// On compte le nombre de valeurs à placer dans le tableau
while(m_file.get(carac)) {
if(carac == '|') {
nb_value++;
}
if(carac == ';') {
nb_value++;
break;
}
}
// On alloue le tableau
a_s_string = new std::string[nb_value + 1];
// On repositionne le curseur
m_file.seekg(pos_cursor, std::ios::beg);
// On met les valeurs dans le tableau
while(m_file.get(carac)) {
if(carac != '|' && carac != ';') {
a_s_string[i] += carac;
}
else if(carac == '|'){
i++;
}
else {
break;
}
}
a_s_string[i + 1] = "END";
return a_s_string;
}
bool timFile::set_bloc(std::string bloc) {
std::string s_string;
s_string.clear();
// On vérifie que le bloc n'existe pas déjà
if(is_bloc(bloc)) {
return false;
}
m_file.close();
m_file.open(m_s_path_file.c_str(),
std::ios::in | std::ios::out | std::ios::ate);
// On créé la chaine
s_string += '[';
s_string += bloc;
s_string += ']';
// On se place à la fin du fichier
m_file.seekp(0, std::ios::end);
// On écrit la chaine dans le fichier
if(m_file << s_string) {
return true;
}
return false;
} |
Partager