Header-only C++ wrappers for POSIX APIs.
This is a header-only library. Copy the modernposix
directory from under include
into your project's include directory.
Compile samples and unittests using cmake . && make
- The library requires C++11 or newer
- All APIs and function calls do NOT provide additional thread-safety over the existing API
Following the CPP naming convention, I use the name pstream as in 'Process stream'.
We have two different pstreams, an input stream (ipstream
) and an output stream (opstream
).
The streams handle the underlying file descriptors and spares the hassle from you (the programmer).
The following snippet executes the ls -l
command and stores the output into a vector of strings:
try
{
mp::ipstream reader("ls -l");
vector<string> output;
reader >> output;
}
catch (std::runtime_error& ex)
{
// Something went wrong and ex.what() will tell you what it is
}
The following snippet executes the wall
broadcast command and writes a message.
Upon destruction the writer closes the stream and flushes the content:
try
{
mp::opstream writer("wall");
writer << "WOW! This library rocks";
}
catch (std::runtime_error& ex)
{
// Something went wrong and ex.what() will tell you what it is
}
This class wraps dlopen()
, dlsym()
, dlclose()
& dlerror()
APIs, allowing easy access to dynamic libraries.
The class assures that the library is closed upon destruction.
You can also specify the type of initilization you require upon construction, default is LAZY and LOCAL, see man dlopen(3)
for more information.
The following snippet loads a shared library written in C and calls one of its exported symbols:
try
{
mp::dynamiclib dlib(TEST_LIB_C);
multiply_fp multiply = static_cast<multiply_fp>(dlib.symbol(TEST_LIB_C_MULTIPLY));
multiply(a,b);
}
catch (std::runtime_error& ex)
{
// Something went wrong and ex.what() will tell you what it is
}
This class simply wraps the opendir()
, readdir()
, rewinddir()
& closedir()
APIs, allowing easy access to directory entry enumeration.
The following snippet reads all the entries in a given directory:
try
{
mp::idstream ds(TEST_DIR);
set<string> output;
ds >> output;
}
catch (std::runtime_error & ex)
{
// Something went wrong and ex.what() will tell you what it is
}
This snippet shows how an application can change its working directory just for a few commands. When you leave the scope, it will change the directory straight back.
You can also use mp::workdir::chdir
directly if you want.
try
{
string initial_workdir;
mp::workdir::getcwd(initial_workdir);
mp::workdir::pushd("/tmp");
// Do stuff...
}
catch (std::runtime_error & ex)
{
// Something went wrong and ex.what() will tell you what it is
}