Skip to content
Snippets Groups Projects
tnlString.h 4.17 KiB
Newer Older
  • Learn to ignore specific revisions
  • /***************************************************************************
    
                              tnlString.h  -  description
    
                                 -------------------
        begin                : 2004/04/10 16:35
        copyright            : (C) 2004 by Tomas Oberhuber
    
        email                : tomas.oberhuber@fjfi.cvut.cz
    
     ***************************************************************************/
    
    
    /* See Copyright Notice in tnl/Copyright */
    
    #pragma once
    
    
    #include <stdio.h>
    #include <iostream>
    
    namespace TNL {
    
    
    //! Class for managing strings
    
    class tnlString
    
    {
       //! Pointer to char ended with zero
       char* string;
    
       //! Length of the allocated piece of memory
       int length;
    
       public:
    
       //! Basic constructor
    
       tnlString();
    
    
       //! 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.
        */
    
       tnlString( const char* c,
    
       tnlString( const tnlString& str );
    
       tnlString( long int number );
    
       tnlString( float number );
    
       tnlString( double number );
    
       ~tnlString();
    
    
       //! 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.
        */
    
                         int prefix_cut_off = 0,
                         int sufix_cut_off = 0 );
    
       //! 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 );
    
    
       /****
        * TODO: the operators do not work properly
        * for example tnlString + const char*
        */
    
    
       tnlString& operator = ( const tnlString& str );
    
       tnlString& operator += ( const char* str );
    
       //! Operator +=
       tnlString& operator += ( const char str );
    
    
       tnlString& operator += ( const tnlString& str );
    
       tnlString operator + ( const tnlString& str ) const;
    
       tnlString operator + ( const char* str ) const;
    
       //! Comparison operator
    
       bool operator == ( const tnlString& str ) const;
    
       //! Comparison operator
    
       bool operator != ( const tnlString& str ) const;
    
    
       //! Comparison operator
       bool operator == ( const char* ) const;
    
       //! Comparison operator
       bool operator != ( const char* ) const;
    
       //! Retyping operator
       operator bool () const;
    
       //! Return length of the string
    
       void replace( const tnlString& pattern,
                     const tnlString& replaceWith );
    
    
       // TODO: remove
       //! Write to a binary file
       bool save( ostream& file ) const;
    
       // TODO: remove
       //! Read from binary file
       bool load( istream& file );
    
    
       //! Broadcast to other nodes in MPI cluster
       void MPIBcast( int root, MPI_Comm mpi_comm = MPI_COMM_WORLD );
    
    
       //! Read one line from given stream.
       bool getLine( istream& stream );
    
       //! Parse the string into list of strings w.r.t. given separator.
       int parse( tnlList< tnlString >& list, const char separator = ' ' ) const;
    
    
       friend ostream& operator << ( ostream& stream, const tnlString& str );
    
    tnlString operator + ( const char* string1, const tnlString& string2 );
    
    
    ostream& operator << ( ostream& stream, const tnlString& str );
    
    template< typename T >
    tnlString convertToString( const T& value )
    {
       std::stringstream str;
       str << value;
       return tnlString( str.str().data() );
    };
    
    template<> inline tnlString convertToString( const bool& b )
    {
       if( b ) return "true";
       return "false";
    }
    
    
    } // namespace TNL