CStringListI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016-2024 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
29 
30 inline char* Foam::CStringList::stringCopy(char* dest, const char* src)
31 {
32  dest = std::copy_n(src, (src ? strlen(src) : 0), dest);
33  *(dest++) = '\0';
34  return dest;
35 }
36 
37 
38 inline char* Foam::CStringList::stringCopy(char *dest, const std::string& src)
39 {
40  dest = std::copy_n(src.data(), src.size(), dest);
41  *(dest++) = '\0';
42  return dest;
43 }
44 
45 
46 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
47 
48 inline int Foam::CStringList::count(const char * const argv[])
49 {
50  int n = 0;
51  if (argv)
52  {
53  while (argv[n])
54  {
55  ++n;
56  }
57  }
58  return n;
59 }
60 
61 
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
63 
65 :
66  argc_(0),
67  nbytes_(0),
68  argv_(nullptr),
69  data_(nullptr)
70 {}
71 
72 
74 (
75  std::initializer_list<const char* const> input
76 )
77 :
79 {
80  reset(input);
81 }
82 
83 
84 template<class StringType>
86 :
88 {
89  reset(input);
90 }
91 
92 
93 template<class StringType>
95 :
96  CStringList()
97 {
98  reset(input);
99 }
100 
101 
102 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
103 
105 {
106  clear();
107 }
108 
109 
110 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
111 
112 inline void Foam::CStringList::clear()
113 {
114  argc_ = 0;
115  nbytes_ = 0;
116 
117  if (data_)
118  {
119  delete[] data_;
120  data_ = nullptr;
121  }
122  if (argv_)
123  {
124  delete[] argv_;
125  argv_ = nullptr;
126  }
127 }
128 
130 inline bool Foam::CStringList::empty() const noexcept
131 {
132  return !argc_;
133 }
134 
136 inline int Foam::CStringList::size() const noexcept
137 {
138  return argc_;
139 }
140 
142 inline char** Foam::CStringList::strings() const noexcept
143 {
144  return argv_;
145 }
146 
147 
148 inline char** Foam::CStringList::strings(int start) const
149 {
150  return &(argv_[start]);
151 }
152 
153 
154 template<class StringType>
156 {
157  return resetContent(input);
158 }
159 
160 
161 template<class StringType>
163 {
164  return resetContent(input);
165 }
166 
167 
168 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
170 inline const char* Foam::CStringList::get(int i) const
171 {
172  return argv_[i];
173 }
174 
175 
176 inline const char* Foam::CStringList::operator[](int i) const
177 {
178  return argv_[i];
179 }
180 
181 
182 // ************************************************************************* //
void clear()
Clear contents and free memory.
Definition: CStringListI.H:105
const char * operator[](int i) const
Return element at the given index. No bounds checking.
Definition: CStringListI.H:169
const char * get(int i) const
Return string element at the given index. No bounds checking.
Definition: CStringListI.H:163
char ** strings() const noexcept
Return the list of C-strings (ie, argv)
Definition: CStringListI.H:135
bool empty() const noexcept
True if the size (ie, argc) is zero.
Definition: CStringListI.H:123
int size() const noexcept
Return the number of C-strings (ie, argc)
Definition: CStringListI.H:129
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition: CStringList.H:60
static int count(const char *const argv[])
Count the number of parameters until the first nullptr.
Definition: CStringListI.H:41
int reset(std::initializer_list< const char *const > input)
Copy the input list of C-strings.
Definition: CStringList.C:27
constexpr CStringList() noexcept
Default construct, adding content later (via reset).
Definition: CStringListI.H:57
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
const direction noexcept
Definition: Scalar.H:258
surface1 clear()
label n
~CStringList()
Destructor. Invokes clear() to free memory.
Definition: CStringListI.H:97
An adapter for copying a list of C++ strings into a list of C-style strings for passing to C code tha...
Definition: CStringList.H:66