TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
Nom.cpp
1/****************************************************************************
2* Copyright (c) 2026, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#include <Nom.h>
17#include <stdio.h>
18#include <string>
19#include <math.h>
20#include <algorithm>
21#include <cmath>
22
23Implemente_instanciable_sans_constructeur_ni_destructeur(Nom,"Nom",Objet_U);
24// XD nom objet_u nom NO_BRACE Class to name the TRUST objects.
25// XD attr mot chaine mot OPT Chain of characters.
26
27/*! @brief Surcharge Objet_U::printOn(Sortie&) Ecriture d'un Nom sur un flot de sortie
28 *
29 * @param (Sortie& s) le flot de sortie a utiliser
30 * @return (Sortie&) le flot de sortie modifie
31 */
32Sortie& Nom::printOn(Sortie& s) const
33{
34 const char* nom=getChar();
35 if(nom)
36 return s << nom;
37 else
38 return s;
39}
40#define BUFLEN 100000
41
42/*! @brief Lecture d'un nom.
43 *
44 * En cas d'echec, le nom vaut "??" a la sortie.
45 *
46 */
48{
49 char buffer[BUFLEN];
50 const int ok = s.get(buffer, BUFLEN);
51 if (ok)
52 operator=(buffer);
53 else
54 operator=("??");
55 return s;
56}
57
58/*! @brief Constructeur par defaut.
59 *
60 * Cree la chaine "??"
61 *
62 */
64{
65 nom_ = "??";
66}
67
68/*! @brief Construction d'un nom a partir d'un caractere
69 *
70 * @param (char c) le caractere du nom
71 */
72Nom::Nom(char c)
73{
74 nom_=c;
75}
76
77
78/*! @brief Construction d'un nom a partir d'un entier La chaine cree est la representation de l'entier
79 *
80 * Exemple : Nom(128) cree la chaine "128"
81 *
82 * @param (int i) l'entier a utiliser
83 */
85{
86 nom_ = "";
87 // 22 caracteres suffisent pour stocker n'importe quel entier
88 char chaine[22];
89 snprintf(chaine, 22, "%d", i);
90 operator=(chaine);
91}
92
93Nom::Nom(long i)
94{
95 nom_ = "";
96 // 22 caracteres suffisent pour stocker n'importe quel entier
97 char chaine[22];
98 snprintf(chaine, 22, "%ld", i);
99 operator=(chaine);
100}
101
102Nom::Nom(long long i)
103{
104 nom_ = "";
105 // 22 caracteres suffisent pour stocker n'importe quel entier long long
106 char chaine[22];
107 snprintf(chaine, 22, "%lld", i);
108 operator=(chaine);
109}
110
111/*! @brief Construction d'un nom a partir d'une chaine de caracteres La chaine est copiee
112 *
113 * @param (const char* nom) la chaine de caracteres a utiliser
114 */
115Nom::Nom(const char* nom) : nom_(nom)
116{
117}
118
119Nom::Nom(const std::string& nom) : nom_(nom)
120{
121}
122
123
124/*! @brief Constructeur par copie d'un nom
125 *
126 * @param (const Nom& nom) le nom a utiliser
127 */
128Nom::Nom(const Nom& nom) : Objet_U(nom), nom_(nom.nom_)
129{
130}
131
132/*! @brief Construction d'un nom a partir d'un flottant La chaine cree est la representation du nombre reel (snprintf)
133 *
134 * @param (double le_reel) le reel a utiliser
135 */
136Nom::Nom(double le_reel)
137{
138 nom_ = "";
139 char la_chaine[80];
140 snprintf(la_chaine,80,"%f",le_reel);
141 operator=(la_chaine);
142}
143
144/*! @brief Construction d'un nom a partir d'un flottant La chaine cree est la representation du nombre reel (snprintf)
145 *
146 * Le format du nombre represente par la chaine est donne par format
147 *
148 * @param (double le_reel) le reel a utiliser
149 */
150Nom::Nom(double le_reel, const char* format)
151{
152 char la_chaine[80];
153 snprintf(la_chaine,80,format,le_reel);
154#ifdef MICROSOFT
155 // sous windows les chiffres s'ecrivent 1.0000e+000 (avec 3 chiffres pour les puissances)
156 // on retire le premier zero
157 unsigned int length=strlen(la_chaine);
158 if (la_chaine[length-5]=='e')
159 {
160 if (la_chaine[length-3]!='0') Process::exit();
161 for (unsigned int i=length-3; i<=length; i++)
162 la_chaine[i]=la_chaine[i+1];
163 }
164#endif
165 nom_ = la_chaine;
166 //delete[] la_chaine;
167}
168
169
170/*! @brief Destructeur
171 *
172 */
174{
175}
176
177/*! @brief Transforme le nom en majuscules Seules les lettres 'a'-'z' sont modifiees
178 *
179 */
181{
182 std::transform(nom_.begin(), nom_.end(), nom_.begin(), ::toupper);
183 return *this;
184}
185
186/*! @brief Renvoie le nombre de caracteres de la chaine du Nom y compris le caractere zero de fin de chaine.
187 *
188 * Exemple : Nom("hello").longueur() == 6
189 *
190 */
191int Nom::longueur() const
192{
193 return (int)nom_.size()+1;
194}
195
196/*! @brief Copie la chaine nom.
197 *
198 * Modif BM pour que nom puisse pointer sur une sous-partie de nom_
199 *
200 */
201Nom& Nom::operator=(const char* const nom)
202{
203 nom_=nom;
204 return *this;
205}
206
207/*! @brief Copie le Nom nom
208 *
209 * @param (const Nom& nom) le nom a copier
210 * @return (Nom&) reference sur this qui represente la chaine du Nom nom
211 */
213{
214 nom_ = nom.nom_;
215 return *this;
216}
217
218/*! @brief Concatenation avec un Nom
219 *
220 * @param (const Nom& x) la nom a concatener
221 * @return (Nom&) reference sur this
222 */
224{
225 nom_ += x.nom_;
226 return *this;
227}
228
229Nom& Nom::operator+=(const char *x)
230{
231 nom_ += x;
232 return *this;
233}
234
235/*! @brief concatenation de chaine
236 *
237 */
239{
240 char n[2];
241 n[0] = x;
242 n[1] = 0;
243 operator+=(n);
244 return *this;
245}
246
247Nom& Nom::operator +=(unsigned char x)
248{
249 char n[2];
250 n[0] = (char)x;
251 n[1] = 0;
252 operator+=(n);
253 return *this;
254}
255
256
258{
259 nom_ += Nom(x);
260 return *this;
261}
262
263/*! @brief Extraction de suffixe : Nom x("azerty");
264 *
265 * x.suffix("aze")
266 * x contient "rty".
267 *
268 * @param (const char* const ch) chaine de caractere a utiliser comme prefixe
269 * @return (Nom&) reference sur this
270 */
271Nom& Nom::suffix(const char* const s)
272{
273 if (debute_par(s))
274 {
275 int n2 = (int)strlen(s);
276 nom_.erase(0,n2);
277 }
278 return *this;
279}
280
281const Nom Nom::getSuffix(const char* const s) const
282{
283 if (debute_par(s))
284 {
285 const int n1 = (int)strlen(s);
286 const int n2 = (int)nom_.size();
287 const std::string str1 = nom_.substr(n1,n2);
288 return Nom(str1);
289
290 }
291 return *this;
292}
293
294int Nom::debute_par(const std::string& ch) const
295{
296 return (nom_.rfind(ch, 0) == 0);
297}
298
299int Nom::finit_par(const std::string& s) const
300{
301 auto l = nom_.size(), e = s.size();
302 if (l >= e)
303 return (0 == nom_.compare(l - e, e, s));
304 else
305 return 0;
306}
307
308int Nom::find(const std::string& n) const
309{
310 std::size_t x = nom_.find(n);
311 return (x != std::string::npos) ? (int)x : -1;
312}
313
314int Nom::find(const char* const n ) const
315{
316 return find(std::string(n));
317}
318
319int Nom::debute_par(const char* const n) const
320{
321 return debute_par(std::string(n));
322}
323
324int Nom::finit_par(const char* const n) const
325{
326 return finit_par(std::string(n));
327}
328
329Nom& Nom::prefix(const char* const s)
330{
331 if (finit_par(s))
332 {
333 int n = (int)nom_.size();
334 int n2 = (int)strlen(s);
335 nom_.erase(n-n2,n2);
336 }
337 return *this;
338}
339
340const Nom Nom::getPrefix(const char* const s) const
341{
342 if (finit_par(s))
343 {
344 const int n1 = (int)nom_.size();
345 const int n2 = (int)strlen(s);
346 const std::string str1 = nom_.substr(0,n1-n2);
347 return Nom(str1);
348
349 }
350 return *this;
351}
352
353/*! @brief Concatenation avec un Nom
354 *
355 * @param (const Nom& x) la nom a concatener
356 * @return (Nom) le nouveau Nom cree avec la concatenation de this et de x
357 */
358Nom Nom::operator +(const Nom& x) const
359{
360 Nom nouveau(*this);
361 nouveau += x;
362 return nouveau;
363}
364
365/*! @brief Comparaison avec un Objet_U l'Objet_U est caste en Nom pour la comparaison
366 *
367 * @param (const Objet_U& x) l'Objet_U a utiliser pour la comparaison
368 * @return (int) 1 si egalite
369 */
370int Nom::est_egal_a(const Objet_U& x) const
371{
372#ifndef LATATOOLS
373 if (!(sub_type(Nom, x))) return 0;
374 return (*this == ref_cast( Nom, x));
375#else
376 const Nom& n2 = dynamic_cast<const Nom&>(x);
377 return (*this == n2);
378#endif
379}
380
381/*! @brief Insere _prefix000n (n=me() ou nproc()) dans un nom de fichier (par ex:toto.
382 *
383 * titi) pour donner toto_prefix000n.titi
384 *
385 * @param (without_padding) flag indiquant que l'on ne souhaite pas ajouter de zeros devant n
386 */
387Nom Nom::nom_me(int n, const char* prefixe, int without_padding) const
388{
389 int compteur=(int)nom_.size();
390 const char* ptr=nom_.c_str()+compteur;
391 while((*ptr!='.') && (*ptr!='/')&&(compteur>0)) // backward loop
392 {
393 ptr--;
394 compteur--;
395 if (*ptr=='/')
396 {
397 compteur=0;
398 }
399 }
400 int pas_de_point=0;
401 if(compteur==0)
402 {
403 compteur=(int)nom_.size();
404 pas_de_point=1 ;
405 }
406 std::string newname=nom_.substr(0,compteur);
407
408 //searching for the number of digits we want to write
409 int digits=0,diviseur=0;
410 if(without_padding)
411 {
412 digits = (n==0) ? 1 : (int)std::lrint(std::truncl(log10(n)+1.0));
413 diviseur = (int)std::lrint(std::truncl(pow(10, digits-1)));
414 }
415 else
416 {
417 if (Process::nproc()<=10000)
418 {
419 //the underscore will be taken into account later
420 //digits=5;
421 digits=4;
422 diviseur=1000;
423 }
424 else if (Process::nproc()<=100000)
425 {
426 //the underscore will be taken into account later
427 //digits=6;
428 digits=5;
429 diviseur=10000;
430 }
431 else if (Process::nproc()<=1000000)
432 {
433 //the underscore will be taken into account later
434 //digits=7;
435 digits=6;
436 diviseur=100000;
437 }
438 else
439 {
440 Cerr << "Error in Nom::nom_me. Contact TRUST support." << finl;
442 }
443
444 }
445
446 int prefix_len = 1; //for the underscore
447 if(prefixe) prefix_len+=(int)strlen(prefixe);
448
449 char *c_numero=new char[prefix_len+digits+1];
450 int resultat;
451 c_numero[0]='_';
452 if(prefixe) strcpy(c_numero+1, prefixe);
453 for (int i=prefix_len; i<prefix_len+digits; i++)
454 {
455 resultat=n/diviseur;
456 char c= (char)((int)'0' + resultat); // on old compilos, '+' is not for char, always int ...
457 c_numero[i]=c;
458 n-=resultat*diviseur;
459 diviseur/=10;
460 }
461 c_numero[prefix_len+digits]='\0';
462 newname+=c_numero;
463 if (pas_de_point==0)
464 newname+=ptr;
465 Nom new_name(newname);
466 delete[] c_numero;
467 return new_name;
468}
469
470/*! @brief Retourne un nom selon la commande usuelle substr ATTENTION : deb = 1 => premier caractere de la chaine !!!
471 *
472 */
473Nom Nom::substr_old(const int deb, const int la_longueur) const
474{
475
476 assert(deb > 0);
477 assert(deb - 1 + la_longueur <= (int) nom_.size());
478 Nom nouveau(nom_.substr(deb-1,la_longueur));
479 return nouveau;
480}
481
482/*! @brief Retourne fichier si le nom est sous la forme /toto/titi/fichier
483 *
484 * @param (Signification)
485 */
487{
488 Nom dirname("");
489 Nom the_basename(nom_);
490 int iLength = (int)nom_.size();
491 for (int i=0; i<iLength; i++)
492 {
493 dirname+=nom_[i];
494 if (nom_[i]=='/' || nom_[i]=='\\') // slash or backslash
495 {
496 the_basename.suffix(dirname);
497 dirname="";
498 }
499 }
500 return the_basename;
501}
502
503/*! @brief Retourne un pointeur sur la chaine de caractere du nom
504 *
505 * @return (char*) pointeur sur la chaine de caractere du nom
506 */
507Nom::operator const char*() const
508{
509 return nom_.c_str();
510}
511
512/*! @brief Comparaison d'un nom avec une chaine de caractere Utilise strcmp
513 *
514 * @param (const Nom& un_nom)
515 * @param (const char* const un_autre)
516 * @return (int) 1 si les noms sont egaux, 0 sinon
517 */
518int operator ==(const Nom& un_nom, const char* const un_autre)
519{
520 int res_actu=(un_nom.nom_.compare(un_autre)==0);
521 return res_actu;
522}
523int operator ==(const Nom& un_nom, const Nom& un_autre)
524{
525 return (un_nom==un_autre.getChar());
526}
527int operator ==(const char* const un_autre, const Nom& un_nom)
528{
529 return (un_nom == un_autre);
530}
531
532/*! @brief Comparaison d'un nom avec une chaine de caractere
533 *
534 * @param (const Nom& un_nom)
535 * @param (const char* const un_autre)
536 * @return (int) 1 si les noms sont differents, 0 sinon
537 */
538int operator !=(const Nom& un_nom, const char* un_autre)
539{
540 return ! (un_nom == un_autre);
541}
542
543int operator !=(const Nom& un_nom, const Nom& un_autre)
544{
545 return ! (un_nom == un_autre);
546}
547
548int operator !=(const char* const un_autre, const Nom& un_nom)
549{
550 return ! (un_autre == un_nom);
551}
552
553bool operator <(const Nom& n1, const Nom& n2)
554{
555 return n1.nom_.compare(n2.nom_) < 0;
556}
557
558
559/*! @brief Renvoie *this;
560 *
561 * @return (const Nom&) reference sur le Nom
562 */
563const Nom& Nom::le_nom() const
564{
565 return *this;
566}
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
virtual int get(int *ob, std::streamsize n)
Definition Entree.cpp:222
class Nom Une chaine de caractere pour nommer les objets de TRUST
Definition Nom.h:31
~Nom() override
Destructeur.
Definition Nom.cpp:173
const char * getChar() const
Definition Nom.h:91
Nom()
Constructeur par defaut.
Definition Nom.cpp:63
virtual int finit_par(const char *const n) const
Definition Nom.cpp:324
Nom substr_old(const int, const int) const
Retourne un nom selon la commande usuelle substr ATTENTION : deb = 1 => premier caractere de la chain...
Definition Nom.cpp:473
int est_egal_a(const Objet_U &) const override
Comparaison avec un Objet_U l'Objet_U est caste en Nom pour la comparaison.
Definition Nom.cpp:370
const Nom getPrefix(const char *const) const
Definition Nom.cpp:340
Nom & operator=(const char *const)
Copie la chaine nom.
Definition Nom.cpp:201
std::string nom_
Definition Nom.h:97
Nom nom_me(int, const char *prefix=0, int without_padding=0) const
Insere _prefix000n (n=me() ou nproc()) dans un nom de fichier (par ex:toto.
Definition Nom.cpp:387
const Nom getSuffix(const char *const) const
Definition Nom.cpp:281
virtual int debute_par(const char *const n) const
Definition Nom.cpp:319
virtual int find(const char *const n) const
Definition Nom.cpp:314
Nom & operator+=(const Nom &x)
Concatenation avec un Nom.
Definition Nom.cpp:223
int longueur() const
Renvoie le nombre de caracteres de la chaine du Nom y compris le caractere zero de fin de chaine.
Definition Nom.cpp:191
Nom & prefix(const char *const)
Definition Nom.cpp:329
Nom & majuscule()
Transforme le nom en majuscules Seules les lettres 'a'-'z' sont modifiees.
Definition Nom.cpp:180
Nom operator+(const Nom &) const
Concatenation avec un Nom.
Definition Nom.cpp:358
Nom & suffix(const char *const)
Extraction de suffixe : Nom x("azerty");.
Definition Nom.cpp:271
Nom basename() const
Retourne fichier si le nom est sous la forme /toto/titi/fichier.
Definition Nom.cpp:486
const Nom & le_nom() const override
Renvoie *this;.
Definition Nom.cpp:563
classe Objet_U Cette classe est la classe de base des Objets de TRUST
Definition Objet_U.h:73
virtual Entree & readOn(Entree &)
Lecture d'un Objet_U sur un flot d'entree Methode a surcharger.
Definition Objet_U.cpp:293
Objet_U()
Constructeur par defaut : attribue un numero d'identifiant unique a l'objet (object_id_),...
Definition Objet_U.cpp:55
virtual Sortie & printOn(Sortie &) const
Ecriture de l'objet sur un flot de sortie Methode a surcharger.
Definition Objet_U.cpp:282
static int nproc()
renvoie le nombre de processeurs dans le groupe courant Voir Comm_Group::nproc() et PE_Groups::curren...
Definition Process.cpp:104
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
Classe de base des flux de sortie.
Definition Sortie.h:52