TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
Char_ptr.cpp
1/****************************************************************************
2* Copyright (c) 2022, 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 <Char_ptr.h>
17
18#include <Motcle.h>
19#include <string>
20
21
22
23/*! @brief Constructeur par defaut.
24 *
25 * Cree la chaine "??"
26 *
27 */
29{
30 nom_ = new char[3];
31 nom_[0] = '?';
32 nom_[1] = '?';
33 nom_[2] = 0;
34}
35
36
37
38/*! @brief Construction d'un nom a partir d'une chaine de caracteres La chaine est copiee
39 *
40 * @param (const char* nom) la chaine de caracteres a utiliser
41 */
42Char_ptr::Char_ptr(const char* nom)
43{
44 nom_ = 0;
45 operator=(nom);
46}
47
48/*! @brief Constructeur par copie d'un nom
49 *
50 * @param (const Char_ptr& nom) le nom a utiliser
51 */
53{
54 nom_ = 0;
55 operator=(nom);
56}
57
58/*! @brief Destructeur
59 *
60 */
62{
63 if(nom_)
64 delete[] nom_;
65}
66
67
68/*! @brief Renvoie le nombre de caracteres de la chaine du Char_ptr y compris le caractere zero de fin de chaine.
69 *
70 * Exemple : Char_ptr("hello").longueur() == 6
71 *
72 */
74{
75 return ((int)strlen(nom_)+1);
76}
77
78/*! @brief Copie la chaine nom.
79 *
80 * Modif BM pour que nom puisse pointer sur une sous-partie de nom_
81 *
82 */
83Char_ptr& Char_ptr::operator=(const char* const nom)
84{
85 if (nom_ == nom)
86 return *this;
87 char *old = nom_;
88 const char *n = nom;
89 if (!n)
90 n = "??";
91 nom_ = new char[strlen(n)+1];
92 strcpy(nom_, n);
93 // On efface l'ancien apres avoir copie le nouveau au cas ou nom est une partie de nom_
94 delete [] old;
95 return *this;
96}
97
98/*! @brief Copie le Char_ptr nom
99 *
100 * @param (const Char_ptr& nom) le nom a copier
101 * @return (Char_ptr&) reference sur this qui represente la chaine du Char_ptr nom
102 */
104{
105 operator=(nom.nom_);
106 return *this;
107}
108
109/*! @brief Retourne un pointeur sur la chaine de caractere du nom
110 *
111 * @return (char*) pointeur sur la chaine de caractere du nom
112 */
113Char_ptr::operator char*() const
114{
115 return nom_;
116}
117
118
120{
121 if (nom_)
122 delete [] nom_;
123 nom_=new char[n+1];
124 for (int i=0; i<n; i++)
125 nom_[i]=' ';
126 nom_[n]='\0';
127}
int longueur() const
Renvoie le nombre de caracteres de la chaine du Char_ptr y compris le caractere zero de fin de chaine...
Definition Char_ptr.cpp:73
char * nom_
Definition Char_ptr.h:49
Char_ptr & operator=(const char *)
Copie la chaine nom.
Definition Char_ptr.cpp:83
virtual ~Char_ptr()
Destructeur.
Definition Char_ptr.cpp:61
Char_ptr()
Constructeur par defaut.
Definition Char_ptr.cpp:28
void allocate(int n)
Definition Char_ptr.cpp:119