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>
|
2013-07-14 16:56:34 +08:00
|
|
|
#include "CCArray.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
#define kMaxStringLen (1024*100)
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
String::String()
|
2013-06-15 14:03:30 +08:00
|
|
|
:_string("")
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
String::String(const char * str)
|
2013-06-15 14:03:30 +08:00
|
|
|
:_string(str)
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
String::String(const std::string& str)
|
2013-06-15 14:03:30 +08:00
|
|
|
:_string(str)
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
String::String(const String& str)
|
2013-06-15 14:03:30 +08:00
|
|
|
:_string(str.getCString())
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
String::~String()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_string.clear();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
String& String::operator= (const String& other)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_string = other._string;
|
2012-04-19 14:35:52 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool String::initWithFormatAndValist(const char* format, va_list ap)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool String::initWithFormat(const char* format, ...)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int String::intValue() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int String::uintValue() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
float String::floatValue() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
double String::doubleValue() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool String::boolValue() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const char* String::getCString() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _string.c_str();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int String::length() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _string.length();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int String::compare(const char * pStr) const
|
2012-06-14 14:56:21 +08:00
|
|
|
{
|
|
|
|
return strcmp(getCString(), pStr);
|
|
|
|
}
|
|
|
|
|
2013-07-10 09:50:13 +08:00
|
|
|
void String::append(const std::string& str)
|
|
|
|
{
|
|
|
|
_string.append(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void String::appendWithFormat(const char* format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
char* pBuf = (char*)malloc(kMaxStringLen);
|
|
|
|
if (pBuf != NULL)
|
|
|
|
{
|
|
|
|
vsnprintf(pBuf, kMaxStringLen, format, ap);
|
|
|
|
_string.append(pBuf);
|
|
|
|
free(pBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:56:34 +08:00
|
|
|
Array* String::componentsSeparatedByString(const char *delimiter)
|
|
|
|
{
|
|
|
|
Array* result = Array::create();
|
|
|
|
|
|
|
|
int cutAt;
|
|
|
|
while( (cutAt = _string.find_first_of(delimiter)) != _string.npos )
|
|
|
|
{
|
|
|
|
if(cutAt > 0)
|
|
|
|
{
|
|
|
|
result->addObject(String::create(_string.substr(0, cutAt)));
|
|
|
|
}
|
|
|
|
_string = _string.substr(cutAt + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_string.length() > 0)
|
|
|
|
{
|
|
|
|
result->addObject(String::create(_string));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool String::isEqual(const Object* pObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2013-06-20 14:13:12 +08:00
|
|
|
const String* pStr = dynamic_cast<const String*>(pObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
String* String::create(const std::string& str)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pRet = new String(str);
|
2012-04-19 14:35:52 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
String* String::createWithData(const unsigned char* pData, unsigned long nLen)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
String* 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);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
pRet = String::create(pStr);
|
2012-04-19 14:35:52 +08:00
|
|
|
free(pStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
String* String::createWithFormat(const char* format, ...)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pRet = String::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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
String* String::createWithContentsOfFile(const char* pszFileName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
unsigned long size = 0;
|
|
|
|
unsigned char* pData = 0;
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pRet = NULL;
|
2013-07-12 06:24:23 +08:00
|
|
|
pData = FileUtils::getInstance()->getFileData(pszFileName, "rb", &size);
|
2013-06-20 14:13:12 +08:00
|
|
|
pRet = String::createWithData(pData, size);
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(pData);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void String::acceptVisitor(DataVisitor &visitor)
|
2013-05-10 15:07:05 +08:00
|
|
|
{
|
|
|
|
visitor.visit(this);
|
|
|
|
}
|
|
|
|
|
2013-07-08 23:05:47 +08:00
|
|
|
String* String::clone() const
|
|
|
|
{
|
|
|
|
return String::create(_string);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|