helper.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <algorithm>
20 #include <cctype>
21 #include <fstream>
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 #ifdef BAZEL_BUILD
27 #include "examples/protos/route_guide.grpc.pb.h"
28 #else
29 #include "route_guide.grpc.pb.h"
30 #endif
31 
32 namespace routeguide {
33 
34 std::string GetDbFileContent(int argc, char** argv) {
35  std::string db_path;
36  std::string arg_str("--db_path");
37  if (argc > 1) {
38  std::string argv_1 = argv[1];
39  size_t start_position = argv_1.find(arg_str);
40  if (start_position != std::string::npos) {
41  start_position += arg_str.size();
42  if (argv_1[start_position] == ' ' || argv_1[start_position] == '=') {
43  db_path = argv_1.substr(start_position + 1);
44  }
45  }
46  } else {
47 #ifdef BAZEL_BUILD
48  db_path = "cpp/route_guide/route_guide_db.json";
49 #else
50  db_path = "route_guide_db.json";
51 #endif
52  }
53  std::ifstream db_file(db_path);
54  if (!db_file.is_open()) {
55  std::cout << "Failed to open " << db_path << std::endl;
56  abort();
57  }
58  std::stringstream db;
59  db << db_file.rdbuf();
60  return db.str();
61 }
62 
63 // A simple parser for the json db file. It requires the db file to have the
64 // exact form of [{"location": { "latitude": 123, "longitude": 456}, "name":
65 // "the name can be empty" }, { ... } ... The spaces will be stripped.
66 class Parser {
67  public:
68  explicit Parser(const std::string& db) : db_(db) {
69  // Remove all spaces.
70  db_.erase(std::remove_if(db_.begin(), db_.end(), isspace), db_.end());
71  if (!Match("[")) {
73  }
74  }
75 
76  bool Finished() { return current_ >= db_.size(); }
77 
78  bool TryParseOne(Feature* feature) {
79  if (failed_ || Finished() || !Match("{")) {
80  return SetFailedAndReturnFalse();
81  }
82  if (!Match(location_) || !Match("{") || !Match(latitude_)) {
83  return SetFailedAndReturnFalse();
84  }
85  long temp = 0;
86  ReadLong(&temp);
87  feature->mutable_location()->set_latitude(temp);
88  if (!Match(",") || !Match(longitude_)) {
89  return SetFailedAndReturnFalse();
90  }
91  ReadLong(&temp);
92  feature->mutable_location()->set_longitude(temp);
93  if (!Match("},") || !Match(name_) || !Match("\"")) {
94  return SetFailedAndReturnFalse();
95  }
96  size_t name_start = current_;
97  while (current_ != db_.size() && db_[current_++] != '"') {
98  }
99  if (current_ == db_.size()) {
100  return SetFailedAndReturnFalse();
101  }
102  feature->set_name(db_.substr(name_start, current_ - name_start - 1));
103  if (!Match("},")) {
104  if (db_[current_ - 1] == ']' && current_ == db_.size()) {
105  return true;
106  }
107  return SetFailedAndReturnFalse();
108  }
109  return true;
110  }
111 
112  private:
114  failed_ = true;
115  return false;
116  }
117 
118  bool Match(const std::string& prefix) {
119  bool eq = db_.substr(current_, prefix.size()) == prefix;
120  current_ += prefix.size();
121  return eq;
122  }
123 
124  void ReadLong(long* l) {
125  size_t start = current_;
126  while (current_ != db_.size() && db_[current_] != ',' &&
127  db_[current_] != '}') {
128  current_++;
129  }
130  // It will throw an exception if fails.
131  *l = std::stol(db_.substr(start, current_ - start));
132  }
133 
134  bool failed_ = false;
136  size_t current_ = 0;
137  const std::string location_ = "\"location\":";
138  const std::string latitude_ = "\"latitude\":";
139  const std::string longitude_ = "\"longitude\":";
140  const std::string name_ = "\"name\":";
141 };
142 
143 void ParseDb(const std::string& db, std::vector<Feature>* feature_list) {
144  feature_list->clear();
145  std::string db_content(db);
146  db_content.erase(
147  std::remove_if(db_content.begin(), db_content.end(), isspace),
148  db_content.end());
149 
150  Parser parser(db_content);
151  Feature feature;
152  while (!parser.Finished()) {
153  feature_list->push_back(Feature());
154  if (!parser.TryParseOne(&feature_list->back())) {
155  std::cout << "Error parsing the db file";
156  feature_list->clear();
157  break;
158  }
159  }
160  std::cout << "DB parsed, loaded " << feature_list->size() << " features."
161  << std::endl;
162 }
163 
164 } // namespace routeguide
fix_build_deps.temp
temp
Definition: fix_build_deps.py:488
routeguide::Parser::failed_
bool failed_
Definition: helper.cc:134
routeguide::Parser::Match
bool Match(const std::string &prefix)
Definition: helper.cc:118
routeguide::Parser::longitude_
const std::string longitude_
Definition: helper.cc:139
routeguide::Parser::db_
std::string db_
Definition: helper.cc:135
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
routeguide::Parser::Parser
Parser(const std::string &db)
Definition: helper.cc:68
routeguide::Parser::name_
const std::string name_
Definition: helper.cc:140
routeguide::Parser::current_
size_t current_
Definition: helper.cc:136
routeguide::GetDbFileContent
std::string GetDbFileContent(int argc, char **argv)
Definition: helper.cc:34
routeguide::ParseDb
void ParseDb(const std::string &db, std::vector< Feature > *feature_list)
Definition: helper.cc:143
start
static uint64_t start
Definition: benchmark-pound.c:74
asyncio_get_stats.parser
parser
Definition: asyncio_get_stats.py:34
routeguide::Parser::ReadLong
void ReadLong(long *l)
Definition: helper.cc:124
routeguide::Parser::SetFailedAndReturnFalse
bool SetFailedAndReturnFalse()
Definition: helper.cc:113
routeguide::Parser::TryParseOne
bool TryParseOne(Feature *feature)
Definition: helper.cc:78
routeguide
Definition: helper.cc:32
routeguide::Parser::latitude_
const std::string latitude_
Definition: helper.cc:138
routeguide::Parser
Definition: helper.cc:66
routeguide::Parser::Finished
bool Finished()
Definition: helper.cc:76
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
route_guide_pb2.Feature
Feature
Definition: multiplex/route_guide_pb2.py:256
routeguide::Parser::location_
const std::string location_
Definition: helper.cc:137


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:12