OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include <algorithm> |
| 35 #include <cctype> |
| 36 #include <fstream> |
| 37 #include <iostream> |
| 38 #include <sstream> |
| 39 #include <string> |
| 40 #include <vector> |
| 41 #include "route_guide.grpc.pb.h" |
| 42 |
| 43 namespace routeguide { |
| 44 |
| 45 std::string GetDbFileContent(int argc, char** argv) { |
| 46 std::string db_path; |
| 47 std::string arg_str("--db_path"); |
| 48 if (argc > 1) { |
| 49 std::string argv_1 = argv[1]; |
| 50 size_t start_position = argv_1.find(arg_str); |
| 51 if (start_position != std::string::npos) { |
| 52 start_position += arg_str.size(); |
| 53 if (argv_1[start_position] == ' ' || |
| 54 argv_1[start_position] == '=') { |
| 55 db_path = argv_1.substr(start_position + 1); |
| 56 } |
| 57 } |
| 58 } else { |
| 59 db_path = "route_guide_db.json"; |
| 60 } |
| 61 std::ifstream db_file(db_path); |
| 62 if (!db_file.is_open()) { |
| 63 std::cout << "Failed to open " << db_path << std::endl; |
| 64 return ""; |
| 65 } |
| 66 std::stringstream db; |
| 67 db << db_file.rdbuf(); |
| 68 return db.str(); |
| 69 } |
| 70 |
| 71 // A simple parser for the json db file. It requires the db file to have the |
| 72 // exact form of [{"location": { "latitude": 123, "longitude": 456}, "name": |
| 73 // "the name can be empty" }, { ... } ... The spaces will be stripped. |
| 74 class Parser { |
| 75 public: |
| 76 explicit Parser(const std::string& db) : db_(db) { |
| 77 // Remove all spaces. |
| 78 db_.erase( |
| 79 std::remove_if(db_.begin(), db_.end(), isspace), |
| 80 db_.end()); |
| 81 if (!Match("[")) { |
| 82 SetFailedAndReturnFalse(); |
| 83 } |
| 84 } |
| 85 |
| 86 bool Finished() { |
| 87 return current_ >= db_.size(); |
| 88 } |
| 89 |
| 90 bool TryParseOne(Feature* feature) { |
| 91 if (failed_ || Finished() || !Match("{")) { |
| 92 return SetFailedAndReturnFalse(); |
| 93 } |
| 94 if (!Match(location_) || !Match("{") || !Match(latitude_)) { |
| 95 return SetFailedAndReturnFalse(); |
| 96 } |
| 97 long temp = 0; |
| 98 ReadLong(&temp); |
| 99 feature->mutable_location()->set_latitude(temp); |
| 100 if (!Match(",") || !Match(longitude_)) { |
| 101 return SetFailedAndReturnFalse(); |
| 102 } |
| 103 ReadLong(&temp); |
| 104 feature->mutable_location()->set_longitude(temp); |
| 105 if (!Match("},") || !Match(name_) || !Match("\"")) { |
| 106 return SetFailedAndReturnFalse(); |
| 107 } |
| 108 size_t name_start = current_; |
| 109 while (current_ != db_.size() && db_[current_++] != '"') { |
| 110 } |
| 111 if (current_ == db_.size()) { |
| 112 return SetFailedAndReturnFalse(); |
| 113 } |
| 114 feature->set_name(db_.substr(name_start, current_-name_start-1)); |
| 115 if (!Match("},")) { |
| 116 if (db_[current_ - 1] == ']' && current_ == db_.size()) { |
| 117 return true; |
| 118 } |
| 119 return SetFailedAndReturnFalse(); |
| 120 } |
| 121 return true; |
| 122 } |
| 123 |
| 124 private: |
| 125 |
| 126 bool SetFailedAndReturnFalse() { |
| 127 failed_ = true; |
| 128 return false; |
| 129 } |
| 130 |
| 131 bool Match(const std::string& prefix) { |
| 132 bool eq = db_.substr(current_, prefix.size()) == prefix; |
| 133 current_ += prefix.size(); |
| 134 return eq; |
| 135 } |
| 136 |
| 137 void ReadLong(long* l) { |
| 138 size_t start = current_; |
| 139 while (current_ != db_.size() && db_[current_] != ',' && db_[current_] != '}
') { |
| 140 current_++; |
| 141 } |
| 142 // It will throw an exception if fails. |
| 143 *l = std::stol(db_.substr(start, current_ - start)); |
| 144 } |
| 145 |
| 146 bool failed_ = false; |
| 147 std::string db_; |
| 148 size_t current_ = 0; |
| 149 const std::string location_ = "\"location\":"; |
| 150 const std::string latitude_ = "\"latitude\":"; |
| 151 const std::string longitude_ = "\"longitude\":"; |
| 152 const std::string name_ = "\"name\":"; |
| 153 }; |
| 154 |
| 155 void ParseDb(const std::string& db, std::vector<Feature>* feature_list) { |
| 156 feature_list->clear(); |
| 157 std::string db_content(db); |
| 158 db_content.erase( |
| 159 std::remove_if(db_content.begin(), db_content.end(), isspace), |
| 160 db_content.end()); |
| 161 |
| 162 Parser parser(db_content); |
| 163 Feature feature; |
| 164 while (!parser.Finished()) { |
| 165 feature_list->push_back(Feature()); |
| 166 if (!parser.TryParseOne(&feature_list->back())) { |
| 167 std::cout << "Error parsing the db file"; |
| 168 feature_list->clear(); |
| 169 break; |
| 170 } |
| 171 } |
| 172 std::cout << "DB parsed, loaded " << feature_list->size() |
| 173 << " features." << std::endl; |
| 174 } |
| 175 |
| 176 |
| 177 } // namespace routeguide |
| 178 |
OLD | NEW |