parsestream.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "stringstream.h"
  5. #include "../sys/filename.h"
  6. #include "../math/vec2.h"
  7. #include "../math/vec3.h"
  8. #include "../math/col3.h"
  9. #include "../math/color.h"
  10. namespace embree
  11. {
  12. /*! helper class for simple command line parsing */
  13. class ParseStream : public Stream<std::string>
  14. {
  15. public:
  16. ParseStream (const Ref<Stream<std::string> >& cin) : cin(cin) {}
  17. ParseStream (const Ref<Stream<int> >& cin, const std::string& seps = "\n\t\r ",
  18. const std::string& endl = "", bool multiLine = false)
  19. : cin(new StringStream(cin,seps,endl,multiLine)) {}
  20. public:
  21. ParseLocation location() { return cin->loc(); }
  22. std::string next() { return cin->get(); }
  23. void force(const std::string& next) {
  24. std::string token = getString();
  25. if (token != next)
  26. THROW_RUNTIME_ERROR("token \""+next+"\" expected but token \""+token+"\" found");
  27. }
  28. std::string getString() {
  29. return get();
  30. }
  31. FileName getFileName() {
  32. return FileName(get());
  33. }
  34. int getInt () {
  35. return atoi(get().c_str());
  36. }
  37. Vec2i getVec2i() {
  38. int x = atoi(get().c_str());
  39. int y = atoi(get().c_str());
  40. return Vec2i(x,y);
  41. }
  42. Vec3ia getVec3ia() {
  43. int x = atoi(get().c_str());
  44. int y = atoi(get().c_str());
  45. int z = atoi(get().c_str());
  46. return Vec3ia(x,y,z);
  47. }
  48. float getFloat() {
  49. return (float)atof(get().c_str());
  50. }
  51. Vec2f getVec2f() {
  52. float x = (float)atof(get().c_str());
  53. float y = (float)atof(get().c_str());
  54. return Vec2f(x,y);
  55. }
  56. Vec3f getVec3f() {
  57. float x = (float)atof(get().c_str());
  58. float y = (float)atof(get().c_str());
  59. float z = (float)atof(get().c_str());
  60. return Vec3f(x,y,z);
  61. }
  62. Vec3fa getVec3fa() {
  63. float x = (float)atof(get().c_str());
  64. float y = (float)atof(get().c_str());
  65. float z = (float)atof(get().c_str());
  66. return Vec3fa(x,y,z);
  67. }
  68. Col3f getCol3f() {
  69. float x = (float)atof(get().c_str());
  70. float y = (float)atof(get().c_str());
  71. float z = (float)atof(get().c_str());
  72. return Col3f(x,y,z);
  73. }
  74. Color getColor() {
  75. float r = (float)atof(get().c_str());
  76. float g = (float)atof(get().c_str());
  77. float b = (float)atof(get().c_str());
  78. return Color(r,g,b);
  79. }
  80. private:
  81. Ref<Stream<std::string> > cin;
  82. };
  83. }