2012-04-19 14:35:52 +08:00
|
|
|
#include "CCString.h"
|
2012-06-19 17:22:55 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "ccMacros.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
#define kMaxStringLen (1024*100)
|
|
|
|
|
|
|
|
CCString::CCString()
|
2013-06-15 14:03:30 +08:00
|
|
|
:_string("")
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
CCString::CCString(const char * str)
|
2013-06-15 14:03:30 +08:00
|
|
|
:_string(str)
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
CCString::CCString(const std::string& str)
|
2013-06-15 14:03:30 +08:00
|
|
|
:_string(str)
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
CCString::CCString(const CCString& str)
|
2013-06-15 14:03:30 +08:00
|
|
|
:_string(str.getCString())
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
CCString::~CCString()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_string.clear();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCString& CCString::operator= (const CCString& other)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_string = other._string;
|
2012-04-19 14:35:52 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-06-15 14:03:30 +08:00
|
|
|
_string = pBuf;
|
2012-04-19 14:35:52 +08:00
|
|
|
free(pBuf);
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCString::initWithFormat(const char* format, ...)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
2013-06-15 14:03:30 +08:00
|
|
|
_string.clear();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
bRet = initWithFormatAndValist(format, ap);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCString::intValue() const
|
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
return atoi(_string.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CCString::uintValue() const
|
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
return (unsigned int)atoi(_string.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float CCString::floatValue() const
|
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0.0f;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
return (float)atof(_string.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double CCString::doubleValue() const
|
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
return atof(_string.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCString::boolValue() const
|
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (0 == strcmp(_string.c_str(), "0") || 0 == strcmp(_string.c_str(), "false"))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCString::getCString() const
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _string.c_str();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CCString::length() const
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _string.length();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 14:56:21 +08:00
|
|
|
int CCString::compare(const char * pStr) const
|
|
|
|
{
|
|
|
|
return strcmp(getCString(), pStr);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCObject* CCString::copyWithZone(CCZone* pZone)
|
|
|
|
{
|
|
|
|
CCAssert(pZone == NULL, "CCString should not be inherited.");
|
2013-06-15 14:03:30 +08:00
|
|
|
CCString* pStr = new CCString(_string.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
return pStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCString::isEqual(const CCObject* pObject)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
const CCString* pStr = dynamic_cast<const CCString*>(pObject);
|
|
|
|
if (pStr != NULL)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (0 == _string.compare(pStr->_string))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2012-06-20 11:48:31 +08:00
|
|
|
CCString* CCString::create(const std::string& str)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-20 11:48:31 +08:00
|
|
|
CCString* pRet = new CCString(str);
|
2012-04-19 14:35:52 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-07-23 22:49:11 +08:00
|
|
|
CCString* CCString::createWithData(const unsigned char* pData, unsigned long nLen)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCString* pRet = NULL;
|
2012-06-20 11:48:31 +08:00
|
|
|
if (pData != NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
char* pStr = (char*)malloc(nLen+1);
|
|
|
|
if (pStr != NULL)
|
|
|
|
{
|
|
|
|
pStr[nLen] = '\0';
|
2012-06-20 11:48:31 +08:00
|
|
|
if (nLen > 0)
|
|
|
|
{
|
|
|
|
memcpy(pStr, pData, nLen);
|
|
|
|
}
|
|
|
|
|
2012-06-14 16:05:58 +08:00
|
|
|
pRet = CCString::create(pStr);
|
2012-04-19 14:35:52 +08:00
|
|
|
free(pStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-06-14 16:05:58 +08:00
|
|
|
CCString* CCString::createWithFormat(const char* format, ...)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-14 16:05:58 +08:00
|
|
|
CCString* pRet = CCString::create("");
|
2012-04-19 14:35:52 +08:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
pRet->initWithFormatAndValist(format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-06-14 16:05:58 +08:00
|
|
|
CCString* CCString::createWithContentsOfFile(const char* pszFileName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
unsigned long size = 0;
|
|
|
|
unsigned char* pData = 0;
|
|
|
|
CCString* pRet = NULL;
|
2012-06-14 16:05:58 +08:00
|
|
|
pData = CCFileUtils::sharedFileUtils()->getFileData(pszFileName, "rb", &size);
|
2012-07-23 22:49:11 +08:00
|
|
|
pRet = CCString::createWithData(pData, size);
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(pData);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-05-10 15:07:05 +08:00
|
|
|
void CCString::acceptVisitor(CCDataVisitor &visitor)
|
|
|
|
{
|
|
|
|
visitor.visit(this);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|