input.cc 915 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "input.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string>
  6. #include <optional>
  7. std::string ReadFile(const std::string &filepath) {
  8. std::ifstream file(filepath, std::ios::binary | std::ios::ate);
  9. std::streamsize size = file.tellg();
  10. file.seekg(0, std::ios::beg);
  11. std::string buffer(size, '\0');
  12. file.read(buffer.data(), size);
  13. return buffer;
  14. }
  15. std::optional<std::string_view> Lines::Next() {
  16. if (text_.empty()) {
  17. return std::nullopt;
  18. }
  19. for (size_t i = 0; i < text_.size(); ++i) {
  20. if (text_[i] == '\n') {
  21. std::string_view result(text_.data(), i);
  22. if (text_.size() > i + 1) {
  23. text_ = std::string_view(text_.data() + i + 1, text_.size() - i - 1);
  24. } else {
  25. text_ = std::string_view();
  26. }
  27. return result;
  28. }
  29. }
  30. std::string_view last_result = text_;
  31. text_ = std::string_view();
  32. return last_result;
  33. }