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