📄 attrdict.cpp
字号:
#include <stdio.h>#include <string.h>#include "AttrDict.h"#include "Logger.h"//AttrMap AttrDict::_attributes=(Attribute*)NULL;//initialize static varible outside constructionbool AttrDict::_initialized=false; //it will be initialized when program starts AttrMap AttrDict::_attributes;AttrDict::AttrDict(){}AttrDict::~AttrDict(){ //NOTE: have to delete elements in _attributes AttrMap::iterator iter = _attributes.begin(); while(iter != _attributes.end()) { delete iter->second; iter->second=NULL; iter++; } //NOTE: have to clear _attributes.clear(); }int AttrDict::init(char* dictFile){ if(!_initialized) return parseDict(dictFile); return 0;}Attribute* AttrDict::getAttrByName(String name){ //cwz note: iterator is a poiter that point to one attribute AttrMap::iterator itor; for(itor=_attributes.begin(); itor!=_attributes.end(); itor++) { if((itor->second)->getName() == name) return itor->second; } return (Attribute*)NULL;}Attribute* AttrDict::getAttrByAttr(Octet attr){ AttrMap::iterator itor; itor=_attributes.find(attr); if(itor != _attributes.end()) return itor->second; return (Attribute*)NULL;}int AttrDict::parseDict(char* fileName){ FILE* fp; char s_attribute[30]; char s_attrName[30]; char s_attr[30]; char s_attrValue[30]; char buffer[2048]; Octet attr; VType valueType; if((fp=fopen(fileName, "r")) == NULL) { Logger::log(L_ERR, "parseDict:Open attribute dict file error!"); return -1; } int lineNo=0; while(fgets(buffer, sizeof(buffer), fp) != NULL) { lineNo++; //skip extract simbol if(*buffer == '#' || *buffer == '\0' || *buffer == '\n') continue; if((sscanf(buffer, "%s%s%s%s", s_attribute, s_attrName, s_attr, s_attrValue)) < 4) { Logger::log(L_ERR, "%s: Invalid attribute on line %d of dictionary\n", fileName, lineNo); return -1; } //sscanf(s_attr, "%i", &attr); attr=atoi(s_attr); if(attr > 255) { Logger::log(L_ERR, "%s: Attribute %s > 255 on line %d of dictionary\n", fileName, s_attrName, lineNo); return -1; } if((strcmp(s_attrValue, "String")) == 0) valueType=VString; else if((strcmp(s_attrValue, "Octet")) == 0) valueType=VOctet; else if((strcmp(s_attrValue, "Octets")) == 0) valueType=VOctets; else if((strcmp(s_attrValue, "Integer")) == 0) valueType=VUInt; else if((strcmp(s_attrValue, "Short")) == 0) valueType=VUShort; //new pair Attribute* attribute=new Attribute(attr, s_attrName, valueType); //help me ??? _attributes.insert(make_pair(attr, attribute)); } fclose(fp); _initialized=true; return 0;}//testint AttrDict::attrPrint(){ AttrMap::iterator itor; //test /* if(_attributes.begin() == _attributes.end()) return 0; else return 1; */ //end test for(itor=_attributes.begin(); itor!=_attributes.end(); itor++) { Attribute* attribute=(*itor).second; String name=attribute->getName(); } /* itor = _attributes.find(5); printf("The name of 5 element is %s\n", ((itor->second)->getName()).c_str()); */ return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -