#include "stdafx.h" #include #include #define BUFF_LEN 4096 /** * Wrap a return string. * This function copies a string to a char buffer making sure the buffer has the correct length. * * @param *buffer Buffer to copy the string to. * @param bufLen Length of the buffer * @param str Th string to copy * @return NSCAPI::success unless the buffer is to short then it will be NSCAPI::invalidBufferLen */ /* int NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, int defaultReturnCode ) { // @todo deprecate this if (str.length() >= bufLen) return -1; strncpy(buffer, str.c_str(), bufLen); return defaultReturnCode; } */ NSCAPI::nagiosReturn NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, NSCAPI::nagiosReturn defaultReturnCode /* = NSCAPI::success */) { if (str.length() >= bufLen) return NSCAPI::returnInvalidBufferLen; strncpy(buffer, str.c_str(), bufLen); return defaultReturnCode; } NSCAPI::errorReturn NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, NSCAPI::errorReturn defaultReturnCode /* = NSCAPI::success */) { if (str.length() >= bufLen) return NSCAPI::isInvalidBufferLen; strncpy(buffer, str.c_str(), bufLen); return defaultReturnCode; } /** * Make a list out of a array of char arrays (arguments type) * @param argLen Length of argument array * @param *argument[] Argument array * @return Argument wrapped as a list */ std::list NSCHelper::arrayBuffer2list(const unsigned int argLen, char *argument[]) { std::list ret; int i=0; for (unsigned int i=0;iNotice it is up to the caller to free the memory allocated in the returned buffer. * * @param lst A list to convert. * @param &argLen Write the length to this argument. * @return A pointer that is managed by the caller. */ char ** NSCHelper::list2arrayBuffer(const std::list lst, unsigned int &argLen) { argLen = static_cast(lst.size()); char **arrayBuffer = new char*[argLen]; std::list::const_iterator it = lst.begin(); for (int i=0;it!=lst.end();++it,i++) { std::string::size_type alen = (*it).size(); arrayBuffer[i] = new char[alen+2]; strncpy(arrayBuffer[i], (*it).c_str(), alen+1); } assert(i == argLen); return arrayBuffer; } /** * Creates an empty arrayBuffer (only used to allow consistency) * @param &argLen [OUT] The length (items) of the arrayBuffer * @return The arrayBuffer */ char ** NSCHelper::createEmptyArrayBuffer(unsigned int &argLen) { argLen = 0; char **arrayBuffer = new char*[0]; return arrayBuffer; } /** * Joins an arrayBuffer back into a string * @param **argument The ArrayBuffer * @param argLen The length of the ArrayBuffer * @param join The char to use as separators when joining * @return The joined arrayBuffer */ std::string NSCHelper::arrayBuffer2string(char **argument, const unsigned int argLen, std::string join) { std::string ret; for (unsigned int i=0;i