Skip to content

Commit

Permalink
cleanup utilities to prevent p8-platform
Browse files Browse the repository at this point in the history
  • Loading branch information
AlwinEsch committed Jun 2, 2020
1 parent 8125c90 commit 57a1103
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
119 changes: 119 additions & 0 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

using namespace ADDON;

#define FORMAT_BLOCK_SIZE 2048 // # of bytes to increment per try

using namespace Utils;

// format related string functions taken from:
// http://www.flipcode.com/archives/Safe_sprintf.shtml

Expand All @@ -24,6 +28,121 @@ bool Str2Bool(const std::string& str)
return str.compare("True") == 0 ? true : false;
}

std::vector<std::string> Split(const std::string& input, const std::string& delimiter, unsigned int iMaxStrings /* = 0 */)
{
std::vector<std::string> results;
if (input.empty())
return results;

size_t iPos = std::string::npos;
size_t newPos = std::string::npos;
size_t sizeS2 = delimiter.size();
size_t isize = input.size();

std::vector<unsigned int> positions;

newPos = input.find(delimiter, 0);

if (newPos == std::string::npos)
{
results.push_back(input);
return results;
}

while (newPos != std::string::npos)
{
positions.push_back(newPos);
iPos = newPos;
newPos = input.find(delimiter, iPos + sizeS2);
}

// numFound is the number of delimiters which is one less
// than the number of substrings
unsigned int numFound = positions.size();
if (iMaxStrings > 0 && numFound >= iMaxStrings)
numFound = iMaxStrings - 1;

for ( unsigned int i = 0; i <= numFound; i++ )
{
std::string s;
if ( i == 0 )
{
if ( i == numFound )
s = input;
else
s = input.substr(i, positions[i]);
}
else
{
size_t offset = positions[i - 1] + sizeS2;
if ( offset < isize )
{
if ( i == numFound )
s = input.substr(offset);
else if ( i > 0 )
s = input.substr( positions[i - 1] + sizeS2,
positions[i] - positions[i - 1] - sizeS2 );
}
}
results.push_back(s);
}
return results;
}

std::string Format(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
std::string str = FormatV(fmt, args);
va_end(args);

return str;
}

std::string FormatV(const char *fmt, va_list args)
{
if (fmt == nullptr)
return "";

int size = FORMAT_BLOCK_SIZE;
va_list argCopy;

char *cstr = reinterpret_cast<char*>(malloc(sizeof(char) * size));
if (cstr == nullptr)
return "";

while (1)
{
va_copy(argCopy, args);

int nActual = vsnprintf(cstr, size, fmt, argCopy);
va_end(argCopy);

if (nActual > -1 && nActual < size) // We got a valid result
{
std::string str(cstr, nActual);
free(cstr);
return str;
}
if (nActual > -1) // Exactly what we will need (glibc 2.1)
size = nActual + 1;
else // Let's try to double the size (glibc 2.0)
size *= 2;

char *new_cstr = reinterpret_cast<char*>(realloc(cstr, sizeof(char) * size));
if (new_cstr == nullptr)
{
free(cstr);
return "";
}

cstr = new_cstr;
}

free(cstr);
return "";
}

bool EndsWith(std::string const& fullString, std::string const& ending)
{
if (fullString.length() >= ending.length())
Expand Down
9 changes: 7 additions & 2 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
#include <string>
#include <vector>

//std::vector<std::string> split(const std::string& s, const std::string& delim);
namespace Utils
{

std::vector<std::string> Split(const std::string& input, const std::string& delimiter, unsigned int iMaxStrings = 0);
bool Str2Bool(const std::string& str);

std::string Format(const char *fmt, ...);
std::string FormatV(const char *fmt, va_list args);
bool EndsWith(std::string const& fullString, std::string const& ending);
bool StartsWith(std::string const& fullString, std::string const& starting);
std::string GetDirectoryPath(std::string const& path);
bool ReadFileContents(std::string const& strFileName, std::string& strResult);
bool WriteFileContents(std::string const& strFileName, std::string& strContent);

} /* namespace Utils */

0 comments on commit 57a1103

Please sign in to comment.