Click here to Skip to main content
15,893,161 members
Articles / Containers / Virtual Machine

TOOL

Rate me:
Please Sign up or sign in to vote.
4.98/5 (52 votes)
23 Oct 200676 min read 231K   5.4K   147  
TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language interpreter. The purpose of this article is to introduce the TOOL interpreter and language from the perspective of a person who has a desire to include a scripting solution as part of his project.
#ifndef FAST_STRING_H_INCLUDED
#define FAST_STRING_H_INCLUDED
/*****************************************************************************/
/*                              HEADER FILE                                  */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

    Description:   Declaration of a string class that does not require MFC
                   included

                   All methods marked with throw() will throw std::bad_alloc
                  if memory allocation is needed and it fails

                   every allocation (including the first one) adds an amount
                   of extra TCHARs to internal buffer  so the buffer will not
                   be resized every time a small operation is performed;

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
/*****************************************************************************/

#include <windows.h>
#include <tchar.h>

class CFastString
{
public:
	CFastString& Fill(const TCHAR chr, int iStart, int iSize);
  CFastString& CFastString::AppendChar(const TCHAR chFillChar, int iStart, int iSize);
  
  static int  BUFFER_EXTRA_SIZE;
  
  CFastString( void ) throw();
  CFastString( const CFastString& roOther ) throw();
  CFastString( const char* pchValue ) throw();
  CFastString( int iAllocationSize ) throw();

  ~CFastString( void );

  CFastString& operator = ( const CFastString& roOther ) throw();
  CFastString& operator = ( const char* pchValue )       throw();
  CFastString& operator = ( int iValue )                 throw();
  CFastString& operator = ( long lValue )                throw();
  CFastString& operator = ( unsigned long ulValue )      throw();
  CFastString& operator = ( double dblValue )            throw();

  bool operator <  ( const CFastString& roTest );
  bool operator <= ( const CFastString& roTest );       
  bool operator >  ( const CFastString& roTest );
  bool operator >= ( const CFastString& roTest );       
  bool operator == ( const CFastString& roTest );       
  bool operator != ( const CFastString& roTest );
      
  TCHAR*  GetString( void ) const { return( Buffer() ); };
  TCHAR*  Buffer( void ) const;
  TCHAR*  Buffer( void );
  TCHAR*  GetBuffer( int idx );
  TCHAR*  GetBufferSetLength( int idx );
  void    ReleaseBuffer( int idx );
  void    ReleaseBuffer( void );
  int     BufferSize() const;

  void  AllocBuffer( int iSize ) throw();

  void    Realloc( int size )     throw();
  void    Compact( void )         throw();
  void    Compact( int iNewSize ) throw();

  int     GetLength( void ) const { return( Length() ); };
  int     Length( void ) const;
  BOOL    IsEmpty( void ) const;

  BOOL    operator == ( const char* pchCompare )    const;
  BOOL    operator != ( const char*& pchCompare )   const;
  int     Compare( const char* pchCompare )         const;
  int     CompareNoCase( const char* pchCompare )   const;

  operator const char*   ( void ) const;
  operator int           ( void ) const;
  operator long          ( void ) const;
  operator unsigned long ( void ) const;
  operator double        ( void ) const;

  TCHAR&       operator [] ( int iIndex );
  const TCHAR& operator [] ( int iIndex ) const;
  
  CFastString  Left( int iSize )  const throw();
  CFastString  Right( int iSize ) const throw();
  CFastString  Mid( int iStart )  const throw();
  CFastString  Mid( int iStart, int iSize ) const throw();
  void         Right( int iCount, CFastString& roOutput );
  void         Mid( int iOffset, int iCount, CFastString& roOutput );
  
  int FindCount( const char* pchToFind ) const; 
  int FindCount( int iStartAt, const char* pchToFind ) const; 
  int Find( const char* pchToFind ) const;
  int Find( const char* pchToFind, int iStartAt ) const;
  int Find( const char  chToFind ) const;

  int FindNthOf( int iOccurance, const char* pchToFind ) const;
  int ReverseFind( const char* pchToFind ) const;
  int ReverseFindNthOf( int iOccrance, const char* pchToFind ) const;

  CFastString&  Empty( void );
  CFastString&  Fill( const TCHAR chr );
  CFastString&  Trim( void );
  CFastString&  TrimLeft( void );
  CFastString&  TrimRight( void );
  CFastString&  Lower( void );
  CFastString&  Upper( void );
  CFastString&  TrimQuotes( void );

  CFastString&  Insert( int iAtIndex, const char* pchToInsert ) throw();
  CFastString&  Prepend( const char* pchToPrepend )             throw();
  CFastString&  Append( const char* pchToAppend )               throw();
  CFastString&  Remove( int iAtIndex, int iLength )             throw();
  CFastString&  Truncate( int iPosition ) ;

  CFastString&  Replace( const char* pchToReplace, const char* pchNew )               throw();
  CFastString&  Replace( int iStartAt, const char* pchToReplace, const char* pchNew ) throw();
  CFastString&  Format( const char* pchFormat, ... )                                  throw();
  CFastString&  FormatV( const char* pchFormat, va_list args )                        throw();
  
  int            GetInt( void )   const;
  long           GetLong( void )   const;
  unsigned long  GetULong( void )  const;
  double         GetDouble( void ) const;

  CFastString&  SetInt( const int iValue )              throw();
  CFastString&  SetLong( const long lValue )            throw();
  CFastString&  SetULong( const unsigned long ulValue ) throw();
  CFastString&  SetDouble( const double dblValue )      throw();

  friend CFastString&  operator << ( CFastString& roOther, const char* pszStr )    throw();
  friend CFastString&  operator << ( CFastString& roOther, int iValue )            throw();
  friend CFastString&  operator << ( CFastString& roOther, long lValue )           throw();
  friend CFastString&  operator << ( CFastString& roOther, unsigned long ulValue ) throw();
  friend CFastString&  operator << ( CFastString& roOther, double dblValue )       throw();

  void operator +=( const CFastString& roToAdd );
  void operator +=( const char* pchToAdd );
  void operator +=( const char chToAdd );

private:
  static int    FORMAT_EXTRA_SIZE;
  static int    INT_SIZE;
  static int    LONG_SIZE;
  static int    ULONG_SIZE;

  void  FreeBuffer( void );
  void  Initialize( int iAllocSize = 1 ) throw();
  int   GetPaddedSize( int iSize );
  void  UpdateBufferSize( const int iSize ) throw();
  void  CopyToBuffer( const char* source, int iSize );
  int   TLEN( int iLength );

  TCHAR* m_pchBuffer;
  int    m_iSize;
  int    m_iLength;
};

#endif


/*****************************************************************************/
/* Check-in history */
/*
 *$Log:  $
*/
/*****************************************************************************/

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions