cppcms::json::value JsonUtils::getJsonElements(MapString& paramsMap) { cppcms::json::value params; for(MapString::iterator p = paramsMap.begin(); p != paramsMap.end(); p++) { params.at(p->first, p->second); } return params; }
bool PConvert::get_map_integer( MapString &map, const std::string &key , int &val ) { MapString::iterator it = map.find( key ) ; if ( it == map.end() ) { return false ; } val = atoi( it->second.c_str() ) ; return true ; }
bool PConvert::get_map_string( MapString &map, const std::string &key , std::string &val ) { MapString::iterator it = map.find( key ) ; if ( it == map.end() ) { return false ; } val = it->second ; return true ; }
cppcms::json::value JsonUtils::getJsonElement(std::string tag, MapString& paramsMap) { cppcms::json::value element; cppcms::json::value params; for(MapString::iterator p = paramsMap.begin(); p != paramsMap.end(); p++) { params.at(p->first, p->second); } element.at("tag", tag); element.at("params", params); return element; }
bool PConvert::split2map( const std::string &s , MapString &val ) { vector<string> vec ; // 处理所有逗号分割处理 if ( ! splitvector( s , vec, "," , 0 ) ) { return false ; } string temp ; size_t pos = 0 , end = 0 ; // 解析参数 for ( pos = 0 ; pos < vec.size(); ++ pos ) { temp = vec[pos] ; end = temp.find( ":" ) ; if ( end == string::npos ) { continue ; } val.insert( pair<string,string>( temp.substr(0,end), temp.substr( end+1 ) ) ) ; } // 解析出监控平台参数部分 return ( ! val.empty() ) ; }
void clearTags() { mUserMapLanguage.clear(); }
std::string replaceTags(const std::string& _data) { // вот хз, что быстрее, итераторы или математика указателей, // дл¤ непон¤тно какого размера одного символа UTF8 std::string line(_data); std::string::iterator end = line.end(); for (std::string::iterator iter=line.begin(); iter!=end; ) { if (*iter == '#') { ++iter; if (iter == end) { return line; } else { if (*iter != '{') { ++iter; continue; } std::string::iterator iter2 = iter; ++iter2; while (true) { if (iter2 == end) { return line; } if (*iter2 == '}') { size_t start = iter - line.begin(); size_t len = (iter2 - line.begin()) - start - 1; const std::string & tag = line.substr(start + 1, len); MapString::iterator replace = mUserMapLanguage.find(tag); replace != mUserMapLanguage.end(); iter = line.erase(iter - size_t(1), iter2 + size_t(1)); size_t pos = iter - line.begin(); if (replace != mUserMapLanguage.end()) { line.insert(pos, replace->second); iter = line.begin() + pos + replace->second.length(); } end = line.end(); if (iter == end) { return line; } break; } ++iter2; }; } } else { ++iter; } } return line; }
namespace wrapper { typedef std::map<std::string, std::string> MapString; MapString mUserMapLanguage; std::string replaceTags(const std::string& _data) { // вот хз, что быстрее, итераторы или математика указателей, // дл¤ непон¤тно какого размера одного символа UTF8 std::string line(_data); std::string::iterator end = line.end(); for (std::string::iterator iter=line.begin(); iter!=end; ) { if (*iter == '#') { ++iter; if (iter == end) { return line; } else { if (*iter != '{') { ++iter; continue; } std::string::iterator iter2 = iter; ++iter2; while (true) { if (iter2 == end) { return line; } if (*iter2 == '}') { size_t start = iter - line.begin(); size_t len = (iter2 - line.begin()) - start - 1; const std::string & tag = line.substr(start + 1, len); MapString::iterator replace = mUserMapLanguage.find(tag); replace != mUserMapLanguage.end(); iter = line.erase(iter - size_t(1), iter2 + size_t(1)); size_t pos = iter - line.begin(); if (replace != mUserMapLanguage.end()) { line.insert(pos, replace->second); iter = line.begin() + pos + replace->second.length(); } end = line.end(); if (iter == end) { return line; } break; } ++iter2; }; } } else { ++iter; } } return line; } void clearTags() { mUserMapLanguage.clear(); } void addTag(const std::string& _tag, const std::string& _data) { mUserMapLanguage[_tag] = _data; } //--------------------------------------------------------------------------------------// // фабрика, создает тип по айди класса и своему айди //--------------------------------------------------------------------------------------// Member* getByRef(const std::string& _compound, const std::string& _member) { xml::ElementPtr element = nullptr; xml::Document doc; const std::string filename = "doxygen/xml/" + _compound + ".xml"; if ( !doc.open(filename) ) { std::cout << doc.getLastError() << std::endl; return nullptr; } xml::ElementEnumerator compound = doc.getRoot()->getElementEnumerator(); while (compound.next("compounddef")) { if (compound->findAttribute("id") != _compound) continue; xml::ElementEnumerator sectiondef = compound->getElementEnumerator(); while (sectiondef.next("sectiondef")) { xml::ElementEnumerator memberdef = sectiondef->getElementEnumerator(); while (memberdef.next("memberdef")) { if (memberdef->findAttribute("id") != _member) continue; element = memberdef.current(); break; } } } if (element == nullptr) { std::cout << "'" << _member << "' not found in '" << _compound << "'" << std::endl; return nullptr; } // а вот тут типа фабрика std::string kind = element->findAttribute("kind"); if (kind == "function") return new MemberFunction(element); else if (kind == "variable") return new MemberVariable(element); return new Member(element); } std::string correctPlatformType(const std::string& _namespace, const std::string& _type) { std::string std_token = (_type.size() > 5) ? _type.substr(0, 5) : ""; if (std_token == "std::" || _type == "int" || _type == "unsigned int" || _type == "short" || _type == "unsigned short" || _type == "char" || _type == "unsigned char" || _type == "size_t" || _type == "float" ) return _type; return _namespace + "::" + _type; } //--------------------------------------------------------------------------------------// // возвращает полное им¤ у типов, которые пр¤чутьс¤ за тайпдифом //--------------------------------------------------------------------------------------// std::string getTypedef(const std::string& _type, Compound * _root) { TypeInfo type(_type); Compound::Enumerator enumerator = _root->getEnumerator(); while (enumerator.next()) { if (enumerator->getType() != "compound" || enumerator->getKind() != "namespace") continue; Compound::Enumerator enumerator2 = enumerator->getEnumerator(); while (enumerator2.next()) { if (enumerator2->getKind() != "typedef" || enumerator2->getName() != type.getType()) continue; Member * member = getByRef(enumerator->getId(), enumerator2->getId()); type.setOnlyType(correctPlatformType(enumerator->getName(), member->getType())); return type.toString(); } } return _type; } //--------------------------------------------------------------------------------------// // возвращает полное им¤ у типов, если они наход¤тс¤ в неймспейсе //--------------------------------------------------------------------------------------// std::string getTypeNamespace(const std::string& _type, Compound * _root, const std::string& _namespace) { TypeInfo type(_type); type.setOnlyType(_namespace + "::" + type.getType()); Compound::Enumerator enumerator = _root->getEnumerator(); while (enumerator.next()) { if (enumerator->getType() != "compound" || enumerator->getName() != type.getType()) continue; return type.toString(); } return _type; } //--------------------------------------------------------------------------------------// // возвращает полное им¤ у типов, которые пр¤чутьс¤ за тайпдифом или в наход¤тс¤ в неймспейсе //--------------------------------------------------------------------------------------// std::string getFullDefinition(const std::string& _type, Compound * _root, const std::string& _namespace) { std::string type = getTypedef(_type, _root); if (type == _type) { type = getTypeNamespace(type, _root, _namespace); } // обрабатываем вложенные тайпдифы size_t start = type.find_first_of("<"); size_t end = type.find_last_of(">"); if (start != std::string::npos && end != std::string::npos && start < end) { std::string inner_type = type.substr(start + 1, end - start - 1); std::vector<std::string> inner_types = utility::split(inner_type, ","); size_t count = inner_types.size(); if (count != 0) { for (size_t index=0; index<count; ++index) { utility::trim(inner_types[index]); inner_types[index] = " " + getFullDefinition(inner_types[index], _root, _namespace) + " "; } if (count == 1) { inner_type = inner_types[0]; } else { inner_type.clear(); for (size_t index=0; index<count-1; ++index) { inner_type += inner_types[index] + " , "; } inner_type += inner_types[count-1]; } type.erase(start + 1, end - start - 1); type.insert(type.begin() + start + 1, inner_type.begin(), inner_type.end()); } } /*if (type == "Message::ViewInfo") { int test = 0; }*/ return type; } //--------------------------------------------------------------------------------------// // возвращает информацию по типу и по имени //--------------------------------------------------------------------------------------// Compound* getCompound(const std::string& _kind, const std::string& _name, Compound * _root) { Compound::Enumerator enumerator = _root->getEnumerator(); while (enumerator.next()) { if (enumerator->getType() != "compound" || enumerator->getKind() != _kind || enumerator->getName() != _name) continue; return enumerator.current(); } return nullptr; } std::vector<std::string> split_params(const std::string& _name) { std::vector<std::string> vec; size_t cov = 0; size_t start = 0; for (size_t index=0; index<_name.size(); ++index) { if (_name[index] == '<') { cov++; } else if (_name[index] == '>') { if (cov == 0) { vec.clear(); break; } cov--; } else if (_name[index] == ',') { if (cov == 0) { vec.push_back(_name.substr(start, index - start - 1)); utility::trim(vec.back()); start = index + 1; } } } if (cov == 0) { vec.push_back(_name.substr(start)); utility::trim(vec.back()); } else { vec.clear(); } return vec; } } // namespace wrapper
// 将GPS数据转成GNSS bool PConvert::convert_gps_info( MapString &mp, GnssData &gps ) { if ( mp.empty() ) return false ; gps.state = 0x00 ; gps.alarm = 0x00 ; int nval = 0 ; if ( get_map_integer( mp, "1", nval ) ) { nval = nval * 10 / 6 ; } // 处理经度不在中国范围内 619066885 if ( nval < 72000000 || nval > 140000000 ){ // 经度范围72-136 OUT_ERROR( NULL, 0, NULL, "error lon %u", nval ) ; return false ; } gps.lon = ntouv32( nval ) ; if ( get_map_integer( mp, "2", nval ) ) { nval = nval * 10 / 6 ; } // 处理纬度不在中国范围内 if ( nval < 18000000 || nval > 55000000 ) { // 纬度范围18-54 OUT_ERROR( NULL, 0, NULL, "error lat %u", nval ) ; return false ; } gps.lat = ntouv32( nval ) ; if ( get_map_integer( mp, "3", nval ) ) { nval = nval/10 ; // 速度808中为1/10km/h } // 处理速度不正确 if ( nval > 220 ) { // 220km/h OUT_ERROR( NULL, 0, NULL, "error speed %u", gps.vec1 ) ; return false ; } gps.vec1 = ntouv16( nval ) ; if ( get_map_integer( mp, "7", nval ) ) { nval = nval/10 ; // 行驶记录仪速度808中为1/10km/h } gps.vec2 = ntouv16( nval ) ; if ( gps.vec2 == 0 ) { gps.vec2 = gps.vec1 ; } string sval ; if ( ! get_map_string( mp, "4", sval ) ) { OUT_ERROR( NULL, 0, NULL, "error time empty" ) ; // 如果没有时间就直接返回了 return false ; } int nyear = 0 , nmonth = 0 , nday = 0 , nhour = 0 ,nmin = 0 , nsec = 0 ; sscanf( sval.c_str(), "%04d%02d%02d/%02d%02d%02d", &nyear, &nmonth, &nday, &nhour, &nmin, &nsec ) ; // 检测时间是否正确 if ( ! checktime( nyear, nmonth, nday, nhour, nmin, nsec ) ) { OUT_ERROR( NULL, 0, NULL, "error time %s", sval.c_str() ) ; return false ; } gps.date[3] = nyear % 256 ; gps.date[2] = nyear / 256 ; gps.date[1] = nmonth ; gps.date[0] = nday ; gps.time[0] = nhour ; gps.time[1] = nmin ; gps.time[2] = nsec ; if ( get_map_integer( mp, "5", nval ) ){ gps.direction = ntouv16( nval ) ; } // 方向在0-360度之间 if ( nval > 360 ) { OUT_ERROR( NULL,0, NULL, "error direction %d", nval ) ; return false ; } if ( get_map_integer( mp, "15", nval ) ) { gps.state = (nval) ? gps.state | 0x02 : gps.state ; } // 报警 if ( get_map_integer( mp, "20" , nval ) ) { gps.alarm = nval ; } if ( get_map_integer( mp, "9" , nval ) ) { gps.vec3 = ntouv32( nval / 10 ); } // 状态 if ( get_map_integer( mp, "8" , nval ) ) { gps.state = nval ; } gps.state = ntouv32( gps.state ) ; gps.alarm = ntouv32( gps.alarm ) ; return true ; }
size_t operator()(const MapString& __t) const noexcept { if (!__t.length()) return 0; return std::hash<std::string_view>()(std::string_view(__t)); }
explicit MapString(const MapString& rval) : alloc(rval.alloc ? new std::string(*rval.alloc) : NULL), str(alloc ? alloc->data() : rval.data(), rval.length()) { }
bool operator==(const MapString& rval) const { if (length() != rval.length()) return false; if (length() == 0) return true; return fastcmp<strncmp>(data(), rval.data(), length()) == 0; }