TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
Motcle.cpp
1/****************************************************************************
2* Copyright (c) 2023, 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 <Motcle.h>
17#include <Noms.h>
18
19Implemente_instanciable_sans_constructeur(Motcle,"Motcle",Nom);
20Implemente_instanciable(Motcles,"Motcles",VECT(Motcle));
21
22
23/*! @brief @brief Writes a Motcle to an output stream. Uses the implementation of the Nom class.
24 *
25 * @param s The output stream to use.
26 * @return The modified output stream.
27 */
29{
30 return Nom::printOn(s);
31}
32
33
34/*! @brief Writes a Motcles array to an output stream.
35 *
36 * @param s The output stream to use.
37 * @return The modified output stream.
38 */
39Sortie& Motcles::printOn(Sortie& s) const
40{
41 return VECT(Motcle)::printOn(s);
42}
43
44
45/*! @brief Reads a Motcle from an input stream. Uses the implementation of the Nom class, then converts the name to uppercase.
46 *
47 * @param s The input stream to use.
48 * @return The modified input stream.
49 */
51{
52 Nom::readOn(s);
53 majuscule();
54 return s;
55}
56
57
58/*! @brief Default constructor. Constructs a Nom and converts it to uppercase.
59 *
60 */
62{
63 majuscule();
64}
65
66
67/*! @brief Constructs a Motcle from a character string. Constructs a Nom and converts it to uppercase.
68 *
69 * @param nom The character string for the Motcle.
70 */
71Motcle::Motcle(const char* const nom) : Nom(nom)
72{
73 majuscule();
74}
75
76Motcle::Motcle(const std::string& nom) : Nom(nom)
77{
78 majuscule();
79}
80
81Motcle::Motcle(const Nom& nom) : Nom(nom)
82{
83 majuscule();
84}
85
86/*! @brief Copy constructor.
87 *
88 * @param nom The Motcle to copy.
89 */
90Motcle::Motcle(const Motcle& nom) : Nom(nom)
91{
92}
93
94/*! @brief Assignment from a character string. Uses the implementation of the Nom class, then converts to uppercase.
95 *
96 * @param mot The character string to assign.
97 * @return Reference to the modified Motcle.
98 */
99Motcle& Motcle::operator=(const char* const mot)
100{
101 Nom::operator=(mot);
102 majuscule();
103 return *this;
104}
105
107{
108 Nom::operator=(mot);
109 majuscule();
110 return *this;
111}
112
113/*! @brief Copy assignment operator. Uses the implementation of the Nom class.
114 *
115 * @param mot The Motcle to copy.
116 * @return Reference to the modified Motcle.
117 */
119{
120 Nom::operator=(mot);
121 return *this;
122}
123
124/*! @brief Reads a Motcles array from an input stream.
125 *
126 * @param s The input stream to use.
127 * @return The modified input stream.
128 */
129Entree& Motcles::readOn(Entree& s)
130{
131 return VECT(Motcle)::readOn(s);
132}
133
134
135/*! @brief Self-test of the Motcle class. Performs control assignments.
136 *
137 * Always returns 1.
138 *
139 * @return Always 1.
140 */
142{
143 Nom un_nom;
144 Motcle un_motcle("PasOk");
145 Motcle un_autre(un_nom);
146 un_nom="Ok";
147 un_motcle=un_nom;
148 un_motcle="Ok";
149 un_motcle=un_autre;
150 return 1;
151}
152
153
154
155/*! @brief Constructor. Creates an array of i elements.
156 *
157 * @param i Number of keywords.
158 */
160 VECT(Motcle)(i)
161{
162}
163
164// "a" == "a|b" returns 0
165// "b" == "a|b" returns 0
166// "a|b" == "a|c" return 0
167static inline int strcmp_uppercase(const char *n1, const char *n2)
168{
169 // loop on n1
170 unsigned char c1,c2;
171 int i=0;
172 int length_i=0;
173 do
174 {
175 c1 = (unsigned char) ::toupper(n1[i]);
176 i++;
177 length_i++;
178 if (c1==0 || c1==124) // Keyword found in n1
179 {
180 // loop on c2
181 int j=0;
182 int length_j=0;
183 do
184 {
185 c2 = (unsigned char) ::toupper(n2[j]);
186 j++;
187 length_j++;
188 if (c2==0 || c2==124) // Keyword found in n2
189 {
190 if (length_i==length_j)
191 {
192 // Same length, we compare each caracter:
193 int l=length_i;
194 int delta=0;
195 unsigned char C1,C2;
196 do
197 {
198 C1 = (unsigned char) ::toupper(n1[i-l]);
199 C2 = (unsigned char) ::toupper(n2[j-l]);
200 delta = C2 - C1;
201 l--;
202 }
203 while (l>1 && delta==0);
204 if (delta==0)
205 return 0; // Found the same keyword
206 }
207 length_j=0;
208 }
209 }
210 while(c2!=0); // End of n2
211 length_i=0;
212 }
213 }
214 while (c1!=0); // End of n1
215 return 1;
216}
217
218/*! @brief Compares a keyword with another keyword (case-insensitive).
219 *
220 * @param nom The first Motcle.
221 * @param un_mot The second Motcle.
222 * @return 1 if the names are equal, 0 otherwise.
223 */
224int operator ==(const Motcle& nom, const Motcle& un_mot)
225{
226 const int resu = strcmp_uppercase((const char *) nom, (const char *)un_mot);
227 return (resu == 0);
228}
229
230int operator ==(const Motcle& un_mot, const char* const nom)
231{
232 const int resu = strcmp_uppercase((const char *)un_mot, nom);
233 return (resu == 0);
234}
235
236int operator ==(const char* const nom, const Motcle& un_mot)
237{
238 return (un_mot == nom);
239}
240
241int operator ==(const Motcle& un_mot, const Nom& nom)
242{
243 const int resu = strcmp_uppercase((const char *)un_mot, (const char *) nom);
244 return (resu == 0);
245}
246
247int operator ==(const Nom& nom, const Motcle& un_mot)
248{
249 return (un_mot == nom);
250}
251
252/*! @brief Compares a keyword with another keyword for inequality (case-insensitive).
253 *
254 * @param nom The first Motcle.
255 * @param un_mot The second Motcle.
256 * @return 1 if the names differ, 0 otherwise.
257 */
258int operator !=(const Motcle& nom, const Motcle& un_mot)
259{
260 return ! (nom == un_mot);
261}
262
263int operator !=(const Motcle& un_mot, const char* const nom)
264{
265 return ! (un_mot == nom);
266}
267
268int operator !=(const char* const nom, const Motcle& un_mot)
269{
270 return ! (nom == un_mot);
271}
272
273int operator !=(const Motcle& un_mot, const Nom& nom)
274{
275 return ! (un_mot == nom);
276}
277
278int operator !=(const Nom& nom, const Motcle& un_mot)
279{
280 return ! (nom == un_mot);
281}
282
283Motcle& Motcle::operator+=(const char * const mot)
284{
285 Nom::operator+=(mot);
286 majuscule();
287 return *this;
288}
289
290Motcles noms_to_motcles(const Noms& a)
291{
292 int n = a.size();
293
294 Motcles b(n);
295 for (int i = 0; i < n; i++)
296 b[i] = a[i]; // odd-looking but most efficient
297 return b;
298}
299
300int Motcle::finit_par(const char* const mot) const
301{
302 Motcle mm(mot);
303 return Nom::finit_par(mm);
304}
305
306
307int Motcle::debute_par(const char* const mot) const
308{
309 Motcle mm(mot);
310 return Nom::debute_par(mm);
311}
312
313int Motcle::find(const char* const mot) const
314{
315 Motcle mm(mot);
316 return Nom::find(mm);
317}
318
319int Motcles::search(const Motcle& t ) const
320{
321 assert(size()>=0);
322 int i=size();
323 while(i--)
324 if (operator[](i)==t)
325 {
326 return i;
327 }
328 return -1;
329}
330
331int Motcles::contient_(const char* const ch) const
332{
333 return (rang(ch)!=-1);
334}
335/* Returns the VECT position number of a string (-1 if not found) */
336int Motcles::rang(const char* const ch) const
337{
338 Nom nom(ch);
339 assert(size()>=0);
340 int i=size();
341 while(i--)
342 if (operator[](i)==nom)
343 {
344 return i;
345 }
346 return -1;
347}
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
A character string (Nom) in uppercase.
Definition Motcle.h:26
int selftest()
Self-test of the Motcle class. Performs control assignments.
Definition Motcle.cpp:141
int finit_par(const char *const n) const override
Definition Motcle.cpp:300
Motcle()
Default constructor. Constructs a Nom and converts it to uppercase.
Definition Motcle.cpp:61
Motcle & operator+=(const char *const)
Definition Motcle.cpp:283
Motcle & operator=(const Motcle &)
Copy assignment operator. Uses the implementation of the Nom class.
Definition Motcle.cpp:118
int debute_par(const char *const n) const override
Definition Motcle.cpp:307
int find(const char *const n) const override
Definition Motcle.cpp:313
An array of Motcle objects.
Definition Motcle.h:63
int rang(const char *const ch) const
Definition Motcle.cpp:336
int contient_(const char *const ch) const
Definition Motcle.cpp:331
Motcles(int)
Constructor. Creates an array of i elements.
Definition Motcle.cpp:159
int search(const Motcle &t) const
Definition Motcle.cpp:319
class Nom: a character string for naming TRUST objects.
Definition Nom.h:31
Nom()
Default constructor. Creates the string "??".
Definition Nom.cpp:59
virtual int finit_par(const char *const n) const
Definition Nom.cpp:319
Nom & operator=(const char *const)
Copies the string nom.
Definition Nom.cpp:197
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
Nom & majuscule()
Converts the name to uppercase. Only letters 'a'-'z' are modified.
Definition Nom.cpp:176
An array of character strings (VECT(Nom)).
Definition Noms.h:26
virtual Entree & readOn(Entree &)
Reads an Objet_U from an input stream. Virtual method to override.
Definition Objet_U.cpp:289
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
Base class for output streams.
Definition Sortie.h:52