fixed #2059: cc.FileUtils.getInstance().getStringFromFile(filename) doesn't return a whole string.

This commit is contained in:
James Chen 2013-04-17 16:08:48 +08:00
parent 2d92ba4d6a
commit ddd6d9d155
6 changed files with 17 additions and 13 deletions

View File

@ -34,8 +34,7 @@ NS_CC_BEGIN
void CCLog(const char * pszFormat, ...) void CCLog(const char * pszFormat, ...)
{ {
printf("Cocos2d: "); printf("Cocos2d: ");
char szBuf[kMaxLogLen]; char szBuf[kMaxLogLen+1] = {0};
va_list ap; va_list ap;
va_start(ap, pszFormat); va_start(ap, pszFormat);
vsnprintf(szBuf, kMaxLogLen, pszFormat, ap); vsnprintf(szBuf, kMaxLogLen, pszFormat, ap);

View File

@ -275,9 +275,12 @@ cc_utf8_get_char (const char * p)
} }
unsigned short* cc_utf8_to_utf16(const char* str_old) unsigned short* cc_utf8_to_utf16(const char* str_old, size_t length/* = -1 */, size_t* rUtf16Size/* = NULL */)
{ {
int len = cc_utf8_strlen(str_old, -1); int len = cc_utf8_strlen(str_old, length);
if (rUtf16Size != NULL) {
*rUtf16Size = len;
}
unsigned short* str_new = new unsigned short[len + 1]; unsigned short* str_new = new unsigned short[len + 1];
str_new[len] = 0; str_new[len] = 0;

View File

@ -59,7 +59,7 @@ CC_DLL std::vector<unsigned short> cc_utf16_vec_from_utf16_str(const unsigned sh
* *
* Return value: the newly created utf8 string. * Return value: the newly created utf8 string.
* */ * */
CC_DLL unsigned short* cc_utf8_to_utf16(const char* str_old); CC_DLL unsigned short* cc_utf8_to_utf16(const char* str_old, size_t length = -1, size_t* rUtf16Size = NULL);
/** /**
* cc_utf16_to_utf8: * cc_utf16_to_utf8:

View File

@ -162,11 +162,11 @@ void ScriptingCore::executeJSFunctionWithThisObj(jsval thisObj, jsval callback,
void js_log(const char *format, ...) { void js_log(const char *format, ...) {
if (_js_log_buf == NULL) { if (_js_log_buf == NULL) {
_js_log_buf = (char *)calloc(sizeof(char), 257); _js_log_buf = (char *)calloc(sizeof(char), kMaxLogLen+1);
} }
va_list vl; va_list vl;
va_start(vl, format); va_start(vl, format);
int len = vsnprintf(_js_log_buf, 256, format, vl); int len = vsnprintf(_js_log_buf, kMaxLogLen, format, vl);
va_end(vl); va_end(vl);
if (len) { if (len) {
CCLOG("JS: %s\n", _js_log_buf); CCLOG("JS: %s\n", _js_log_buf);
@ -1525,14 +1525,16 @@ jsval std_string_to_jsval(JSContext* cx, std::string& v) {
return c_string_to_jsval(cx, v.c_str()); return c_string_to_jsval(cx, v.c_str());
} }
jsval c_string_to_jsval(JSContext* cx, const char* v) { jsval c_string_to_jsval(JSContext* cx, const char* v, size_t length /* = -1 */) {
if (v == NULL) { if (v == NULL) {
return JSVAL_NULL; return JSVAL_NULL;
} }
jsval ret = JSVAL_NULL; jsval ret = JSVAL_NULL;
jschar* strUTF16 = (jschar*)cc_utf8_to_utf16(v); size_t utf16_size = 0;
if (strUTF16) { jschar* strUTF16 = (jschar*)cc_utf8_to_utf16(v, length, &utf16_size);
JSString* str = JS_NewUCStringCopyZ(cx, strUTF16);
if (strUTF16 && utf16_size > 0) {
JSString* str = JS_NewUCStringCopyN(cx, strUTF16, utf16_size);
if (str) { if (str) {
ret = STRING_TO_JSVAL(str); ret = STRING_TO_JSVAL(str);
} }

View File

@ -229,7 +229,7 @@ jsval int32_to_jsval( JSContext *cx, int32_t l);
jsval uint32_to_jsval( JSContext *cx, uint32_t number ); jsval uint32_to_jsval( JSContext *cx, uint32_t number );
jsval long_long_to_jsval(JSContext* cx, long long v); jsval long_long_to_jsval(JSContext* cx, long long v);
jsval std_string_to_jsval(JSContext* cx, string& v); jsval std_string_to_jsval(JSContext* cx, string& v);
jsval c_string_to_jsval(JSContext* cx, const char* v); jsval c_string_to_jsval(JSContext* cx, const char* v, size_t length = -1);
jsval ccpoint_to_jsval(JSContext* cx, CCPoint& v); jsval ccpoint_to_jsval(JSContext* cx, CCPoint& v);
jsval ccrect_to_jsval(JSContext* cx, CCRect& v); jsval ccrect_to_jsval(JSContext* cx, CCRect& v);
jsval ccsize_to_jsval(JSContext* cx, CCSize& v); jsval ccsize_to_jsval(JSContext* cx, CCSize& v);

View File

@ -1 +1 @@
158877faeea5c5d8bf8b8e51dc69aa902c382d32 094ddece4a019c0c5fe27fb260b78a90e1334208