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-2022 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
58// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59
60namespace Foam
61{
62
63// Forward Declarations
64template<class StringType> class SubStrings;
65
66/*---------------------------------------------------------------------------*\
67 Class CStringList Declaration
68\*---------------------------------------------------------------------------*/
70class CStringList
71{
72 // Private Data
73
74 //- Number of strings
75 int argc_;
76
77 //- Overall length of the raw content
78 // Does not include the final nul-character
79 size_t nbytes_;
80
81 //- List of strings, including trailing nullptr
82 char** argv_;
83
84 //- Flattened content with interspersed nul-characters
85 char* data_;
86
87
88 // Private Member Functions
89
90 //- Copy string characters into dest as NUL-terminated string.
91 // Forces conversion of std::sub_match to string form
92 //
93 // \return location one-past end of dest (ie, the next destination)
94 static inline char* stringCopy(char *dest, const std::string& src);
95
96 //- Copy the input list of strings.
97 // \return number of arguments (argc)
98 template<class ListType>
99 int resetContent(const ListType& input);
100
101
102 //- No copy construct
103 CStringList(const CStringList&) = delete;
104
105 //- No copy assignment
106 void operator=(const CStringList&) = delete;
107
108
109public:
110
111 // Constructors
112
113 //- Default construct, adding content later (via reset).
114 inline constexpr CStringList() noexcept;
115
116 //- Copy construct from a list of strings
117 // Copies the input characters.
118 template<class StringType>
119 inline explicit CStringList(const UList<StringType>& input);
120
121 //- Copy construct from a list of sub-string references
122 // Copies the input characters.
123 template<class StringType>
124 inline explicit CStringList(const SubStrings<StringType>& input);
125
126
127 //- Destructor. Invokes clear() to free memory.
128 inline ~CStringList();
129
130
131 // Public Members
132
133 //- Count the number of parameters until the first nullptr
134 // Return 0 if argv is nullptr.
135 static inline int count(const char * const argv[]);
136
137
138 // Access
139
140 //- True if the size (ie, argc) is zero.
141 inline bool empty() const noexcept;
142
143 //- Return the number of C-strings (ie, argc)
144 inline int size() const noexcept;
145
146 //- The flattened character content, with interspersed nul-chars
147 const char* cdata_bytes() const noexcept { return data_; }
148
149 //- Overall length of the flattened character (data) content
150 //- including interspersed nul-chars but not the trailing nul-char
151 size_t size_bytes() const noexcept { return nbytes_; }
152
153 //- Same as cdata_bytes()
154 const char* data() const noexcept { return data_; }
155
156 //- Same as size_bytes()
157 size_t length() const noexcept { return nbytes_; }
158
159 //- Return string element at the given index. No bounds checking.
160 const char* get(int i) const;
161
162 //- Return the list of C-strings (ie, argv)
163 // The position at argc is a nullptr
164 inline char** strings() const noexcept;
165
166 //- Return the sublist of C-strings (ie, argv) starting at the
167 //- specified offset.
168 // \param start the offset, must be less than argc
169 inline char** strings(int start) const;
170
171
172 // Edit
173
174 //- Clear contents and free memory
175 inline void clear();
176
177 //- Copy the input list of strings.
178 // \return number of arguments (argc)
179 template<class StringType>
180 inline int reset(const UList<StringType>& input);
181
182 //- Copy the input list of strings.
183 // \return number of arguments (argc)
184 template<class StringType>
185 inline int reset(const SubStrings<StringType>& input);
186
187
188 // Other
189
190 //- Create a list from argc/argv parameters.
191 // A null pointer for argv is permissible when argc is zero.
192 template<class StringType>
193 static List<StringType> asList(int argc, const char * const argv[]);
194
195 //- Create a list from a nullptr-terminated list of argv parameters.
196 // Using a nullptr for argv is permissible.
197 template<class StringType>
198 static inline List<StringType> asList(const char * const argv[]);
199
200
201 // Member Operators
202
203 //- Return element at the given index. No bounds checking.
204 inline const char* operator[](int i) const;
205};
206
207
208// IOstream Operators
209
210//- Output space-separated list
211Ostream& operator<<(Ostream& os, const CStringList& list);
212
213
214// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
215
216} // End namespace Foam
217
218// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219
220#include "CStringListI.H"
221
222#ifdef NoRepository
223# include "CStringListTemplates.C"
224#endif
225
226// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
227
228#endif
229
230// ************************************************************************* //
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:70
~CStringList()
Destructor. Invokes clear() to free memory.
Definition: CStringListI.H:92
int size() const noexcept
Return the number of C-strings (ie, argc)
Definition: CStringListI.H:124
static List< StringType > asList(int argc, const char *const argv[])
Create a list from argc/argv parameters.
constexpr CStringList() noexcept
Default construct, adding content later (via reset).
Definition: CStringListI.H:63
const char * data() const noexcept
Same as cdata_bytes()
Definition: CStringList.H:153
static int count(const char *const argv[])
Count the number of parameters until the first nullptr.
Definition: CStringListI.H:47
bool empty() const noexcept
True if the size (ie, argc) is zero.
Definition: CStringListI.H:118
char ** strings() const noexcept
Return the list of C-strings (ie, argv)
Definition: CStringListI.H:130
int reset(const UList< StringType > &input)
Copy the input list of strings.
Definition: CStringListI.H:143
size_t length() const noexcept
Same as size_bytes()
Definition: CStringList.H:156
size_t size_bytes() const noexcept
Definition: CStringList.H:150
const char * cdata_bytes() const noexcept
The flattened character content, with interspersed nul-chars.
Definition: CStringList.H:146
void clear()
Clear contents and free memory.
Definition: CStringListI.H:100
const char * get(int i) const
Return string element at the given index. No bounds checking.
Definition: CStringListI.H:158
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition: SubStrings.H:54
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:94
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
const direction noexcept
Definition: Scalar.H:223