📄 mpcommon.cxx
字号:
return false; } //======================================================================================= //ParmValContainer implementation //--------------------------------------------------------------------------------------- ParmValContainer::ParmValContainer(const ParmValContainer &pvc) : pvc_Separator(pvc.pvc_Separator), pvc_Prefix(pvc.pvc_Prefix), pvc_Between(pvc.pvc_Between), pvc_Postfix(pvc.pvc_Postfix), pvc_CaseFlag(pvc.pvc_CaseFlag) { pvc_Array = pvc.pvc_Array; } //--------------------------------------------------------------------------------------- ParmValContainer & ParmValContainer::operator = (const ParmValContainer &pvc) { if(&pvc != this) { pvc_Separator = pvc.pvc_Separator; pvc_Prefix = pvc.pvc_Prefix; pvc_Between = pvc.pvc_Between; pvc_Postfix = pvc.pvc_Postfix; pvc_CaseFlag = pvc.pvc_CaseFlag; pvc_Array.clear(); pvc_Array = pvc.pvc_Array; } return *this; } //--------------------------------------------------------------------------------------- ParmVal & ParmValContainer::insertBefore(unsigned index, const std::mstring & name) { ParmValArray::iterator it; if(index >= pvc_Array.size()) it = pvc_Array.end(); else it = &pvc_Array[index]; return *(pvc_Array.insert(it, ParmVal(name, ""))); } //--------------------------------------------------------------------------------------- ParmVal & ParmValContainer::operator [] (unsigned index) { while(index >= pvc_Array.size()) { pvc_Array.push_back(ParmVal()); } return pvc_Array[index]; } const ParmVal & ParmValContainer::operator [] (unsigned index) const { if(index >= pvc_Array.size()) { throw MpAccessError("ParmValContainer: Out of index"); } return pvc_Array[index]; } //--------------------------------------------------------------------------------------- ParmVal & ParmValContainer::operator [] (const std::string &name) { ParmValArray::iterator i; for(i = pvc_Array.begin(); i != pvc_Array.end(); ++i) { if(i->chk_name(name, pvc_CaseFlag)) return *i; } pvc_Array.push_back(ParmVal(name, std::mstring())); return pvc_Array[pvc_Array.size() - 1]; } //--------------------------------------------------------------------------------------- bool ParmValContainer::empty() const { for(ParmValArray::const_iterator i = pvc_Array.begin(); i != pvc_Array.end(); ++i) { if(!i->encode(pvc_Separator).empty()) return false; } return true; } //--------------------------------------------------------------------------------------- std::mstring ParmValContainer::encode() const { std::mstring ret; std::mstring ts; if(!empty()) { if(pvc_Prefix) ret += pvc_Prefix; ParmValArray::const_iterator i; for(i = pvc_Array.begin(); i != pvc_Array.end(); ++i) { ts = i->encode(pvc_Separator); if(!ts.empty()) { ret += ts; if((i + 1) != pvc_Array.end() && pvc_Between) ret += pvc_Between; } } if(pvc_Postfix) ret += pvc_Postfix; } return ret; } //--------------------------------------------------------------------------------------- void ParmValContainer::decode(const std::mstring &str) { pvc_Array.clear(); std::string::size_type start = 0; std::mstring t_str; ParmVal t_pv; std::string sep; //The first token must be prefix + between if(pvc_Prefix) sep += pvc_Prefix; if(pvc_Between) sep += pvc_Between; if(!sep.empty()) { if((start = toktrim(str, &t_str, start, sep)) != std::string::npos) { if(!t_str.empty()) { t_pv.decode(t_str, pvc_Separator); (*this)[t_pv.parm()] = t_pv; } } //Other tokens must be between + postfix sep = ""; if(pvc_Between) sep += pvc_Between; if(pvc_Postfix) sep += pvc_Postfix; if(!sep.empty()) { while((start = toktrim(str, &t_str, start, sep)) != std::string::npos) { if(!t_str.empty()) { t_pv.decode(t_str, pvc_Separator); (*this)[t_pv.parm()] = t_pv; } } } } } //======================================================================================= //ValAssistent implementation //--------------------------------------------------------------------------------------- void ValAssistant::check(const char *item, const char *name, const std::mstring &val) { unsigned i; if(val.empty()) return; for(i = 0; vaRestrict[i].item || vaRestrict[i].name; i++) { if(strcmp(item, vaRestrict[i].item) == 0 && strcmp(name, vaRestrict[i].name) == 0) { check(vaRestrict[i], val); break; } } } //--------------------------------------------------------------------------------------- const char * ValAssistant::getDefaultVal(const char *item, const char *name, const std::mstring &val) { unsigned i; for(i = 0; vaRestrict[i].item || vaRestrict[i].name; i++) { if(item == vaRestrict[i].item && name == vaRestrict[i].name) { return vaRestrict[i].def_val; } } return ""; } //--------------------------------------------------------------------------------------- void ValAssistant::throwChar(const char * name, const std::mstring &val) { throw MpSyntaxError(std::string("ValAssistant: Parameter '") + name + "' has an illegal charater: " + val); } //--------------------------------------------------------------------------------------- void ValAssistant::check(const ValRestriction & restrict, const std::mstring &val) { double dval; std::mstring::const_iterator it; std::string::size_type start = 0; std::mstring value; std::mstring ts; bool flag; switch(restrict.cat) { case vcAny: //Any value break; case vcNumeric: //Numeric value, check range if(restrict.min != 0.0 || restrict.max != 0.0) { dval = double(val); if(dval < restrict.min || dval > restrict.max) { throw MpSyntaxError(std::string("ValAssistant: Value '") + restrict.name + "' is out of range: " + val); } } case vcDigits: //0...9, without checking range for(it = val.begin(); it != val.end(); ++it) { if(!isdigit(*it) && strchr(restrict.value, *it) == 0) { throwChar(restrict.name, val); } } break; case vcHex: //Hexdecimal value, without checking range for(it = val.begin(); it != val.end(); ++it) { if(!isxdigit(*it) && strchr(restrict.value, *it) == 0) { throwChar(restrict.name, val); } } break; case vcAlpha: //a...z, A...Z for(it = val.begin(); it != val.end(); ++it) { if(!isalpha(*it) && strchr(restrict.value, *it) == 0) { throwChar(restrict.name, val); } } break; case vcAlNum: //a...z, A...Z, 0...9 for(it = val.begin(); it != val.end(); ++it) { if(!isalnum(*it) && strchr(restrict.value, *it) == 0) { throwChar(restrict.name, val); } } break; case vcList: //List of values, separated by '|' start = 0; value = restrict.value; flag = false; while((start = value.token(&ts, start, "|", "", 0)) != std::string::npos) { if(restrict.case_flag) { if(val.compare(ts) == 0) { flag = true; break; } } else { if(val.comparei(ts) == 0) { flag = true; break; } } } if(!flag) { throw MpSyntaxError(std::string("ValAssistant: Parameter '") + restrict.name + "' has an illegal value: " + val); } break; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -