Debian Codegraph @

src/vecteurs.cpp

raw
1 /***************************************************************************
2 vecteurs.cpp - Librairie d'objets permettant de manipuler des vecteurs
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 <strstream>
19
20 //using namespace std;
21
22 #include"vecteurs.h"
23
24 namespace biolib {
25 namespace vecteurs {
26
27 void GetLigneFlot(istream& entree, string & ligne) {
28 char car;
29 ligne.assign("");
30 car = entree.peek();
31 while (((car != 10) & (car != 13)) & (entree.good()) & (entree.eof() == 0)) {
32 entree.get(car);
33 ligne += car;
34 car = entree.peek();
35 }
36 while (((car == 10) | (car == 12) | (car == 13)) & entree.good()
37 & (entree.eof() == 0)) {
38 entree.get(car);
39 car = entree.peek();
40 }
41 }
42
43 void ChaineCar::GetLigneFlot(istream& entree) {
44 //cerr << "ChaineCar::GetLigneFlot debut" << endl;
45
46 char car;
47
48 assign("");
49 car = entree.peek();
50 //cerr << "ChaineCar::GetLigneFlot debut " << car << endl;
51 while (((car != 10) && (car != 13)) && (entree.good())) {
52
53 entree.clear();
54
55 entree.get(car);
56
57 //cerr << "ChaineCar::GetLigneFlot car" << car << endl;
58 assign(*this + car);
59 car = entree.peek();
60 }
61 while (((car == 10) || (car == 13)) && (entree.good())) {
62 entree.get(car);
63 car = entree.peek();
64 }
65
66 //cerr << "ChaineCar::GetLigneFlot fin" << endl;
67 }
68
69 void ChaineCar::assigner(const string& chorigine, signed long deb,
70 signed long fin) {
71 //assigne seuleument une partie de la chorigine
72 // de deb a fin inclus
73
74 signed long i;
75
76 assign("");
77 if (deb < 0)
78 deb = 0;
79
80 for (i = deb; i <= fin; i++) {
81 if (i >= (signed long) chorigine.size())
82 break;
83 *this += chorigine[i];
84 // cout << chorigine[i];
85 }
86 // cout << endl;
87 }
88
89 bool ChaineCar::EstUnChiffre() const {
90 unsigned long i;
91
92 //cerr << "ChaineCar::EstUnChiffre()" << endl;
93 for (i = 0; i < size(); i++) {
94 if ((operator[](i) < 48) || (operator[](i) > 57))
95 return (false);
96 }
97
98 //cerr << "ChaineCar::EstUnChiffre() vrai" << endl;
99 return (true);
100 }
101
102 ChaineCar& ChaineCar::AjEntier(long i) {
103 //conversion automatique en base 10 et concaténation
104 int unite;
105 long facteur;
106 char chiffre;
107 bool debut(false);
108
109 if (i == 0) {
110 this->string::operator+=("0");
111 } else
112 for (facteur = 1000000; facteur > 0; facteur = facteur / 10) {
113
114 unite = i / facteur;
115
116 if ((debut) || (unite > 0)) {
117 debut = true;
118 i = i - (unite * facteur);
119 chiffre = unite + 48;
120 this->string::operator+=(chiffre);
121 }
122 }
123 return (*this);
124 }
125
126 long ChaineCar::GetNbMots() const {
127 //retourne le nombre de mots contenus
128 // dans la chaine (séparateur= ' ' ou '/t')
129 long resultat(0);
130 long i, taille;
131 bool change(true);
132 bool quote(false);
133
134 taille = this->size();
135
136 //cerr << "ChaineCar::GetNbMots()" << *this <<"Fin" << endl;
137 for (i = 0; i < taille; i++) {
138 if (this->operator[](i) == '\'')
139 quote = !(quote);
140 if ((quote == false) && (this->operator[](i) > 32)) {
141 if (change) {
142 //cerr << this->operator[](i) << endl;
143 resultat++;
144 change = false;
145 }
146 } else {
147 change = true;
148 }
149 }
150
151 //cerr << *this << endl;
152 //cerr << "ChaineCar::GetNbMots()" << resultat << endl;
153 return (resultat);
154 }
155
156 void ChaineCar::GetMot(unsigned int numero, string &mot) const {
157 //retourne le numeroième mot contenu
158 // dans la chaine (séparateur= ' ' ou '/t')
159 if (numero < 1) {
160 mot = "";
161 return;
162 }
163
164 unsigned int resultat(0);
165 unsigned int i, taille, debut(0), fin(0);
166 bool change(true);
167 bool quote(false);
168
169 taille = this->size();
170
171 //cerr << "mot:" << *this <<"Fin" << endl;
172 for (i = 0; i < taille; i++) {
173 //cerr << this->operator[](i) << endl;
174 if (this->operator[](i) == '\'') {
175 if ((resultat == numero) && (quote)) {
176 fin = (i - 1);
177 break;
178 }
179 quote = !(quote);
180 if ((resultat == numero) && (quote)) {
181 debut = (i + 1);
182 // break;
183 }
184 }
185 if ((quote) || (this->operator[](i) > 32)) {
186 //caractere normal
187 if (change) {
188 change = false;
189 resultat++;
190 debut = i;
191 if (quote)
192 debut++;
193 }
194 fin = i;
195 } else {
196 if (resultat == numero)
197 break;
198 change = true;
199 }
200 }
201 fin++;
202
203 if (resultat != numero) {
204 mot = "";
205 return;
206 }
207
208 mot = substr(debut, (fin - debut));
209 //cerr << "mot:" << mot <<"Fin" << endl;
210
211 }
212
213 const Titre& Titre::operator=(const Titre &rval) {
214 unsigned long t(rval.size());
215 unsigned long i;
216
217 for (i = 0; i < size(); i++) {
218 delete vector<ChaineCar*>::operator[](i);
219 // delete at(i);
220 }
221
222 erase(begin(), end());
223 reserve(t);
224 for (i = 0; i < t; i++) {
225 push_back(rval.GetTitre(i));
226 }
227 return (*this);
228 }
229
230 Titre::~Titre() {
231 unsigned long i;
232
233 for (i = 0; i < size(); i++) {
234 delete vector<ChaineCar*>::operator[](i);
235 // delete at(i);
236 }
237 }
238
239 long Titre::Position(const ChaineCar& chaine) const {
240 unsigned long i;
241 signed long res(-1);
242
243 for (i = 0; i < size(); i++) {
244 if (!(chaine.compare(GetTitre(i)))) {
245 //cerr << chaine << GetTitre(i) << "_" << endl;
246 // if (chaine.compare(GetTitre(i)) == 0) {
247 res = i;
248 break;
249 }
250 }
251
252 return (res);
253 }
254 ;
255
256 void Titre::GetArguments(const string & ligne) {
257 //doit se comporter comme le shell
258 // sépare les mots, et accepte les ' ou les "
259 unsigned int i;
260 bool quote(false), vide(false);
261
262 push_back("");
263
264 for (i = 0; i < ligne.size(); i++) {
265 if ((quote == false) && ((back().size() > 0) || vide) && ((ligne[i]
266 == ' ') || (ligne[i] == '\t'))) {//séparateur
267 push_back("");
268 vide = false;
269 } else {
270 if ((ligne[i] == '"') || (ligne[i] == '\'')) {
271 if ((quote) && (back().size() == 0))
272 vide = true;
273 quote = !(quote);
274 } else
275 back() += ligne[i];
276 }
277 }
278 /*
279 cerr << "Titre::GetArguments(const string & ligne)" << endl;
280 for (i=0; i < size(); i++) {
281 cerr << get_titre(i) << endl;
282 }
283 */
284 }
285
286 bool Titre::operator==(const Titre &rval) {
287 // comparaison de deux titres
288 unsigned long i;
289
290 if (rval.size() != size())
291 return (false);
292
293 for (i = 0; i < size(); i++) {
294 if (GetTitre(i).compare(rval.GetTitre(i)) != 0)
295 return (false);
296 }
297
298 return (true);
299 }
300
301 Titre::Titre(char ** commandes, int nbcommandes) {
302 long i;
303
304 for (i = 0; i < nbcommandes; i++) {
305 push_back(commandes[i]);
306 }
307 }
308
309 /*
310 Titre::Titre(const Titre& rval) {
311
312 *this = rval;
313
314 }
315 */
316
317 void Titre::resize(long nouvtaille) {
318 //on efface:
319 long i, taille(size());
320
321 for (i = 0; i < taille; i++)
322 delete vector<ChaineCar*>::operator[](i);
323
324 //on réalloue
325 vector<ChaineCar*>::resize(nouvtaille);
326 for (i = 0; i < nouvtaille; i++)
327 vector<ChaineCar*>::operator[](i) = new ChaineCar;
328
329 }
330
331 Titre Titre::operator+(const Titre &rval) const {
332 //marche pas...
333 long i, taille(rval.size());
334 Titre Resultat;
335
336 Resultat = *this;
337
338 for (i = 0; i < taille; i++) {
339 //cerr << rval.GetTitre(i);
340 Resultat.push_back(rval.GetTitre(i));
341 }
342
343 return Resultat;
344 }
345
346 void ChaineCar::fmajus() {
347 //convertit la chaine en majuscules
348 // après une lecture par exemple
349 int taille(size());
350 int i;
351
352 for (i = 0; i < taille; i++) {
353 if ((operator[](i) > 96) && (operator[](i) < 124))
354 operator[](i) = operator[](i) - 32;
355 }
356 }
357
358 void ChaineCar::fsupprchiffres() {
359 //supprime les chiffres contenu dans une chaine
360 // après une lecture par exemple
361 int taille(size() - 1);
362 int car, i;
363
364 for (i = taille; i >= 0; i--) {
365 car = operator[](i);
366 if ((car > 47) && (car < 58))
367 erase(i, 1);
368 }
369 }
370
371 void ChaineCar::fsupprgauche() {
372 //supprime les espaces ou tabulations à gauche
373 // unsigned int i;
374 //cerr << "ChaineCar::fsupprgauche() [" << (int) operator[](0) << "]" << *this << endl;
375
376 // i = 0;
377 // while (((operator[](i) == ' ')||(operator[](i) == '\t')) && (i < size())) {
378 while (((operator[](0) == ' ') || (operator[](0) == '\t')) && (size() > 0)) {
379 //while ((operator[](i) < 33) && (i < size())) {
380 erase(0, 1);
381 // i++;
382 }
383 //cerr << "ChaineCar::fsupprgauche() [" << *this << endl;
384 }
385
386 void ChaineCar::fsupprdroite() {
387 //supprime les espaces ou tabulations à droite
388 signed int i;
389
390 i = size() - 1;
391 while (((operator[](i) == ' ') || (operator[](i) == '\t')) && (i >= 0)) {
392 //while ((operator[](i) < 33) && (i >= 0)) {
393 erase(i, 1);
394 i--;
395 }
396 }
397
398 void ChaineCar::fnettoie(const string & mot) {
399 //supprime de la chaine de caractere toutes les occurences de "mot"
400
401 int pos;
402
403 pos = find(mot, 0);
404 while (pos != -1) {
405 replace(pos, mot.size(), "");
406 pos = find(mot, 0);
407 }
408 }
409
410 unsigned long ChaineCar::GetNbMots(const string & separateur) const {
411 //retourne le nombre de mot separes par separateur
412
413 long pos;
414 unsigned long iteration(0);
415
416 if (size() == 0)
417 return (0);
418
419 pos = find(separateur, 0);
420 while (pos != -1) {
421 pos = find(separateur, pos + 1);
422 iteration++;
423 }
424
425 return (iteration + 1);
426 }
427
428 void ChaineCar::GetMot(unsigned int numero, string &mot,
429 const string & separateur) const {
430 //retourne le nieme mot separe par separateur
431
432 signed int debut(0);
433 int fin(-1);
434 unsigned int iteration(1);
435 int taillesep(separateur.size());
436 mot.assign("");
437
438 fin = find(separateur, 0);
439 if ((fin == -1) && (numero < 2)) {
440 mot.assign(*this);
441 return;
442 }
443 if (fin == -1)
444 fin = size();
445 while ((debut != -1) && (iteration < numero)) {
446 debut = fin + taillesep;
447 //cerr << "debut " << debut << endl;
448 fin = find(separateur, debut);
449 if (fin == -1)
450 fin = size();
451 //else fin = pos;
452 iteration++;
453 }
454
455 if (debut == -1)
456 return;
457 if (debut > (signed int) size())
458 return;
459 if (fin > (signed int) size())
460 return;
461 if (debut > fin)
462 return;
463
464 mot = substr(debut, (fin - debut));
465 }
466
467 ChaineCar& Titre::GetTitre(long i) const {
468 return (*(vector<ChaineCar*>::operator[](i)));
469 }
470
471 void Titre::Suppr(unsigned long pos) {
472 if (pos >= size())
473 throw Anomalie(1);
474 delete (vector<ChaineCar*>::operator[](pos));
475 erase(begin() + pos);
476 }
477
478 /** remplacer les occurences de "couper" par "coller" */
479 void ChaineCar::Remplacer(const string & couper, const string & coller) {
480
481 int pos;
482
483 pos = find(couper, 0);
484 while (pos != -1) {
485 replace(pos, couper.size(), coller);
486 pos = find(couper, 0);
487 }
488 }
489
490 int ChaineCar::Position(const string & motif) const {
491 return (find(motif, 0));
492 }
493
494 }//namespace biolib {
495 }//namespace vecteurs {
496
497