CFX_StringData getbuffer

// 你必须定义一个 `main()` 函数入口。
#include <iostream>
using namespace std;
#include <string.h>
typedef const char* FX_LPCSTR;
typedef char FX_CHAR;
typedef int FX_STRSIZE;
typedef int FX_BOOL;
typedef unsigned char FX_BYTE;
typedef char * FX_LPSTR;
#define FX_Alloc(type, size)                        (type*)calloc(size, sizeof(type))
#define FX_Free(ptr)                                free(ptr)
struct CFX_StringData {
    long        m_nRefs;
    FX_STRSIZE    m_nDataLength;
    FX_STRSIZE    m_nAllocLength;
    FX_CHAR        m_String[1];
};
static CFX_StringData* FX_AllocString(int nLen)
{
    // |nLen| is currently declared as in |int|. TODO(palmer): It should be
    // a |size_t|, or at least unsigned.
 if (nLen == 0 || nLen < 0) {
        return NULL;
    }
    int nSize = nLen;
    nSize += sizeof(long) * 3 + 1;
    CFX_StringData* pData = (CFX_StringData*)FX_Alloc(FX_BYTE, nSize);
    if (!pData) {
        return NULL;
    }
    pData->m_nAllocLength = nLen;
    pData->m_nDataLength = nLen;
    pData->m_nRefs = 1;
    pData->m_String[nLen] = 0;
    return pData;
}
 static void FX_ReleaseString(CFX_StringData* pData)
{
    if (pData == NULL) {
        return;
    }
    pData->m_nRefs --;
    if (pData->m_nRefs <= 0) {
        FX_Free(pData);
    }
}




class CFX_Object
{
  public:
  void* operator new(size_t size, FX_LPCSTR file, int line)
  {
    return malloc(size);
  }
   
  void operator delete(void*p,FX_LPCSTR file,int size)
  {
    free(p);
  }
  void* operator new(size_t size)
  {
    return malloc(size);
  }
  void operator delete(void* p)
  {
    free(p);
  }
    void*            operator new[] (size_t size, FX_LPCSTR file, int line)
    {
        return malloc(size);
    }
    void            operator delete[] (void* p, FX_LPCSTR file, int line)
    {
        free(p);
    }
    void*            operator new[] (size_t size)
    {
        return malloc(size);
    }
    void            operator delete[] (void* p)
    {
        free(p);
    }
    void*            operator new (size_t, void* buf)
    {
        return buf;
    }
    void            operator delete (void*, void*)                            {}
};
 
 
class CFX_ByteString : public CFX_Object
{
public:
 CFX_ByteString()
    {
        m_pData = NULL;
    }
   CFX_ByteString(FX_LPCSTR ptr, FX_STRSIZE len = -1);
   CFX_ByteString(char ch);
    FX_LPSTR                GetBuffer(FX_STRSIZE len);     
protected:
    struct CFX_StringData*    m_pData;
    void                    AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const;
};
 
CFX_ByteString::CFX_ByteString(FX_LPCSTR lpsz, FX_STRSIZE nLen)
{
    if (nLen < 0) {
        nLen = lpsz ? (FX_STRSIZE)strlen(lpsz) : 0;
    }
    if (nLen) {
        m_pData = FX_AllocString(nLen);
        if (m_pData) {
            memcpy(m_pData->m_String, lpsz, nLen);
        }
    } else {
        m_pData = NULL;
    }
}
 
CFX_ByteString::CFX_ByteString(char ch)
{
    m_pData = FX_AllocString(1);
    if (m_pData) {
        m_pData->m_String[0] = ch;
    }
}
FX_LPSTR CFX_ByteString::GetBuffer(FX_STRSIZE nMinBufLength)
{
    if (m_pData == NULL && nMinBufLength == 0) {
        return NULL;
    }
    if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nMinBufLength) {
        return m_pData->m_String;
    }
    if (m_pData == NULL) {
        m_pData = FX_AllocString(nMinBufLength);
        if (!m_pData) {
            return NULL;
        }
        m_pData->m_nDataLength = 0;
        m_pData->m_String[0] = 0;
        return m_pData->m_String;
    }
    CFX_StringData* pOldData = m_pData;
    FX_STRSIZE nOldLen = pOldData->m_nDataLength;
    if (nMinBufLength < nOldLen) {
        nMinBufLength = nOldLen;
    }
    m_pData = FX_AllocString(nMinBufLength);
    if (!m_pData) {
        return NULL;
    }
    memcpy(m_pData->m_String, pOldData->m_String, (nOldLen + 1));
    m_pData->m_nDataLength = nOldLen;
    pOldData->m_nRefs --;
    if (pOldData->m_nRefs <= 0) {
        FX_Free(pOldData);
    }
    return m_pData->m_String;
}

void CFX_ByteString::AllocCopy(CFX_ByteString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const
{
  return;
}

int main()
{
   CFX_ByteString  bb("abc",3);
   cout<<bb.GetBuffer(0)<<endl;
  return 0;
}
 
        

 

上一篇:类的构造函数 类型一样 变量名不一样无所谓


下一篇:[CSS属性设置,盒子模型,网页布局]