2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 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.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __CCSTRING_H__
|
|
|
|
#define __CCSTRING_H__
|
|
|
|
|
|
|
|
#include <string>
|
2012-06-16 12:48:20 +08:00
|
|
|
#include <functional>
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCObject.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class CC_DLL CCString : public CCObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCString();
|
|
|
|
CCString(const char* str);
|
|
|
|
CCString(const std::string& str);
|
|
|
|
CCString(const CCString& str);
|
|
|
|
|
|
|
|
virtual ~CCString();
|
|
|
|
|
|
|
|
/* override assignment operator */
|
|
|
|
CCString& operator= (const CCString& other);
|
|
|
|
|
|
|
|
/** init a string with format, it's similar with the c function 'sprintf' */
|
|
|
|
bool initWithFormat(const char* format, ...);
|
|
|
|
|
|
|
|
/** convert to int value */
|
|
|
|
int intValue() const;
|
|
|
|
|
|
|
|
/** convert to unsigned int value */
|
|
|
|
unsigned int uintValue() const;
|
|
|
|
|
|
|
|
/** convert to float value */
|
|
|
|
float floatValue() const;
|
|
|
|
|
|
|
|
/** convert to double value */
|
|
|
|
double doubleValue() const;
|
|
|
|
|
|
|
|
/** convert to bool value */
|
|
|
|
bool boolValue() const;
|
|
|
|
|
|
|
|
/** get the C string */
|
|
|
|
const char* getCString() const;
|
|
|
|
|
|
|
|
/** get the length of string */
|
|
|
|
unsigned int length() const;
|
|
|
|
|
2012-06-14 14:56:21 +08:00
|
|
|
/** compare to a c string */
|
|
|
|
int compare(const char *) const;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/* override functions */
|
|
|
|
virtual CCObject* copyWithZone(CCZone* pZone);
|
|
|
|
virtual bool isEqual(const CCObject* pObject);
|
|
|
|
|
|
|
|
/* static funcitons */
|
2012-06-14 14:56:21 +08:00
|
|
|
/** create a string with c string
|
2012-04-19 14:35:52 +08:00
|
|
|
* @return A CCString pointer which is an autorelease object pointer,
|
|
|
|
* it means that you needn't do a release operation unless you retain it.
|
2012-06-14 16:05:58 +08:00
|
|
|
@warning: This interface will be deprecated in future.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCString* stringWithCString(const char* pStr);
|
2012-06-14 14:56:21 +08:00
|
|
|
|
|
|
|
/** create a string with std::string
|
|
|
|
* @return A CCString pointer which is an autorelease object pointer,
|
|
|
|
* it means that you needn't do a release operation unless you retain it.
|
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCString* stringWithString(const std::string& str);
|
2012-06-14 14:56:21 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes,
|
|
|
|
* if you want to change it, you should modify the kMaxStringLen macro in CCString.cpp 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.
|
2012-06-14 16:05:58 +08:00
|
|
|
@warning: This interface will be deprecated in future.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCString* stringWithFormat(const char* format, ...);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** create a string with binary data
|
|
|
|
* @return A CCString pointer which is an autorelease object pointer,
|
|
|
|
* it means that you needn't do a release operation unless you retain it.
|
2012-06-14 16:05:58 +08:00
|
|
|
@warning: This interface will be deprecated in future.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCString* stringWithData(const unsigned char* pData, unsigned long nLen);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** 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.
|
2012-06-14 16:05:58 +08:00
|
|
|
@warning: This interface will be deprecated in future.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static CCString* stringWithContentsOfFile(const char* pszFileName);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-20 11:48:31 +08:00
|
|
|
/** create a string with std string, you can also pass a c string pointer because the default constuctor of std::string can access a c string pointer.
|
2012-06-14 16:05:58 +08:00
|
|
|
* @return A CCString pointer which is an autorelease object pointer,
|
|
|
|
* it means that you needn't do a release operation unless you retain it.
|
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
static CCString* create(const std::string& str);
|
2012-06-14 16:05:58 +08:00
|
|
|
|
|
|
|
/** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes,
|
|
|
|
* if you want to change it, you should modify the kMaxStringLen macro in CCString.cpp 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* createWithFormat(const char* format, ...);
|
|
|
|
|
|
|
|
/** create a string with binary data
|
|
|
|
* @return A CCString pointer which is an autorelease object pointer,
|
|
|
|
* it means that you needn't do a release operation unless you retain it.
|
|
|
|
*/
|
2012-06-20 11:48:31 +08:00
|
|
|
static CCString* createWithData(const unsigned char* pData, unsigned long nLen);
|
2012-06-14 16:05:58 +08:00
|
|
|
|
|
|
|
/** 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);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/** only for internal use */
|
|
|
|
bool initWithFormatAndValist(const char* format, va_list ap);
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::string m_sString;
|
|
|
|
};
|
|
|
|
|
2012-06-15 04:50:47 +08:00
|
|
|
struct CCStringCompare : public std::binary_function<CCString *, CCString *, bool> {
|
|
|
|
public:
|
|
|
|
bool operator() (CCString * a, CCString * b) const {
|
|
|
|
return strcmp(a->getCString(), b->getCString()) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-14 16:05:58 +08:00
|
|
|
#define CCStringMake(str) CCString::create(str)
|
2012-04-19 14:35:52 +08:00
|
|
|
#define ccs CCStringMake
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2011-03-18 14:31:29 +08:00
|
|
|
#endif //__CCSTRING_H__
|