/*------------------------------------------------------------------- Function: fetchData(pos, p_data) Purpose: fetch data Parameters: pos -- [IN], position in json array p_data -- [OUT], result return: 0 -- success -1 -- failed -------------------------------------------------------------------*/ int CEhJsonReader::fetchData(int pos, void* p_data) { cJSON* json_item = NULL; cJSON* json_temp = NULL; TB001_DEVOBJ* p_devObj = (TB001_DEVOBJ*)p_data; if (pos < 0) return -1; if (pos >= getDataCount()) return -1; json_item = cJSON_GetArrayItem(m_jsonTransData, pos); if (!json_item) return -1; //id json_temp = cJSON_GetObjectItem(json_item, JSON_ADDR_ID); if (!json_temp) return -1; p_devObj->iID = json_temp->valueint; //description json_temp = cJSON_GetObjectItem(json_item, JSON_ADDR_DESC); if (!json_temp) return -1; strcpy(p_devObj->szDesc, json_temp->valuestring); //data type json_temp = cJSON_GetObjectItem(json_item, JSON_ADDR_DATATYPE); if (!json_temp) return -1; p_devObj->iDataType = getDataTypeFromStr(json_temp->valuestring); //protocol p_devObj->iProtocolID = 1; //group addr json_temp = cJSON_GetObjectItem(json_item, JSON_ADDR_GROUPADDR); if (!json_temp) return -1; strcpy(p_devObj->szGroupAddr, json_temp->valuestring); //status group addr json_temp = cJSON_GetObjectItem(json_item, JSON_ADDR_STATUS_GROUPADDR); if (!json_temp) return -1; strcpy(p_devObj->szGroupAddr2, json_temp->valuestring); //value strcpy(p_devObj->szValue, "0"); return 0; }
std::vector< std::pair<double,double> > ImageHistogram<T>::getData() const { int dataCount = getDataCount(); std::vector< std::pair<double,double> > data( dataCount ); for ( int i = 0; i < dataCount; i++ ) { data[i] = std::pair<double,double>(m_xValues[i],m_yValues[i]); } return data; }
int priorProbability(Data_t *dataset, double datacount, double *retval, int datatype, int value) { int ret = SUCCEED; double count = 0.0; ret = getDataCount(dataset, datatype, &count, value); *retval = count / datacount; return ret; }
int minorProbability(Data_t *dataset, double *retval, int chdatatype, int chdatavalue, int datatype, int value) { int ret = SUCCEED; double count = 0.0; double countClass = 0.0; ret = getDataCount(dataset, chdatatype, &count, chdatavalue); ret = getDataCountForClass(dataset, datatype, &countClass, value, chdatavalue); *retval = countClass / count; return ret; }
static Template parse(const dotX39::Node& node, const std::vector<Ressource>& ressourceList) { auto tmp = Template(); tmp.qualifier = node.getName(); #pragma region Argument Parsing for (int i = 0; i < node.getArgumentCount(); i++) { const dotX39::Data* arg = node.getArgument(i); if (arg->getName().compare("path") == 0) { if (arg->getType() != dotX39::DataTypes::STRING) { std::cerr << std::string("the nodes '").append(tmp.qualifier).append("' 'path' argument should be STRING") << std::endl; throw Exceptions::InvalidTypeStructureException(); } tmp.filePath = std::string(Globals::getInstance().basePath.templates).append(static_cast<const dotX39::DataString*>(arg)->getDataAsString()); } else if (arg->getName().compare("fileName") == 0) { if (arg->getType() != dotX39::DataTypes::STRING) { std::cerr << std::string("the nodes '").append(tmp.qualifier).append("' 'fileName' argument should be STRING") << std::endl; throw Exceptions::InvalidTypeStructureException(); } tmp.outputFileName = std::string(static_cast<const dotX39::DataString*>(arg)->getDataAsString()); } else if (arg->getName().compare("fileExtension") == 0) { if (arg->getType() != dotX39::DataTypes::STRING) { std::cerr << std::string("the nodes '").append(tmp.qualifier).append("' 'fileExtension' argument should be STRING") << std::endl; throw Exceptions::InvalidTypeStructureException(); } tmp.outputFileExtension = std::string(static_cast<const dotX39::DataString*>(arg)->getDataAsString()); } else { if (Globals::getInstance().verbosity) std::cout << std::string("the node '").append(tmp.qualifier).append("' contains the unknown argument '").append(arg->getName()).append("', ignored.") << std::endl; } } #pragma endregion #pragma region Data Parsing for (int i = 0; i < node.getDataCount(); i++) { const dotX39::Data* data = node.getData(i); if (data->getName().compare("ressources") == 0) { if (data->getType() != dotX39::DataTypes::ARRAY) { std::cerr << std::string("the nodes '").append(tmp.qualifier).append("' 'ressources' argument should be ARRAY") << std::endl; throw Exceptions::InvalidTypeStructureException(); } auto tmpDataArray = static_cast<const dotX39::DataArray*>(data); //We want to validate ALL ressources here so lets create an error counter instead of throwing an exception instantly size_t errCount = 0; for (int j = 0; j < tmpDataArray->getDataCount(); j++) { auto data = tmpDataArray->getDataElement(j); if (data->getType() != dotX39::DataTypes::STRING) { std::cerr << std::string("the nodes '").append(tmp.qualifier).append("' 'ressource' argument should be ARRAY with just STRINGs") << std::endl; throw Exceptions::InvalidTypeStructureException(); } //Find the ressource in available ressources and push it to the templates ressource list bool flag = false; auto ressourceName = static_cast<const dotX39::DataString*>(data)->getDataAsString(); for (auto& it : ressourceList) { if (it.getQualifier().compare(ressourceName) == 0) { tmp.ressources.push_back(it); flag = true; break; } } if (!flag) { errCount++; std::cerr << "Ressource '" << ressourceName << "' is not existing (template: '" << tmp.qualifier << "')" << std::endl; } } //if we did found errors, throw the invalid_argument exception if (errCount > 0) throw std::invalid_argument("Some Ressources are not existing for a Template"); } else { if (Globals::getInstance().verbosity) std::cout << std::string("the node '").append(tmp.qualifier).append("' contains the unknown argument '").append(data->getName()).append("', ignored.") << std::endl; } } #pragma endregion #pragma region Node Parsing for (int i = 0; i < node.getNodeCount(); i++) { auto subnode = node.getNode(i); if (subnode->getName().compare("templateKeywords") == 0) { for (int j = 0; j < subnode->getDataCount(); j++) { auto data = subnode->getData(j); if (data->getType() != dotX39::DataTypes::STRING) { std::cerr << std::string("the nodes '").append(tmp.qualifier).append("' subnodes '").append(subnode->getName()).append("' data datatypes should be STRING") << std::endl; throw Exceptions::InvalidTypeStructureException(); } auto templateKeyword = TemplateKeyword::parse(*static_cast<const dotX39::DataString*>(data)); tmp.keywords.templates.push_back(templateKeyword); } } else if (subnode->getName().compare("replacementKeywords") == 0) { for (int j = 0; j < subnode->getDataCount(); j++) { auto data = subnode->getData(j); if (data->getType() != dotX39::DataTypes::ARRAY) { std::cerr << std::string("the nodes '").append(tmp.qualifier).append("' subnodes '").append(subnode->getName()).append("' data datatypes should be ARRAY") << std::endl; throw Exceptions::InvalidTypeStructureException(); } tmp.keywords.replacements.push_back(ReplacementKeyword::parse(*static_cast<const dotX39::DataArray*>(data))); } } else { if (Globals::getInstance().verbosity) std::cout << std::string("the node '").append(tmp.qualifier).append("' contains the unknown node '").append(subnode->getName()).append("', ignored.") << std::endl; } } #pragma endregion if (tmp.filePath.empty()) { std::cerr << std::string("the node '").append(tmp.getQualifier()).append("' has no path argument provided") << std::endl; exit(-1); } std::ifstream stream = std::ifstream(std::string(tmp.filePath).c_str()); if (!stream.is_open() || !stream.good()) { auto errMsg = std::string("Could not read file: ").append(tmp.filePath); std::cerr << errMsg << std::endl; throw std::runtime_error(errMsg); } char s[256]; while (!stream.eof()) { memset(s, 0, sizeof(s)); stream.read(s, 255); tmp.fileData.append(s); } stream.close(); return tmp; }