Starting from 2021-07-01, all LRZ GitLab users will be required to explicitly accept the GitLab Terms of Service. Please see the detailed information at https://doku.lrz.de/display/PUBLIC/GitLab and make sure that your projects conform to the requirements.

Commit c2bc7a7c authored by schultezub's avatar schultezub
Browse files

* introduced StringUtils

 * added WeaklyTypedPointer documentation

git-svn-id: https://camplinux.in.tum.de/svn/campvis/trunk@166 bb408c1c-ae56-11e1-83d9-df6b3e0c105e
parent 659aa814
#include "stringutils.h"
#include <algorithm>
namespace TUMVis {
std::string StringUtils::lowercase(const std::string& str) {
std::string toReturn(str);
std::transform(toReturn.begin(), toReturn.end(), toReturn.begin(), ::tolower);
return toReturn;
}
std::string StringUtils::uppercase(const std::string& str) {
std::string toReturn(str);
std::transform(toReturn.begin(), toReturn.end(), toReturn.begin(), ::toupper);
return toReturn;
}
std::vector<std::string> StringUtils::parseFloats(const std::string& str) {
static const std::string floatCharacters("0123456789.-");
std::vector<std::string> toReturn;
size_t strpos = 0;
size_t endpos = 0;
// we just started or just finished parsing an entry, check if finished and skip to beginning of next entry
while ((endpos != std::string::npos) && (strpos = str.find_first_of(floatCharacters, strpos)) != std::string::npos) {
// strpos currently points to the beginning of a float, now find its end
endpos = str.find_first_not_of(floatCharacters, strpos+1);
// extract float
std::string token = str.substr(strpos, endpos - strpos);
// sanity checks
size_t signPos = token.rfind('-');
if (signPos == 0 || signPos == std::string::npos) { // sign only allowed at beginning
if (token.find('.') == token.rfind('.')) { // only one . allowed
toReturn.push_back(token);
}
}
strpos = endpos + 1;
}
return toReturn;
}
std::string StringUtils::replaceAll(const std::string& str, const std::string& from, const std::string& to) {
std::string toReturn(str);
std::string::size_type strpos = 0;
std::string::size_type foundpos;
while((foundpos = toReturn.find(from, strpos)) != std::string::npos) {
toReturn.replace(foundpos, from.size(), to);
strpos = foundpos + to.size();
}
return toReturn;
}
std::vector<std::string> StringUtils::split(const std::string& line, const std::string& delimiter) {
std::vector<std::string> toReturn;
std::string::size_type linepos = 0;
std::string::size_type endpos = 0;
// we are at the beginning of an entry, skip whitespaces and check if not already reached end of line
while (endpos != std::string::npos) {
endpos = line.find_first_of(delimiter, linepos);
toReturn.push_back(line.substr(linepos, endpos - linepos));
linepos = endpos + 1;
}
return toReturn;
}
std::vector<std::string> StringUtils::spiltStringsafe(const std::string& str, const std::string& delimiter, char quotes /*= '"'*/, const std::string& whitespace /*= " \t"*/) {
std::vector<std::string> toReturn;
std::string::size_type strpos = 0;
std::string::size_type endpos = 0;
// we are at the beginning of an entry, skip whitespaces and check if not already reached end of str
while ((endpos != std::string::npos) && (strpos = str.find_first_not_of(whitespace, strpos)) != std::string::npos) {
// now strpos points to the first non blank character, here starts the entry
// check whether there are quotes
if (str[strpos] == quotes) {
// find position of closing quotes
endpos = str.find_first_of('"', strpos + 1);
std::string toPush = str.substr(strpos + 1 , endpos - strpos - 1);
// ensure we haven't found double quotes ("") which shall be resolved to one double quote in resulting string
while ((endpos != std::string::npos) && (endpos + 1 < str.length()) && (str[endpos + 1] == '"')) {
strpos = endpos + 1;
endpos = str.find_first_of('"', endpos + 2);
toPush.append(str.substr(strpos, endpos - strpos));
}
// push string in quotes onto toReturn
toReturn.push_back(StringUtils::trim(toPush, whitespace));
// ignore everything until next delimiter
endpos = str.find_first_of(delimiter, endpos);
}
// ok, this entry is not in quotes - just push everything until next delimiter onto toReturn
else {
endpos = str.find_first_of(delimiter, strpos);
toReturn.push_back(StringUtils::trim(str.substr(strpos, endpos - strpos), whitespace));
}
strpos = endpos + 1;
}
return toReturn;
}
std::string StringUtils::trim(const std::string& str, const std::string& whitespace /*= " \t"*/) {
std::string::size_type first = str.find_first_not_of(whitespace);
if(first == std::string::npos) {
return "";
}
else {
std::string::size_type last = str.find_last_not_of(whitespace);
return str.substr(first, last - first + 1);
}
}
}
\ No newline at end of file
#ifndef STRINGUTILS_H__
#define STRINGUTILS_H__
#include "tgt/logmanager.h"
#include <sstream>
#include <string>
#include <vector>
namespace TUMVis {
/**
* Collection of various helper methods for strings.
*
* \sa std::string
*
* \todo Test, test, test!
*/
class StringUtils {
/**
* Converts the string \a str to lowercase.
* \param str String to convert.
* \return Lowercase version of \a str.
*/
static std::string lowercase(const std::string& str);
/**
* Converts the string \a str to uppercase.
* \param str String to convert.
* \return Uppercase version of \a str.
*/
static std::string uppercase(const std::string& str);
/**
* Splits \a str into a vector of strings representing float values.
* Floats formatted as [-][0-9]*.[0-9]* are considered, all other characters in between are ignored.
*
* \param str Input string to parse.
*
* \note TODO: The detection algorithm is a litte simplified and will not yield correct results
* in every case.
**/
static std::vector<std::string> parseFloats(const std::string& str);
/**
* Replaces all occurences of \a from in \a str with \a to.
* \param str String to perform replacement on.
* \param from String to be replaced.
* \param to String repace.
* \return \a str with all occurences of \a from replaced with \a to.
*/
static std::string replaceAll(const std::string& str, const std::string& from, const std::string& to);
/**
* Splits the string \a str into pieces separated by the delimiters in \a delimiter.
* \param str String to split.
* \param delimiter Set of delimiters.
* \return Vector of the split substrings.
*/
static std::vector<std::string> split(const std::string& str, const std::string& delimiter);
/**
* Splits the string \a str into trimmed pieces separated by the delimiters in \a delimiter.
* Delimiters in quoted strings (\a quotes) will be ignored, double quotes within quoted strings will be
* interpreted as literal quotes. Each token will be trimmed.
*
* \param str String to split.
* \param delimiter Set of delimiters.
* \param quotes Character used for quotes.
* \param whitespace Set of whitespace characters which shall be removed during trimming.
* \return Vector of the split substrings.
*/
static std::vector<std::string> spiltStringsafe(const std::string& str, const std::string& delimiter, char quotes = '"', const std::string& whitespace = " \t");
/**
* Trims the string \a str.
* All leading and trailing occurences of the characters in \a whitespace will be removed.
*
* \param str The string to trim.
* \param whitespace Set of whitespace characters which shall be removed at the beginning and the end.
* \return The original string without leading and trailing whitespaces.
*/
static std::string trim(const std::string& str, const std::string& whitespace = " \t");
/**
* Converts the value \a value to a string.
* \param value The value to convert, must be compatible with std::stringstream.
* \return A string representation of \a value.
*/
template<class T>
std::string toString(const T& value);
/**
* Converts the string \a str to its original value.
* \param str The string to convert
* \return The back-converted value of \a str, type must be compatible with std::stringstream.
*/
template<class T>
T fromString(const std::string& str);
/**
* Joins the substrings in \a tokens together using \a delimiter in between.
* \param tokens List of substrings to join.
* \param delimiter Delimiter which shall be placed between the substrings.
* \return A string containing the joined substrings.
*/
template<typename T>
static std::string join(const std::vector<T>& tokens, const std::string& delimiter);
};
// - Template definition --------------------------------------------------------------------------
template<class T>
std::string toString(const T& value) {
std::ostringstream stream;
stream << value;
return stream.str();
}
template<class T>
T fromString(const std::string& str) {
T toReturn;
std::istringstream stream;
stream.str(str);
if (!(stream >> toReturn))
LERRORC("TumVis.core.tools.StringUtils::fromString()", "failed to convert string '" + str + "'");
return toReturn;
}
template<typename T>
std::string join(const std::vector<T>& tokens, const std::string& delimiter) {
if (tokens.empty())
return "";
std::stringstream s;
s << tokens[0];
for (std::vector<T>::size_type i = 1; i < tokens.size(); ++i)
s << delimiter << tokens[i];
return s.str();
}
}
#endif // STRINGUTILS_H__
\ No newline at end of file
......@@ -5,19 +5,25 @@
namespace TUMVis {
/**
* Struct for handling void pointers slightly more typesafe.
* Therfore it stores an enum value together with the pointer infering its data type.
**/
struct WeaklyTypedPointer {
/**
* Base data type.
**/
enum PointerType {
UCHAR,
CHAR,
USHORT,
SHORT,
UINT,
INT,
FLOAT,
ULONG,
LONG,
DOUBLE
UCHAR, ///< unsigned char
CHAR, ///< char
USHORT, ///< unsigned short
SHORT, ///< short
UINT, ///< unsigned int
INT, ///< int
FLOAT, ///< float
ULONG, ///< unsigned long
LONG, ///< long
DOUBLE ///< double
};
/**
......@@ -49,20 +55,18 @@ namespace TUMVis {
};
/**
*
*
* \param pt
* \param ptr
**/
* Constructs a new weakly typed pointer.
* \param pt Base data type of the pointer.
* \param ptr Pointer to the data.
*/
WeaklyTypedPointer(PointerType pt, void* ptr)
: _pointerType(pt)
, _pointer(ptr)
{
};
PointerType _pointerType;
void* _pointer;
PointerType _pointerType; ///< Base data type of the pointer
void* _pointer; ///< Pointer to the data
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment