handlemsg.cpp
来自「液晶电视完整代码可实现人机界面」· C++ 代码 · 共 2,266 行 · 第 1/4 页
CPP
2,266 行
// 需要从属性中获得 int untrimtype = 0; // 对齐方式:0-左, 1-右 char untrimcode = ' '; // 补齐字符 if(len==0) //如果长度等于0,不再做长度检查,直接输出内部流该item的内容 { if ( !format->GetTagAttri( XML_FUNC , func ) ) { ContentFunc( func, value, value ); } len = strlen(value); outputResult = Output( value, len ); } else { if ( len < (int)strlen(buf) ) { buf[len] = 0; } else if ( len > (int)strlen(buf) ) { // 用指定的字符将字符串填充到指定长度 UnTrim( buf, len, untrimtype, untrimcode ); } if ( !format->GetTagAttri( XML_FUNC , func ) ) { ContentFunc( func, buf, buf ); len = strlen(buf); } outputResult = Output(buf, len ); } // 向输出流输出长为 len 的字符串 if ( outputResult ) { ThrowMsgException( GENERR , "Internal error: outdatalen is more than outputsize" ); } Log( LOGSTEP, "---the output at the end of item ", output ) ;}void HandleData::PackDeliData( CXMLParser *format , char *referstr, int DeliPos, char DeliAsc ){ char node_name[NODELEN + 1]; char rightnode[NODELEN + 1]; char fmt_currnode[NODELEN + 1]; int Ret = 0; char buf[M_TMPBUF_LEN + 1]; int iRepeat= 0; char refer[(MAXDEPTH) * (NODELEN + 1) + 1]; int deli_pos; char deli_asc; strcpy( refer, referstr ); memset( node_name , 0 , NODELEN + 1 ); Ret = format->GetTagAttri( XML_F_NAME , node_name ); if ( !Ret ) {// 注:根节点没有名字 Log( LOGSTEP, "---group ", node_name ) ; strcat( refer, "." ); strcat( refer, node_name ); } // 取分割类型(1-前/0-后) Ret = format->GetTagAttri( M_XML_DELIMITER_POS , buf ); if ( Ret ) { if ( DeliPos == M_NO_POS_VALUE ) ThrowMsgException( GENERR , "Without delimiter position!" ); else deli_pos = DeliPos; } else { if ( strncmp ( buf, XML_FRONT, sizeof(XML_FRONT) ) == 0 ) deli_pos = M_POS_FRONT_VALUE; else if ( strncmp ( buf, XML_BACK, sizeof(XML_BACK) ) == 0 ) deli_pos = M_POS_BACK_VALUE; else { ThrowMsgException( GENERR , "Unknown delimiter position!" ); } } // 取分割符并转换为字符 // 取分割符的 ASCII 码 Ret = format->GetTagAttri( M_XML_DELIMITER_ASC , buf ); if ( Ret ) { if ( DeliAsc == NOASC ) ThrowMsgException( GENERR , "Without delimiter !" ); else deli_asc = DeliAsc; } else { deli_asc = (char)atoi( buf ); } Ret = format->GetTagAttri( XML_GROUP_ROWS , buf ); if ( Ret ) iRepeat = 1; else { if ( SelfData( buf ) ) { char value[M_TMPBUF_LEN + 1]; if ( GetSelfData( buf, value ) ) { iRepeat = atoi( value ); } } else { iRepeat = atoi( buf ); } } for ( int loop=0; loop<iRepeat; loop++ ) { memset( fmt_currnode , 0 , NODELEN + 1 ); Ret = format->GetChildNode( fmt_currnode ); while( Ret == 0 ) { // 若分割类型为"前"则输出分割符 if ( deli_pos == M_POS_FRONT_VALUE) { Output( deli_asc ); } // 域 if( strncmp( fmt_currnode , ITEM , sizeof(ITEM) ) == 0 ) { // Modified by HCH -- 2002.04.08 // PackDeliItem( format , refer ) ; PackDeliItem( format , refer, loop ) ; // 若分割类型为"后"则输出分割符 if ( deli_pos == M_POS_BACK_VALUE ) { Output( deli_asc ); } } // 组 或 组合域 else if( strncmp( fmt_currnode , GROUP , sizeof(GROUP) ) == 0 ) { // 若存在其他类型则调用同一打包函数 if ( format->GetTagAttri( M_XML_DELIMITER_TYPE , buf ) == 0 ) PackGroupData( buf, format, refer ); else PackDeliData( format, refer, deli_pos, deli_asc ); Log( LOGSTEP, "---the output at the end of group ", output ) ; } // 格式或内容检查函数 else if( strncmp( fmt_currnode , XML_CHECK , sizeof(XML_CHECK) ) == 0 ) { char func[M_MAX_ITEMCONTENT_LEN + 1]; if ( !format->GetTagAttri( XML_FUNC , func ) ) { Log(LOGSTEP, "Now execute check function ", func); CheckFunc( func ); } } else { Log( LOGPROMPT, "---无效的节点:", fmt_currnode ) ; } memset( rightnode , 0 , NODELEN + 1 ); Ret = format->GetRightNode( fmt_currnode , rightnode ); memset( fmt_currnode , 0 , NODELEN + 1); strcpy( fmt_currnode , rightnode ); } format->GetParentNode(); }}// Modified by HCH -- 2002.04.08// void HandleData::PackDeliItem( CXMLParser *format , char *referstr )void HandleData::PackDeliItem( CXMLParser *format , char *referstr, int iSeqNo){ int len=0; char node_name[NODELEN + 1]; char isneeded[NODELEN + 1]; char buf[M_MAX_ITEMCONTENT_LEN + 1]; char func[M_MAX_ITEMCONTENT_LEN + 1]; char tmpstr[MSGLEN + 1]; char value[M_MAX_ITEMCONTENT_LEN + 1]; char refer[(MAXDEPTH) * (NODELEN + 1) + 1]; if ( format->GetTagAttri( XML_F_NAME , node_name ) ){ sprintf( tmpstr, " Find item name failed!" ); ThrowMsgException( GENERR , tmpstr ); } if (iSeqNo != 0) sprintf( refer, "%s.%s%d.", referstr, node_name, iSeqNo); else sprintf( refer, "%s.%s.", referstr, node_name ); Log( LOGSTEP, "---item ", refer ) ; if ( format->GetTagAttri( XML_ITEM_LENGTH , buf ) == 0 ) { // 要求定长 len = atoi( buf ); if ( len > M_MAX_ITEMCONTENT_LEN ) { Log(LOGPROMPT, "Data too large, cut it:", node_name ); len = M_MAX_ITEMCONTENT_LEN; } } else { len = 0; } if ( format->GetTagAttri( XML_ITEM_VALUE , buf ) ) { // 无value说明,需要引用内部数据流数据 if ( GetSelfData( refer, value ) ) { memcpy( buf, value, M_MAX_ITEMCONTENT_LEN ); } else { sprintf( tmpstr, "[%s -- %d]item %s : Can't get internal data!", __FILE__, __LINE__, node_name ); ThrowMsgException( GENERR , tmpstr ); } } else { if ( SelfData( buf ) ) { // 指明引用内部数据流数据 if ( GetSelfData( buf, value ) ) { memcpy( buf, value, M_MAX_ITEMCONTENT_LEN ); } else { sprintf( tmpstr, "[%s -- %d]item %s : Can't get internal data!", __FILE__, __LINE__, node_name ); ThrowMsgException( GENERR , tmpstr ); } } } // value 指出的普通ascii字符串直接引用 format->GetTagAttri( XML_ITEM_NEED , isneeded ); if ( len ) // 要求定长, 需要补齐或截断 { // 需要从属性中获得 int untrimtype = 1; // 对齐方式:0-左, 1-右 char untrimcode = ' '; // 补齐字符 if ( len < (int)strlen(buf) ) { buf[len] = 0; } else if ( len > (int)strlen(buf) ) { UnTrim( buf, len, untrimtype, untrimcode ); } } else len = (int)strlen(buf); if ( !format->GetTagAttri( XML_FUNC , func ) ) { ContentFunc( func, buf, buf ); len = strlen(buf); } if ( Output( buf, len ) ) { ThrowMsgException( GENERR , "Internal error: outdatalen is more than outputsize" ); } Log( LOGSTEP, "---the output at the end of item ", output ) ; }// =======================================================================void HandleData::PackTagData( CXMLParser *format , char *referstr){ char node_name[NODELEN + 1]; char rightnode[NODELEN + 1]; char fmt_currnode[NODELEN + 1]; int Ret = 0; char buf[M_TMPBUF_LEN + 1]; int iRepeat= 0; char refer[(MAXDEPTH) * (NODELEN + 1) + 1]; int iTagPos; strcpy( refer, referstr ); memset( node_name , 0 , NODELEN + 1 ); Ret = format->GetTagAttri( XML_F_NAME , node_name ); if ( !Ret ) { // 注:根节点没有名字 Log( LOGSTEP, "---group ", node_name ) ; strcat( refer, "." ); strcat( refer, node_name ); } // 取 TAG 的位置(1-前/0-后) Ret = format->GetTagAttri(M_XML_TAG_POS, buf); if ( Ret ) { printf("[%s - %d]\n", __FILE__, __LINE__); ThrowMsgException( GENERR , "Without tag position!" ); } else { if ( strncmp(buf, XML_FRONT, sizeof(XML_FRONT) ) == 0 ) iTagPos = M_POS_FRONT_VALUE; else if ( strncmp ( buf, XML_BACK, sizeof(XML_BACK) ) == 0 ) iTagPos = M_POS_BACK_VALUE; else ThrowMsgException( GENERR , "Unknown tag position!" ); } // 取重复次数 Ret = format->GetTagAttri( XML_GROUP_ROWS , buf ); if ( Ret ) iRepeat = 1; else { if ( SelfData( buf ) ) { char value[M_TMPBUF_LEN + 1]; if ( GetSelfData( buf, value ) ) { iRepeat = atoi( value ); } } else { iRepeat = atoi( buf ); } } for ( int loop=0; loop<iRepeat; loop++ ) { memset( fmt_currnode , 0 , NODELEN + 1 ); Ret = format->GetChildNode( fmt_currnode ); while( Ret == 0 ) { if( strncmp( fmt_currnode , ITEM , sizeof(ITEM) ) == 0 ) { // Modified by HCH -- 2002.04.08 // PackTagItem(format, refer, iTagPos); PackTagItem(format, refer, iTagPos, loop); } // 组 或 组合域 else if( strncmp( fmt_currnode , GROUP , sizeof(GROUP) ) == 0 ) { // 若存在其他类型则调用同一打包函数 if ( format->GetTagAttri( M_XML_DELIMITER_TYPE , buf ) == 0 ) PackGroupData( buf, format, refer ); else PackTagData( format, refer); Log( LOGSTEP, "---the output at the end of group ", output ) ; } // 格式或内容检查函数 else if( strncmp( fmt_currnode , XML_CHECK , sizeof(XML_CHECK) ) == 0 ) { char func[M_MAX_ITEMCONTENT_LEN + 1]; if ( !format->GetTagAttri( XML_FUNC , func ) ) { Log(LOGSTEP, "Now execute check function ", func); CheckFunc( func ); } } else { Log( LOGPROMPT, "---无效的节点:", fmt_currnode ) ; } memset( rightnode , 0 , NODELEN + 1 ); Ret = format->GetRightNode( fmt_currnode , rightnode ); memset( fmt_currnode , 0 , NODELEN + 1); strcpy( fmt_currnode , rightnode ); } format->GetParentNode(); }}void HandleData::PackTagItem(CXMLParser *format, char *referstr, int iTagPos, int iSeqNo){ int len=0; char node_name[NODELEN + 1]; char isneeded[NODELEN + 1]; char buf[M_MAX_ITEMCONTENT_LEN + 1]; char func[M_MAX_ITEMCONTENT_LEN + 1]; char tmpstr[MSGLEN + 1]; char value[M_MAX_ITEMCONTENT_LEN + 1]; char refer[(MAXDEPTH) * (NODELEN + 1) + 1]; char strTagName[M_MAX_TAGNAME_LEN + 1]; char strTag[M_MAX_TAGNAME_LEN + 3]; int iTagLen; int iNeedFlag; // Get item name if ( format->GetTagAttri( XML_F_NAME , node_name ) ) { sprintf( tmpstr, " Find item name failed!" ); ThrowMsgException( GENERR , tmpstr ); } if (iSeqNo != 0) sprintf( refer, "%s.%s%d.", referstr, node_name, iSeqNo); else sprintf( refer, "%s.%s.", referstr, node_name ); Log( LOGSTEP, "---item ", refer ) ; iNeedFlag = 1; if ( format->GetTagAttri(XML_ITEM_NEED, isneeded ) == 0 ) { if ( strncmp(isneeded, UNNECESSARY, sizeof(UNNECESSARY) ) == 0 ) iNeedFlag = 0; } memset(strTagName, 0, sizeof(strTagName)); // Get TAG name if ( format->GetTagAttri(M_XML_TAG_NAME, strTagName) ) { if (iNeedFlag) { sprintf(tmpstr, " Search %s's tag name failed!", node_name ); ThrowMsgException( GENERR , tmpstr ); } else return; } Log( LOGSTEP, "\t---TAG: ", node_name ) ; iTagLen = strlen(strTagName); sprintf(strTag, ":%s:", strTagName); if ( format->GetTagAttri( XML_ITEM_LENGTH , buf ) == 0 ) { // 要求定长 len = atoi( buf ); if ( len > M_MAX_ITEMCONTENT_LEN ) { Log(LOGPROMPT, "Data too large, cut it:", node_name ); len = M_MAX_ITEMCONTENT_LEN; } } else { len = 0; } if ( format->GetTagAttri( XML_ITEM_VALUE , buf ) ) { // 无value说明,需要引用内部数据流数据 if ( GetSelfData( refer, value ) ){ memcpy( buf, value, M_MAX_ITEMCONTENT_LEN ); } else { sprintf( tmpstr, "[%s -- %d]item %s : Can't get internal data!", __FILE__, __LINE__, node_name ); ThrowMsgException( GENERR , tmpstr ); } } else { if ( SelfData( buf ) ) { // 指明引用内部数据流数据 if ( GetSelfData( buf, value ) ){ memcpy( buf, value, M_MAX_ITEMCONTENT_LEN ); } else { sprintf( tmpstr, "[%s -- %d]item %s : Can't get internal data!", __FILE__, __LINE__, node_name ); ThrowMsgException( GENERR , tmpstr ); } } } // value 指出的普通ascii字符串直接引用 if ( len ) { // 要求定长, 需要补齐或截断 // 需要从属性中获得 int untrimtype = 1; // 对齐方式:0-左, 1-右 char untrimcode = ' '; // 补齐字符 if ( len < (int)strlen(buf) ) { buf[len] = 0; } else if ( len > (int)strlen(buf) ) { UnTrim( buf, len, untrimtype, untrimcode ); } } else len = (int)strlen(buf); if ( !format->GetTagAttri( XML_FUNC , func ) ) { ContentFunc( func, buf, buf ); len = strlen(buf); } // 若为可选项,且内容为空则忽略该tag if ( iNeedFlag == 0 && SPACESTR(buf) ) { return ; } // 输出 TAG if (iTagPos == M_POS_FRONT_VALUE) { Output(strTag, iTagLen + 2); } // 输出数据域 if ( Output( buf, len ) ) { ThrowMsgException( GENERR , "Internal error: outdatalen is more than outputsize" ); } if (iTagPos == M_POS_BACK_VALUE) { Output(strTag, iTagLen + 2); } Log( LOGSTEP, "---the output at the end of item ", output ) ;}void GetErrMsg(int iErrCode, char *strErrMsg){ int idx; for(idx=0; ; idx++) { if (ErrorCode[idx].iErrCode == 9999) { strcpy(strErrMsg, "Unknow Error"); return; } else if (ErrorCode[idx].iErrCode == iErrCode) { strcpy(strErrMsg, ErrorCode[idx].strErrMsg); return; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?