//////////////////////////////////////
// START of File: C0_DYSTR.CPP
//////////////////////////////////////

//===================================================================
//
// COPYRIGHT NOTICE:  This code is protected by Copyright!
//
// Copyright (c) 1994, 1995, 1996, 1997 by DPC Technology.
// All rights reserved.
//
// See files "cf_copyr.h" and "cf_licen.h" for details of
// copyright and license specifications, and contact information.
//
//===================================================================

#include	<cf_api/bf_defs.h>
#include    <cf_api/c0_dystr.hpp>

//===================================================================
// Implement the (Base-Framework INDEPENDENT) Dynamic String Object:
//===================================================================
cf_String
    ::cf_String()
{
    // Content initialized as empty string
    iCurrStrLen_ = 0;
    iCurrBuffSize_ = 1;
    p_cContent_ = new char[ 1 ];    // Faster to use const 1 .vs. iBuffSize
    *p_cContent_ = 0;               // Avoids any overhead of a ::strcpy() call
}

cf_String
    ::cf_String( cf_String &cr_cfsSource )
{
    // Content-copy the source cf_String
    iCurrStrLen_ = cr_cfsSource.iCurrStrLen_;
    iCurrBuffSize_ = cr_cfsSource.iCurrBuffSize_;
    p_cContent_ = new char[ iCurrBuffSize_ ];
    ::strcpy( p_cContent_, (char*) cr_cfsSource );
}


cf_String
    ::cf_String( const char *p_cSource )
{
    // Content is copy of source string
    iCurrStrLen_ = ::strlen( p_cSource );
    iCurrBuffSize_ = iCurrStrLen_ + 1;
    p_cContent_ = new char[ iCurrBuffSize_ ];
    ::strcpy( p_cContent_, p_cSource );
}


cf_String
    ::cf_String(    const double    dUserVal,
                    const char*     ca_cFormatString,
                    const int       iBufferSize )
{
    // Content is string conversion of user's numerical param
    iCurrBuffSize_ = iBufferSize;
    p_cContent_ = new char[ iCurrBuffSize_ ];
    ::sprintf( p_cContent_, ca_cFormatString, dUserVal );

    // Calc the string-length NOW, so all we have to do whenever
    // cf_strlen() gets called (MANY) times  is just REPORT it back!
    iCurrStrLen_ = ::strlen( p_cContent_ );
}

cf_String
    ::cf_String(    const int       iUserVal,
                    const char*     ca_cFormatString,
                    const int       iBufferSize )
{
    // Content is string conversion of user's numerical param
    iCurrBuffSize_ = iBufferSize;
    p_cContent_ = new char[ iCurrBuffSize_ ];
    ::sprintf( p_cContent_, ca_cFormatString, iUserVal );

    // Calc the string-length NOW, so all we have to do whenever
    // cf_strlen() gets called (MANY) times  is just REPORT it back!
    iCurrStrLen_ = ::strlen( p_cContent_ );
}


cf_String
    ::~cf_String()
        { delete [] p_cContent_; }


cf_String
    ::operator char*()
        const
        { return p_cContent_; }


void
    cf_String
        ::operator=( const cf_String &cr_cfsSource )
{
    iCurrStrLen_ = cr_cfsSource.iCurrStrLen_;
    iCurrBuffSize_ = cr_cfsSource.iCurrBuffSize_;
    char *p_cTextBuff = new char[ iCurrBuffSize_ ];
    ::strcpy( p_cTextBuff, ( cr_cfsSource.p_cContent_ ) );
    delete [] p_cContent_;
    p_cContent_ = p_cTextBuff;
}


void
    cf_String
        ::operator=( const char *a_cSource )
{
    // Copy the string parameter to our current content
    iCurrStrLen_ = ::strlen( a_cSource );
    iCurrBuffSize_ = iCurrStrLen_ + 1;
    char *p_cTextBuff = new char[ iCurrBuffSize_ ];
    ::strcpy( p_cTextBuff, a_cSource );
    delete [] p_cContent_;
    p_cContent_ = p_cTextBuff;
}


void
    cf_String
        ::operator&=( const char* a_cCString )
{
    // Append the user string parameter to our content string:
    iCurrStrLen_ += ::strlen( a_cCString );
    iCurrBuffSize_ += iCurrStrLen_;
            // NB: Not +1 because we've already GOT the null-pad-char!
    char *p_cTextBuff = new char[ iCurrBuffSize_ ];
    ::strcpy( p_cTextBuff, p_cContent_ );
    strcat( p_cTextBuff, a_cCString );
    delete [] p_cContent_;
    p_cContent_ = p_cTextBuff;
}

void
    cf_String
        ::cf_append(  const double  dUserVal,
                      const char*   ca_cFormatString,
                      const int     iBufferSize )
{
    // Content is string conversion of user's numerical param
    delete [] p_cContent_;
    iCurrBuffSize_ = iBufferSize;
    p_cContent_ = new char[ iCurrBuffSize_ ];
    ::sprintf( p_cContent_, ca_cFormatString, dUserVal );

    // Calc the string-length NOW, so all we have to do whenever
    // cf_strlen() gets called (MANY) times  is just REPORT it back!
    iCurrStrLen_ = ::strlen( p_cContent_ );
}

void
    cf_String
        ::cf_append(  const int     iUserVal,
                      const char*   ca_cFormatString,
                      const int     iBufferSize )
{
    // Content is string conversion of user's numerical param
    delete [] p_cContent_;
    iCurrBuffSize_ = iBufferSize;
    p_cContent_ = new char[ iCurrBuffSize_ ];
    ::sprintf( p_cContent_, ca_cFormatString, iUserVal );

    // Calc the string-length NOW, so all we have to do whenever
    // cf_strlen() gets called (MANY) times  is just REPORT it back!
    iCurrStrLen_ = ::strlen( p_cContent_ );
}

int
    cf_String
        ::cf_strlen()
            const
{ return( iCurrStrLen_ ); }


void
    cf_String
        ::reset()
{
    delete [] p_cContent_;
    iCurrStrLen_ = 0;
    iCurrBuffSize_ = 1;
    p_cContent_ = new char[ 1 ];    // faster to use const 1 .vs. iBuffSize
    *p_cContent_ = 0;               // avoid any overhead of a ::strcpy() call
}

//
// End of cf_String Implementation.
//

//////////////////////////////////////
// END of File: C0_DYSTR.CPP
//////////////////////////////////////

