BOOL COneCommand::GetReceivedBuffer(CByteArray &buffer) { BOOL ret;int i,size;BYTE src,dst; if((size=m_ReceiveBuffer.GetSize())!=buffer.GetSize()) { buffer.RemoveAll(); buffer.Append(m_ReceiveBuffer); return TRUE; } else { ret=FALSE; for(i=0;i<size;i++) { src=m_ReceiveBuffer.GetAt(i); dst=buffer.GetAt(i); if(src!=dst) { buffer.SetAt(i,src); ret=TRUE; } } return ret; } }
BOOL CRegistry::SetBinaryValue(LPCTSTR name_of_value, const CByteArray& bytes_to_write) { ASSERT(name_of_value != NULL); if (name_of_value == NULL) { m_ErrorCode = ERROR_INVALID_PARAMETER; return(FALSE); } DWORD size_of_buffer = bytes_to_write.GetSize(); LPBYTE memory_buffer = NULL; try { memory_buffer = new BYTE[ size_of_buffer ]; } catch(...) { memory_buffer = NULL; } if (memory_buffer == NULL) { m_ErrorCode = ::GetLastError(); return(FALSE); } DWORD index = 0; while(index < size_of_buffer) { memory_buffer[ index ] = bytes_to_write.GetAt(index); index++; } BOOL ret = SetValue(name_of_value, typeBinary, memory_buffer, size_of_buffer); delete [] memory_buffer; return(ret); }
/** * Modifies a user param byte array with the ExtParam data passed with * argument pParam. ExtParam data objects will be build from * Ext_User_Prm_Data_Const/Ref fields within the GSD. Extparam data * describes the datatype of the Ext_User_Prm_Data, the offset within * the user param byte array and the value. The passed user param byte * array (see argument UserParam) grows as necessary. * Existing user param bytes within the array will be or-ed with the * new byte value. * * @param CByteArray & UserParam - user param byte array to * modify. The byte array will grow as necessary. * @param ExtParam *pParam - Reference to ExtParam data * object describing the Ext_User_Prm_Data. Shall not * be NULL. * @exception CMemoryException * @see GSD: Ext_User_Prm_Data_Const/Ref */ void ModifyParamArray(CByteArray & UserParam, ExtParam *pParam) { int offset = 0; int sizeArray = 0; int sizeBytes = 0; ASSERT(pParam); switch(pParam->GetParamType()) { case Array: { CByteArray* pBytes = pParam->GetByteArray(); sizeBytes = pBytes->GetSize(); sizeArray = UserParam.GetSize(); offset = pParam->GetByteOffset(); if(offset + sizeBytes > sizeArray) { //grow the array UserParam.SetSize(offset + sizeBytes); BYTE *pb = UserParam.GetData(); //preset array size_t sizeSet = offset + sizeBytes - sizeArray; memset(&pb[sizeArray],0,sizeSet); } for (int x = 0; x < sizeBytes; x++) { BYTE userbyte = UserParam.GetAt(offset); BYTE setbyte = pBytes->GetAt(x); userbyte |= setbyte; UserParam.SetAt(offset++, userbyte); }// next x } break; case Byte: { offset = pParam->GetByteOffset(); sizeArray = UserParam.GetSize(); sizeBytes = sizeof(BYTE); if(offset + sizeBytes > sizeArray) { //grow the array UserParam.SetSize(offset + sizeBytes); BYTE *pb = UserParam.GetData(); //preset array size_t sizeSet = offset + sizeBytes - sizeArray; memset(&pb[sizeArray],0,sizeSet); } BYTE userbyte = UserParam.GetAt(offset); // Get current value in Bytearray if (pParam->IsBitArea()) { userbyte &= ~MakeBitFieldInByte(pParam->GetBitAreaStart(), pParam->GetBitAreaLength()); userbyte |= (BYTE)(pParam->GetUsedValue() << pParam->GetBitAreaStart()); } else { userbyte = (BYTE)pParam->GetUsedValue(); } UserParam.SetAt(offset, userbyte); // Set new value in ByteArray } break; case Word: { BYTE setbyte = 0; WORD data = (WORD)pParam->GetUsedValue(); offset = pParam->GetByteOffset(); sizeArray = UserParam.GetSize(); sizeBytes = sizeof(data); if(offset + sizeBytes > sizeArray) { //grow the array UserParam.SetSize(offset + sizeBytes); BYTE *pb = UserParam.GetData(); //preset array size_t sizeSet = offset + sizeBytes - sizeArray; memset(&pb[sizeArray],0,sizeSet); } for (int i = sizeBytes - 1; i >= 0 ; i--) { BYTE userbyte = UserParam.GetAt(offset); setbyte = (BYTE)((data >> (i*8)) & 0x000000FF); userbyte = setbyte; UserParam.SetAt(offset++, userbyte); } } break; case DWord: { BYTE setbyte = 0; DWORD data = (DWORD)pParam->GetUsedValue(); offset = pParam->GetByteOffset(); sizeArray = UserParam.GetSize(); sizeBytes = sizeof(data); if(offset + sizeBytes > sizeArray) { //grow the array UserParam.SetSize(offset + sizeBytes); BYTE *pb = UserParam.GetData(); //preset array size_t sizeSet = offset + sizeBytes - sizeArray; memset(&pb[sizeArray],0,sizeSet); } for (int i = sizeBytes - 1; i >= 0 ; i--) { BYTE userbyte = UserParam.GetAt(offset); setbyte = (BYTE)((data >> (i*8)) & 0x000000FF); userbyte = setbyte; UserParam.SetAt(offset++, userbyte); } } break; default: break; } }
/** * @brief 从7张牌中分析出5张最大牌型 * @param bHandCards[] 要分析的手牌 * @param nCount 手牌张数 * @param bPublicCards[] 要分析公共牌 * @param nPublicCount 公共牌张数 * @param bResultCard[] 返回分析得到的数据牌 * @return 牌型 */ int CUpGradeGameLogic::AnalysisCard(BYTE bHandCards[], int nHandCount, BYTE bPublicCards[], int nPublicCount, BYTE bResultCard[]) { if ((nHandCount + nPublicCount) != 7) { return 0; } int i, j; CByteArray arrCards; for (i = 0; i < nHandCount; i++) { arrCards.Add(bHandCards[i]); } for (i = 0; i < nPublicCount; i++) { arrCards.Add(bPublicCards[i]); } BYTE bCard[5] = {0}; int nCardKind[21] = {0}; //// 21种组成方法 //BYTE bIndex[21][5] = {0, 1, 2, 3, 4, \ // 0, 1, 2, 3, 5, \ // 0, 1, 2, 3, 6, \ // 0, 1, 2, 4, 5, \ // 0, 1, 2, 4, 6, \ // 0, 1, 2, 5, 6, \ // 0, 1, 3, 4, 5, \ // 0, 1, 3, 4, 6, \ // 0, 1, 3, 5, 6, \ // 0, 1, 4, 5, 6, \ // 0, 2, 3, 4, 5, \ // 0, 2, 3, 4, 6, \ // 0, 2, 3, 5, 6, \ // 0, 2, 4, 5, 6, \ // 0, 3, 4, 5, 6, \ // 1, 2, 3, 4, 5, \ // 1, 2, 3, 4, 6, \ // 1, 2, 3, 5, 6, \ // 1, 2, 4, 5, 6, \ // 1, 3, 4, 5, 6, \ // 2, 3, 4, 5, 6, \ // }; // 21种组成方法 BYTE bIndex[21][7] = {0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 5, 4, 6, 0, 1, 2, 3, 6, 4, 5, 0, 1, 2, 4, 5, 3, 6, 0, 1, 2, 4, 6, 3, 5, 0, 1, 2, 5, 6, 3, 4, 0, 1, 3, 4, 5, 2, 6, 0, 1, 3, 4, 6, 2, 5, 0, 1, 3, 5, 6, 2, 4, 0, 1, 4, 5, 6, 2, 3, 0, 2, 3, 4, 5, 1, 6, 0, 2, 3, 4, 6, 1, 5, 0, 2, 3, 5, 6, 1, 4, 0, 2, 4, 5, 6, 1, 3, 0, 3, 4, 5, 6, 1, 2, 1, 2, 3, 4, 5, 0, 6, 1, 2, 3, 4, 6, 0, 5, 1, 2, 3, 5, 6, 0, 4, 1, 2, 4, 5, 6, 0, 3, 1, 3, 4, 5, 6, 0, 2, 2, 3, 4, 5, 6, 0, 1 }; for (i = 0; i < 21; i++) { for (j = 0; j < 5; j++) { // 按牌下标取出5张牌 bCard[j] = arrCards.GetAt(bIndex[i][j]); } // 获取牌型 nCardKind[i] = GetCardShape(bCard, 5); //CString str; //str.Format("dxh: 第%d种方法, 牌型: %d", i, nCardKind[i]); //OutputDebugString(str); } // 取最大牌型位置 int nMax = 0; for (i = 1; i < 21; i++) { if (nCardKind[i] > nCardKind[nMax]) { nMax = i; } } BYTE bCompareCard1[5], bCompareCard2[5]; int nCompareMax = nMax; // 找出最大牌型后, 再找出相同牌型最大牌组合(牌型一样情况下, 则会比大小, 所以这里就需要取最大的相同的牌型) for (i = 0; i < 21; i++) { if (i == nMax) { continue; } for (j = 0; j < 5; j++) { bCompareCard1[j] = arrCards.GetAt(bIndex[i][j]); bCompareCard2[j] = arrCards.GetAt(bIndex[nCompareMax][j]); } // 比牌(-1:输, 0:和, 1:赢) if (CompareCard(bCompareCard1, 5, bCompareCard2, 5) > 0) { nCompareMax = i; } } // 从7张手牌中取出5终最终牌型 for (i = 0; i < 7; i++) { bResultCard[i] = arrCards.GetAt(bIndex[nCompareMax][i]); } CString str; str.Format("dxh: 当前最大牌型索引: %d, 牌型ID:%d", nCompareMax, nCardKind[nCompareMax]); OutputDebugString(str); return nCardKind[nCompareMax]; }
CTobCompiler::EToken CTobCompiler::CompilePass1() { EToken r; CString str, buf; CByteArray* bin = 0; for(;;) { if((r = GetToken(str)) <= 0) return r; if(r == SYM_KEY) { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found keyword after '.'"; return ERR_NORMAL; } if(str == "def") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found token after '.def'"; return ERR_NORMAL; } if((r = GetToken(buf)) < 0) return r; if(r == SYM_BIN) { bin = new CByteArray; if((r = GetBinary(*bin)) < 0) { delete bin; return r; } } else if(r == SYM_STR) { if((r = GetString(buf, false)) < 0) return r; bin = new CByteArray; for(LPCTSTR p = buf; *p; ++p) bin->Add(*(U8*)p); if(r == SYM_STRING) bin->Add(0); } else if(r == SYM_STRING) { if((r = GetString(buf, true)) < 0) return r; bin = new CByteArray; for(LPCTSTR p = buf; *p; ++p) bin->Add(*(U8*)p); if(r == SYM_STRING) bin->Add(0); } else { m_err = "not found '[' or ''' or '\"' after '.def <token>''"; return ERR_NORMAL; } m_defbin.SetAt(str, bin); bin = 0; } else if(str == "change") { if((r = GetToken(str)) < 0) return r; if(r != SYM_STR) { m_err = "not found ''' after '.change'"; return ERR_NORMAL; } if((r = GetString(str, false)) < 0) return r; if( str.IsEmpty() || str.GetLength()>2 || str.GetLength() == 2 && (U8)str[0]<0x80) { m_err = "only one character is allowed after '.change'"; return ERR_NORMAL; } U16 c = (U8)str[0]; if(str.GetLength() == 2) c = (c<<8) + (U8)str[1]; if((r = GetToken(str)) < 0) return r; if(r == SYM_BIN) { CByteArray tmp; if((r = GetBinary(tmp)) < 0) return r; buf.Empty(); int i, n = tmp.GetSize(); for(i = 0; i < n; ++i) { U8 b = tmp.GetAt(i); if(!b) break; buf += (TCHAR)b; } } else if(r == SYM_STR) { if((r = GetString(buf, false)) < 0) return r; } else if(r == SYM_STRING) { if((r = GetString(buf, true)) < 0) return r; } else { m_err = "not found ''' or '\"' after '.change <char>'"; return ERR_NORMAL; } m_change.SetAt(c, buf); } else if(str == "bit") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.bit'"; return ERR_NORMAL; } int bit = atoi(str); if(bit!=8 && bit!=16 && bit!=24 && bit!=32) { m_err = "only '.bit 8/16/24/32' is available"; return ERR_NORMAL; } m_bit_i = bit; } else if(str == "maxc") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.maxc'"; return ERR_NORMAL; } m_maxc = atoi(str); } else if(str == "maxw") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.maxw'"; return ERR_NORMAL; } m_maxw = atoi(str); } else if(str == "maxs") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.maxs'"; return ERR_NORMAL; } m_maxs = atoi(str); } else { m_err.Format("unknwon keyword '%s' after '.'", (LPCTSTR)str); return ERR_NORMAL; } } else if(r == SYM_BIN) { if((r = GetBinary(m_bin)) < 0) return r; } else if(r == SYM_STR) { if((r = GetString(buf, false)) < 0) return r; for(LPCTSTR p=buf;* p; ++p) m_bin.Add(*(U8*)p); if(r == SYM_STRING) m_bin.Add(0); } else if(r == SYM_STRING) { if((r = GetString(buf, true)) < 0) return r; for(LPCTSTR p=buf;* p; ++p) m_bin.Add(*(U8*)p); if(r == SYM_STRING) m_bin.Add(0); } else if(r == SYM_TOKEN) { if(!m_defbin.Lookup(str, bin)) { m_err.Format("not defined token '%s'", (LPCTSTR)str); return ERR_NORMAL; } m_bin.Append(*bin); bin = 0; } else if(r == SYM_ADDR) { if((r = GetToken(str)) < 0) return r; SExpInfo* ti = new SExpInfo(m_bin.GetSize(), m_line); if(r == SYM_EXP_BEGIN) { for(;;) { if((r = GetToken(buf)) < 0) { delete ti; return r; } if(r == SYM_TOKEN || r == SYM_NUM) ti->m_item.Add(buf); else if(r == SYM_ADD) ti->m_item.Add("+"); else if(r == SYM_SUB) ti->m_item.Add("-"); else if(r == SYM_ADDR) ti->m_item.Add("@"); else if(r == SYM_EXP_END) break; else { delete ti; m_err.Format("found unknown token '%s' in expression", (LPCTSTR)buf); return ERR_NORMAL; } } } else if(r == SYM_TOKEN) ti->m_item.Add(str); else { delete ti; m_err = "not found token or expression after '@'"; return ERR_NORMAL; } m_exp.Add(ti); for(int i = m_bit_i/8; i--;) m_bin.Add(0); } else if(r == SYM_LABEL) { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found label after '#'"; return ERR_NORMAL; } m_label.SetAt(str, m_bin.GetSize()); } else { m_err = "unknown or bad symbol"; return ERR_NORMAL; } } }
BOOL bGetMsgInfoFromMsgStr( CONST CString& omSendMsgLine, PSTCANDATA psCANData, BOOL bHexON) { CString omStrTemp =""; CString omStrMsgID =""; CString omStrDLC =""; CString omStrData =""; CString omStrMsgIDType =""; CHAR* pcStopString = nullptr; BOOL nReturn = FALSE; CByteArray omByteArrayDataTx; // Get the string before first white space charactor omStrTemp = omSendMsgLine.SpanExcluding("\t "); if(omStrTemp.IsEmpty()==0) { INT nIndex = omStrTemp.GetLength(); if(nIndex>0) { // Remove the time stamp string omStrTemp = omSendMsgLine.Right(omSendMsgLine.GetLength() - nIndex -1); if(omStrTemp.IsEmpty() ==0) { omStrTemp.TrimLeft(); omStrTemp.TrimRight(); // Get the message ID after removing Tx/Rx string omStrMsgID = omStrTemp.SpanExcluding("\t "); if( omStrMsgID.Compare("Tx") == 0 ) { psCANData->m_ucDataType = TX_FLAG; } else { psCANData->m_ucDataType = RX_FLAG; } nIndex = omStrMsgID.GetLength(); omStrTemp = omStrTemp.Right(omStrTemp.GetLength() - nIndex - 1); omStrTemp.TrimLeft(); // Channel ID omStrMsgID = omStrTemp.SpanExcluding("\t "); UCHAR ucChannel = (UCHAR) strtol( (LPCTSTR )omStrMsgID,&pcStopString ,10); nIndex = omStrMsgID.GetLength(); omStrTemp = omStrTemp.Right(omStrTemp.GetLength() - nIndex -1); omStrTemp.TrimLeft(); // Get the message with name omStrMsgID = omStrTemp.SpanExcluding("\t "); // Get the rest of the string. nIndex = omStrMsgID.GetLength(); omStrTemp = omStrTemp.Right(omStrTemp.GetLength() - nIndex -1); omStrTemp.TrimLeft(); // Get message ID string after removing any message name. omStrMsgID = omStrMsgID.SpanExcluding(defMSGID_NAME_DELIMITER); UINT unMsgID = 0; if( bHexON == TRUE) { unMsgID = (UINT) strtol( (LPCTSTR )omStrMsgID,&pcStopString ,16); } else { unMsgID = (UINT) strtol( (LPCTSTR )omStrMsgID,&pcStopString ,10); } // Get the message ID Type omStrMsgIDType = omStrTemp.SpanExcluding("\t "); // Message Id type is EXTENDED if(omStrMsgIDType.Find(defMSGID_EXTENDED) != -1) { psCANData->m_uDataInfo.m_sCANMsg.m_ucEXTENDED = 1; }// Message Id type is STD else if(omStrMsgIDType.Find(defMSGID_STD)!= -1) { psCANData->m_uDataInfo.m_sCANMsg.m_ucEXTENDED = 0; } // Message Id type is RTR if(omStrMsgIDType.Find(defMSGID_RTR)!= -1) { psCANData->m_uDataInfo.m_sCANMsg.m_ucRTR = 1; } else { psCANData->m_uDataInfo.m_sCANMsg.m_ucRTR = 0; } nIndex = omStrMsgIDType.GetLength(); omStrTemp = omStrTemp.Right(omStrTemp.GetLength() - nIndex -1); omStrTemp.TrimLeft(); // Get the DLC omStrDLC = omStrTemp.SpanExcluding("\t "); nIndex = omStrDLC.GetLength(); UINT unDLC = (UINT) strtol((LPCTSTR)omStrDLC,&pcStopString ,16); omStrTemp = omStrTemp.Right(omStrTemp.GetLength() - nIndex -1); omStrTemp.TrimLeft(); // Get the data string omStrData = omStrTemp; // Check if Message ID and DLC is valid. if(unMsgID>0 && unDLC<=8 && unDLC>0) { nIndex = omStrData.GetLength(); vConvStrtoByteArray(&omByteArrayDataTx, omStrData.GetBuffer(nIndex),bHexON); omStrData.ReleaseBuffer(nIndex); INT nTotalData = (INT)omByteArrayDataTx.GetSize(); // Check if String to Byte array conversion // has return a valid data if(nTotalData<=8 ) { for(INT i = 0; i<nTotalData; i++) { psCANData->m_uDataInfo.m_sCANMsg.m_ucData[i] = omByteArrayDataTx.GetAt(i); } psCANData->m_uDataInfo.m_sCANMsg.m_unMsgID = unMsgID; psCANData->m_uDataInfo.m_sCANMsg.m_ucDataLen = (UCHAR)unDLC; psCANData->m_uDataInfo.m_sCANMsg.m_ucChannel = ucChannel; nReturn = TRUE; } } } } } return nReturn; }