TrioCFD 1.9.9_beta
TrioCFD documentation
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 Overrides Objet_U::printOn(Sortie&). Writes a Nom to an output stream.
28 *
29 * @param (Sortie& s) the output stream to use
30 * @return (Sortie&) the modified output stream
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 Reads a name. On failure, the name is set to "??".
43 *
44 */
46{
47 char buffer[BUFLEN];
48 const int ok = s.get(buffer, BUFLEN);
49 if (ok)
50 operator=(buffer);
51 else
52 operator=("??");
53 return s;
54}
55
56/*! @brief Default constructor. Creates the string "??".
57 *
58 */
60{
61 nom_ = "??";
62}
63
64/*! @brief Constructs a name from a single character.
65 *
66 * @param (char c) the character of the name
67 */
68Nom::Nom(char c)
69{
70 nom_=c;
71}
72
73
74/*! @brief Constructs a name from an integer. The string created is the decimal representation of the integer.
75 *
76 * Example: Nom(128) creates the string "128".
77 *
78 * @param (int i) the integer to use
79 */
81{
82 nom_ = "";
83 // 22 characters are sufficient to store any integer
84 char chaine[22];
85 snprintf(chaine, 22, "%d", i);
86 operator=(chaine);
87}
88
89Nom::Nom(long i)
90{
91 nom_ = "";
92 // 22 characters are sufficient to store any integer
93 char chaine[22];
94 snprintf(chaine, 22, "%ld", i);
95 operator=(chaine);
96}
97
98Nom::Nom(long long i)
99{
100 nom_ = "";
101 // 22 characters are sufficient to store any long long integer
102 char chaine[22];
103 snprintf(chaine, 22, "%lld", i);
104 operator=(chaine);
105}
106
107/*! @brief Constructs a name from a character string. The string is copied.
108 *
109 * @param (const char* nom) the character string to use
110 */
111Nom::Nom(const char* nom) : nom_(nom)
112{
113}
114
115Nom::Nom(const std::string& nom) : nom_(nom)
116{
117}
118
119
120/*! @brief Copy constructor for a name.
121 *
122 * @param (const Nom& nom) the name to use
123 */
124Nom::Nom(const Nom& nom) : Objet_U(nom), nom_(nom.nom_)
125{
126}
127
128/*! @brief Constructs a name from a floating-point number. The string created is the decimal representation of the real number (snprintf).
129 *
130 * @param (double le_reel) the real number to use
131 */
132Nom::Nom(double le_reel)
133{
134 nom_ = "";
135 char la_chaine[80];
136 snprintf(la_chaine,80,"%f",le_reel);
137 operator=(la_chaine);
138}
139
140/*! @brief Constructs a name from a floating-point number with a custom format. The string created is the decimal representation of the real number (snprintf).
141 *
142 * The format of the number in the string is given by format.
143 *
144 * @param (double le_reel) the real number to use
145 */
146Nom::Nom(double le_reel, const char* format)
147{
148 char la_chaine[80];
149 snprintf(la_chaine,80,format,le_reel);
150#ifdef MICROSOFT
151 // under windows, numbers are written as 1.0000e+000 (with 3 digits for exponents)
152 // remove the first zero
153 unsigned int length=strlen(la_chaine);
154 if (la_chaine[length-5]=='e')
155 {
156 if (la_chaine[length-3]!='0') Process::exit();
157 for (unsigned int i=length-3; i<=length; i++)
158 la_chaine[i]=la_chaine[i+1];
159 }
160#endif
161 nom_ = la_chaine;
162 //delete[] la_chaine;
163}
164
165
166/*! @brief Destructor.
167 *
168 */
170{
171}
172
173/*! @brief Converts the name to uppercase. Only letters 'a'-'z' are modified.
174 *
175 */
177{
178 std::transform(nom_.begin(), nom_.end(), nom_.begin(), ::toupper);
179 return *this;
180}
181
182/*! @brief Returns the number of characters in the Nom string, including the null terminator.
183 *
184 * Example: Nom("hello").longueur() == 6.
185 *
186 */
187int Nom::longueur() const
188{
189 return (int)nom_.size()+1;
190}
191
192/*! @brief Copies the string nom.
193 *
194 * BM modification so that nom can point to a sub-part of nom_.
195 *
196 */
197Nom& Nom::operator=(const char* const nom)
198{
199 nom_=nom;
200 return *this;
201}
202
203/*! @brief Copies the Nom nom.
204 *
205 * @param (const Nom& nom) the name to copy
206 * @return (Nom&) reference to this, representing the string of Nom nom
207 */
209{
210 nom_ = nom.nom_;
211 return *this;
212}
213
214/*! @brief Concatenation with a Nom.
215 *
216 * @param (const Nom& x) the name to concatenate
217 * @return (Nom&) reference to this
218 */
220{
221 nom_ += x.nom_;
222 return *this;
223}
224
225Nom& Nom::operator+=(const char *x)
226{
227 nom_ += x;
228 return *this;
229}
230
231/*! @brief String concatenation.
232 *
233 */
235{
236 char n[2];
237 n[0] = x;
238 n[1] = 0;
239 operator+=(n);
240 return *this;
241}
242
243Nom& Nom::operator +=(unsigned char x)
244{
245 char n[2];
246 n[0] = (char)x;
247 n[1] = 0;
248 operator+=(n);
249 return *this;
250}
251
252
254{
255 nom_ += Nom(x);
256 return *this;
257}
258
259/*! @brief Suffix extraction: Nom x("azerty");
260 *
261 * x.suffix("aze") leaves x containing "rty".
262 *
263 * @param (const char* const ch) character string to use as prefix to strip
264 * @return (Nom&) reference to this
265 */
266Nom& Nom::suffix(const char* const s)
267{
268 if (debute_par(s))
269 {
270 int n2 = (int)strlen(s);
271 nom_.erase(0,n2);
272 }
273 return *this;
274}
275
276const Nom Nom::getSuffix(const char* const s) const
277{
278 if (debute_par(s))
279 {
280 const int n1 = (int)strlen(s);
281 const int n2 = (int)nom_.size();
282 const std::string str1 = nom_.substr(n1,n2);
283 return Nom(str1);
284
285 }
286 return *this;
287}
288
289int Nom::debute_par(const std::string& ch) const
290{
291 return (nom_.rfind(ch, 0) == 0);
292}
293
294int Nom::finit_par(const std::string& s) const
295{
296 auto l = nom_.size(), e = s.size();
297 if (l >= e)
298 return (0 == nom_.compare(l - e, e, s));
299 else
300 return 0;
301}
302
303int Nom::find(const std::string& n) const
304{
305 std::size_t x = nom_.find(n);
306 return (x != std::string::npos) ? (int)x : -1;
307}
308
309int Nom::find(const char* const n ) const
310{
311 return find(std::string(n));
312}
313
314int Nom::debute_par(const char* const n) const
315{
316 return debute_par(std::string(n));
317}
318
319int Nom::finit_par(const char* const n) const
320{
321 return finit_par(std::string(n));
322}
323
324Nom& Nom::prefix(const char* const s)
325{
326 if (finit_par(s))
327 {
328 int n = (int)nom_.size();
329 int n2 = (int)strlen(s);
330 nom_.erase(n-n2,n2);
331 }
332 return *this;
333}
334
335const Nom Nom::getPrefix(const char* const s) const
336{
337 if (finit_par(s))
338 {
339 const int n1 = (int)nom_.size();
340 const int n2 = (int)strlen(s);
341 const std::string str1 = nom_.substr(0,n1-n2);
342 return Nom(str1);
343
344 }
345 return *this;
346}
347
348/*! @brief Concatenation with a Nom.
349 *
350 * @param (const Nom& x) the name to concatenate
351 * @return (Nom) the new Nom created by concatenating this and x
352 */
353Nom Nom::operator +(const Nom& x) const
354{
355 Nom nouveau(*this);
356 nouveau += x;
357 return nouveau;
358}
359
360/*! @brief Comparison with an Objet_U. The Objet_U is cast to Nom for the comparison.
361 *
362 * @param (const Objet_U& x) the Objet_U to use for comparison
363 * @return (int) 1 if equal
364 */
365int Nom::est_egal_a(const Objet_U& x) const
366{
367#ifndef LATATOOLS
368 if (!(sub_type(Nom, x))) return 0;
369 return (*this == ref_cast( Nom, x));
370#else
371 const Nom& n2 = dynamic_cast<const Nom&>(x);
372 return (*this == n2);
373#endif
374}
375
376/*! @brief Inserts _prefix000n (n=me() or nproc()) into a file name (e.g. toto.titi) to produce toto_prefix000n.titi.
377 *
378 * @param (without_padding) flag indicating that leading zeros should not be added before n
379 */
380Nom Nom::nom_me(int n, const char* prefixe, int without_padding) const
381{
382 int compteur=(int)nom_.size();
383 const char* ptr=nom_.c_str()+compteur;
384 while((*ptr!='.') && (*ptr!='/')&&(compteur>0)) // backward loop
385 {
386 ptr--;
387 compteur--;
388 if (*ptr=='/')
389 {
390 compteur=0;
391 }
392 }
393 int pas_de_point=0;
394 if(compteur==0)
395 {
396 compteur=(int)nom_.size();
397 pas_de_point=1 ;
398 }
399 std::string newname=nom_.substr(0,compteur);
400
401 //searching for the number of digits we want to write
402 int digits=0,diviseur=0;
403 if(without_padding)
404 {
405 digits = (n==0) ? 1 : (int)std::lrint(std::truncl(log10(n)+1.0));
406 diviseur = (int)std::lrint(std::truncl(pow(10, digits-1)));
407 }
408 else
409 {
410 if (Process::nproc()<=10000)
411 {
412 //the underscore will be taken into account later
413 //digits=5;
414 digits=4;
415 diviseur=1000;
416 }
417 else if (Process::nproc()<=100000)
418 {
419 //the underscore will be taken into account later
420 //digits=6;
421 digits=5;
422 diviseur=10000;
423 }
424 else if (Process::nproc()<=1000000)
425 {
426 //the underscore will be taken into account later
427 //digits=7;
428 digits=6;
429 diviseur=100000;
430 }
431 else
432 {
433 Cerr << "Error in Nom::nom_me. Contact TRUST support." << finl;
435 }
436
437 }
438
439 int prefix_len = 1; //for the underscore
440 if(prefixe) prefix_len+=(int)strlen(prefixe);
441
442 char *c_numero=new char[prefix_len+digits+1];
443 int resultat;
444 c_numero[0]='_';
445 if(prefixe) strcpy(c_numero+1, prefixe);
446 for (int i=prefix_len; i<prefix_len+digits; i++)
447 {
448 resultat=n/diviseur;
449 char c= (char)((int)'0' + resultat); // on old compilers, '+' is not for char, always int ...
450 c_numero[i]=c;
451 n-=resultat*diviseur;
452 diviseur/=10;
453 }
454 c_numero[prefix_len+digits]='\0';
455 newname+=c_numero;
456 if (pas_de_point==0)
457 newname+=ptr;
458 Nom new_name(newname);
459 delete[] c_numero;
460 return new_name;
461}
462
463/*! @brief Returns a name using the usual substr command. NOTE: deb = 1 means the first character of the string.
464 *
465 */
466Nom Nom::substr_old(const int deb, const int la_longueur) const
467{
468
469 assert(deb > 0);
470 assert(deb - 1 + la_longueur <= (int) nom_.size());
471 Nom nouveau(nom_.substr(deb-1,la_longueur));
472 return nouveau;
473}
474
475/*! @brief Returns the filename part if the name is in the form /toto/titi/filename.
476 *
477 */
479{
480 Nom dirname("");
481 Nom the_basename(nom_);
482 int iLength = (int)nom_.size();
483 for (int i=0; i<iLength; i++)
484 {
485 dirname+=nom_[i];
486 if (nom_[i]=='/' || nom_[i]=='\\') // slash or backslash
487 {
488 the_basename.suffix(dirname);
489 dirname="";
490 }
491 }
492 return the_basename;
493}
494
495/*! @brief Returns a pointer to the character string of the name.
496 *
497 * @return (char*) pointer to the character string of the name
498 */
499Nom::operator const char*() const
500{
501 return nom_.c_str();
502}
503
504/*! @brief Compares a name with a character string using strcmp.
505 *
506 * @param (const Nom& un_nom)
507 * @param (const char* const un_autre)
508 * @return (int) 1 if the names are equal, 0 otherwise
509 */
510int operator ==(const Nom& un_nom, const char* const un_autre)
511{
512 int res_actu=(un_nom.nom_.compare(un_autre)==0);
513 return res_actu;
514}
515int operator ==(const Nom& un_nom, const Nom& un_autre)
516{
517 return (un_nom==un_autre.getChar());
518}
519int operator ==(const char* const un_autre, const Nom& un_nom)
520{
521 return (un_nom == un_autre);
522}
523
524/*! @brief Compares a name with a character string.
525 *
526 * @param (const Nom& un_nom)
527 * @param (const char* const un_autre)
528 * @return (int) 1 if the names are different, 0 otherwise
529 */
530int operator !=(const Nom& un_nom, const char* un_autre)
531{
532 return ! (un_nom == un_autre);
533}
534
535int operator !=(const Nom& un_nom, const Nom& un_autre)
536{
537 return ! (un_nom == un_autre);
538}
539
540int operator !=(const char* const un_autre, const Nom& un_nom)
541{
542 return ! (un_autre == un_nom);
543}
544
545bool operator <(const Nom& n1, const Nom& n2)
546{
547 return n1.nom_.compare(n2.nom_) < 0;
548}
549
550
551/*! @brief Returns *this.
552 *
553 * @return (const Nom&) reference to the Nom
554 */
555const Nom& Nom::le_nom() const
556{
557 return *this;
558}
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: a character string for naming TRUST objects.
Definition Nom.h:31
~Nom() override
Destructor.
Definition Nom.cpp:169
const char * getChar() const
Definition Nom.h:91
Nom()
Default constructor. Creates the string "??".
Definition Nom.cpp:59
virtual int finit_par(const char *const n) const
Definition Nom.cpp:319
Nom substr_old(const int, const int) const
Returns a name using the usual substr command. NOTE: deb = 1 means the first character of the string.
Definition Nom.cpp:466
int est_egal_a(const Objet_U &) const override
Comparison with an Objet_U. The Objet_U is cast to Nom for the comparison.
Definition Nom.cpp:365
const Nom getPrefix(const char *const) const
Definition Nom.cpp:335
Nom & operator=(const char *const)
Copies the string nom.
Definition Nom.cpp:197
std::string nom_
Definition Nom.h:97
Nom nom_me(int, const char *prefix=0, int without_padding=0) const
Inserts _prefix000n (n=me() or nproc()) into a file name (e.g. toto.titi) to produce toto_prefix000n....
Definition Nom.cpp:380
const Nom getSuffix(const char *const) const
Definition Nom.cpp:276
virtual int debute_par(const char *const n) const
Definition Nom.cpp:314
virtual int find(const char *const n) const
Definition Nom.cpp:309
Nom & operator+=(const Nom &x)
Concatenation with a Nom.
Definition Nom.cpp:219
int longueur() const
Returns the number of characters in the Nom string, including the null terminator.
Definition Nom.cpp:187
Nom & prefix(const char *const)
Definition Nom.cpp:324
Nom & majuscule()
Converts the name to uppercase. Only letters 'a'-'z' are modified.
Definition Nom.cpp:176
Nom operator+(const Nom &) const
Concatenation with a Nom.
Definition Nom.cpp:353
Nom & suffix(const char *const)
Suffix extraction: Nom x("azerty");.
Definition Nom.cpp:266
Nom basename() const
Returns the filename part if the name is in the form /toto/titi/filename.
Definition Nom.cpp:478
const Nom & le_nom() const override
Returns *this.
Definition Nom.cpp:555
Base class for TRUST objects (Objet_U).
Definition Objet_U.h:68
virtual Entree & readOn(Entree &)
Reads an Objet_U from an input stream. Virtual method to override.
Definition Objet_U.cpp:289
Objet_U()
Default constructor: assigns a unique identifier to the object (object_id_) and registers the object ...
Definition Objet_U.cpp:54
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
static int nproc()
Returns the number of processors in the current group. See Comm_Group::nproc() and PE_Groups::current...
Definition Process.cpp:102
static void exit(int exit_code=-1)
Exit routine for TRUST within a Kokkos region.
Definition Process.cpp:466
Base class for output streams.
Definition Sortie.h:52