|
1 | 1 | #include "imports/ArduinoJson7/ArduinoJson.hpp" |
2 | 2 |
|
| 3 | +#include <algorithm> |
| 4 | + |
3 | 5 | #include "api.hpp" |
4 | 6 | #include "cryptolens_internals.hpp" |
5 | 7 | #include "LicenseKeyInformation.hpp" |
@@ -351,6 +353,73 @@ ResponseParser_ArduinoJson7::parse_last_message_response(basic_Error & e, std::s |
351 | 353 | return ""; |
352 | 354 | } |
353 | 355 |
|
| 356 | +bool |
| 357 | +ResponseParser_ArduinoJson7::has_template_feature(basic_Error & err, std::string const& features_json, std::string const& feature) const |
| 358 | +{ |
| 359 | + if (err) { return false; } |
| 360 | + |
| 361 | + using namespace ::cryptolens_io::latest::errors; |
| 362 | + using namespace ::ArduinoJson; |
| 363 | + api::main api; |
| 364 | + |
| 365 | + JsonDocument doc; |
| 366 | + DeserializationError json_error = deserializeJson(doc, features_json); |
| 367 | + if (json_error) { err.set(api, Subsystem::Json); return false; } |
| 368 | + |
| 369 | + if (!doc.is<JsonArray>()) { err.set(api, Subsystem::Json); return false; } |
| 370 | + |
| 371 | + JsonArray j = doc.as<JsonArray>(); |
| 372 | + |
| 373 | + using string_const_iterator = std::string::const_iterator; |
| 374 | + |
| 375 | + string_const_iterator p = feature.cbegin(); |
| 376 | + string_const_iterator const e = feature.cend(); |
| 377 | + while (p != e && !j.isNull()) { |
| 378 | + string_const_iterator q = std::find(p, e, '.'); |
| 379 | + |
| 380 | + bool found = false; |
| 381 | + for (JsonVariant const elem : j) { |
| 382 | + char const* cc = nullptr; |
| 383 | + JsonArray jj; |
| 384 | + if (elem.is<char const*>()) { |
| 385 | + cc = elem.as<char const*>(); |
| 386 | + } else if (elem.is<JsonArray>()) { |
| 387 | + JsonArray a = elem.as<JsonArray>(); |
| 388 | + |
| 389 | + if (a.size() >= 2 && a[0].is<char const*>() && a[1].is<JsonArray>()) { |
| 390 | + cc = a[0].as<char const*>(); |
| 391 | + jj = a[1].as<JsonArray>();; |
| 392 | + } |
| 393 | + } |
| 394 | + |
| 395 | + if (cc == nullptr) { continue; } |
| 396 | + |
| 397 | + string_const_iterator pp = p; |
| 398 | + while (pp != q && *cc != '\0') { |
| 399 | + if (*pp != *cc) { |
| 400 | + break; |
| 401 | + } |
| 402 | + |
| 403 | + ++pp; |
| 404 | + ++cc; |
| 405 | + } |
| 406 | + |
| 407 | + if (pp == q && *cc == '\0') { |
| 408 | + if (pp != e) { ++pp; } |
| 409 | + |
| 410 | + found = true; |
| 411 | + p = pp; |
| 412 | + j = jj; |
| 413 | + break; |
| 414 | + } |
| 415 | + } |
| 416 | + |
| 417 | + if (!found) { break; } |
| 418 | + } |
| 419 | + |
| 420 | + return p == e; |
| 421 | +} |
| 422 | + |
354 | 423 | } // namespace v20190401 |
355 | 424 |
|
356 | 425 | } // namespace cryptolens_io |
0 commit comments