helper.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include <algorithm>
  19. #include <cctype>
  20. #include <fstream>
  21. #include <iostream>
  22. #include <sstream>
  23. #include <string>
  24. #include <vector>
  25. #ifdef BAZEL_BUILD
  26. #include "examples/protos/route_guide.grpc.pb.h"
  27. #else
  28. #include "route_guide.grpc.pb.h"
  29. #endif
  30. namespace routeguide {
  31. std::string GetDbFileContent(int argc, char** argv) {
  32. std::string db_path;
  33. std::string arg_str("--db_path");
  34. if (argc > 1) {
  35. std::string argv_1 = argv[1];
  36. size_t start_position = argv_1.find(arg_str);
  37. if (start_position != std::string::npos) {
  38. start_position += arg_str.size();
  39. if (argv_1[start_position] == ' ' || argv_1[start_position] == '=') {
  40. db_path = argv_1.substr(start_position + 1);
  41. }
  42. }
  43. } else {
  44. #ifdef BAZEL_BUILD
  45. db_path = "cpp/route_guide/route_guide_db.json";
  46. #else
  47. db_path = "route_guide_db.json";
  48. #endif
  49. }
  50. std::ifstream db_file(db_path);
  51. if (!db_file.is_open()) {
  52. std::cout << "Failed to open " << db_path << std::endl;
  53. return "";
  54. }
  55. std::stringstream db;
  56. db << db_file.rdbuf();
  57. return db.str();
  58. }
  59. // A simple parser for the json db file. It requires the db file to have the
  60. // exact form of [{"location": { "latitude": 123, "longitude": 456}, "name":
  61. // "the name can be empty" }, { ... } ... The spaces will be stripped.
  62. class Parser {
  63. public:
  64. explicit Parser(const std::string& db) : db_(db) {
  65. // Remove all spaces.
  66. db_.erase(std::remove_if(db_.begin(), db_.end(), isspace), db_.end());
  67. if (!Match("[")) {
  68. SetFailedAndReturnFalse();
  69. }
  70. }
  71. bool Finished() { return current_ >= db_.size(); }
  72. bool TryParseOne(Feature* feature) {
  73. if (failed_ || Finished() || !Match("{")) {
  74. return SetFailedAndReturnFalse();
  75. }
  76. if (!Match(location_) || !Match("{") || !Match(latitude_)) {
  77. return SetFailedAndReturnFalse();
  78. }
  79. long temp = 0;
  80. ReadLong(&temp);
  81. feature->mutable_location()->set_latitude(temp);
  82. if (!Match(",") || !Match(longitude_)) {
  83. return SetFailedAndReturnFalse();
  84. }
  85. ReadLong(&temp);
  86. feature->mutable_location()->set_longitude(temp);
  87. if (!Match("},") || !Match(name_) || !Match("\"")) {
  88. return SetFailedAndReturnFalse();
  89. }
  90. size_t name_start = current_;
  91. while (current_ != db_.size() && db_[current_++] != '"') {
  92. }
  93. if (current_ == db_.size()) {
  94. return SetFailedAndReturnFalse();
  95. }
  96. feature->set_name(db_.substr(name_start, current_ - name_start - 1));
  97. if (!Match("},")) {
  98. if (db_[current_ - 1] == ']' && current_ == db_.size()) {
  99. return true;
  100. }
  101. return SetFailedAndReturnFalse();
  102. }
  103. return true;
  104. }
  105. private:
  106. bool SetFailedAndReturnFalse() {
  107. failed_ = true;
  108. return false;
  109. }
  110. bool Match(const std::string& prefix) {
  111. bool eq = db_.substr(current_, prefix.size()) == prefix;
  112. current_ += prefix.size();
  113. return eq;
  114. }
  115. void ReadLong(long* l) {
  116. size_t start = current_;
  117. while (current_ != db_.size() && db_[current_] != ',' &&
  118. db_[current_] != '}') {
  119. current_++;
  120. }
  121. // It will throw an exception if fails.
  122. *l = std::stol(db_.substr(start, current_ - start));
  123. }
  124. bool failed_ = false;
  125. std::string db_;
  126. size_t current_ = 0;
  127. const std::string location_ = "\"location\":";
  128. const std::string latitude_ = "\"latitude\":";
  129. const std::string longitude_ = "\"longitude\":";
  130. const std::string name_ = "\"name\":";
  131. };
  132. void ParseDb(const std::string& db, std::vector<Feature>* feature_list) {
  133. feature_list->clear();
  134. std::string db_content(db);
  135. db_content.erase(
  136. std::remove_if(db_content.begin(), db_content.end(), isspace),
  137. db_content.end());
  138. Parser parser(db_content);
  139. Feature feature;
  140. while (!parser.Finished()) {
  141. feature_list->push_back(Feature());
  142. if (!parser.TryParseOne(&feature_list->back())) {
  143. std::cout << "Error parsing the db file";
  144. feature_list->clear();
  145. break;
  146. }
  147. }
  148. std::cout << "DB parsed, loaded " << feature_list->size() << " features."
  149. << std::endl;
  150. }
  151. } // namespace routeguide