libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
BaseLib::Io Class Reference

This class provides functions to make your life easier. More...

#include <Io.h>

Public Member Functions

 Io ()
 Constructor. More...
 
virtual ~Io ()
 Destructor. More...
 
void init (SharedObjects *baseLib)
 Initialized the object. More...
 
bool copyFile (const std::string &source, const std::string &dest)
 Copys a file. More...
 
std::string sha512 (const std::string &file)
 Calculates the SHA-2 SHA-512 of a given file. More...
 

Static Public Member Functions

static bool directoryExists (const std::string &path)
 Checks if a directory exists. More...
 
static int32_t isDirectory (const std::string &path, bool &result)
 Checks if a path is a directory. More...
 
static bool linkExists (const std::string &path)
 Checks if a link exists. More...
 
static bool createDirectory (const std::string &path, uint32_t mode)
 Creates a new directory. More...
 
static std::string getFileContent (const std::string &filename)
 Reads a file and returns the content as a string. More...
 
static std::vector< char > getBinaryFileContent (const std::string &filename, uint32_t maxBytes=0)
 Reads a file and returns the content as a signed binary array. More...
 
static std::vector< uint8_t > getUBinaryFileContent (const std::string &filename)
 Reads a file and returns the content as an unsigned binary array. More...
 
static Security::SecureVector< uint8_t > getUBinaryFileContentSecure (const std::string &filename)
 Reads a file and returns the content as an unsigned binary secure array, which is cleaned up automatically on destruction. More...
 
static bool fileExists (const std::string &filename)
 Checks if a file exists. More...
 
static void writeFile (const std::string &filename, const std::string &content)
 Writes a string to a file. More...
 
static void writeFile (const std::string &filename, const std::vector< char > &content, uint32_t length)
 Writes binary data to a file. More...
 
static void writeFile (const std::string &filename, const std::vector< uint8_t > &content, uint32_t length)
 Writes binary data to a file. More...
 
static void appendToFile (const std::string &filename, const std::string &content)
 Writes binary data to a file. More...
 
static void appendToFile (const std::string &filename, const std::vector< char > &content, uint32_t length)
 Writes binary data to a file. More...
 
static void appendToFile (const std::string &filename, const std::vector< uint8_t > &content, uint32_t length)
 Writes binary data to a file. More...
 
static std::vector< std::string > getFiles (const std::string &path, bool recursive=false)
 Returns an array of all files within a path. More...
 
static std::vector< std::string > getDirectories (const std::string &path, bool recursive=false)
 Returns an array of all directories within a path. More...
 
static int32_t getFileLastModifiedTime (const std::string &filename)
 Gets the last modified time of a file. More...
 
static bool moveFile (const std::string &source, const std::string &dest)
 Moves a file. More...
 
static bool deleteFile (const std::string &file) noexcept
 Deletes a file. More...
 
static bool writeLockFile (int fileDescriptor, bool wait)
 Write locks a file using fcntl as defined in the Single Unix Specification. More...
 
static bool readLockFile (int fileDescriptor, bool wait)
 Read locks a file using fcntl as defined in the Single Unix Specification. More...
 

Detailed Description

This class provides functions to make your life easier.

Constructor & Destructor Documentation

§ Io()

BaseLib::Io::Io ( )

Constructor.

It does nothing. You need to call init() to initialize the object.

§ ~Io()

BaseLib::Io::~Io ( )
virtual

Destructor.

Does nothing.

Member Function Documentation

§ appendToFile() [1/3]

void BaseLib::Io::appendToFile ( const std::string &  filename,
const std::string &  content 
)
static

Writes binary data to a file.

If the file doesn't exist, it will be created. If the file already exists the data will be appended.

Parameters
filenameThe path to the file to write.
contentThe content to write to the file.

§ appendToFile() [2/3]

void BaseLib::Io::appendToFile ( const std::string &  filename,
const std::vector< char > &  content,
uint32_t  length 
)
static

Writes binary data to a file.

If the file doesn't exist, it will be created. If the file already exists the data will be appended.

Parameters
filenameThe path to the file to write.
contentThe content to write to the file.
lengthThe number of bytes to write.

§ appendToFile() [3/3]

void BaseLib::Io::appendToFile ( const std::string &  filename,
const std::vector< uint8_t > &  content,
uint32_t  length 
)
static

Writes binary data to a file.

If the file doesn't exist, it will be created. If the file already exists the data will be appended.

Parameters
filenameThe path to the file to write.
contentThe content to write to the file.
lengthThe number of bytes to write.

§ copyFile()

bool BaseLib::Io::copyFile ( const std::string &  source,
const std::string &  dest 
)

Copys a file.

Parameters
sourceThe path to the file.
destThe destination path to copy the file to.
Returns
Returns true on success.

§ createDirectory()

bool BaseLib::Io::createDirectory ( const std::string &  path,
uint32_t  mode 
)
static

Creates a new directory.

Make sure, the directory doesn't exist.

Parameters
pathThe directory to create.
modeThe creation mode.
Returns
Returns 0 on success or -1 on error.

§ deleteFile()

bool BaseLib::Io::deleteFile ( const std::string &  file)
staticnoexcept

Deletes a file.

Parameters
fileThe file to delete.
Returns
Returns true on success.

§ directoryExists()

bool BaseLib::Io::directoryExists ( const std::string &  path)
static

Checks if a directory exists.

Parameters
pathThe path to the directory.
Returns
Returns true when the directory exists, or false if the directory couldn't be accessed.

§ fileExists()

bool BaseLib::Io::fileExists ( const std::string &  filename)
static

Checks if a file exists.

Parameters
filenameThe path to the file.
Returns
Returns true when the file exists, or false if the file couldn't be accessed.

§ getBinaryFileContent()

std::vector< char > BaseLib::Io::getBinaryFileContent ( const std::string &  filename,
uint32_t  maxBytes = 0 
)
static

Reads a file and returns the content as a signed binary array.

Parameters
filenameThe path to the file to read.
maxBytesMaximum number of bytes to read.
Returns
Returns the content of the file as a char array.

§ getDirectories()

std::vector< std::string > BaseLib::Io::getDirectories ( const std::string &  path,
bool  recursive = false 
)
static

Returns an array of all directories within a path.

The directories are not prefixed with "path".

Parameters
pathThe path to get all directories for.
recursiveAlso return directories within subdirectories. The directories are prefixed with the subdirectory.
Returns
Returns an array of all directory names within path.

§ getFileContent()

std::string BaseLib::Io::getFileContent ( const std::string &  filename)
static

Reads a file and returns the content as a string.

Parameters
filenameThe path to the file to read.
Returns
Returns the content of the file as a string.
Exceptions
ThrowsException on errors.

§ getFileLastModifiedTime()

int32_t BaseLib::Io::getFileLastModifiedTime ( const std::string &  filename)
static

Gets the last modified time of a file.

Parameters
filenameThe file to get the last modified time for.
Returns
The unix time stamp of the last modified time.

§ getFiles()

std::vector< std::string > BaseLib::Io::getFiles ( const std::string &  path,
bool  recursive = false 
)
static

Returns an array of all files within a path.

The files are not prefixed with "path".

Parameters
pathThe path to get all files for.
recursiveAlso return files of subdirectories. The files are prefixed with the subdirectory.
Returns
Returns an array of all file names within path.

§ getUBinaryFileContent()

std::vector< uint8_t > BaseLib::Io::getUBinaryFileContent ( const std::string &  filename)
static

Reads a file and returns the content as an unsigned binary array.

Parameters
filenameThe path to the file to read.
Returns
Returns the content of the file as an unsigned char array.

§ getUBinaryFileContentSecure()

BaseLib::Security::SecureVector< uint8_t > BaseLib::Io::getUBinaryFileContentSecure ( const std::string &  filename)
static

Reads a file and returns the content as an unsigned binary secure array, which is cleaned up automatically on destruction.

Parameters
filenameThe path to the file to read.
Returns
Returns the content of the file as an unsigned binary secure array, which is cleaned up automatically on destruction.

§ init()

void BaseLib::Io::init ( SharedObjects baseLib)

Initialized the object.

Parameters
baseLibPointer to the common base library object.

§ isDirectory()

int32_t BaseLib::Io::isDirectory ( const std::string &  path,
bool &  result 
)
static

Checks if a path is a directory.

Parameters
pathThe path to check.
[out]resultTrue when the path is a directory otherwise false
Returns
Returns 0 on success or -1 on error.

§ linkExists()

bool BaseLib::Io::linkExists ( const std::string &  path)
static

Checks if a link exists.

Parameters
pathThe path to check.
Returns
Returns true when path is a link.

§ moveFile()

bool BaseLib::Io::moveFile ( const std::string &  source,
const std::string &  dest 
)
static

Moves a file.

Parameters
sourceThe path to the file.
destThe destination path to move the file to.
Returns
Returns true on success.

§ readLockFile()

bool BaseLib::Io::readLockFile ( int  fileDescriptor,
bool  wait 
)
static

Read locks a file using fcntl as defined in the Single Unix Specification.

Note that the file stays locked until the file descriptor is closed the first time.

Parameters
fileDescriptorThe filedescriptor of the file to lock.
waitWait for the lock to be acquired.
Returns
Returns true if the file was locked successful and false if the file is locked already.

§ sha512()

std::string BaseLib::Io::sha512 ( const std::string &  file)

Calculates the SHA-2 SHA-512 of a given file.

Parameters
fileThe file to calculate the SHA-512 for.
Returns
Returns the SHA-512 or an empty string on errors.

§ writeFile() [1/3]

void BaseLib::Io::writeFile ( const std::string &  filename,
const std::string &  content 
)
static

Writes a string to a file.

If the file already exists it will be overwritten.

Parameters
filenameThe path to the file to write.
contentThe content to write to the file.

§ writeFile() [2/3]

void BaseLib::Io::writeFile ( const std::string &  filename,
const std::vector< char > &  content,
uint32_t  length 
)
static

Writes binary data to a file.

If the file already exists it will be overwritten.

Parameters
filenameThe path to the file to write.
contentThe content to write to the file.
lengthThe number of bytes to write.

§ writeFile() [3/3]

void BaseLib::Io::writeFile ( const std::string &  filename,
const std::vector< uint8_t > &  content,
uint32_t  length 
)
static

Writes binary data to a file.

If the file already exists it will be overwritten.

Parameters
filenameThe path to the file to write.
contentThe content to write to the file.
lengthThe number of bytes to write.

§ writeLockFile()

bool BaseLib::Io::writeLockFile ( int  fileDescriptor,
bool  wait 
)
static

Write locks a file using fcntl as defined in the Single Unix Specification.

Note that the file stays locked until the file descriptor is closed the first time.

Parameters
fileDescriptorThe filedescriptor of the file to lock.
waitWait for the lock to be acquired.
Returns
Returns true if the file was locked successful and false if the file is locked already.

The documentation for this class was generated from the following files: