CCString(内部std:string 实现。 成员:std::string变量 operator= intValue createWithFormat createWithContents)

#ifndef __CCSTRING_H__

#define __CCSTRING_H__


#if (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)

#include <string.h>

#endif


#include <stdarg.h>

#include <string>

#include <functional>

#include "CCObject.h"


NS_CC_BEGIN


class CC_DLL CCString : public CCObject

{

public:

    CCString();

    CCString(const char* str);  //初始化:字符串首地址

    CCString(const std::string& str);//初始化:std::string类型变量引用

    CCString(const CCString& str);   //初始化:CCString类型变量引用


    virtual ~CCString();

    

    CCString& operator= (const CCString& other);// 重载“=” 参数是CCString


    bool initWithFormat(const char* format, ...) CC_FORMAT_PRINTF(2, 3); //以特定格式初始化 用法类似sprintf


    int intValue() const;//转换到int 要求字符串是数字才有意义

int CCString::intValue() const

{

    if (length() == 0)

    {

        return 0;

    }

    return atoi(m_sString.c_str()); //参数字符串,如果第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。否则,返回零

}

    unsigned int uintValue() const;//转换到unsigned int


    float floatValue() const; //转换到float


    double doubleValue() const;//转换到double


    bool boolValue() const;//转换到bool


    const char* getCString() const;//return m_sString.c_str();

const char* CCString::getCString() const

{

    return m_sString.c_str();

}


    unsigned int length() const;//得到字符串长度



    int compare(const char *) const;//比较两个字符串大小

int CCString::compare(const char * pStr) const

{

    return strcmp(getCString(), pStr);

//

s1=s2,strcmp(s1,s2) == 0;
s1>s2, strcmp(s1,s2) == 1;
s1<s2, strcmp(s1,s2) == -1;
//

}



    /* override functions */

    virtual CCObject* copyWithZone(CCZone* pZone);


    virtual bool isEqual(const CCObject* pObject);//是否相等


    static CCString* create(const std::string& str);//创建ccstring 参数:std::string引用

CCString* CCString::create(const std::string& str)

{

    CCString* pRet = new CCString(str);

    pRet->autorelease();

    return pRet;

}

    static CCString* createWithFormat(const char* format, ...) CC_FORMAT_PRINTF(1, 2); //以特定格式(类似sprintf)创建ccstring 


    static CCString* createWithData(const unsigned char* pData, unsigned long nLen);//截取一段字符串 创建ccstring

CCString* CCString::createWithData(const unsigned char* pData, unsigned long nLen)

{

    CCString* pRet = NULL;

    if (pData != NULL)

    {

        char* pStr = (char*)malloc(nLen+1);

        if (pStr != NULL)

        {

            pStr[nLen] = '\0';

            if (nLen > 0)

            {

                memcpy(pStr, pData, nLen);

            }

            

            pRet = CCString::create(pStr);

            free(pStr);

        }

    }

    return pRet;

}

    /** create a string with a file, 

     *  @return A CCString pointer which is an autorelease object pointer,

     *          it means that you needn't do a release operation unless you retain it.

     */

    static CCString* createWithContentsOfFile(const char* pszFileName);

CCString* CCString::createWithContentsOfFile(const char* pszFileName)

{

    unsigned long size = 0;

    unsigned char* pData = 0;

    CCString* pRet = NULL;

    pData = CCFileUtils::sharedFileUtils()->getFileData(pszFileName, "rb", &size);

    pRet = CCString::createWithData(pData, size);

    CC_SAFE_DELETE_ARRAY(pData);

    return pRet;

}


    virtual void acceptVisitor(CCDataVisitor &visitor);

void CCString::acceptVisitor(CCDataVisitor &visitor)

{

    visitor.visit(this);

}

private:


    /** only for internal use */

    bool initWithFormatAndValist(const char* format, va_list ap);

bool CCString::initWithFormatAndValist(const char* format, va_list ap)

{

    bool bRet = false;

    char* pBuf = (char*)malloc(kMaxStringLen);

    if (pBuf != NULL)

    {

        vsnprintf(pBuf, kMaxStringLen, format, ap);

        m_sString = pBuf;

        free(pBuf);

        bRet = true;

    }

    return bRet;

}



public:

    std::string m_sString;// 内部含有一个std::string类型成员

};


struct CCStringCompare : public std::binary_function<CCString *, CCString *, bool> {

    public:

        bool operator() (CCString * a, CCString * b) const {

            return strcmp(a->getCString(), b->getCString()) < 0;

        }

};


#define CCStringMake(str) CCString::create(str)

#define ccs               CCStringMake


// end of data_structure group

/// @}


NS_CC_END


#endif //__CCSTRING_H__


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值