parsestream.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "stringstream.h"
  18. #include "../sys/filename.h"
  19. #include "../math/vec2.h"
  20. #include "../math/vec3.h"
  21. #include "../math/col3.h"
  22. #include "../math/color.h"
  23. namespace embree
  24. {
  25. /*! helper class for simple command line parsing */
  26. class ParseStream : public Stream<std::string>
  27. {
  28. public:
  29. ParseStream (const Ref<Stream<std::string> >& cin) : cin(cin) {}
  30. ParseStream (const Ref<Stream<int> >& cin, const std::string& seps = "\n\t\r ",
  31. const std::string& endl = "", bool multiLine = false)
  32. : cin(new StringStream(cin,seps,endl,multiLine)) {}
  33. public:
  34. ParseLocation location() { return cin->loc(); }
  35. std::string next() { return cin->get(); }
  36. void force(const std::string& next) {
  37. std::string token = getString();
  38. if (token != next)
  39. THROW_RUNTIME_ERROR("token \""+next+"\" expected but token \""+token+"\" found");
  40. }
  41. std::string getString() {
  42. return get();
  43. }
  44. FileName getFileName() {
  45. return FileName(get());
  46. }
  47. int getInt () {
  48. return atoi(get().c_str());
  49. }
  50. Vec2i getVec2i() {
  51. int x = atoi(get().c_str());
  52. int y = atoi(get().c_str());
  53. return Vec2i(x,y);
  54. }
  55. Vec3ia getVec3ia() {
  56. int x = atoi(get().c_str());
  57. int y = atoi(get().c_str());
  58. int z = atoi(get().c_str());
  59. return Vec3ia(x,y,z);
  60. }
  61. float getFloat() {
  62. return (float)atof(get().c_str());
  63. }
  64. Vec2f getVec2f() {
  65. float x = (float)atof(get().c_str());
  66. float y = (float)atof(get().c_str());
  67. return Vec2f(x,y);
  68. }
  69. Vec3f getVec3f() {
  70. float x = (float)atof(get().c_str());
  71. float y = (float)atof(get().c_str());
  72. float z = (float)atof(get().c_str());
  73. return Vec3f(x,y,z);
  74. }
  75. Vec3fa getVec3fa() {
  76. float x = (float)atof(get().c_str());
  77. float y = (float)atof(get().c_str());
  78. float z = (float)atof(get().c_str());
  79. return Vec3fa(x,y,z);
  80. }
  81. Col3f getCol3f() {
  82. float x = (float)atof(get().c_str());
  83. float y = (float)atof(get().c_str());
  84. float z = (float)atof(get().c_str());
  85. return Col3f(x,y,z);
  86. }
  87. Color getColor() {
  88. float r = (float)atof(get().c_str());
  89. float g = (float)atof(get().c_str());
  90. float b = (float)atof(get().c_str());
  91. return Color(r,g,b);
  92. }
  93. private:
  94. Ref<Stream<std::string> > cin;
  95. };
  96. }