TrioCFD 1.9.9_beta
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 Default constructor.
24 *
25 * Creates the string "??"
26 *
27 */
29{
30 nom_ = new char[3];
31 nom_[0] = '?';
32 nom_[1] = '?';
33 nom_[2] = 0;
34}
35
36
37
38/*! @brief Constructs a name from a character string. The string is copied.
39 *
40 * @param (const char* nom) the character string to use
41 */
42Char_ptr::Char_ptr(const char* nom)
43{
44 nom_ = 0;
45 operator=(nom);
46}
47
48/*! @brief Copy constructor of a name.
49 *
50 * @param (const Char_ptr& nom) the name to use
51 */
53{
54 nom_ = 0;
55 operator=(nom);
56}
57
58/*! @brief Destructor.
59 *
60 */
62{
63 if(nom_)
64 delete[] nom_;
65}
66
67
68/*! @brief Returns the number of characters in the Char_ptr string including the terminating null character.
69 *
70 * Example: Char_ptr("hello").longueur() == 6
71 *
72 */
74{
75 return ((int)strlen(nom_)+1);
76}
77
78/*! @brief Copies the string nom.
79 *
80 * Modified by BM so that nom can point to a sub-part of 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 // Delete the old after copying the new one, in case nom is a part of nom_
94 delete [] old;
95 return *this;
96}
97
98/*! @brief Copies the Char_ptr nom.
99 *
100 * @param (const Char_ptr& nom) the name to copy
101 * @return (Char_ptr&) reference to this, representing the string of Char_ptr nom
102 */
104{
105 operator=(nom.nom_);
106 return *this;
107}
108
109/*! @brief Returns a pointer to the character string of the name.
110 *
111 * @return (char*) pointer to the character string of the name
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
Returns the number of characters in the Char_ptr string including the terminating null character.
Definition Char_ptr.cpp:73
char * nom_
Definition Char_ptr.h:49
Char_ptr & operator=(const char *)
Copies the string nom.
Definition Char_ptr.cpp:83
virtual ~Char_ptr()
Destructor.
Definition Char_ptr.cpp:61
Char_ptr()
Default constructor.
Definition Char_ptr.cpp:28
void allocate(int n)
Definition Char_ptr.cpp:119