CStringListI.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-2017 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 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
29 
30 inline char* Foam::CStringList::stringCopy(char *dest, const std::string& src)
31 {
32  // Like string::copy() but without extra checks
33  const size_t len = src.length();
34  for (size_t i = 0; i < len; ++i)
35  {
36  *dest = src[i];
37  ++dest;
38  }
39  *(dest++) = '\0';
40 
41  return dest;
42 }
43 
44 
45 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
46 
47 inline int Foam::CStringList::count(const char * const argv[])
48 {
49  int n = 0;
50  if (argv)
51  {
52  while (argv[n])
53  {
54  ++n;
55  }
56  }
57  return n;
58 }
59 
60 
61 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
62 
64 :
65  argc_(0),
66  nbytes_(0),
67  argv_(nullptr),
68  data_(nullptr)
69 {}
70 
71 
72 template<class StringType>
74 :
76 {
77  reset(input);
78 }
79 
80 
81 template<class StringType>
83 :
84  CStringList()
85 {
86  reset(input);
87 }
88 
89 
90 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
91 
93 {
94  clear();
95 }
96 
97 
98 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
99 
100 inline void Foam::CStringList::clear()
101 {
102  argc_ = 0;
103  nbytes_ = 0;
104 
105  if (data_)
106  {
107  delete[] data_;
108  data_ = nullptr;
109  }
110  if (argv_)
111  {
112  delete[] argv_;
113  argv_ = nullptr;
114  }
115 }
116 
118 inline bool Foam::CStringList::empty() const noexcept
119 {
120  return !argc_;
121 }
122 
124 inline int Foam::CStringList::size() const noexcept
125 {
126  return argc_;
127 }
128 
130 inline char** Foam::CStringList::strings() const noexcept
131 {
132  return argv_;
133 }
134 
135 
136 inline char** Foam::CStringList::strings(int start) const
137 {
138  return &(argv_[start]);
139 }
140 
141 
142 template<class StringType>
144 {
145  return resetContent(input);
146 }
147 
148 
149 template<class StringType>
151 {
152  return resetContent(input);
153 }
154 
155 
156 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
158 inline const char* Foam::CStringList::get(int i) const
159 {
160  return argv_[i];
161 }
162 
163 
164 inline const char* Foam::CStringList::operator[](int i) const
165 {
166  return argv_[i];
167 }
168 
169 
170 // ************************************************************************* //
int reset(const UList< StringType > &input)
Copy the input list of strings.
Definition: CStringListI.H:136
void clear()
Clear contents and free memory.
Definition: CStringListI.H:93
const char * operator[](int i) const
Return element at the given index. No bounds checking.
Definition: CStringListI.H:157
const char * get(int i) const
Return string element at the given index. No bounds checking.
Definition: CStringListI.H:151
char ** strings() const noexcept
Return the list of C-strings (ie, argv)
Definition: CStringListI.H:123
bool empty() const noexcept
True if the size (ie, argc) is zero.
Definition: CStringListI.H:111
int size() const noexcept
Return the number of C-strings (ie, argc)
Definition: CStringListI.H:117
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition: CStringList.H:57
static int count(const char *const argv[])
Count the number of parameters until the first nullptr.
Definition: CStringListI.H:40
constexpr CStringList() noexcept
Default construct, adding content later (via reset).
Definition: CStringListI.H:56
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
patchWriters clear()
const direction noexcept
Definition: Scalar.H:258
label n
~CStringList()
Destructor. Invokes clear() to free memory.
Definition: CStringListI.H:85
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:63