Implement utils::atof()

This commit is contained in:
boyu0 2014-07-14 20:42:08 +08:00
parent 6d063fb97b
commit 7fd0ca217c
2 changed files with 29 additions and 0 deletions

View File

@ -24,6 +24,9 @@ THE SOFTWARE.
****************************************************************************/ ****************************************************************************/
#include "base/ccUtils.h" #include "base/ccUtils.h"
#include <stdlib.h>
#include "base/CCDirector.h" #include "base/CCDirector.h"
#include "renderer/CCCustomCommand.h" #include "renderer/CCCustomCommand.h"
#include "renderer/CCRenderer.h" #include "renderer/CCRenderer.h"
@ -161,6 +164,27 @@ std::vector<Node*> findChildren(const Node &node, const std::string &name)
return vec; return vec;
} }
#define MAX_ITOA_BUFFER_SIZE 256
double atof(const char* str)
{
if (str == nullptr)
{
return 0.0;
}
char buf[MAX_ITOA_BUFFER_SIZE];
strncpy(buf, str, MAX_ITOA_BUFFER_SIZE);
// strip string, only remain 7 numbers after '.'
char* dot = strchr(buf, '.');
if (dot != nullptr && dot - buf + 8 < MAX_ITOA_BUFFER_SIZE)
{
dot[8] = '\0';
}
return ::atof(buf);
}
} }
NS_CC_END NS_CC_END

View File

@ -73,6 +73,11 @@ namespace utils
* @since v3.2 * @since v3.2
*/ */
std::vector<Node*> findChildren(const Node &node, const std::string &name); std::vector<Node*> findChildren(const Node &node, const std::string &name);
/** Same to ::atof, but strip the string, remain 7 numbers after '.' before call atof。
* Why we need this? Because in android c++_static, atof ( and std::atof ) is unsupported for numbers have long decimal part and contain several numbers can approximate to 1 like 90.099998474121094 ), it will return inf. this function is used to fix this bug.
*/
double atof(const char* str);
} }
NS_CC_END NS_CC_END