ccUtils.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /****************************************************************************
  2. Copyright (c) 2010 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "base/ccUtils.h"
  23. #include "base/base64.h"
  24. #include "platform/CCFileUtils.h"
  25. #include <cmath>
  26. #include <stdlib.h>
  27. NS_CC_BEGIN
  28. namespace
  29. {
  30. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  31. #include "platform/CCStdC.h"
  32. int gettimeofday(struct timeval * val, void *)
  33. {
  34. if (val)
  35. {
  36. LARGE_INTEGER liTime, liFreq;
  37. QueryPerformanceFrequency(&liFreq);
  38. QueryPerformanceCounter(&liTime);
  39. val->tv_sec = (long)(liTime.QuadPart / liFreq.QuadPart);
  40. val->tv_usec = (long)(liTime.QuadPart * 1000000.0 / liFreq.QuadPart - val->tv_sec * 1000000.0);
  41. }
  42. return 0;
  43. }
  44. #endif
  45. }
  46. namespace utils
  47. {
  48. #define MAX_ITOA_BUFFER_SIZE 256
  49. double atof(const char* str)
  50. {
  51. if (str == nullptr)
  52. {
  53. return 0.0;
  54. }
  55. char buf[MAX_ITOA_BUFFER_SIZE];
  56. strncpy(buf, str, MAX_ITOA_BUFFER_SIZE);
  57. // strip string, only remain 7 numbers after '.'
  58. char* dot = strchr(buf, '.');
  59. if (dot != nullptr && dot - buf + 8 < MAX_ITOA_BUFFER_SIZE)
  60. {
  61. dot[8] = '\0';
  62. }
  63. return ::atof(buf);
  64. }
  65. double gettime()
  66. {
  67. struct timeval tv;
  68. gettimeofday(&tv, nullptr);
  69. return (double)tv.tv_sec + (double)tv.tv_usec/1000000;
  70. }
  71. long long getTimeInMilliseconds()
  72. {
  73. struct timeval tv;
  74. gettimeofday (&tv, nullptr);
  75. return (long long)tv.tv_sec * 1000 + tv.tv_usec / 1000;
  76. }
  77. int nextPOT(int x)
  78. {
  79. x = x - 1;
  80. x = x | (x >> 1);
  81. x = x | (x >> 2);
  82. x = x | (x >> 4);
  83. x = x | (x >> 8);
  84. x = x | (x >> 16);
  85. return x + 1;
  86. }
  87. }
  88. NS_CC_END