日常问题总结
程序员文章站
2022-06-09 12:05:51
...
-
c语言版本的avl-tree代码。默认内部有旋转操作,即使设置了自定义比较函数,也无法保证后续的遍历顺序。因为比较函数只是为了说明插入到节点的左边或右边,但每一次插入, root可能会被改变(由于平衡旋转).
而且head始终会指向新插入的节点。所以后续取节点时,只能自己判断出顺序,比如自己去找出最小序号的节点等等。 -
链表常用的函数:
/* 链表初始化 */
dlq_createSQue(&m_flow_data_list);
//判断内存中是否已经有相同编号的no,如果有则说明冲突了
OFP_FLOW_DATA_PARA *flow_data_node = NULL;
for (flow_data_node = (OFP_FLOW_DATA_PARA *)dlq_firstEntry(&m_flow_data_list);
flow_data_node != NULL;
flow_data_node = (OFP_FLOW_DATA_PARA *)dlq_nextEntry(flow_data_node))
{
if (flow_data_node->iNo == iNo)
{
has_conflit_ino = TRUE;
goto EXIT_LABEL;
}
}
void CServiceEditConfigDlg::ofp_free_meter_data_list(dlq_hdr_t *meter_data_list)
{
if (dlq_empty(meter_data_list))
{
return;
}
//遍历列表,释放内存
OFP_METER_DATA_NODE *meter_data_node = NULL;
OFP_METER_DATA_NODE *next_meter_data_node = NULL;
for (meter_data_node = (OFP_METER_DATA_NODE *)dlq_firstEntry(meter_data_list);
meter_data_node != NULL;
meter_data_node = next_meter_data_node)
{
next_meter_data_node = (OFP_METER_DATA_NODE *)dlq_nextEntry(meter_data_node);
//摘除节点
dlq_remove(meter_data_node);
ofp_free_meter_data_node(meter_data_node);
}
return;
}
- avl-tree的常用函数:
ret = avl_create(&m_send_data_tree, 0, ofp_send_data_cmp);
- 自定义mfc消息
ON_MESSAGE(WM_USER_REFRESH_DEVICE_IP, OnRefreshDeviceIP)
- 获取对话框大小:
//获取对话框的初始大小
CRect rect;
GetClientRect(&rect); //取客户区大小
m_ptOldDlg.x = rect.right - rect.left;
m_ptOldDlg.y = rect.bottom - rect.top;
- list-ctr控件的相关函数
/*插入流表标题*/
m_flow_list.InsertColumn(0, _T(OFP_FLOW_COL_NAME_NO), LVCFMT_LEFT, 70);
m_flow_list.InsertColumn(1, _T(OFP_FLOW_COL_NAME_TABLE_ID), LVCFMT_LEFT, 60);
/* 初始化每行的数据 */
m_flow_list.InsertItem(0, _T("1"));
m_flow_list.SetItemText(0, 1, _T("10"));
m_flow_list.SetItemText(0, 2, _T("add"));
m_flow_list.SetItemText(0, 3, _T("oxm"));
m_flow_list.SetExtendedStyle(LVS_EX_FULLROWSELECT |
LVS_EX_CHECKBOXES | LVS_EX_GRIDLINES | LVS_OWNERDRAWFIXED);
//在表头上添加checkbox
CHeaderCtrl *pHeadCtrl = m_flow_list.GetHeaderCtrl();
ASSERT(pHeadCtrl->GetSafeHwnd());
VERIFY(m_checkImgList.Create(IDB_BITMAP_CHECKBOX, 16, 3, RGB(255, 0, 255)));
int i = m_checkImgList.GetImageCount();
pHeadCtrl->SetImageList(&m_checkImgList);
HDITEM hdItem;
hdItem.mask = HDI_IMAGE | HDI_FORMAT;
VERIFY(pHeadCtrl->GetItem(0, &hdItem));
hdItem.iImage = 1;
hdItem.fmt |= HDF_IMAGE;
VERIFY(pHeadCtrl->SetItem(0, &hdItem));
AfxMessageBox(_T("encode oxm_field failed, unknown exp_oxm_type."), MB_OK | MB_ICONERROR);
int flow_row_nums = m_flow_list.GetItemCount();
/* 单元格值 */
tmp_cell_cstr = m_flow_list.GetItemText(i, tmp_no_col_index);
tmp_iNo = _ttoi(tmp_cell_cstr); /* 通用ansi和unicode */
/* 注意要倒着删 */
for (int i = nRows-1; i>=0; i--)
{
if (m_flow_list.GetCheck(i))
{
m_flow_list.DeleteItem(i);
}
}
- cJson相关函数:
//增加一个数组,buckets:[],内含n个bucket
cJSON_AddItemToObject(group_row_json, OFP_JSON_KEY_GROUP_BUCKETS,
buckets_array_json = cJSON_CreateArray());
//向数组中添加bucket对象
cJSON_AddItemToArray(buckets_array_json, bucket_json = cJSON_CreateObject());
//增加type
tmp_cstr.Format(_T("%S (%d)"), OFP_JSON_VAL_OFPMBT_EXPERIMENTER, band_data->band_exp.type);
cJSON_AddStringToObject(band_json, OFP_JSON_KEY_FLOW_ACTION_TYPE,
CT2A(tmp_cstr.GetBuffer()));
cJSON_AddNumberToObject(band_json, OFP_JSON_KEY_METER_BAND_RATE, band_data->band_exp.rate);
/* 赋值 */
tmp_json = cJSON_GetObjectItem(oxm_field_json, OFP_JSON_KEY_FLOW_TUNNEL_ID);
if (tmp_json == NULL)
{
AfxMessageBox(_T("parse oxm_field failed, tunnel_id not found."), MB_OK | MB_ICONERROR);
goto EXIT_LABEL;
}
field_data->tunnel_id = (NBB_UINT64)tmp_json->valuedouble;
- 其他函数
NBB_BYTE CServiceEditConfigDlg::ofp_switch_char(char chStr)
{
if (chStr >= '0' && chStr <= '9')
{
return (chStr - '0');
}
else if (chStr >= 'A' && chStr <= 'F')
{
return (chStr - 'A' + 10);
}
else if (chStr >= 'a' && chStr <= 'f')
{
return (chStr - 'a' + 10);
}
else
{
return 0;
}
}
BOOL CServiceEditConfigDlg::ofp_convert_mac_str_to_array(char *mac_str, NBB_BYTE byte_array[])
{
BOOL ret = FALSE;
char *next_string = NULL;
char *sep = ":-";
char *pchStr = strtok_s(mac_str, sep, &next_string);
if (NULL == pchStr)
{
AfxMessageBox(_T("parse oxm_field failed, dst_mac fomart is error."), MB_OK | MB_ICONERROR);
goto EXIT_LABEL;
}
int nTotal = 0;
while (NULL != pchStr)
{
byte_array[nTotal++] = (ofp_switch_char(*pchStr) << 4) | ofp_switch_char(*(pchStr + 1));
pchStr = strtok_s(NULL, sep, &next_string);
}
ret = TRUE;
EXIT_LABEL:
return ret;
}
/* 获取主文档窗口类,方便调用其成员函数 */
CMainFrame *pFrame = (CMainFrame *)(AfxGetApp()->m_pMainWnd);
CLeftUpView *pLeftUpView = pFrame->m_pLeftUpView;
`
int session_nums = pListConSession->GetItemCount();
for (i = 0; i < session_nums; i++)
{
remote_ip_cstr = pListConSession->GetItemText(i, remote_ip_col_index);
remote_port_cstr = pListConSession->GetItemText(i, remote_port_col_index);
//tmp_port = _ttoi(remote_port_cstr); /* 通用ansi和unicode */
//sprintf_s函数会在赋值之后将未使用的内存填充为0xfe, 可通过第二个参数规避,要慎重使用
//sprintf_s(tmp_str, 21, "%s:%d", CT2A(remote_ip_cstr.GetBuffer()), tmp_port);
dev_str = remote_ip_cstr + ":" + remote_port_cstr;
m_combo_sel_dev.InsertString(i, dev_str);
}
int CServiceEditConfigDlg::ofp_get_group_col_index_by_str(char *column_name)
{
/* 根据header标题字符串,决定如何赋值 */
char str[256] = { 0 };
int nColNum = 0;
CString tmp_col_cstr;
CString input_col_cstr = CA2W(column_name);
LVCOLUMN lvcol;
lvcol.mask = LVCF_TEXT;
lvcol.pszText = CA2W(str); /* ansi to lpwstr宽字符 */
// MultiByteToWideChar(0,0,lvcol.pszText,256,str,512);
lvcol.cchTextMax = 256;
/*得到item的信息*/
/***
TCHAR szBuf[1024];
LVITEM lvi;
lvi.iItem = iRow;
lvi.iSubItem = 0;
lvi.mask = LVIF_TEXT;
lvi.pszText = szBuf;
lvi.cchTextMax = 1024;
m_flow_list.GetItem(&lvi);
***/
while (m_group_list.GetColumn(nColNum, &lvcol))
{
/* 列名 */
tmp_col_cstr = lvcol.pszText;
if (tmp_col_cstr.CompareNoCase(input_col_cstr) == 0)
{
break;
}
nColNum++;
}
return nColNum;
``