Entity.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <sstream>
  2. #include <kapusha/core/Log.h>
  3. #include "Entity.h"
  4. Entity::Entity(void)
  5. {
  6. }
  7. Entity::~Entity(void)
  8. {
  9. }
  10. off_t streamSkipSpacesUntilAfterChars(kapusha::Stream* stream, const char* chars)
  11. {
  12. off_t ret = -1;
  13. while(stream->error_ == kapusha::Stream::ErrorNone)
  14. {
  15. for(; stream->cursor_ < stream->end_; ++stream->cursor_)
  16. {
  17. if (ret != -1)
  18. return ret;
  19. if (!isspace(*stream->cursor_))
  20. {
  21. for(const char* p = chars; *p != 0; ++p)
  22. if (*stream->cursor_ == *p)
  23. {
  24. // do one more cycle to reload
  25. ret = p - chars;
  26. break;
  27. }
  28. if (ret == -1)
  29. return -1;
  30. }
  31. }
  32. stream->refill();
  33. }
  34. return ret;
  35. }
  36. off_t streamExtractUntilAfterChars(kapusha::Stream* stream, char end,
  37. char* out, int outmax)
  38. {
  39. char *p = out;
  40. while(stream->error_ == kapusha::Stream::ErrorNone)
  41. {
  42. for(; stream->cursor_ < stream->end_; ++stream->cursor_)
  43. {
  44. if (*stream->cursor_ == end)
  45. {
  46. ++stream->cursor_;
  47. *p = 0;
  48. return p - out;
  49. }
  50. *p++ = *stream->cursor_;
  51. KP_ASSERT(p < (out + outmax));
  52. }
  53. stream->refill();
  54. }
  55. return -1;
  56. }
  57. Entity* Entity::readNextEntity(kapusha::Stream* stream)
  58. {
  59. if (0 != streamSkipSpacesUntilAfterChars(stream, "{"))
  60. return 0;
  61. char key[32];
  62. char value[1024];
  63. Entity *ret = new Entity();
  64. for(;;)
  65. {
  66. off_t chidx = streamSkipSpacesUntilAfterChars(stream, "\"}");
  67. if (chidx == 1)
  68. return ret;
  69. if (chidx == -1)
  70. break;
  71. if (-1 == streamExtractUntilAfterChars(stream, '"', key, sizeof key))
  72. break;
  73. if (-1 == streamSkipSpacesUntilAfterChars(stream, "\""))
  74. break;
  75. if (-1 == streamExtractUntilAfterChars(stream, '"', value, sizeof value))
  76. break;
  77. ret->params_[key] = value;
  78. }
  79. delete ret;
  80. return 0;
  81. }
  82. const std::string* Entity::getParam(const std::string& name) const
  83. {
  84. auto entry = params_.find(name);
  85. if (entry == params_.end())
  86. return 0;
  87. return &entry->second;
  88. }
  89. kapusha::vec3f Entity::getVec3Param(const std::string& name) const
  90. {
  91. auto entry = params_.find(name);
  92. if (entry == params_.end())
  93. return kapusha::vec3f();
  94. kapusha::vec3f value;
  95. std::stringstream ss(entry->second);
  96. ss >> value.x;
  97. ss >> value.y;
  98. ss >> value.z;
  99. return value;
  100. }
  101. kapusha::vec4f Entity::getVec4Param(const std::string& name) const
  102. {
  103. auto entry = params_.find(name);
  104. if (entry == params_.end())
  105. return kapusha::vec4f();
  106. kapusha::vec4f value;
  107. std::stringstream ss(entry->second);
  108. ss >> value.x;
  109. ss >> value.y;
  110. ss >> value.z;
  111. ss >> value.w;
  112. return value;
  113. }
  114. void Entity::print() const
  115. {
  116. L("Entity {");
  117. for(auto it = params_.begin(); it != params_.end(); ++it)
  118. {
  119. L("\t%s: %s", it->first.c_str(), it->second.c_str());
  120. }
  121. L("}");
  122. }