std::size_t GeoJSONFileParser::operator()() { rapidjson::IStreamWrapper stream_wrapper{m_file}; rapidjson::Document doc; if (doc.ParseStream<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(stream_wrapper).HasParseError()) { error(std::string{"JSON error at offset "} + std::to_string(doc.GetErrorOffset()) + " : " + rapidjson::GetParseError_En(doc.GetParseError())); } if (!doc.IsObject()) { error("Top-level value must be an object."); } const std::string type{get_value_as_string(doc, "type")}; if (type.empty()) { error("Expected 'type' name with the value 'Feature' or 'FeatureCollection'."); } if (type == "Feature") { return parse_top(doc); } if (type == "FeatureCollection") { const auto json_features = doc.FindMember("features"); if (json_features == doc.MemberEnd()) { error("Missing 'features' name."); } if (!json_features->value.IsArray()) { error("Expected 'features' value to be an array."); } const auto json_features_array = json_features->value.GetArray(); if (json_features_array.Empty()) { throw config_error{"Features array must contain at least one polygon."}; } const auto& json_first_feature = json_features_array[0]; if (!json_first_feature.IsObject()) { error("Expected values of 'features' array to be a objects."); } const std::string feature_type{get_value_as_string(json_first_feature, "type")}; if (feature_type != "Feature") { error("Expected 'type' value to be 'Feature'."); } return parse_top(json_first_feature); } error("Expected 'type' value to be 'Feature'."); }
SaErrorT show_Rpt(Rpt_t *Rpt, hpi_ui_print_cb_t proc) { int i = 0; Attributes_t *Attrs; union_type_t val; char buf[1024]; char A[256], *name; SaHpiTextBufferT tbuf; SaErrorT rv; Attrs = &(Rpt->Attrutes); for (;; i++) { name = get_attr_name(Attrs, i); if (name == (char *)NULL) break; if (strcmp(name, "ResCapabilities") == 0) { rv = get_value(Attrs, i, &val); if (rv != SA_OK) continue; rv = oh_decode_capabilities(val.i, &tbuf); if (rv != SA_OK) continue; strncpy(A, tbuf.Data, 256); } else { rv = get_value_as_string(Attrs, i, A, 256); if (rv != SA_OK) continue; }; snprintf(buf, 1024, "%s: %s\n", name, A); if (proc(buf) != 0) return(-1); }; return(SA_OK); }
std::size_t GeoJSONFileParser::parse_top(const rapidjson::Value& top) { const auto json_geometry = top.FindMember("geometry"); if (json_geometry == top.MemberEnd()) { error("Missing 'geometry' name."); } if (!json_geometry->value.IsObject()) { error("Expected 'geometry' value to be an object."); } std::string geometry_type{get_value_as_string(json_geometry->value, "type")}; if (geometry_type.empty()) { error("Missing 'geometry.type'."); } if (geometry_type != "Polygon" && geometry_type != "MultiPolygon") { error("Expected 'geometry.type' value to be 'Polygon' or 'MultiPolygon'."); } const auto json_coordinates = json_geometry->value.FindMember("coordinates"); if (json_coordinates == json_geometry->value.MemberEnd()) { error("Missing 'coordinates' name in 'geometry' object."); } if (!json_coordinates->value.IsArray()) { error("Expected 'geometry.coordinates' value to be an array."); } if (geometry_type == "Polygon") { return parse_polygon_array(json_coordinates->value, m_buffer); } return parse_multipolygon_array(json_coordinates->value, m_buffer); }
SaErrorT get_rpt_attr_as_string(Rpt_t *rpt, char *attr_name, char *val, int len) { int i; SaErrorT ret; if ((attr_name == (char *)NULL) || (val == (char *)NULL) || (len == 0)) return(SA_ERR_HPI_INVALID_PARAMS); i = find_attr(&(rpt->Attrutes), attr_name); if (i < 0) return(SA_ERR_HPI_INVALID_PARAMS); ret = get_value_as_string(&(rpt->Attrutes), i, val, len); return(ret); }
SaErrorT show_Rdr(Rdr_t *Rdr, hpi_ui_print_cb_t proc) { int i = 0; Attributes_t *Attrs; char buf[1024]; char A[256], *name; SaErrorT rv; Attrs = &(Rdr->Attrutes); for (;; i++) { name = get_attr_name(Attrs, i); if (name == (char *)NULL) break; rv = get_value_as_string(Attrs, i, A, 256); if (rv != SA_OK) continue; snprintf(buf, 1024, "%s: %s\n", name, A); if (proc(buf) != 0) return(-1); }; return(SA_OK); }
static Pr_ret_t show_attrs(Attributes_t *Attrs, int delta, hpi_ui_print_cb_t proc) { int i, type, len, del; char tmp[256], *name; char buf[SHOW_BUF_SZ]; union_type_t val; SaErrorT rv; Pr_ret_t ret; memset(buf, ' ', SHOW_BUF_SZ); del = delta << 1; len = SHOW_BUF_SZ - del; for (i = 0; i < Attrs->n_attrs; i++) { name = get_attr_name(Attrs, i); if (name == (char *)NULL) break; rv = get_value(Attrs, i, &val); if (rv != SA_OK) continue; type = get_attr_type(Attrs, i); switch (type) { case NO_TYPE: continue; case STRUCT_TYPE: snprintf(buf + del, len, "%s:\n", name); if (proc(buf) != 0) return(-1); rv = get_value(Attrs, i, &val); if (rv != SA_OK) continue; ret = show_attrs((Attributes_t *)(val.a), delta + 1, proc); if (ret != HPI_UI_OK) return(HPI_UI_END); continue; case LOOKUP_TYPE: strncpy(tmp, lookup_proc(Attrs->Attrs[i].lunum, val.i), 256); break; case DECODE_TYPE: rv = decode_proc(Attrs->Attrs[i].lunum, val.a, tmp, 256); if (rv != SA_OK) continue; break; case DECODE1_TYPE: rv = decode1_proc(Attrs->Attrs[i].lunum, val.i, tmp, 256); if (rv != SA_OK) continue; break; case READING_TYPE: if (thres_value(val.a, tmp, 256) != SA_OK) continue; break; case TEXT_BUFF_TYPE: snprintf(buf + del, len, "%s: ", name); if (proc(buf) != HPI_UI_OK) return(HPI_UI_END); ret = print_text_buffer(NULL, val.a, "\n", proc); if (ret != HPI_UI_OK) return(HPI_UI_END); continue; default: rv = get_value_as_string(Attrs, i, tmp, 256); if (rv != SA_OK) continue; }; snprintf(buf + del, len, "%s: %s\n", name, tmp); if (proc(buf) != HPI_UI_OK) return(HPI_UI_END); }; return(0); }