2013-10-12 15:25:45 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCString.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "CCArray.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
#define kMaxStringLen (1024*100)
|
|
|
|
|
|
|
|
String::String()
|
|
|
|
:_string("")
|
|
|
|
{}
|
|
|
|
|
|
|
|
String::String(const char * str)
|
|
|
|
:_string(str)
|
|
|
|
{}
|
|
|
|
|
|
|
|
String::String(const std::string& str)
|
|
|
|
:_string(str)
|
|
|
|
{}
|
|
|
|
|
|
|
|
String::String(const String& str)
|
|
|
|
:_string(str.getCString())
|
|
|
|
{}
|
|
|
|
|
|
|
|
String::~String()
|
|
|
|
{
|
|
|
|
CCLOGINFO("deallocing String: %p", this);
|
|
|
|
|
|
|
|
_string.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
String& String::operator= (const String& other)
|
|
|
|
{
|
|
|
|
_string = other._string;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::initWithFormatAndValist(const char* format, va_list ap)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
char* pBuf = (char*)malloc(kMaxStringLen);
|
|
|
|
if (pBuf != NULL)
|
|
|
|
{
|
|
|
|
vsnprintf(pBuf, kMaxStringLen, format, ap);
|
|
|
|
_string = pBuf;
|
|
|
|
free(pBuf);
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::initWithFormat(const char* format, ...)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
_string.clear();
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
|
|
|
bRet = initWithFormatAndValist(format, ap);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
int String::intValue() const
|
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return atoi(_string.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int String::uintValue() const
|
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (unsigned int)atoi(_string.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
float String::floatValue() const
|
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
return (float)atof(_string.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
double String::doubleValue() const
|
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
return atof(_string.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::boolValue() const
|
|
|
|
{
|
|
|
|
if (length() == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == strcmp(_string.c_str(), "0") || 0 == strcmp(_string.c_str(), "false"))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* String::getCString() const
|
|
|
|
{
|
|
|
|
return _string.c_str();
|
|
|
|
}
|
|
|
|
|
2013-12-06 16:32:06 +08:00
|
|
|
int String::length() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-12-06 16:32:06 +08:00
|
|
|
return static_cast<int>(_string.length());
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int String::compare(const char * pStr) const
|
|
|
|
{
|
|
|
|
return strcmp(getCString(), pStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Array* String::componentsSeparatedByString(const char *delimiter)
|
|
|
|
{
|
|
|
|
Array* result = Array::create();
|
|
|
|
|
2013-12-06 16:32:06 +08:00
|
|
|
size_t cutAt;
|
2013-10-12 15:25:45 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String::isEqual(const Object* pObject)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
const String* pStr = dynamic_cast<const String*>(pObject);
|
|
|
|
if (pStr != NULL)
|
|
|
|
{
|
|
|
|
if (0 == _string.compare(pStr->_string))
|
|
|
|
{
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
String* String::create(const std::string& str)
|
|
|
|
{
|
2013-11-06 09:36:44 +08:00
|
|
|
String* ret = new String(str);
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
String* String::createWithData(const unsigned char* data, int nLen)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2013-11-06 09:36:44 +08:00
|
|
|
String* ret = NULL;
|
|
|
|
if (data != NULL)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
char* pStr = (char*)malloc(nLen+1);
|
|
|
|
if (pStr != NULL)
|
|
|
|
{
|
|
|
|
pStr[nLen] = '\0';
|
|
|
|
if (nLen > 0)
|
|
|
|
{
|
2013-11-06 09:36:44 +08:00
|
|
|
memcpy(pStr, data, nLen);
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2013-11-06 09:36:44 +08:00
|
|
|
ret = String::create(pStr);
|
2013-10-12 15:25:45 +08:00
|
|
|
free(pStr);
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 09:36:44 +08:00
|
|
|
return ret;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
String* String::createWithFormat(const char* format, ...)
|
|
|
|
{
|
2013-11-06 09:36:44 +08:00
|
|
|
String* ret = String::create("");
|
2013-10-12 15:25:45 +08:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2013-11-06 09:36:44 +08:00
|
|
|
ret->initWithFormatAndValist(format, ap);
|
2013-10-12 15:25:45 +08:00
|
|
|
va_end(ap);
|
|
|
|
|
2013-11-06 09:36:44 +08:00
|
|
|
return ret;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
String* String::createWithContentsOfFile(const char* filename)
|
|
|
|
{
|
2013-12-05 17:19:01 +08:00
|
|
|
ssize_t size = 0;
|
2013-11-06 09:36:44 +08:00
|
|
|
unsigned char* data = 0;
|
|
|
|
String* ret = NULL;
|
|
|
|
data = FileUtils::getInstance()->getFileData(filename, "rb", &size);
|
2013-12-05 17:19:01 +08:00
|
|
|
ret = String::createWithData(data, static_cast<int>(size));
|
2013-11-12 10:09:47 +08:00
|
|
|
free(data);
|
2013-11-06 09:36:44 +08:00
|
|
|
return ret;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void String::acceptVisitor(DataVisitor &visitor)
|
|
|
|
{
|
|
|
|
visitor.visit(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
String* String::clone() const
|
|
|
|
{
|
|
|
|
return String::create(_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|