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-2019 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 CStringList_H
52 #define CStringList_H
53 
54 #include "fileNameList.H"
55 #include "stringList.H"
56 #include "wordList.H"
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 namespace Foam
61 {
62 
63 // Forward Declarations
64 template<class String> class SubStrings;
65 
66 /*---------------------------------------------------------------------------*\
67  Class CStringList Declaration
68 \*---------------------------------------------------------------------------*/
69 
70 class 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 len_;
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 the location one-past the end of dest, which is used
94  // for the next destination
95  static inline char* stringCopy(char *dest, const std::string& str);
96 
97  //- Copy the input list of strings.
98  // \return number of arguments (argc)
99  template<class ListType>
100  int resetContent(const ListType& input);
101 
102 
103  //- No copy construct
104  CStringList(const CStringList&) = delete;
105 
106  //- No copy assignment
107  void operator=(const CStringList&) = delete;
108 
109 
110 public:
111 
112  // Constructors
113 
114  //- Construct empty, adding content later (via reset).
115  inline CStringList();
116 
117  //- Copy construct from a list of strings
118  // Copies the input characters.
119  template<class StringType>
120  inline explicit CStringList(const UList<StringType>& input);
121 
122  //- Copy construct from a list of sub-string references
123  // Copies the input characters.
124  template<class StringType>
125  inline explicit CStringList(const SubStrings<StringType>& input);
126 
127 
128  //- Destructor. Invokes clear() to free memory.
129  inline ~CStringList();
130 
131 
132  // Public Members
133 
134  //- Count the number of parameters until the first nullptr
135  // Return 0 if argv is nullptr.
136  static inline int count(const char * const argv[]);
137 
138 
139  // Access
140 
141  //- True if the size is zero.
142  inline bool empty() const noexcept;
143 
144  //- Return the number of C-strings (ie, argc)
145  inline int size() const noexcept;
146 
147  //- Return the list of C-strings (ie, argv)
148  // The position at argc is a nullptr
149  inline char** strings() const;
150 
151  //- Return the sublist of C-strings (ie, argv) starting at the
152  //- specified offset.
153  // \param start the offset, must be less than argc
154  inline char** strings(int start) const;
155 
156 
157  //- Overall length of the flattened character (data) content
158  inline size_t length() const;
159 
160  //- The flattened character content, with interspersed nul-chars
161  inline const char* data() const;
162 
163 
164  // Edit
165 
166  //- Clear contents and free memory
167  inline void clear();
168 
169  //- Copy the input list of strings.
170  // \return number of arguments (argc)
171  template<class StringType>
172  inline int reset(const UList<StringType>& input);
173 
174  //- Copy the input list of strings.
175  // \return number of arguments (argc)
176  template<class StringType>
177  inline int reset(const SubStrings<StringType>& input);
178 
179 
180  // Other
181 
182  //- Create a list from argc/argv parameters.
183  // A null pointer for argv is permissible when argc is zero.
184  template<class StringType>
185  static List<StringType> asList(int argc, const char * const argv[]);
186 
187  //- Create a list from a nullptr-terminated list of argv parameters.
188  // Using a nullptr for argv is permissible.
189  template<class StringType>
190  static inline List<StringType> asList(const char * const argv[]);
191 
192 
193  // Member operators
194 
195  //- Return element at the given index. No bounds checking.
196  inline const char* operator[](int i) const;
197 };
198 
199 
200 // IOstream Operators
201 
202 //- Output space-separated list
203 Ostream& operator<<(Ostream& os, const CStringList& list);
204 
205 
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207 
208 } // End namespace Foam
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
212 #include "CStringListI.H"
213 
214 #ifdef NoRepository
215 # include "CStringListTemplates.C"
216 #endif
217 
218 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219 
220 #endif
221 
222 // ************************************************************************* //
Foam::CStringList::strings
char ** strings() const
Return the list of C-strings (ie, argv)
Definition: CStringListI.H:134
CStringListI.H
Foam::SubStrings
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition: CStringList.H:63
wordList.H
Foam::CStringList::reset
int reset(const UList< StringType > &input)
Copy the input list of strings.
Definition: CStringListI.H:153
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::CStringList::operator[]
const char * operator[](int i) const
Return element at the given index. No bounds checking.
Definition: CStringListI.H:168
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::CStringList::data
const char * data() const
The flattened character content, with interspersed nul-chars.
Definition: CStringListI.H:146
Foam::CStringList::CStringList
CStringList()
Construct empty, adding content later (via reset).
Definition: CStringListI.H:61
fileNameList.H
Foam::CStringList::size
int size() const noexcept
Return the number of C-strings (ie, argc)
Definition: CStringListI.H:122
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::UList< StringType >
Foam::CStringList::asList
static List< StringType > asList(int argc, const char *const argv[])
Create a list from argc/argv parameters.
Foam::CStringList::length
size_t length() const
Overall length of the flattened character (data) content.
Definition: CStringListI.H:128
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::CStringList::~CStringList
~CStringList()
Destructor. Invokes clear() to free memory.
Definition: CStringListI.H:90
stringList.H
CStringListTemplates.C
Foam::CStringList::empty
bool empty() const noexcept
True if the size is zero.
Definition: CStringListI.H:116
Foam::CStringList::clear
void clear()
Clear contents and free memory.
Definition: CStringListI.H:98
Foam::CStringList::count
static int count(const char *const argv[])
Count the number of parameters until the first nullptr.
Definition: CStringListI.H:45
Foam::CStringList
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:69