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