Show StringUtil.cpp syntax highlighted
/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG *
*Copyright (C) 2006, Tony Sparks *
* *
*This library is free software; you can redistribute it and/or *
*modify it under the terms of the GNU Lesser General Public *
*License as published by the Free Software Foundation; either *
*version 2.1 of the License, or (at your option) any later version. *
* *
*This library is distributed in the hope that it will be useful, *
*but WITHOUT ANY WARRANTY; without even the implied warranty of *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
*Lesser General Public License for more details. *
* *
*You should have received a copy of the GNU Lesser General Public *
*License along with this library; if not, write to the Free Software *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
**************************************************************************************
*/
#include "StdAfx.h"
#include "StringUtil.h"
#include <sstream>
StringUtil::StringUtil(void)
{
}
StringUtil::StringUtil(std::string s, char delim)
{
str = s;
// initial spot for substr
int j = 0, i = 0;
int size = 0;
// search through the whole string to
// find tokens
while(i < str.length() && j < str.length() )
{
size = 0;
// eat white space
while( str[j] == delim && j < str.length() ) {
j++;
if ( j < str.length() )
i = j;
}
// if we find a space
while( str[i] != delim && i < str.length() )
{
i++;
size++;
}
if ( j <= i-1 )
// add this token to the stack
tokens.push_back( str.substr(j, size) );
if ( j >= str.length() )
return;
j = i;
}
//tokens.push_back( str.substr( j, i ) );
}
StringUtil::StringUtil(std::string s)
{
str = s;
// initial spot for substr
int j = 0, i = 0;
int size = 0;
// search through the whole string to
// find tokens
while(i < str.length() && j < str.length() )
{
size = 0;
// eat white space
while( str[j] == (char)32 && j < str.length() ) {
j++;
if ( j < str.length() )
i = j;
}
// if we find a space
while( str[i] != (char)32 && i < str.length() )
{
i++;
size++;
}
if ( j <= i-1 )
// add this token to the stack
tokens.push_back( str.substr(j, size) );
if ( j >= str.length() )
return;
j = i;
}
//tokens.push_back( str.substr( j, i ) );
}
StringUtil::~StringUtil(void)
{
}
template<class T>
std::string StringUtil::ToString( T val )
{
std::ostringstream sstream;
sstream << val;
return sstream.str();
}
// helper function
bool IsNumber_c( char c )
{
// a number
char nums[] = { '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', '.'
};
// search the array
for ( int i = 0; i < 11; i++ )
{
// if we have a match
// it is a number
if ( c == nums[i] )
return true;
}
return false;
}
/** Check for a number
Note very fragile - TODO add a better check for decimals
*/
bool StringUtil::IsNumber( std::string &str )
{
for ( int i = 0; i < str.length(); i++ )
{
if ( !IsNumber_c( str[i] ) )
return false;
}
return true;
}
See more files for this project here