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-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 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
29 
30 inline char* Foam::CStringList::stringCopy(char* dest, const char* src)
31 {
32  dest = std::copy_n(src, (src ? strlen(src) : 0), dest);
33  *(dest++) = '\0';
34  return dest;
35 }
36 
37 
38 inline char* Foam::CStringList::stringCopy(char *dest, const std::string& src)
39 {
40  dest = std::copy_n(src.data(), src.size(), dest);
41  *(dest++) = '\0';
42  return dest;
43 }
44 
45 
46 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
47 
48 inline int Foam::CStringList::count(const char * const argv[])
49 {
50  int n = 0;
51  if (argv)
52  {
53  while (argv[n])
54  {
55  ++n;
56  }
57  }
58  return n;
59 }
60 
61 
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
63 
65 :
66  argc_(0),
67  nbytes_(0),
68  argv_(nullptr),
69  data_(nullptr)
70 {}
71 
72 
74 (
75  std::initializer_list<const char* const> input
76 )
77 :
79 {
80  reset(input);
81 }
82 
83 
84 template<class StringType>
86 :
87  CStringList()
88 {
89  resetContent(input);
90 }
91 
92 
93 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
94 
96 {
97  clear();
98 }
99 
100 
101 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
102 
103 inline void Foam::CStringList::clear()
104 {
105  argc_ = 0;
106  nbytes_ = 0;
107 
108  if (data_)
109  {
110  delete[] data_;
111  data_ = nullptr;
112  }
113  if (argv_)
114  {
115  delete[] argv_;
116  argv_ = nullptr;
117  }
118 }
119 
120 
121 inline char** Foam::CStringList::strings(int start) const
122 {
123  return &(argv_[start]);
124 }
125 
126 
127 // ************************************************************************* //
void clear()
Clear contents and free memory.
Definition: CStringListI.H:96
static int count(const char *const argv[])
Count the number of parameters until the first nullptr.
Definition: CStringListI.H:41
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
limits reset(1/(limits.max()+VSMALL), 1/(limits.min()+VSMALL))
surface1 clear()
label n
~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