stringstream.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #include "stringstream.h"
  17. namespace embree
  18. {
  19. /* creates map for fast categorization of characters */
  20. static void createCharMap(bool map[256], const std::string& chrs) {
  21. for (size_t i=0; i<256; i++) map[i] = false;
  22. for (size_t i=0; i<chrs.size(); i++) map[uint8_t(chrs[i])] = true;
  23. }
  24. /* simple tokenizer */
  25. StringStream::StringStream(const Ref<Stream<int> >& cin, const std::string& seps, const std::string& endl, bool multiLine)
  26. : cin(cin), endl(endl), multiLine(multiLine)
  27. {
  28. createCharMap(isSepMap,seps);
  29. }
  30. std::string StringStream::next()
  31. {
  32. /* skip separators */
  33. while (cin->peek() != EOF) {
  34. if (endl != "" && cin->peek() == '\n') { cin->drop(); return endl; }
  35. if (multiLine && cin->peek() == '\\') {
  36. cin->drop();
  37. if (cin->peek() == '\n') { cin->drop(); continue; }
  38. cin->unget();
  39. }
  40. if (!isSeparator(cin->peek())) break;
  41. cin->drop();
  42. }
  43. /* parse everything until the next separator */
  44. std::vector<char> str; str.reserve(64);
  45. while (cin->peek() != EOF && !isSeparator(cin->peek()))
  46. str.push_back((char)cin->get());
  47. str.push_back(0);
  48. return std::string(str.data());
  49. }
  50. }