| 153 | } |
| 154 | |
| 155 | std::map<std::string, int32_t> json_parse(const std::string & fname) { |
| 156 | std::map<std::string, int32_t> result; |
| 157 | |
| 158 | // read file into string |
| 159 | std::string json; |
| 160 | { |
| 161 | std::ifstream ifs(fname); |
| 162 | if (!ifs) { |
| 163 | fprintf(stderr, "Failed to open %s\n", fname.c_str()); |
| 164 | exit(1); |
| 165 | } |
| 166 | |
| 167 | json = std::string((std::istreambuf_iterator<char>(ifs)), |
| 168 | (std::istreambuf_iterator<char>())); |
| 169 | } |
| 170 | |
| 171 | if (json[0] != '{') { |
| 172 | return result; |
| 173 | } |
| 174 | |
| 175 | // parse json |
| 176 | { |
| 177 | bool has_key = false; |
| 178 | bool in_token = false; |
| 179 | |
| 180 | std::string str_key = ""; |
| 181 | std::string str_val = ""; |
| 182 | |
| 183 | int n = json.size(); |
| 184 | for (int i = 1; i < n; ++i) { |
| 185 | if (!in_token) { |
| 186 | if (json[i] == ' ') continue; |
| 187 | if (json[i] == '"') { |
| 188 | in_token = true; |
| 189 | continue; |
| 190 | } |
| 191 | } else { |
| 192 | if (json[i] == '\\' && i+1 < n) { |
| 193 | if (has_key == false) { |
| 194 | str_key += json[i]; |
| 195 | } else { |
| 196 | str_val += json[i]; |
| 197 | } |
| 198 | ++i; |
| 199 | } else if (json[i] == '"') { |
| 200 | if (has_key == false) { |
| 201 | has_key = true; |
| 202 | ++i; |
| 203 | while (json[i] == ' ') ++i; |
| 204 | ++i; // : |
| 205 | while (json[i] == ' ') ++i; |
| 206 | if (json[i] != '\"') { |
| 207 | while (json[i] != ',' && json[i] != '}') { |
| 208 | str_val += json[i++]; |
| 209 | } |
| 210 | has_key = false; |
| 211 | } else { |
| 212 | in_token = true; |
no test coverage detected