| 1 |
/*************************************************************************** |
| 2 |
applications.cpp - Librairie d'objets pour creer des applications |
| 3 |
------------------- |
| 4 |
begin : ven aug 14 10:25:55 CEST 2000 |
| 5 |
copyright : (C) 2000 by Olivier Langella CNRS UPR9034 |
| 6 |
email : [email protected] |
| 7 |
***************************************************************************/ |
| 8 |
|
| 9 |
/*************************************************************************** |
| 10 |
* * |
| 11 |
* This program is free software; you can redistribute it and/or modify * |
| 12 |
* it under the terms of the GNU General Public License as published by * |
| 13 |
* the Free Software Foundation; either version 2 of the License, or * |
| 14 |
* (at your option) any later version. * |
| 15 |
* * |
| 16 |
***************************************************************************/ |
| 17 |
|
| 18 |
#include"applications.h" |
| 19 |
|
| 20 |
Application::Application() { |
| 21 |
#ifndef GZSTREAM_H |
| 22 |
_formatMatrice = 1; |
| 23 |
#else |
| 24 |
_formatMatrice = 1; //5 format gnumeric |
| 25 |
#endif |
| 26 |
_confirmeEcraseFichier = true; |
| 27 |
} |
| 28 |
void Application::affPubSortie() const { |
| 29 |
|
| 30 |
cout << endl << endl; |
| 31 |
cout << "Programmation: Olivier Langella" << endl; |
| 32 |
cout << "" << endl; |
| 33 |
cout << "e-mail: [email protected]" << endl; |
| 34 |
cout << "" << endl; |
| 35 |
cout << endl << endl; |
| 36 |
} |
| 37 |
|
| 38 |
void Application::affPubEntree() const { |
| 39 |
|
| 40 |
cout << endl << endl; |
| 41 |
cout << "**************************************" << endl; |
| 42 |
// cout << "* Populations 1.1.00 CNRS UPR9034 *" << endl; |
| 43 |
cout << "* [email protected] *" << endl; |
| 44 |
cout << "* *" << endl; |
| 45 |
cout << "**************************************" << endl; |
| 46 |
cout << endl << endl; |
| 47 |
} |
| 48 |
|
| 49 |
void Application::lancement(int nbcommandes, char ** commandes) { |
| 50 |
|
| 51 |
if (nbcommandes > 1) { |
| 52 |
fLigneCommande(commandes, nbcommandes); |
| 53 |
} else { |
| 54 |
affPubEntree(); |
| 55 |
|
| 56 |
while (menu_principal() != 0) { |
| 57 |
} |
| 58 |
|
| 59 |
affPubSortie(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
void Application::fLigneCommande(char ** commandes, int nbcommandes) { |
| 64 |
affPubEntree(); |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
int Application::menu_principal() { |
| 69 |
int choix; |
| 70 |
cout << endl << endl; |
| 71 |
cout << "0) Quitter" << endl; |
| 72 |
|
| 73 |
/* cout << endl << "choix: "; |
| 74 |
cin >> choix; |
| 75 |
*/ |
| 76 |
choix = DemandeChoix(0, 0); |
| 77 |
|
| 78 |
switch (choix) { |
| 79 |
case 0: |
| 80 |
return (0); |
| 81 |
break; |
| 82 |
// case 1: |
| 83 |
// return(1); |
| 84 |
// break; |
| 85 |
default: |
| 86 |
return (0); |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
int Application::menu_formatMatrice() { |
| 92 |
int choix; |
| 93 |
cout << endl << endl; |
| 94 |
cout << _("Matrix outputfile format:") << endl; |
| 95 |
cout << "1) " << _("ASCII, (excell or gnumeric compliant)") << endl; |
| 96 |
cout << "2) " << _("NtSys") << endl; |
| 97 |
cout << "3) " << _("Phylip") << endl; |
| 98 |
cout << "4) " << _("xgobi") << endl; |
| 99 |
cout << "5) " << _("Gnumeric spreadsheet") << endl; |
| 100 |
|
| 101 |
/* cout << endl << "choix: "; |
| 102 |
cin >> choix; |
| 103 |
*/ |
| 104 |
choix = DemandeChoix(1, 5); |
| 105 |
|
| 106 |
switch (choix) { |
| 107 |
case 1: |
| 108 |
_formatMatrice = 1; |
| 109 |
return (1); |
| 110 |
break; |
| 111 |
case 2: |
| 112 |
_formatMatrice = 2; |
| 113 |
return (2); |
| 114 |
break; |
| 115 |
case 3: |
| 116 |
_formatMatrice = 4; |
| 117 |
return (3); |
| 118 |
break; |
| 119 |
case 4: |
| 120 |
_formatMatrice = 3; |
| 121 |
return (4); |
| 122 |
break; |
| 123 |
case 5: |
| 124 |
_formatMatrice = 5; |
| 125 |
return (5); |
| 126 |
break; |
| 127 |
default: |
| 128 |
return (0); |
| 129 |
break; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
bool Application::litMatriceLD(MatriceLD &mat) { |
| 134 |
bool ok(false); |
| 135 |
|
| 136 |
/* if (*PPmat != NULL) { |
| 137 |
delete *PPmat; |
| 138 |
*PPmat = NULL; |
| 139 |
}*/ |
| 140 |
|
| 141 |
while (_fichier.is_open() == 0) { |
| 142 |
cout << _("Matrix filename ?") << endl; |
| 143 |
cin >> _nomFichier; |
| 144 |
|
| 145 |
_fichier.open(_nomFichier.c_str(), ios::in); |
| 146 |
_fichier.clear(); |
| 147 |
} |
| 148 |
|
| 149 |
try { |
| 150 |
// *PPmat = new MatriceLD(); |
| 151 |
cout << _("Reading file...") << endl; |
| 152 |
|
| 153 |
//cerr << " Application::litMatriceLD(MatriceLD &mat)" << endl; |
| 154 |
//cerr << _fichier; |
| 155 |
// char c; |
| 156 |
// while ( _fichier.get(c)) |
| 157 |
// cerr << c; |
| 158 |
|
| 159 |
mat.iFichier(_fichier); |
| 160 |
_fichier.clear(); |
| 161 |
_fichier.close(); |
| 162 |
ok = true; |
| 163 |
} catch (MatriceLD::Anomalie pb) { |
| 164 |
_fichier.close(); |
| 165 |
#ifdef GZSTREAM_H |
| 166 |
igzstream gzfichier; |
| 167 |
gzfichier.open(_nomFichier.c_str(), ios::in); |
| 168 |
|
| 169 |
try { |
| 170 |
//cerr << " Application::litMatriceLD(MatriceLD &mat) gzfile" << endl; |
| 171 |
// char c; |
| 172 |
// while ( _fichier.get(c)) |
| 173 |
// cerr << c; |
| 174 |
mat.iGnumeric(gzfichier); |
| 175 |
ok = true; |
| 176 |
gzfichier.close(); |
| 177 |
//cerr << " Application::litMatriceLD(MatriceLD &mat)" << endl; |
| 178 |
//mat.oExcel(cerr); |
| 179 |
return(ok); |
| 180 |
} |
| 181 |
catch (MatriceLD::Anomalie pb) { |
| 182 |
ok = false; |
| 183 |
} |
| 184 |
gzfichier.close(); |
| 185 |
#else |
| 186 |
ok = false; |
| 187 |
#endif |
| 188 |
// if (*PPmat != NULL) delete *PPmat; |
| 189 |
|
| 190 |
|
| 191 |
switch (pb.le_pb) { |
| 192 |
default: |
| 193 |
cout << _("This Matrix file format is not recognized...") << endl; |
| 194 |
break; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
return (ok); |
| 199 |
} |
| 200 |
|
| 201 |
bool Application::ecritMatriceLD(MatriceLD &mat, string nomFichier, int format) { |
| 202 |
bool ok(false); |
| 203 |
ofstream sortie; |
| 204 |
|
| 205 |
if (format == 0) |
| 206 |
format = _formatMatrice; |
| 207 |
|
| 208 |
/* if (*PPmat != NULL) { |
| 209 |
return (false); |
| 210 |
}*/ |
| 211 |
//cerr << format; |
| 212 |
//_sortie << ends; |
| 213 |
//_sortie.close(); |
| 214 |
|
| 215 |
while (nomFichier == "") { |
| 216 |
cout << _("Matrix filename to write into ?") << endl; |
| 217 |
cin >> nomFichier; |
| 218 |
} |
| 219 |
|
| 220 |
sortie.open(nomFichier.c_str(), ios::out); |
| 221 |
|
| 222 |
//sortie << "coucou application::ecritMatriceLD"; |
| 223 |
// cerr << "coucou pplication::ecritMatriceLD"; |
| 224 |
// _sortie << ends; |
| 225 |
// _sortie.close(); |
| 226 |
|
| 227 |
try { |
| 228 |
mat.ofFormat(sortie, format, nomFichier); |
| 229 |
//sortie << ends; |
| 230 |
sortie.close(); |
| 231 |
ok = true; |
| 232 |
} catch (MatriceLD::Anomalie pb) { |
| 233 |
sortie.close(); |
| 234 |
ok = false; |
| 235 |
|
| 236 |
switch (pb.le_pb) { |
| 237 |
default: |
| 238 |
cout << _("Error writing matrix") << " MatriceLD::Anomalie " |
| 239 |
<< pb.le_pb << endl; |
| 240 |
break; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return (ok); |
| 245 |
} |
| 246 |
|
| 247 |
bool Application::DemandeOuiNon(char laquestion[]) const { |
| 248 |
string rep; |
| 249 |
|
| 250 |
cout << laquestion << endl; |
| 251 |
cin >> rep; |
| 252 |
// cout << endl; |
| 253 |
if (rep == "") |
| 254 |
rep.assign("N"); |
| 255 |
|
| 256 |
switch (rep[0]) { |
| 257 |
case 'Y': |
| 258 |
return (true); |
| 259 |
break; |
| 260 |
case 'y': |
| 261 |
return (true); |
| 262 |
break; |
| 263 |
case 'o': |
| 264 |
return (true); |
| 265 |
break; |
| 266 |
case 'O': |
| 267 |
return (true); |
| 268 |
break; |
| 269 |
default: |
| 270 |
return (false); |
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
string Application::DemandeString(char laquestion[]) const { |
| 276 |
string rep(""); |
| 277 |
|
| 278 |
while (rep == "") { |
| 279 |
cout << endl << laquestion << endl; |
| 280 |
cin >> rep; |
| 281 |
} |
| 282 |
return (rep); |
| 283 |
} |
| 284 |
|
| 285 |
string Application::DemandeFichier(const string & laquestion) { |
| 286 |
string rep(""); |
| 287 |
bool ok(false); |
| 288 |
|
| 289 |
while ((rep == "") || (ok == false)) { |
| 290 |
ok = false; |
| 291 |
cout << endl << laquestion << endl; |
| 292 |
cin >> rep; |
| 293 |
if ((rep != "") && (_confirmeEcraseFichier) && (fFichierExiste( |
| 294 |
rep.c_str()))) { |
| 295 |
cout << _("The file ") << rep << _(" already exist.") << endl; |
| 296 |
if (DemandeOuiNon(_("Choose Y if you to overwrite it: "))) |
| 297 |
ok = true; |
| 298 |
} else |
| 299 |
ok = true; |
| 300 |
} |
| 301 |
|
| 302 |
return (rep); |
| 303 |
} |
| 304 |
|
| 305 |
float Application::DemandeReel(char laquestion[], float inf, float sup) const { |
| 306 |
float reel(-9999); |
| 307 |
string rep; |
| 308 |
|
| 309 |
while ((reel < inf) || (reel > sup)) { |
| 310 |
cout << endl << laquestion << endl; |
| 311 |
cin >> rep; |
| 312 |
reel = atof(rep.c_str()); |
| 313 |
} |
| 314 |
return (reel); |
| 315 |
} |
| 316 |
|
| 317 |
int Application::DemandeEntier(const string & laquestion, int inf, int sup) const { |
| 318 |
int entier(-999); |
| 319 |
string rep; |
| 320 |
|
| 321 |
while ((entier < inf) || (entier > sup)) { |
| 322 |
cout << endl << laquestion << endl; |
| 323 |
cin >> rep; |
| 324 |
entier = atoi(rep.c_str()); |
| 325 |
} |
| 326 |
return (entier); |
| 327 |
} |
| 328 |
|
| 329 |
int Application::DemandeEntier(char laquestion[], int inf, int sup, int defaut) const { |
| 330 |
int entier(-999); |
| 331 |
char car; |
| 332 |
string mot; |
| 333 |
if ((defaut < inf) || (defaut > sup)) |
| 334 |
cerr << "DemandeEntier defaut ERROR" << endl; |
| 335 |
|
| 336 |
while ((entier < inf) || (entier > sup)) { |
| 337 |
cout << endl << laquestion << endl; |
| 338 |
cin.get(car); |
| 339 |
cin.get(car); |
| 340 |
// rep[0] = rep[0]+50; |
| 341 |
// cout << rep; |
| 342 |
if (car == '\n') |
| 343 |
entier = defaut; |
| 344 |
else { |
| 345 |
cin >> mot; |
| 346 |
mot = car + mot; |
| 347 |
entier = atoi(mot.c_str()); |
| 348 |
} |
| 349 |
} |
| 350 |
return (entier); |
| 351 |
} |
| 352 |
|
| 353 |
int Application::DemandeChoix(int inf, int sup) const { |
| 354 |
|
| 355 |
return (DemandeEntier(_("Your choice: "), inf, sup)); |
| 356 |
} |
| 357 |
|
| 358 |
bool Application::litJeuMatriceLD(JeuMatriceLD &jeumat) { |
| 359 |
bool ok(false); |
| 360 |
|
| 361 |
/* if (*PPmat != NULL) { |
| 362 |
delete *PPmat; |
| 363 |
*PPmat = NULL; |
| 364 |
}*/ |
| 365 |
|
| 366 |
while (_fichier.is_open() == 0) { |
| 367 |
cout << _("Filename of the set of matrix ?") << endl; |
| 368 |
cin >> _nomFichier; |
| 369 |
|
| 370 |
_fichier.open(_nomFichier.c_str(), ios::in); |
| 371 |
_fichier.clear(); |
| 372 |
} |
| 373 |
|
| 374 |
try { |
| 375 |
// *PPmat = new MatriceLD(); |
| 376 |
cout << _("Reading file...") << endl; |
| 377 |
jeumat.iFlux(_fichier); |
| 378 |
_fichier.clear(); |
| 379 |
_fichier.close(); |
| 380 |
ok = true; |
| 381 |
} catch (MatriceLD::Anomalie pb) { |
| 382 |
_fichier.close(); |
| 383 |
ok = false; |
| 384 |
// if (*PPmat != NULL) delete *PPmat; |
| 385 |
|
| 386 |
switch (pb.le_pb) { |
| 387 |
default: |
| 388 |
cout << _("This file format is not recognized...") << endl; |
| 389 |
break; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
return (ok); |
| 394 |
} |
| 395 |
|
| 396 |
bool Application::ecritJeuMatriceLD(const JeuMatriceLD &jeumat, |
| 397 |
string nomFichier, int format) { |
| 398 |
bool ok(false); |
| 399 |
//string nomFichier; |
| 400 |
|
| 401 |
if (format == 0) |
| 402 |
format = _formatMatrice; |
| 403 |
|
| 404 |
/* if (*PPmat != NULL) { |
| 405 |
return (false); |
| 406 |
}*/ |
| 407 |
//cerr << format; |
| 408 |
_sortie << ends; |
| 409 |
_sortie.close(); |
| 410 |
|
| 411 |
while (_sortie.is_open() == 0) { |
| 412 |
while (nomFichier == "") { |
| 413 |
cout << _("Name of the file to write the set of matrix ?") << endl; |
| 414 |
cin >> nomFichier; |
| 415 |
} |
| 416 |
_sortie.open(_nomFichier.c_str(), ios::out); |
| 417 |
} |
| 418 |
|
| 419 |
try { |
| 420 |
nomFichier = _nomFichier; |
| 421 |
jeumat.oFlux(_sortie, format); |
| 422 |
_sortie << ends; |
| 423 |
_sortie.close(); |
| 424 |
ok = true; |
| 425 |
} catch (MatriceLD::Anomalie pb) { |
| 426 |
_sortie.close(); |
| 427 |
ok = false; |
| 428 |
|
| 429 |
switch (pb.le_pb) { |
| 430 |
default: |
| 431 |
cout << _("Writing error...") << endl; |
| 432 |
break; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
return (ok); |
| 437 |
} |
| 438 |
/** test si nomfichier existe */ |
| 439 |
bool Application::fFichierExiste(const string & nomfichier) { |
| 440 |
_fichier.open(nomfichier.c_str(), ios::in); |
| 441 |
|
| 442 |
if (_fichier.is_open() == 0) { |
| 443 |
_fichier.clear(); |
| 444 |
return (false); |
| 445 |
} |
| 446 |
_fichier.close(); |
| 447 |
return (true); |
| 448 |
} |