Newer
Older
/***************************************************************************
String.h - description
-------------------
begin : 2004/04/10 16:35
copyright : (C) 2004 by Tomas Oberhuber
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
#include <iostream>
#include <sstream>
namespace Containers {
template< class T > class List;
}
template< typename T >
String convertToString( const T& value );
//! Class for managing strings
{
//! Pointer to char ended with zero
char* string;
//! Length of the allocated piece of memory
int length;
//! Basic constructor
Tomáš Oberhuber
committed
//! Constructor from const char*
String( const char* str );
//! Constructor with char pointer
/*! @param prefix_cut_off says length of the prefix that is going to be omitted and
@param sufix_cut_off says the same about sufix.
*/
String( const char* c,
Tomáš Oberhuber
committed
int prefix_cut_off,
//! Copy constructor
String( const String& str );
Tomáš Oberhuber
committed
//////
/// Templated constructor
///
/// It must be explicit otherwise it is called recursively from inside of its
/// definition ( in operator << ). It leads to stack overflow and segmentation fault.
Tomáš Oberhuber
committed
explicit
String( T value )
: string( nullptr ), length( 0 )
{
Tomáš Oberhuber
committed
std::stringstream str;
str << value;
setString( str.str().data() );
Tomáš Oberhuber
committed
static String getType();
//! Return length of the string
int getLength() const;
int getSize() const;
//! Return currently allocated size
int getAllocatedSize() const;
Tomáš Oberhuber
committed
//! Reserve space for given number of characters
void setSize( int size );
//! Set string from given char pointer
/*! @param prefix_cut_off says length of the prefix that is going to be omitted and
@param sufix_cut_off says the same about sufix.
*/
Tomáš Oberhuber
committed
void setString( const char* c,
int prefix_cut_off = 0,
int sufix_cut_off = 0 );
Tomáš Oberhuber
committed
//! Return pointer to data
const char* getString() const;
//! Return pointer to data
char* getString();
//! Operator for accesing particular chars of the string
const char& operator[]( int i ) const;
//! Operator for accesing particular chars of the string
char& operator[]( int i );
String& operator=( const char* str );
String& operator+=( const char* str );
String operator+( const char* str ) const;
bool operator==( const char* str ) const;
bool operator!=( const char* str ) const;
/****
* Operators for Strings
*/
String& operator=( const String& str );
String& operator+=( const String& str );
String operator+( const String& str ) const;
bool operator==( const String& str ) const;
bool operator!=( const String& str ) const;
/****
* Operators for single characters
*/
String& operator=( char str );
String& operator+=( char str );
String operator+( char str ) const;
bool operator==( char str ) const;
bool operator!=( char str ) const;
//! Cast to bool with negation operator
String replace( const String& pattern,
const String& replaceWith,
int count = 0 ) const;
String strip( char strip = ' ' ) const;
//! Split the string into list of strings w.r.t. given separator.
int split( Containers::List< String >& list, const char separator = ' ' ) const;
//! Write to a binary file
bool save( File& file ) const;
//! Read from binary file
bool load( File& file );
//! Broadcast to other nodes in MPI cluster
// void MPIBcast( int root, MPI_Comm mpi_comm = MPI_COMM_WORLD );
Tomáš Oberhuber
committed
//! Read one line from given stream.
Tomáš Oberhuber
committed
friend std::ostream& operator<<( std::ostream& stream, const String& str );
String operator+( char string1, const String& string2 );
String operator+( const char* string1, const String& string2 );
std::ostream& operator<<( std::ostream& stream, const String& str );
template< typename T >
String convertToString( const T& value )
{
std::stringstream str;
str << value;
return String( str.str().data() );
template<> inline String convertToString( const bool& b )
{
if( b ) return "true";
return "false";
}