nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
text.cc
Go to the documentation of this file.
1#include "content/text.h"
2
3#include <fstream>
4#include <sstream>
5#include <string>
6#include <string_view>
7
8#include "absl/strings/str_cat.h"
9#include "google/protobuf/io/tokenizer.h"
10#include "google/protobuf/text_format.h"
11
12namespace nanommo {
13
14namespace {
15
16class Errors : public google::protobuf::io::ErrorCollector {
17 public:
18 void RecordError(int line, google::protobuf::io::ColumnNumber /*column*/,
19 absl::string_view message) override {
20 // The parser counts lines from zero, and editors count them from one.
21 absl::StrAppend(&text_, "line ", line + 1, ": ", message);
22 }
23
24 const std::string& text() const { return text_; }
25
26 private:
27 std::string text_;
28};
29
30} // namespace
31
32absl::Status ParseText(std::string_view text,
33 google::protobuf::Message& message) {
34 Errors errors;
35 google::protobuf::TextFormat::Parser parser;
36 parser.RecordErrorsTo(&errors);
37 if (!parser.ParseFromString(text, &message)) {
38 return absl::InvalidArgumentError(errors.text());
39 }
40 return absl::OkStatus();
41}
42
43absl::StatusOr<std::string> ReadFile(const std::string& path) {
44 std::ifstream file(path);
45 if (!file) {
46 return absl::NotFoundError(absl::StrCat("couldn't read ", path));
47 }
48 std::ostringstream text;
49 text << file.rdbuf();
50 return text.str();
51}
52
53} // namespace nanommo
Definition chat.cc:9
absl::Status ParseText(std::string_view text, google::protobuf::Message &message)
Definition text.cc:32
absl::StatusOr< std::string > ReadFile(const std::string &path)
Definition text.cc:43