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-2025 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, std::string_view src)
39 {
40  dest = std::copy_n(src.data(), src.size(), dest);
41  *(dest++) = '\0';
42  return dest;
43 }
44 
45 
46 inline char* Foam::CStringList::stringCopy(char *dest, const std::string& src)
47 {
48  dest = std::copy_n(src.data(), src.size(), dest);
49  *(dest++) = '\0';
50  return dest;
51 }
52 
53 
54 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
55 
56 inline int Foam::CStringList::count(const char * const argv[])
57 {
58  int n = 0;
59  if (argv)
60  {
61  while (argv[n])
62  {
63  ++n;
64  }
65  }
66  return n;
67 }
68 
69 
70 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
71 
73 :
74  argc_(0),
75  nbytes_(0),
76  argv_(nullptr),
77  data_(nullptr)
78 {}
79 
80 
82 (
83  std::initializer_list<const char* const> input
84 )
85 :
87 {
88  reset(input);
89 }
90 
91 
92 template<class StringType>
94 :
95  CStringList()
96 {
97  resetContent(input);
98 }
99 
100 
102 :
104 {
105  resetContent(input);
106 }
107 
108 
110 (
111  const std::vector<std::string_view>& input
112 )
113 :
114  CStringList()
115 {
116  resetContent(input);
117 }
118 
119 
120 inline Foam::CStringList::CStringList(const SubStrings& input)
121 :
122  CStringList()
123 {
124  resetContent(input);
125 }
126 
127 
128 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
129 
131 {
132  clear();
133 }
134 
135 
136 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
138 inline std::string_view Foam::CStringList::view() const
139 {
140  return std::string_view(data_, nbytes_);
141 }
142 
143 
144 inline void Foam::CStringList::clear()
145 {
146  argc_ = 0;
147  nbytes_ = 0;
148 
149  if (data_)
150  {
151  delete[] data_;
152  data_ = nullptr;
153  }
154  if (argv_)
155  {
156  delete[] argv_;
157  argv_ = nullptr;
158  }
159 }
160 
161 
162 inline char** Foam::CStringList::strings(int start) const
163 {
164  return &(argv_[start]);
165 }
166 
167 
168 // ************************************************************************* //
void clear()
Clear contents and free memory.
Definition: CStringListI.H:137
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition: SubStrings.H:46
static int count(const char *const argv[])
Count the number of parameters until the first nullptr.
Definition: CStringListI.H:49
constexpr CStringList() noexcept
Default construct, adding content later (via reset).
Definition: CStringListI.H:65
std::string_view view() const
The flattened character content, with interspersed nul-chars.
Definition: CStringListI.H:131
const direction noexcept
Definition: scalarImpl.H:265
limits reset(1/(limits.max()+VSMALL), 1/(limits.min()+VSMALL))
surface1 clear()
label n
~CStringList()
Destructor. Invokes clear() to free memory.
Definition: CStringListI.H:123
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:67
char ** strings() const noexcept
Return the list of C-strings (ie, argv)
Definition: CStringList.H:256