estring.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "platform.h"
  5. #include "../math/vec2.h"
  6. #include "../math/vec3.h"
  7. #include "../math/vec4.h"
  8. namespace embree
  9. {
  10. class IOStreamStateRestorer
  11. {
  12. public:
  13. IOStreamStateRestorer(std::ostream& iostream)
  14. : iostream(iostream), flags(iostream.flags()), precision(iostream.precision()) {
  15. }
  16. ~IOStreamStateRestorer() {
  17. iostream.flags(flags);
  18. iostream.precision(precision);
  19. }
  20. private:
  21. std::ostream& iostream;
  22. std::ios::fmtflags flags;
  23. std::streamsize precision;
  24. };
  25. struct IndentOStream : public std::streambuf
  26. {
  27. explicit IndentOStream(std::ostream &ostream, int indent = 2)
  28. : streambuf(ostream.rdbuf())
  29. , start_of_line(true)
  30. , ident_str(indent, ' ')
  31. , stream(&ostream)
  32. {
  33. // set streambuf of ostream to this and save original streambuf
  34. stream->rdbuf(this);
  35. }
  36. virtual ~IndentOStream()
  37. {
  38. if (stream != NULL) {
  39. // restore old streambuf
  40. stream->rdbuf(streambuf);
  41. }
  42. }
  43. protected:
  44. virtual int overflow(int ch) {
  45. if (start_of_line && ch != '\n') {
  46. streambuf->sputn(ident_str.data(), ident_str.size());
  47. }
  48. start_of_line = ch == '\n';
  49. return streambuf->sputc(ch);
  50. }
  51. private:
  52. std::streambuf *streambuf;
  53. bool start_of_line;
  54. std::string ident_str;
  55. std::ostream *stream;
  56. };
  57. std::string toLowerCase(const std::string& s);
  58. std::string toUpperCase(const std::string& s);
  59. Vec2f string_to_Vec2f ( std::string str );
  60. Vec3f string_to_Vec3f ( std::string str );
  61. Vec4f string_to_Vec4f ( std::string str );
  62. }