mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-12-28 03:37:59 +00:00
ENH: adapter for a list of C++ strings <-> a list of C-style strings
- Translate a list of C++ strings into C-style (argc, argv) pair. - Translate C-style (argc, argv) pair to list of C++ strings. Useful when interfacing to external C-code and some libraries
This commit is contained in:
3
applications/test/cstring/Make/files
Normal file
3
applications/test/cstring/Make/files
Normal file
@ -0,0 +1,3 @@
|
||||
Test-cstring.C
|
||||
|
||||
EXE = $(FOAM_USER_APPBIN)/Test-cstring
|
||||
2
applications/test/cstring/Make/options
Normal file
2
applications/test/cstring/Make/options
Normal file
@ -0,0 +1,2 @@
|
||||
/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */
|
||||
/* EXE_LIBS = -lfiniteVolume */
|
||||
99
applications/test/cstring/Test-cstring.C
Normal file
99
applications/test/cstring/Test-cstring.C
Normal file
@ -0,0 +1,99 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2016 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
|
||||
OpenFOAM is free software: you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
|
||||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Description
|
||||
Test some string functionality
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "CStringList.H"
|
||||
#include "DynamicList.H"
|
||||
#include "IOstreams.H"
|
||||
#include "fileNameList.H"
|
||||
#include "stringList.H"
|
||||
#include "wordList.H"
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
int print(int argc, char *argv[])
|
||||
{
|
||||
Info<< "argc=" << argc << endl;
|
||||
for (int i=0; i<argc; ++i)
|
||||
{
|
||||
Info<< " argv[" << i << "] = \"" << argv[i] << "\"" << endl;
|
||||
}
|
||||
return argc;
|
||||
}
|
||||
|
||||
int print(const CStringList& cstrLst)
|
||||
{
|
||||
return print(cstrLst.size(), cstrLst.strings());
|
||||
}
|
||||
|
||||
|
||||
// Main program:
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
DynamicList<string> dynlst;
|
||||
dynlst.reserve(16);
|
||||
|
||||
dynlst.append("string1 with content");
|
||||
dynlst.append("string2 other content");
|
||||
dynlst.append("string3 done");
|
||||
|
||||
{
|
||||
CStringList inC(dynlst);
|
||||
|
||||
Info<< "input: " << dynlst << endl;
|
||||
print(inC);
|
||||
}
|
||||
|
||||
Info<<"command-line with " << CStringList::count(argv) << " items"<< endl;
|
||||
|
||||
print(argc, argv);
|
||||
{
|
||||
dynlst.clear();
|
||||
for (int i=0; i<argc; ++i)
|
||||
{
|
||||
dynlst.append(argv[i]);
|
||||
}
|
||||
|
||||
Info<< "input: " << dynlst << endl;
|
||||
CStringList inC(dynlst);
|
||||
inC.reset(dynlst);
|
||||
|
||||
print(inC);
|
||||
Info<< "length: " << inC.length() << endl;
|
||||
std::cout.write(inC.data(), inC.length());
|
||||
}
|
||||
|
||||
|
||||
Info<< "\nEnd\n" << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
175
src/OpenFOAM/primitives/strings/lists/CStringList.H
Normal file
175
src/OpenFOAM/primitives/strings/lists/CStringList.H
Normal file
@ -0,0 +1,175 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2016 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
|
||||
OpenFOAM is free software: you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
|
||||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Class
|
||||
Foam::CStringList
|
||||
|
||||
Description
|
||||
An adapter for copying a list of C++ strings into a list of C-style
|
||||
strings for passing to C code that expects argc/argv parameters.
|
||||
|
||||
In addition to providing a C-compatible list of C-strings,
|
||||
the string lists are flattened into a single string of data that can be
|
||||
also be passed en mass.
|
||||
|
||||
Example use:
|
||||
\code
|
||||
wordList myStrings; ...
|
||||
CStringList cstr(myStrings);
|
||||
|
||||
// pass as argc, argv:
|
||||
someMain(cstr.size(), cstr.strings());
|
||||
|
||||
// access the raw characters:
|
||||
os.write(cstr.data(), cstr.length());
|
||||
\endcode
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef CStringList_H
|
||||
#define CStringList_H
|
||||
|
||||
#include "fileNameList.H"
|
||||
#include "stringList.H"
|
||||
#include "wordList.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
namespace Foam
|
||||
{
|
||||
|
||||
/*---------------------------------------------------------------------------*\
|
||||
Class CStringList Declaration
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
class CStringList
|
||||
{
|
||||
// Private data
|
||||
|
||||
//- Number of strings
|
||||
int argc_;
|
||||
|
||||
//- Overall length of the raw content
|
||||
// Does not include the final nul-character
|
||||
size_t len_;
|
||||
|
||||
//- List of strings, including trailing NULL pointer
|
||||
char** argv_;
|
||||
|
||||
//- Flattened content with interspersed nul-characters
|
||||
char* data_;
|
||||
|
||||
|
||||
// Private Member Functions
|
||||
|
||||
//- Disallow default bitwise copy construct
|
||||
CStringList(const CStringList&) = delete;
|
||||
|
||||
//- Disallow default bitwise assignment
|
||||
void operator=(const CStringList&) = delete;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
// Constructors
|
||||
|
||||
//- Construct empty, adding content later (via reset).
|
||||
inline CStringList();
|
||||
|
||||
|
||||
//- Construct from a list of strings
|
||||
// Copies the input characters.
|
||||
template<class StringType>
|
||||
CStringList(const UList<StringType>& input);
|
||||
|
||||
|
||||
//- Destructor
|
||||
inline ~CStringList();
|
||||
|
||||
|
||||
// Public Members
|
||||
|
||||
//- Count the number of parameters until the first NULL pointer.
|
||||
// Return 0 if argv is NULL.
|
||||
static inline int count(const char * const argv[]);
|
||||
|
||||
|
||||
// Access
|
||||
|
||||
//- Return the number of C-strings (ie, argc)
|
||||
inline int size() const;
|
||||
|
||||
//- Return the list of C-strings (ie, argv)
|
||||
// The position at argc is a NULL pointer
|
||||
inline char** strings() const;
|
||||
|
||||
|
||||
//- Overall length of the flattened character (data) content
|
||||
inline size_t length() const;
|
||||
|
||||
//- The flattened character content, with interspersed nul-chars
|
||||
inline char* data() const;
|
||||
|
||||
|
||||
// Edit
|
||||
|
||||
//- Clear contents and free memory
|
||||
inline void clear();
|
||||
|
||||
//- Copy the input list of strings.
|
||||
template<class StringType>
|
||||
void reset(const UList<StringType>& input);
|
||||
|
||||
|
||||
// Other
|
||||
|
||||
//- Create a list from argc/argv parameters.
|
||||
// A null pointer for argv is permissible when argc is zero.
|
||||
template<class StringType>
|
||||
static List<StringType> asList(int argc, const char * const argv[]);
|
||||
|
||||
//- Create a list from a NULL-terminated list of argv parameters.
|
||||
// A null pointer for argv is permissible.
|
||||
template<class StringType>
|
||||
static inline List<StringType> asList(const char * const argv[]);
|
||||
|
||||
};
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
} // End namespace Foam
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#include "CStringListI.H"
|
||||
|
||||
#ifdef NoRepository
|
||||
# include "CStringListTemplates.C"
|
||||
#endif
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
#endif
|
||||
|
||||
// ************************************************************************* //
|
||||
106
src/OpenFOAM/primitives/strings/lists/CStringListI.H
Normal file
106
src/OpenFOAM/primitives/strings/lists/CStringListI.H
Normal file
@ -0,0 +1,106 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2016 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
|
||||
OpenFOAM is free software: you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
|
||||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
inline int Foam::CStringList::count(const char * const argv[])
|
||||
{
|
||||
int nElem = 0;
|
||||
if (argv)
|
||||
{
|
||||
while (argv[nElem])
|
||||
{
|
||||
++nElem;
|
||||
}
|
||||
}
|
||||
|
||||
return nElem;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
inline Foam::CStringList::CStringList()
|
||||
:
|
||||
argc_(0),
|
||||
len_(0),
|
||||
argv_(0),
|
||||
data_(0)
|
||||
{}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
|
||||
|
||||
inline Foam::CStringList::~CStringList()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
inline void Foam::CStringList::clear()
|
||||
{
|
||||
argc_ = 0;
|
||||
len_ = 0;
|
||||
|
||||
if (data_)
|
||||
{
|
||||
delete[] data_;
|
||||
data_ = 0;
|
||||
}
|
||||
if (argv_)
|
||||
{
|
||||
delete[] argv_;
|
||||
argv_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline int Foam::CStringList::size() const
|
||||
{
|
||||
return argc_;
|
||||
}
|
||||
|
||||
|
||||
inline size_t Foam::CStringList::length() const
|
||||
{
|
||||
return len_;
|
||||
}
|
||||
|
||||
|
||||
inline char** Foam::CStringList::strings() const
|
||||
{
|
||||
return argv_;
|
||||
}
|
||||
|
||||
|
||||
inline char* Foam::CStringList::data() const
|
||||
{
|
||||
return data_;
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
115
src/OpenFOAM/primitives/strings/lists/CStringListTemplates.C
Normal file
115
src/OpenFOAM/primitives/strings/lists/CStringListTemplates.C
Normal file
@ -0,0 +1,115 @@
|
||||
/*---------------------------------------------------------------------------*\
|
||||
========= |
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2016 OpenCFD Ltd.
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
|
||||
OpenFOAM is free software: you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
|
||||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
template<class StringType>
|
||||
Foam::CStringList::CStringList
|
||||
(
|
||||
const UList<StringType>& input
|
||||
)
|
||||
:
|
||||
argc_(0),
|
||||
len_(0),
|
||||
argv_(0),
|
||||
data_(0)
|
||||
{
|
||||
reset(input);
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
template<class StringType>
|
||||
void Foam::CStringList::reset
|
||||
(
|
||||
const UList<StringType>& input
|
||||
)
|
||||
{
|
||||
clear();
|
||||
|
||||
argc_ = input.size();
|
||||
forAll(input, argI)
|
||||
{
|
||||
len_ += input[argI].size();
|
||||
++len_; // nul terminator for C-strings
|
||||
}
|
||||
|
||||
argv_ = new char*[argc_+1];
|
||||
argv_[argc_] = NULL; // extra terminator
|
||||
|
||||
if (argc_ > 0)
|
||||
{
|
||||
// allocation includes final nul terminator,
|
||||
// but overall count does not
|
||||
data_ = new char[len_--];
|
||||
|
||||
char* ptr = data_;
|
||||
forAll(input, argI)
|
||||
{
|
||||
argv_[argI] = ptr;
|
||||
|
||||
const std::string& str =
|
||||
static_cast<const std::string&>(input[argI]);
|
||||
|
||||
for
|
||||
(
|
||||
std::string::const_iterator iter = str.begin();
|
||||
iter != str.end();
|
||||
++iter
|
||||
)
|
||||
{
|
||||
*(ptr++) = *iter;
|
||||
}
|
||||
*(ptr++) = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
Foam::List<StringType>
|
||||
Foam::CStringList::asList(int argc, const char * const argv[])
|
||||
{
|
||||
List<StringType> lst(argc);
|
||||
|
||||
for (int i=0; i < argc; ++i)
|
||||
{
|
||||
lst[i] = argv[i];
|
||||
}
|
||||
|
||||
return lst;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
Foam::List<StringType>
|
||||
Foam::CStringList::asList(const char * const argv[])
|
||||
{
|
||||
return asList<StringType>(count(argv), argv);
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
Reference in New Issue
Block a user