Skip to content
Snippets Groups Projects
String.h 4.09 KiB
Newer Older
  • Learn to ignore specific revisions
  • /***************************************************************************
    
                              String.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>
    
    #include <TNL/mpi-supp.h>
    
    namespace TNL {
    
    template< class T > class List;
    class File;
    
    
    //! Class for managing strings
    
    {
       //! Pointer to char ended with zero
       char* string;
    
       //! Length of the allocated piece of memory
       int length;
    
       public:
    
       //! Basic constructor
    
    
       //! 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.
        */
    
       static String getType();
    
       String( const String& str );
    
       String( unsigned number );
    
       String( long int number );
    
       String( float number );
    
       String( double number );
    
    
       //! 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 String + const char*
    
       String& operator = ( const String& str );
    
       String& operator += ( const char* str );
    
       String& operator += ( const char str );
    
       String& operator += ( const String& str );
    
       String operator + ( const String& str ) const;
    
       String operator + ( const char* str ) const;
    
       //! Comparison operator
    
       bool operator == ( const String& str ) const;
    
       //! Comparison operator
    
       bool operator != ( const String& 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 String& pattern,
                     const String& replaceWith );
    
       bool save( std::ostream& file ) const;
    
       bool load( std::istream& file );
    
       bool save( File& file ) const;
    
       bool load( File& file );
    
    
       //! Broadcast to other nodes in MPI cluster
       void MPIBcast( int root, MPI_Comm mpi_comm = MPI_COMM_WORLD );
    
    
       bool getLine( std::istream& stream );
    
    
       //! Parse the string into list of strings w.r.t. given separator.
    
       int parse( List< String >& list, const char separator = ' ' ) const;
    
       friend std::ostream& operator << ( std::ostream& stream, const String& str );
    
    String operator + ( const char* string1, const String& string2 );
    
    std::ostream& operator << ( std::ostream& stream, const String& str );
    
    String convertToString( const T& value )
    
    {
       std::stringstream str;
       str << value;
    
       return String( str.str().data() );
    
    template<> inline String convertToString( const bool& b )
    
    } // namespace TNL