FBXParseTools.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*************************************************************************/
  2. /* FBXParseTools.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef FBX_PARSE_TOOLS_H
  31. #define FBX_PARSE_TOOLS_H
  32. #include "core/error/error_macros.h"
  33. #include "core/string/ustring.h"
  34. #include <stdint.h>
  35. #include <algorithm>
  36. #include <locale>
  37. template <class char_t>
  38. inline bool IsNewLine(char_t c) {
  39. return c == '\n' || c == '\r';
  40. }
  41. template <class char_t>
  42. inline bool IsSpace(char_t c) {
  43. return (c == (char_t)' ' || c == (char_t)'\t');
  44. }
  45. template <class char_t>
  46. inline bool IsSpaceOrNewLine(char_t c) {
  47. return IsNewLine(c) || IsSpace(c);
  48. }
  49. template <class char_t>
  50. inline bool IsLineEnd(char_t c) {
  51. return (c == (char_t)'\r' || c == (char_t)'\n' || c == (char_t)'\0' || c == (char_t)'\f');
  52. }
  53. // ------------------------------------------------------------------------------------
  54. // Special version of the function, providing higher accuracy and safety
  55. // It is mainly used by fast_atof to prevent ugly and unwanted integer overflows.
  56. // ------------------------------------------------------------------------------------
  57. inline uint64_t strtoul10_64(const char *in, bool &errored, const char **out = 0, unsigned int *max_inout = 0) {
  58. unsigned int cur = 0;
  59. uint64_t value = 0;
  60. errored = *in < '0' || *in > '9';
  61. ERR_FAIL_COND_V_MSG(errored, 0, "The string cannot be converted parser error");
  62. for (;;) {
  63. if (*in < '0' || *in > '9') {
  64. break;
  65. }
  66. const uint64_t new_value = (value * (uint64_t)10) + ((uint64_t)(*in - '0'));
  67. // numeric overflow, we rely on you
  68. if (new_value < value) {
  69. //WARN_PRINT( "Converting the string \" " + in + " \" into a value resulted in overflow." );
  70. return 0;
  71. }
  72. value = new_value;
  73. ++in;
  74. ++cur;
  75. if (max_inout && *max_inout == cur) {
  76. if (out) { /* skip to end */
  77. while (*in >= '0' && *in <= '9') {
  78. ++in;
  79. }
  80. *out = in;
  81. }
  82. return value;
  83. }
  84. }
  85. if (out) {
  86. *out = in;
  87. }
  88. if (max_inout) {
  89. *max_inout = cur;
  90. }
  91. return value;
  92. }
  93. #endif // FBX_PARSE_TOOLS_H