From ddd6d9d15529bf8ba6dac298fd28d9c21a473349 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 17 Apr 2013 16:08:48 +0800 Subject: [PATCH] fixed #2059: cc.FileUtils.getInstance().getStringFromFile(filename) doesn't return a whole string. --- cocos2dx/platform/ios/CCCommon.mm | 3 +-- cocos2dx/support/ccUTF8.cpp | 7 +++++-- cocos2dx/support/ccUTF8.h | 2 +- scripting/javascript/bindings/ScriptingCore.cpp | 14 ++++++++------ scripting/javascript/bindings/ScriptingCore.h | 2 +- .../bindings/cocos2d_specifics.cpp.REMOVED.git-id | 2 +- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/cocos2dx/platform/ios/CCCommon.mm b/cocos2dx/platform/ios/CCCommon.mm index 750b11fe2e..312ae78a08 100644 --- a/cocos2dx/platform/ios/CCCommon.mm +++ b/cocos2dx/platform/ios/CCCommon.mm @@ -34,8 +34,7 @@ NS_CC_BEGIN void CCLog(const char * pszFormat, ...) { printf("Cocos2d: "); - char szBuf[kMaxLogLen]; - + char szBuf[kMaxLogLen+1] = {0}; va_list ap; va_start(ap, pszFormat); vsnprintf(szBuf, kMaxLogLen, pszFormat, ap); diff --git a/cocos2dx/support/ccUTF8.cpp b/cocos2dx/support/ccUTF8.cpp index 3e0ad2bc0f..53d90b13d6 100644 --- a/cocos2dx/support/ccUTF8.cpp +++ b/cocos2dx/support/ccUTF8.cpp @@ -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]; str_new[len] = 0; diff --git a/cocos2dx/support/ccUTF8.h b/cocos2dx/support/ccUTF8.h index d7750a4d34..e8799e8733 100644 --- a/cocos2dx/support/ccUTF8.h +++ b/cocos2dx/support/ccUTF8.h @@ -59,7 +59,7 @@ CC_DLL std::vector cc_utf16_vec_from_utf16_str(const unsigned sh * * 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: diff --git a/scripting/javascript/bindings/ScriptingCore.cpp b/scripting/javascript/bindings/ScriptingCore.cpp index 0ce33e2226..2a2f32ad70 100644 --- a/scripting/javascript/bindings/ScriptingCore.cpp +++ b/scripting/javascript/bindings/ScriptingCore.cpp @@ -162,11 +162,11 @@ void ScriptingCore::executeJSFunctionWithThisObj(jsval thisObj, jsval callback, void js_log(const char *format, ...) { 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_start(vl, format); - int len = vsnprintf(_js_log_buf, 256, format, vl); + int len = vsnprintf(_js_log_buf, kMaxLogLen, format, vl); va_end(vl); if (len) { 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()); } -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) { return JSVAL_NULL; } jsval ret = JSVAL_NULL; - jschar* strUTF16 = (jschar*)cc_utf8_to_utf16(v); - if (strUTF16) { - JSString* str = JS_NewUCStringCopyZ(cx, strUTF16); + size_t utf16_size = 0; + jschar* strUTF16 = (jschar*)cc_utf8_to_utf16(v, length, &utf16_size); + + if (strUTF16 && utf16_size > 0) { + JSString* str = JS_NewUCStringCopyN(cx, strUTF16, utf16_size); if (str) { ret = STRING_TO_JSVAL(str); } diff --git a/scripting/javascript/bindings/ScriptingCore.h b/scripting/javascript/bindings/ScriptingCore.h index e5e54f232d..ccd4686123 100644 --- a/scripting/javascript/bindings/ScriptingCore.h +++ b/scripting/javascript/bindings/ScriptingCore.h @@ -229,7 +229,7 @@ jsval int32_to_jsval( JSContext *cx, int32_t l); jsval uint32_to_jsval( JSContext *cx, uint32_t number ); jsval long_long_to_jsval(JSContext* cx, long long 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 ccrect_to_jsval(JSContext* cx, CCRect& v); jsval ccsize_to_jsval(JSContext* cx, CCSize& v); diff --git a/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id b/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id index 789b08ad0c..098e861d91 100644 --- a/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id +++ b/scripting/javascript/bindings/cocos2d_specifics.cpp.REMOVED.git-id @@ -1 +1 @@ -158877faeea5c5d8bf8b8e51dc69aa902c382d32 \ No newline at end of file +094ddece4a019c0c5fe27fb260b78a90e1334208 \ No newline at end of file