Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

stringutils.cc

Go to the documentation of this file.
00001 /*
00002 * stringutils.cc -- C++ STL string functions
00003 * Copyright (C) 2003-2007 Dan Dennedy <dan@dennedy.org>
00004 *
00005 * This program is free software; you can redistribute it and/or modify
00006 * it under the terms of the GNU General Public License as published by
00007 * the Free Software Foundation; either version 2 of the License, or
00008 * (at your option) any later version.
00009 *
00010 * This program is distributed in the hope that it will be useful,
00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 * GNU General Public License for more details.
00014 *
00015 * You should have received a copy of the GNU General Public License
00016 * along with this program; if not, write to the Free Software Foundation,
00017 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 *
00019 */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024 
00025 #include <sstream>
00026 using std::ostringstream;
00027 #include <stdio.h>
00028 
00029 #include "stringutils.h"
00030 
00031 string StringUtils::replaceAll ( string haystack, string needle, string s )
00032 {
00033     string::size_type pos = 0;
00034     while ( ( pos = haystack.find ( needle, pos ) ) != string::npos )
00035     {
00036         haystack.erase ( pos, needle.length() );
00037         haystack.insert ( pos, s );
00038         pos += s.length();
00039     }
00040     return haystack;
00041 }
00042 
00043 string StringUtils::stripWhite ( string s )
00044 {
00045     ostringstream dest;
00046     char c;
00047     for ( string::size_type pos = 0; pos < s.size(); ++pos )
00048     {
00049         c = s.at( pos );
00050         if ( c != 0x20 && c != 0x09 && c != 0x0d && c != 0x0a )
00051             dest << c;
00052     }
00053     return dest.str();
00054 }
00055 
00056 
00057 bool StringUtils::begins( string source, string sub )
00058 {
00059     return ( source.substr ( 0, sub.length() ) == sub );
00060 }
00061 
00062 bool StringUtils::ends( string source, string sub )
00063 {
00064     if ( sub.length() >= source.length() )
00065         return false;
00066     return ( source.substr( source.length() - sub.length() ) == sub );
00067 }
00068 
00069 string StringUtils::ltos( long num )
00070 {
00071     char s[ 81 ];
00072 
00073     sprintf ( s, "%ld", num );
00074     return string( s );
00075 }
00076 
00077 string StringUtils::itos( int num )
00078 {
00079     char s[ 81 ];
00080 
00081     sprintf ( s, "%d", num );
00082     return string( s );
00083 }
00084 
00090 int StringUtils::split( const string &input, const string &delimiter, vector< string > &items, const bool clean )
00091 {
00092     int delimiter_size = delimiter.size();
00093     int input_size = input.size();
00094 
00095     // Find the first delimiter
00096     int position = 0;
00097     int end_position = input.find( delimiter, 0 );
00098 
00099     // While we have a valid position
00100     while ( end_position >= position )
00101     {
00102         // Obtain the substr and push if valid
00103         string s = input.substr( position, end_position - position );
00104         if ( !clean || s.size() > 0 )
00105             items.push_back( s );
00106 
00107         // Find the next delimiter
00108         position = end_position + delimiter_size;
00109         end_position = input.find( delimiter, position );
00110     }
00111 
00112     // Obtain the substr of what's left and push if valid
00113     string s = input.substr( position, input_size - position );
00114     if ( !clean || s.size() > 0 )
00115         items.push_back( s );
00116 
00117     // Return the number of items found
00118     return items.size();
00119 }
00120 
00126 string StringUtils::join( vector< string >&items, const string &delimiter )
00127 {
00128     string output( "" );
00129 
00130     // Loop through the items
00131     for ( vector< string >::iterator item = items.begin( ); item != items.end( ); item ++ )
00132     {
00133         if ( item == items.begin() )
00134             output += *item;
00135         else
00136             output += delimiter + *item;
00137     }
00138 
00139     return string( output );
00140 }

Generated on Sun Mar 11 22:11:47 2007 for Kino by  doxygen 1.4.2