axmol/cocos/deprecated/CCString.cpp

301 lines
6.2 KiB
C++
Raw Normal View History

2013-10-12 15:25:45 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies
2013-10-12 15:25:45 +08:00
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.
****************************************************************************/
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h"
#include "2d/platform/CCFileUtils.h"
2014-04-30 08:37:36 +08:00
#include "base/ccMacros.h"
2013-10-12 15:25:45 +08:00
#include <stdlib.h>
#include <stdio.h>
#include "CCArray.h"
NS_CC_BEGIN
#define kMaxStringLen (1024*100)
__String::__String()
2013-10-12 15:25:45 +08:00
:_string("")
{}
__String::__String(const char * str)
2013-10-12 15:25:45 +08:00
:_string(str)
{}
__String::__String(const std::string& str)
2013-10-12 15:25:45 +08:00
:_string(str)
{}
__String::__String(const __String& str)
2013-10-12 15:25:45 +08:00
:_string(str.getCString())
{}
__String::~__String()
2013-10-12 15:25:45 +08:00
{
CCLOGINFO("deallocing __String: %p", this);
2013-10-12 15:25:45 +08:00
_string.clear();
}
__String& __String::operator= (const __String& other)
2013-10-12 15:25:45 +08:00
{
if (this != &other) {
_string = other._string;
}
2013-10-12 15:25:45 +08:00
return *this;
}
bool __String::initWithFormatAndValist(const char* format, va_list ap)
2013-10-12 15:25:45 +08:00
{
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, ...)
2013-10-12 15:25:45 +08:00
{
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
2013-10-12 15:25:45 +08:00
{
if (length() == 0)
{
return 0;
}
return atoi(_string.c_str());
}
unsigned int __String::uintValue() const
2013-10-12 15:25:45 +08:00
{
if (length() == 0)
{
return 0;
}
return (unsigned int)atoi(_string.c_str());
}
float __String::floatValue() const
2013-10-12 15:25:45 +08:00
{
if (length() == 0)
{
return 0.0f;
}
return (float)atof(_string.c_str());
}
double __String::doubleValue() const
2013-10-12 15:25:45 +08:00
{
if (length() == 0)
{
return 0.0;
}
return atof(_string.c_str());
}
bool __String::boolValue() const
2013-10-12 15:25:45 +08:00
{
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
2013-10-12 15:25:45 +08:00
{
return _string.c_str();
}
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
2013-10-12 15:25:45 +08:00
{
return strcmp(getCString(), pStr);
}
void __String::append(const std::string& str)
2013-10-12 15:25:45 +08:00
{
_string.append(str);
}
void __String::appendWithFormat(const char* format, ...)
2013-10-12 15:25:45 +08:00
{
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)
2013-10-12 15:25:45 +08:00
{
__Array* result = __Array::create();
std::string strTmp = _string;
2013-12-06 16:32:06 +08:00
size_t cutAt;
while( (cutAt = strTmp.find_first_of(delimiter)) != strTmp.npos )
2013-10-12 15:25:45 +08:00
{
if(cutAt > 0)
{
result->addObject(__String::create(strTmp.substr(0, cutAt)));
2013-10-12 15:25:45 +08:00
}
strTmp = strTmp.substr(cutAt + 1);
2013-10-12 15:25:45 +08:00
}
if(strTmp.length() > 0)
2013-10-12 15:25:45 +08:00
{
result->addObject(__String::create(strTmp));
2013-10-12 15:25:45 +08:00
}
return result;
}
bool __String::isEqual(const Ref* pObject)
2013-10-12 15:25:45 +08:00
{
bool bRet = false;
const __String* pStr = dynamic_cast<const __String*>(pObject);
2013-10-12 15:25:45 +08:00
if (pStr != NULL)
{
if (0 == _string.compare(pStr->_string))
{
bRet = true;
}
}
return bRet;
}
__String* __String::create(const std::string& str)
2013-10-12 15:25:45 +08:00
{
__String* ret = new __String(str);
ret->autorelease();
return ret;
2013-10-12 15:25:45 +08:00
}
2014-03-20 16:48:12 +08:00
__String* __String::createWithData(const unsigned char* data, size_t nLen)
2013-10-12 15:25:45 +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)
{
memcpy(pStr, data, nLen);
2013-10-12 15:25:45 +08:00
}
ret = __String::create(pStr);
2013-10-12 15:25:45 +08:00
free(pStr);
}
}
return ret;
2013-10-12 15:25:45 +08:00
}
__String* __String::createWithFormat(const char* format, ...)
2013-10-12 15:25:45 +08:00
{
__String* ret = __String::create("");
2013-10-12 15:25:45 +08:00
va_list ap;
va_start(ap, format);
ret->initWithFormatAndValist(format, ap);
2013-10-12 15:25:45 +08:00
va_end(ap);
return ret;
2013-10-12 15:25:45 +08:00
}
2014-05-08 05:38:41 +08:00
__String* __String::createWithContentsOfFile(const std::string &filename)
2013-10-12 15:25:45 +08:00
{
std::string str = FileUtils::getInstance()->getStringFromFile(filename);
return __String::create(std::move(str));
2013-10-12 15:25:45 +08:00
}
void __String::acceptVisitor(DataVisitor &visitor)
2013-10-12 15:25:45 +08:00
{
visitor.visit(this);
}
__String* __String::clone() const
2013-10-12 15:25:45 +08:00
{
return __String::create(_string);
2013-10-12 15:25:45 +08:00
}
namespace StringUtils {
std::string format(const char* format, ...)
{
#define CC_MAX_STRING_LENGTH (1024*100)
std::string ret;
va_list ap;
va_start(ap, format);
char* buf = (char*)malloc(CC_MAX_STRING_LENGTH);
if (buf != nullptr)
{
vsnprintf(buf, CC_MAX_STRING_LENGTH, format, ap);
ret = buf;
free(buf);
}
va_end(ap);
return ret;
}
} // namespace StringUtils {
2013-10-12 15:25:45 +08:00
NS_CC_END