Loading...
Searching...
No Matches
CStringList.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-------------------------------------------------------------------------------
10License
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
26Class
27 Foam::CStringList
28
29Description
30 An adapter for copying a list of C++ strings into a list of C-style
31 strings for passing to C code that expects argc/argv parameters.
32
33 In addition to providing a C-compatible list of C-strings,
34 the string lists are flattened into a single string of data that can be
35 also be passed en mass.
36
37 Example use:
38 \code
39 wordList myStrings; ...
40 CStringList cstr(myStrings);
41
42 // pass as argc, argv:
43 someMain(cstr.size(), cstr.strings());
44
45 // access the raw characters:
46 os.write(cstr.data(), cstr.length());
47 \endcode
48
49\*---------------------------------------------------------------------------*/
50
51#ifndef Foam_CStringList_H
52#define Foam_CStringList_H
53
54#include "fileNameList.H"
55#include "stringList.H"
56#include "wordList.H"
57#include "SubStrings.H"
58
59#include <algorithm> // std::copy
60#include <utility> // std::initializer_list
61#include <vector>
62#include <string_view>
63#include <string>
64
65// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66
67namespace Foam
68{
69
70/*---------------------------------------------------------------------------*\
71 Class CStringList Declaration
72\*---------------------------------------------------------------------------*/
73
74class CStringList
75{
76 // Private Data
77
78 //- Number of strings
79 int argc_;
80
81 //- Overall length of the raw content
82 // Does not include the final nul-character
83 size_t nbytes_;
84
85 //- List of strings, including trailing nullptr
86 char** argv_;
87
88 //- Flattened content with interspersed nul-characters
89 char* data_;
90
91
92 // Private Member Functions
93
94 //- Copy characters into dest as NUL-terminated string.
95 //
96 // \return location one-past end of dest (ie, the next destination)
97 static inline char* stringCopy(char* dest, const char* src);
98
99 //- Copy string characters into dest as NUL-terminated string.
100 //
101 // \return location one-past end of dest (ie, the next destination)
102 static inline char* stringCopy(char *dest, std::string_view src);
103
104 //- Copy string characters into dest as NUL-terminated string.
105 // Forces conversion of std::sub_match to string form
106 //
107 // \return location one-past end of dest (ie, the next destination)
108 static inline char* stringCopy(char *dest, const std::string& src);
109
110 //- Copy the input list of strings.
111 // \return number of arguments (argc)
112 template<class ListType>
113 int resetContent(const ListType& input);
114
115
116public:
117
118 // Generated Methods
119
120 //- No copy construct
121 CStringList(const CStringList&) = delete;
122
123 //- No copy assignment
124 void operator=(const CStringList&) = delete;
125
126
127 // Constructors
128
129 //- Default construct, adding content later (via reset).
130 inline constexpr CStringList() noexcept;
131
132 //- Copy construct from a list of C-strings
133 // Copies the input characters.
134 inline explicit CStringList
135 (
136 std::initializer_list<const char* const> input
137 );
138
139 //- Copy construct from a list of strings
140 // Copies the input characters.
141 template<class StringType>
142 inline explicit CStringList(const UList<StringType>& input);
143
144 //- Copy construct from a list of string_views
145 // Copies the input characters.
146 inline explicit CStringList(const UList<std::string_view>& input);
147
148 //- Copy construct from a list of string_views
149 // Copies the input characters.
150 inline explicit CStringList(const std::vector<std::string_view>& input);
151
152 //- Copy construct from a list of sub-string references
153 // Copies the input characters.
154 inline explicit CStringList(const SubStrings& input);
155
156
157 //- Destructor. Invokes clear() to free memory.
158 inline ~CStringList();
159
160
161 // Public Members
162
163 //- Count the number of parameters until the first nullptr
164 // Return 0 if argv is nullptr.
165 static inline int count(const char * const argv[]);
166
167
168 // Access
169
170 //- True if the size (ie, argc) is zero.
171 bool empty() const noexcept { return !argc_; }
172
173 //- Return the number of C-strings (ie, argc)
174 int size() const noexcept { return argc_; }
175
176 //- The flattened character content, with interspersed nul-chars
177 inline std::string_view view() const;
178
179 //- The flattened character content, with interspersed nul-chars
180 const char* cdata_bytes() const noexcept { return data_; }
181
182 //- Overall length of the flattened character (data) content
183 //- including interspersed nul-chars but not the trailing nul-char
184 size_t size_bytes() const noexcept { return nbytes_; }
185
186 //- Same as cdata_bytes()
187 const char* data() const noexcept { return data_; }
188
189 //- Same as size_bytes()
190 size_t length() const noexcept { return nbytes_; }
191
192 //- Return string element at the given index. No bounds checking.
193 const char* get(int i) const { return argv_[i]; }
194
195 //- Return the list of C-strings (ie, argv)
196 // The position at argc is a nullptr
197 char** strings() const noexcept { return argv_; }
198
199 //- Return the sublist of C-strings (ie, argv) starting at the
200 //- specified offset.
201 // \param start the offset, must be less than argc
202 inline char** strings(int start) const;
203
204
205 // Edit
206
207 //- Clear contents and free memory
208 inline void clear();
209
210 //- Copy the input list of C-strings
211 // \return number of arguments (argc)
212 int reset(std::initializer_list<const char* const> input);
214 //- Copy the input list of strings.
215 // \return number of arguments (argc)
216 template<class StringType>
217 int reset(const UList<StringType>& input)
219 return resetContent(input);
220 }
221
222 //- Copy the input list of strings.
223 // \return number of arguments (argc)
224 int reset(const SubStrings& input)
225 {
226 return resetContent(input);
227 }
229
230 // Other
231
232 //- Create a list from argc/argv parameters.
233 // A null pointer for argv is permissible when argc is zero.
234 template<class StringType = std::string>
235 static List<StringType> asList(int argc, const char * const argv[]);
236
237 //- Create a list from a nullptr-terminated list of argv parameters.
238 // Using a nullptr for argv is permissible.
239 template<class StringType = std::string>
240 static inline List<StringType> asList(const char * const argv[]);
241
242
243 // Member Operators
245 //- Return element at the given index. No bounds checking.
246 const char* operator[](int i) const { return argv_[i]; }
247};
248
250// IOstream Operators
251
252//- Output space-separated list
253Ostream& operator<<(Ostream& os, const CStringList& list);
254
255
256// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
257
258} // End namespace Foam
259
260// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
261
262#include "CStringListI.H"
263
264#ifdef NoRepository
265# include "CStringList.txx"
266#endif
267
268// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
269
270#endif
271
272// ************************************************************************* //
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:68
~CStringList()
Destructor. Invokes clear() to free memory.
int size() const noexcept
Return the number of C-strings (ie, argc).
const char * operator[](int i) const
Return element at the given index. No bounds checking.
static List< StringType > asList(int argc, const char *const argv[])
Create a list from argc/argv parameters.
static List< StringType > asList(const char *const argv[])
Create a list from a nullptr-terminated list of argv parameters.
constexpr CStringList() noexcept
Default construct, adding content later (via reset).
const char * data() const noexcept
Same as cdata_bytes().
static int count(const char *const argv[])
Count the number of parameters until the first nullptr.
bool empty() const noexcept
True if the size (ie, argc) is zero.
char ** strings() const noexcept
Return the list of C-strings (ie, argv).
void operator=(const CStringList &)=delete
No copy assignment.
int reset(std::initializer_list< const char *const > input)
Copy the input list of C-strings.
CStringList(const CStringList &)=delete
No copy construct.
std::string_view view() const
The flattened character content, with interspersed nul-chars.
int reset(const UList< StringType > &input)
Copy the input list of strings.
size_t length() const noexcept
Same as size_bytes().
size_t size_bytes() const noexcept
Overall length of the flattened character (data) content including interspersed nul-chars but not the...
const char * cdata_bytes() const noexcept
The flattened character content, with interspersed nul-chars.
void clear()
Clear contents and free memory.
int reset(const SubStrings &input)
Copy the input list of strings.
const char * get(int i) const
Return string element at the given index. No bounds checking.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition SubStrings.H:49
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
const direction noexcept
Definition scalarImpl.H:265
Vector< scalar > vector
Definition vector.H:57