src/tools/tString.cpp File Reference

#include "tMemManager.h"
#include "tString.h"
#include "tLocale.h"
#include "tConfiguration.h"
#include "tException.h"
#include <ctype.h>
#include <string>
#include <iostream>

Include dependency graph for tString.cpp:

Go to the source code of this file.

Classes

class  tCharacterFilter

Functions

static bool st_ReadEscapeSequence (char &c, char &c2, std::istream &s)
static bool st_ReadEscapeSequence (char &c, std::istream &s)
std::istream & operator>> (std::istream &s, tString &x)
 stream reading operator honoring escape sequences and quoting
template<class T>
void ToT (tString const &source, T &target, tString::size_type pos, char const *name)
static int GetInt (const tString &s, int &pos)
static int RemoveTrailingColor (tString &s, int maxLen=-1)
static int RTC (REAL x)
static char int_to_hex (int i)
tColoredStringoperator<< (tColoredString &s, const tColoredStringProxy &colorCode)
 string building streaming operator
std::stringstream & operator<< (std::stringstream &s, const tString &t)
bool tIsInList (tString const &list_, tString const &item)
 check whether item is in a comma or whitespace separated list
void tToLower (tString &toTransform)
 converts a string to lowercase
void tToUpper (tString &toTransform)
 converts a string to uppercase

Variables

static tString delimiters ("!?.:;_()-, ")
static tConfItemLine st_wordDelimiters ("WORD_DELIMITERS", delimiters)
bool st_colorStrings = true
static tConfItem< bool > cs ("COLOR_STRINGS", st_colorStrings)
static char hex_array [] = "0123456789abcdef"


Function Documentation

static int GetInt ( const tString s,
int &  pos 
) [static]

Definition at line 833 of file tString.cpp.

References tString::Len().

Referenced by tString::CompareAlphaNumerical().

00834 {
00835     int ret = 0;
00836     int digit = 0;
00837     while ( pos < s.Len() && digit >= 0 && digit <= 9 )
00838     {
00839         ret = ret*10 + digit;
00840         digit = s[pos] - '0';
00841         pos++;
00842     }
00843 
00844     if ( ret > 0 )
00845     {
00846         return ret + 0x10000;
00847     }
00848     else
00849     {
00850         return digit + '0';
00851     }
00852 }

Here is the call graph for this function:

Here is the caller graph for this function:

static char int_to_hex ( int  i  )  [static]

Definition at line 1675 of file tString.cpp.

01675                              {
01676     if (i<0 || i >15)
01677         return 'Q';
01678     else
01679         return hex_array[i];
01680 }

std::stringstream& operator<< ( std::stringstream &  s,
const tString t 
)

Definition at line 1852 of file tString.cpp.

01853 {
01854     static_cast<std::ostream&>(s) << static_cast<const char *>(t);
01855     return s;
01856 }

tColoredString& operator<< ( tColoredString s,
const tColoredStringProxy colorCode 
)

string building streaming operator

Appends a color code to a colored string

Parameters:
s the string to append to
colorCode the colorcode to append

Definition at line 1694 of file tString.cpp.

References tColoredStringProxy::b_, cs, tColoredStringProxy::g_, int_to_hex(), tColoredStringProxy::r_, and RTC().

01695 {
01696     if (st_colorStrings)
01697     {
01698         char cs[9];
01699         cs[0]='0';
01700         cs[1]='x';
01701 
01702         int RGB[3];
01703         RGB[0]=RTC(colorCode.r_);
01704         RGB[1]=RTC(colorCode.g_);
01705         RGB[2]=RTC(colorCode.b_);
01706 
01707         for(int i=0;i<3;i++){
01708             int lp=RGB[i]%16;
01709             int hp=(RGB[i]-lp)/16;
01710             cs[2+2*i]=int_to_hex(hp);
01711             cs[2+2*i+1]=int_to_hex(lp);
01712         }
01713         cs[8]=0;
01714 
01715         s << cs;
01716     }
01717 
01718     return s;
01719 }

Here is the call graph for this function:

std::istream& operator>> ( std::istream &  s,
tString x 
)

stream reading operator honoring escape sequences and quoting

Todo:
write symmetric stream writing operator
Parameters:
s stream to read from
x string to read to
Returns:
reference to stream for chaining

Definition at line 111 of file tString.cpp.

References c, tString::Clear(), isblank(), and st_ReadEscapeSequence().

00112 {
00113     x.Clear();
00114 
00115     std::ws(s);
00116 
00117     char c=s.get();
00118 
00119     // check if the string is quoted
00120     bool quoted = false;
00121     char quoteChar = c;   // if it applies, this is the quoting character
00122     if ( c == '"' || c == '\'' )
00123     {
00124         // yes, it is
00125         quoted = true;
00126         c = s.get();
00127     }
00128 
00129     while((quoted || !( isblank(c) || c == '\n' || c == '\r' ) ) && s.good() && !s.eof()){
00130         x += c;
00131         c=s.get();
00132 
00133         // read and interpret escape sequences
00134         if ( !st_ReadEscapeSequence( c, s) )
00135         {
00136             // interpret special characters
00137             if ( quoted && c == quoteChar )
00138             {
00139                 // this marks the end of a quoted string; abort.
00140                 c = s.get();
00141                 break;
00142             }
00143         }
00144         else if ( isblank( c ) )
00145         {
00146             // include escaped spaces
00147             x += c;
00148             c=s.get();
00149         }
00150 
00151     }
00152     s.putback(c);
00153     return s;
00154 }

Here is the call graph for this function:

static int RemoveTrailingColor ( tString s,
int  maxLen = -1 
) [static]

Definition at line 1562 of file tString.cpp.

References tString::SetSize(), and tString::Size().

01563 {
01564     // count bytes lost to color codes
01565     int posDisplacement = 0;
01566     int len = 0;
01567 
01568     // walk through string
01569     for ( size_t g=0; g+1 < s.Size(); g++)
01570     {
01571         if (s(g) == '0' && s(g+1) == 'x')
01572         {
01573             // test if the code is legal ( not so far at the end that it overlaps )
01574             if ( s.Size() >= g + 8 )
01575             {
01576                 // everything is in order, record color code usage and advance
01577                 posDisplacement+=8;
01578                 g+=7;
01579             }
01580             else
01581             {
01582                 // illegal code! Remove it.
01583                 s.SetSize( g );
01584                 // s[g]=0;
01585             }
01586         }
01587         else if ( maxLen > 0 )
01588         {
01589             if ( ++len >= maxLen )
01590             {
01591                 // maximal end reached, cut it off
01592                 s.SetSize( g-1 );
01593                 // s[g]=0;
01594             }
01595         }
01596     }
01597 
01598     return posDisplacement;
01599 }

Here is the call graph for this function:

static int RTC ( REAL  x  )  [static]

Definition at line 1664 of file tString.cpp.

Referenced by operator<<().

01664                       {
01665     int ret=int(x*255);
01666     if (ret<0)
01667         ret=0;
01668     if (ret>255)
01669         ret=255;
01670     return ret;
01671 }

Here is the caller graph for this function:

static bool st_ReadEscapeSequence ( char &  c,
std::istream &  s 
) [static]

Definition at line 89 of file tString.cpp.

References st_ReadEscapeSequence().

00090 {
00091     char c2 = '\0';
00092     bool ret = st_ReadEscapeSequence( c, c2, s );
00093     if ( c2 )
00094         s.putback( c2 );
00095     return ret;
00096 }

Here is the call graph for this function:

static bool st_ReadEscapeSequence ( char &  c,
char &  c2,
std::istream &  s 
) [static]

Definition at line 47 of file tString.cpp.

Referenced by operator>>(), tString::ReadLine(), and st_ReadEscapeSequence().

00048 {
00049     c2 = '\0';
00050 
00051     // detect escaping
00052     if ( c == '\\' && !s.eof() && s.good() )
00053     {
00054         c2 = s.get();
00055 
00056         // nothing useful read?
00057         if ( s.eof() )
00058         {
00059             c2 = 0;
00060             return false;
00061         }
00062 
00063         // interpret special escape sequences
00064         switch (c2)
00065         {
00066         case 'n':
00067             // turn \n into newline
00068             c = '\n';
00069             c2 = 0;
00070             return true;
00071         case '"':
00072         case ' ':
00073         case '\'':
00074         case '\n':
00075             // include quoting character as literal
00076             return true;
00077         default:
00078             // take the whole \x sequence as it appeared.
00079             return false;
00080         }
00081     }
00082 
00083     return false;
00084 }

Here is the caller graph for this function:

bool tIsInList ( tString const &  list_,
tString const &  item 
)

check whether item is in a comma or whitespace separated list

Parameters:
list The string representation of the list
item The item to look for
Returns:
true if the list contains the item
Example: tIsInList( "ab, cd", "ab" ) -> true

Definition at line 2166 of file tString.cpp.

References isblank(), tString::Len(), pos, tString::StrPos(), and tString::SubStr().

Referenced by sn_BothHave(), and sn_GetSupportedMethods().

02167 {
02168     tString list = list_;
02169 
02170     while( list != "" )
02171     {
02172         // find the item
02173         int pos = list.StrPos( item );
02174 
02175         // no traditional match? shoot.
02176         if ( pos < 0 )
02177         {
02178             return false;
02179         }
02180 
02181         // check whether the match is a true list match
02182         if ( 
02183             ( pos == 0 || list[pos-1] == ',' || isblank(list[pos-1]) )
02184             &&
02185             ( pos + item.Len() >= list.Len() || list[pos+item.Len()-1] == ',' || isblank(list[pos+item.Len()-1]) )
02186             )
02187         {
02188             return true;
02189         }
02190         else
02191         {
02192             // no? truncate the list and go on.
02193             list = list.SubStr( pos + 1 );
02194         }
02195     }
02196     
02197     return false;
02198 }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T>
void ToT ( tString const &  source,
T &  target,
tString::size_type  pos,
char const *  name 
) [inline]

Definition at line 775 of file tString.cpp.

References tString::Convert(), and tString::Size().

Referenced by tString::ToFloat(), and tString::ToInt().

00776 {
00777     if ( pos > source.Size() )
00778         pos = source.Size();
00779 
00780     if ( !source.Convert( target, pos ) )
00781     {
00782         std::ostringstream message;
00783 
00784         if ( pos > 0 )
00785         {
00786             message << "Expected an " << name << " at position " << pos << " of string \"" << source << "\", but got \"" << static_cast<tString::CHAR const *>(source) + pos << "\".";
00787         }
00788         else
00789         {
00790             message << "Expected an " << name << ", but got \"" << source << "\".";
00791         }
00792         throw tGenericException( message.str().c_str(), "Conversion Error" );
00793     }
00794 }

Here is the call graph for this function:

Here is the caller graph for this function:

void tToLower ( tString toTransform  ) 

converts a string to lowercase

Parameters:
toTransform The string to transform

Definition at line 2209 of file tString.cpp.

References tString::Len().

Referenced by eVoter::HandleChat(), nKrawall::nMethod::nMethod(), tConfItemBase::tConfItemBase(), and uAction::uAction().

02210 {
02211     for( int i = toTransform.Len()-2; i >= 0; --i )
02212     {
02213         toTransform[i] = tolower( toTransform[i] );
02214     }
02215 }

Here is the call graph for this function:

Here is the caller graph for this function:

void tToUpper ( tString toTransform  ) 

converts a string to uppercase

Parameters:
toTransform The string to transform

Definition at line 2226 of file tString.cpp.

References tString::Len().

Referenced by tConfItemBase::LoadLine(), and s_Veto().

02227 {
02228     for( int i = toTransform.Len()-2; i >= 0; --i )
02229     {
02230         toTransform[i] = toupper( toTransform[i] );
02231     }
02232 }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

tConfItem<bool> cs("COLOR_STRINGS", st_colorStrings) [static]

tString delimiters("!?.:;_()-, ") [static]

Referenced by tString::PosWordRight().

char hex_array[] = "0123456789abcdef" [static]

Definition at line 1673 of file tString.cpp.

bool st_colorStrings = true

Definition at line 1660 of file tString.cpp.

tConfItemLine st_wordDelimiters("WORD_DELIMITERS", delimiters) [static]


Generated on Sat Mar 15 23:12:44 2008 for Armagetron Advanced by  doxygen 1.5.4