再度请大侠出手将C++语句转换为Delphi语句,先奉上300分,不够另来! ( 积分: 300 )

  • 主题发起人 主题发起人 huayan889
  • 开始时间 开始时间
H

huayan889

Unregistered / Unconfirmed
GUEST, unregistred user!
#define MAX_M_NUM 256
#define MAX_P_NUM 64
#define MAX_T_NUM (32*MAX_P_NUM)
enum REMOVE_STATE
{
REMOVE_STATE_NONE = 0,
REMOVE_STATE_START = 1,
REMOVE_STATE_READY = 2,
};
typedef struct
{
char m_s32FType

short m_s32FCnl

}CallID_t;
typedef struct
{
long m_s32DevMain

logn m_s32ID

long m_s32DevNum

}Dev_List_Head_t
typedef struct
{
short m_s16Main

short m_s16Sub

Char m_s8ID

Char m_Ru1[1]

short m_s16ID

short m_s16Group

char m_Ru2[2]

CallID_t m_CallID

}DeviceID_t;
typedef struct
{
Char m_s8ID

short m_s16ID

}CNL_MAP_TABLE;
typedef struct
{
DeviceID_t deviceID;
int iSeqID;
.....
M_STATE ModuleState;
} T_STRUCT;
typedef struct
{
Handle_t m_s32Handle

DeviceID_t m_DeviceID

long m_s32ESize

long m_s32EType

}Evt_t;
enum V_STATE {
V_WAITOPEN,
V_FREE,
V_USED,
V_WAIT_REMOVE
} ;
typedef struct
{
DeviceID_t deviceID;
int iSeqID;
DeviceID_t UsedDevID;
V_STATE State
} V_STRUCT;
typedef struct
{
DeviceID_t deviceID;
int iSeqID;
....
Byte u8E1Type;
} P_STRUCT;
// -----------------------------------------------------------------------------
DEVICE_RES AllDeviceRes[MAX_M_NUM];
int iTotalM = 0;
char MapT_M[MAX_M_NUM];
int iTotalP = 0;
int iTotalPOpened = 0;
CNL_MAP_TABLE MapT_P[MAX_P_NUM];
int iTotalT = 0;
int iTotalTOpened = 0;
CNL_MAP_TABLE MapT_T[MAX_T_NUM];
int iTotalV = 0;
int iTotalVOpened = 0;
int iTotalVFree = 0;
CNL_MAP_TABLE MapT_V[MAX_T_NUM];
// ----- 以上结构及定义主要是为了描述下面语句中的引用 --------------------------
typedef struct
{
long lFlag;
DeviceID_t deviceID;
int iSeqID;
bool OpenFlag;
bool ErrFlag;
REMOVE_STATE RemoveState;
long lVNum;
long lVOpened;
long lVFreeNum;
V_STRUCT *pV;
long lPNum;
long lPOpened;
P_STRUCT *pP;
long lTNum;
long lTOpened;
T_STRUCT *pT;
}DEVICE_RES;

#define M_OneV(DevID) AllDeviceRes[(DevID).m_s8ID].pV[(DevID).m_s16ID]
#define M_OneP(DevID) AllDeviceRes[(DevID).m_s8ID].pP[(DevID).m_s16ID]
#define M_OneT(DevID) AllDeviceRes[(DevID).m_s8ID].pT[(DevID).m_s16ID]

DJ_S32 PlayFile ( DeviceID_t *pVDevID, char *s8FileName, byte u8PTag, bool bIsQueue = false );

void TWork ( T_STRUCT *pOneT, Evt_t *pEvt );


//=======================================================================================
extern int iPWork;
// -------------------------------------------------------------------------------------
void DispEvtInfo ( Evt_t *pEvt )
{
char TmpStr[256];
char TmpS[128];
Dev_List_Head_t * pDevList = NULL;
sprintf ( TmpStr, "EVT(%4d) : ", pEvt->m_s32ESize );
switch ( pEvt->m_s32EType )
{
.....................
case E_QUERY_DEV:
pDevList = (Dev_List_Head_t *) FetchEventData(pEvt);
........
default:
break;
}
}
// --------------------------------------------------------------------------
void CloseDeviceOK ( DeviceID_t *pDevice )
{
T_STRUCT *pOneT;
.......
if ( pDevice->m_s16Main == DEVMAIN_PORT )
{
M_OneP(*pDevice).bOpenFlag = false;
.......
}
}
void OpenAllDevice_D ( char s8ModID )
{
int i;
......
for ( i = 0
i < AllDeviceRes[s8ModID].lVocNum
i++ )
{
OpenVocDevice ( &amp;AllDeviceRes[s8ModID].pV );
}
.......
}
void HandleState ( Evt_t *pEvt )
{
T_STRUCT *pOneT;
P_STRUCT *pOneP;
......
if ( pEvt->m_DeviceID.m_s16Main == DEVMAIN_PORT )
{
pOneP = &amp;M_OneP(pEvt->m_DeviceID);
pOneP->u8E1Type = (pGenData->m_s32DevState >> 16) &amp
0xFF;
}
............
}
void FreeOneDevRes ( int ID )
{
if ( AllDeviceRes[ID].p != NULL )
delete [] AllDeviceRes[ID].pV;
.......
memset ( &amp;AllDeviceRes[ID], 0, sizeof (DEVICE_RES) );
}
// --------------------------------------------------------------------------
void RefreshMapTable ( void )
{
int i, j;
int TCount, VCount, PCount, MCount;
int iModSeqID;

MCount = TCount = VCount = PCount = 0;
for ( i = 0
i < MAX_M_NUM
i ++ )
{
if ( AllDeviceRes.lFlag == 1 )
{
AllDeviceRes.iSeqID = MCount;
MapT_M[MCount] = i;
..........
iModSeqID = 0;
for ( j = 0
j < AllDeviceRes.lTNum
j ++ )
{
AllDeviceRes.pT[j].iSeqID = TCount;
MapT_T[TCount].m_s8ID = AllDeviceRes.pT[j].deviceID.m_s8ID;
......
if ( AllDeviceRes.pT[j].deviceID.m_s16Sub != DEVSUB_UNUSABLE )
{
AllDeviceRes.pT[j].iModSeqID = iModSeqID;
....
}
}
}
}
.....
}

void AddDevRes_T ( char s8ModID, Dev_List_Head_t *pDevList )
{
DJ_S32 s32Num;
int i;
char TmpStr[256];
s32Num = pDevList->m_s32DevNum;
if ( (AllDeviceRes[s8ModID].lTNum == 0) &amp;&amp
(s32Num > 0) )
{
AllDeviceRes[s8ModID].pT = new T_STRUCT[s32Num];
if( !AllDeviceRes[s8ModID].pT )
{
AllDeviceRes[s8ModID].lTNum = 0;
sprintf ( TmpStr, &quot;new T_STRUCT[%d] fail in AddDevRes_T()&quot
);
}
else
{
memset ( AllDeviceRes[s8ModID].pT, 0, sizeof(T_STRUCT)*s32Num );
DeviceID_t *pDev;
pDev = (DeviceID_t *)((BYTE *)pDevList + sizeof(Dev_List_Head_t));
for ( i = 0
i < s32Num
i ++ )
{
AllDeviceRes[s8ModID].pT.deviceID = pDev;
AllDeviceRes[s8ModID].pT.State = T_WAITOPEN;
}
}
}
else if ( (AllDeviceRes[s8ModID].lTkNum > 0) &amp;&amp
(s32Num == 0) )
{
for ( i = 0
i < AllDeviceRes[s8ModID].lTNum
i ++ )
{
if ( AllDeviceRes[s8ModID].pT.State != T_WAITOPEN )
CloseDeviceOK ( &amp;AllDeviceRes[s8ModID].pT.deviceID );
}
delete [] AllDeviceRes[s8ModID].pT;
AllDeviceRes[s8ModID].pT = NULL;
}
}
Void E_Handler( unsigned long ePrm)
{
Evt_t * pEvt = NULL;
Dev_List_Head_t * pDevList = NULL;
pEvt = (Evt_t *) ePrm;
DispEvtInfo ( pEvt );
switch ( pEvt->m_s32EType )
{
case EVT_QUERY_DEVICE:
if ( ( iPWork == 0 ) || (pEvt->m_DeviceID.m_s8ID == iPWorkID) )
{
pDevList = ( Dev_List_Head_t *) FetchEventData(pEvt);
AddDeviceRes ( pDevList );
}
break

case EVT_OPEN_DEVICE:
OpenDeviceOK ( &amp;pEvt->m_DeviceID );
break;
........
default:
if ( pEvt->m_DeviceID.m_s16Main == DEVMAIN_CH )
{
TWork ( &amp;M_OneT(Evt->m_DeviceID), pEvt );
}
else if ( pEvt->m_DeviceID.m_s16Main == DEVMAIN_V )
{
DeviceID_t *pDevID;
pDevID = &amp;M_OneV(pEvt->m_DeviceID).UsedDevID;
if ( pDevID->m_s16Main == DEVMAIN_CH )
{
TWork ( &amp;M_OneT(*pDevID), pEvt );
}
}
break;
}
}
//-----------------------------------
memcpy( pParam->m_CT.m_u8NumH, TmpStr, pParam->m_CT.m_u8NumHLen )

//-------------------------------------
为了把问题描述更清楚,把相关的一些结构及定义都帖上了,所以显的比较长,呵呵。请大侠出手将其转换一下,尤其是其中对结构使用下标,还有Memset中使用FillChar进行转换时会提示错误:E2197 Constant object cannot be passed as var parameter;
先送上300分!!
 
const
MAX_M_NUM = 256;
MAX_P_NUM = 64;
MAX_T_NUM = (32 * MAX_P_NUM);
type
REMOVE_STATE =
(
REMOVE_STATE_NONE = 0,
REMOVE_STATE_START = 1,
REMOVE_STATE_READY = 2,
REMOVE_NULL = 10241024 //占位凑够4字节,和C++枚举凑齐大小
);
CallID_t = record
m_s32FType: Byte;
m_s32FCnl: Smallint;
end;
Dev_List_Head_t = record
m_s32DevMain: Integer;
m_s32ID: Integer;
m_s32DevNum: Integer;
end;

DeviceID_t = record
m_s16Main: Smallint;
m_s16Sub: Smallint;
m_s8ID: Byte;
m_Ru1: array[0..0] of Byte;
m_s16ID: Smallint;
m_s16Group: Smallint;
m_Ru2: array[0..1] of Byte;
m_CallID: CallID_t;
end;
CNL_MAP_TABLE = record
m_s8ID: Byte;
m_s16ID: Smallint;
end;

T_STRUCT = record
deviceID: DeviceID_t;
iSeqID: Integer;
// .....
ModuleState: M_STATE
//M_STATE??
end;
Evt_t = record
m_s32Handle: Handle_t
//Handle_t??
m_DeviceID: DeviceID_t;
m_s32ESize: Integer;
m_s32EType: Integer;
end;

V_STATE = (
V_WAITOPEN,
V_FREE,
V_USED,
V_WAIT_REMOVE,
V_WAIT_NULL = 10241024 //占位凑够4字节,和C++枚举凑齐大小
);
V_STRUCT = record
deviceID: DeviceID_t;
iSeqID: Integer;
UsedDevID: DeviceID_t;
State: V_STATE;
end;

P_STRUCT = record
deviceID: DeviceID_t;
iSeqID: integer;
//....
u8E1Type: Byte;
end;
前面的结构体翻译如上面.
代码我就不一一翻译了.
memset ( &amp;AllDeviceRes[ID], 0, sizeof (DEVICE_RES) );
可以翻译成如下几种形式
ZeroMemory(@AllDeviceRes[ID], sizeof (DEVICE_RES));//传递指针
FillMemory(@AllDeviceRes[ID], sizeof (DEVICE_RES),0);//传递指针
FillChar(AllDeviceRes[ID], sizeof (DEVICE_RES),0);//传递引用而不是指针
 
To 武稀松大侠:
那些结构主要是为了描述下面语句的一些引用,主要问题在于以下几点:
1:typedef struct
{
.....
V_STRUCT *pV

.....
P_STRUCT *pP

.......
T_STRUCT *pT

}DEVICE_RES;
#define M_OneV(DevID) AllDeviceRes[(DevID).m_s8ID].pV[(DevID).m_s16ID]
#define M_OneP(DevID) AllDeviceRes[(DevID).m_s8ID].pP[(DevID).m_s16ID]
#define M_OneT(DevID) AllDeviceRes[(DevID).m_s8ID].pT[(DevID).m_s16ID]
void CloseDeviceOK ( DeviceID_t *pDevice )
{
T_STRUCT *pOneT;
.......
if ( pDevice->m_s16Main == DEVMAIN_PORT )
{
M_OneP(*pDevice).bOpenFlag = false;
.......
}
}
2.void FreeOneDevRes ( int ID )
{
if ( AllDeviceRes[ID].p != NULL )
delete [] AllDeviceRes[ID].pV;
.......
memset ( &amp;AllDeviceRes[ID], 0, sizeof (DEVICE_RES) );
}//这里会有提示E2197 Constant object cannot be passed as var parameter

3.void RefreshMapTable ( void )
{
int i, j;
int TCount, VCount, PCount, MCount;
int iModSeqID;

MCount = TCount = VCount = PCount = 0;
for ( i = 0
i < MAX_M_NUM
i ++ )
{
if ( AllDeviceRes.lFlag == 1 )
{
AllDeviceRes.iSeqID = MCount;
MapT_M[MCount] = i;
..........
iModSeqID = 0;
for ( j = 0
j < AllDeviceRes.lTNum
j ++ )
{
AllDeviceRes.pT[j].iSeqID = TCount;
MapT_T[TCount].m_s8ID = AllDeviceRes.pT[j].deviceID.m_s8ID;
......
if ( AllDeviceRes.pT[j].deviceID.m_s16Sub != DEVSUB_UNUSABLE )
{
AllDeviceRes.pT[j].iModSeqID = iModSeqID;
....
//这里AllDeviceRes是一个数组,引用下标没有问题,但pT[j]会出错[DCC Error] E2016 Array type required
4.
void AddDevRes_T ( char s8ModID, Dev_List_Head_t *pDevList )
{
DJ_S32 s32Num;
int i;
char TmpStr[256];
s32Num = pDevList->m_s32DevNum;
if ( (AllDeviceRes[s8ModID].lTNum == 0) &amp;&amp
(s32Num > 0) )
{
AllDeviceRes[s8ModID].pT = new T_STRUCT[s32Num];
if( !AllDeviceRes[s8ModID].pT )
{
AllDeviceRes[s8ModID].lTNum = 0;
sprintf ( TmpStr, &quot;new T_STRUCT[%d] fail in AddDevRes_T()&quot
);
}
else
{
memset ( AllDeviceRes[s8ModID].pT, 0, sizeof(T_STRUCT)*s32Num )

DeviceID_t *pDev;
pDev = (DeviceID_t *)((BYTE *)pDevList + sizeof(Dev_List_Head_t));
for ( i = 0
i < s32Num
i ++ )
{
AllDeviceRes[s8ModID].pT.deviceID = pDev;
AllDeviceRes[s8ModID].pT.State = T_WAITOPEN;
.......
//这里的New 及同样存在Array type required问题。还有下划线那一句,都是强制转换?
5.
memcpy( pParam->m_CT.m_u8NumH, TmpStr, pParam->m_CT.m_u8NumHLen )


6.
Void E_Handler( unsigned long ePrm)
{
Evt_t * pEvt = NULL;
Dev_List_Head_t * pDevList = NULL;
pEvt = (Evt_t *) ePrm;
DispEvtInfo ( pEvt );
switch ( pEvt->m_s32EType )
......
{
DeviceID_t *pDevID;
pDevID = &amp;M_OneV(pEvt->m_DeviceID).UsedDevID;
if ( pDevID->m_s16Main == DEVMAIN_CH )
{
TWork ( &amp;M_OneT(*pDevID), pEvt );
}
........这里的TWork(&amp;M_OneT(*pDevID)),pEvt)有的用的是pDevID,而有的就是这种*pDevID,这种怎么处理呢?

-----------------------
烦请大侠出手帮助。
 
先说一下C中的数组和指针吧.
C中绝大多数情况下数组就可以看成是指针.指向数据的第一个元素地址.
C里面 类型* 的声明标识该类型的指针,*变量 标识该指针变量的数据.
Pascal里面 ^类型 标识该类型的指针,变量^ 表示该指针变量的数据
这样第6个的疑问你自己琢磨吧.
//--------------------------------------------
对于Pascal和C里面数组的含义不同所以很多时候要变通实现.
可以声明一个数组类型,然后用该数组类型的指针.就相当于C中的数组概念了.
//
const
MAX_M_NUM = 256;
MAX_P_NUM = 64;
MAX_T_NUM = (32 * MAX_P_NUM);
type
REMOVE_STATE =
(
REMOVE_STATE_NONE = 0,
REMOVE_STATE_START = 1,
REMOVE_STATE_READY = 2,
REMOVE_NULL = 10241024 //占位凑够4字节,和C++枚举凑齐大小
);
CallID_t = record
m_s32FType: Byte;
m_s32FCnl: Smallint;
end;
Dev_List_Head_t = record
m_s32DevMain: Integer;
m_s32ID: Integer;
m_s32DevNum: Integer;
end;

DeviceID_t = record
m_s16Main: Smallint;
m_s16Sub: Smallint;
m_s8ID: Byte;
m_Ru1: array[0..0] of Byte;
m_s16ID: Smallint;
m_s16Group: Smallint;
m_Ru2: array[0..1] of Byte;
m_CallID: CallID_t;
end;

PDeviceID_t = ^DeviceID_t;

CNL_MAP_TABLE = record
m_s8ID: Byte;
m_s16ID: Smallint;
end;

T_STRUCT = record
deviceID: DeviceID_t;
iSeqID: Integer;
// .....
ModuleState: M_STATE
//M_STATE??
end;
pT_STRUCT = ^T_STRUCT;

Evt_t = record
m_s32Handle: Handle_t
//Handle_t??
m_DeviceID: DeviceID_t;
m_s32ESize: Integer;
m_s32EType: Integer;
end;

V_STATE = (
V_WAITOPEN,
V_FREE,
V_USED,
V_WAIT_REMOVE,
V_WAIT_NULL = 10241024 //占位凑够4字节,和C++枚举凑齐大小
);
V_STRUCT = record
deviceID: DeviceID_t;
iSeqID: Integer;
UsedDevID: DeviceID_t;
State: V_STATE;
end;

PV_STRUCT = ^V_STRUCT;

P_STRUCT = record
deviceID: DeviceID_t;
iSeqID: integer;
//....
u8E1Type: Byte;
end;

PP_STRUCT = ^P_STRUCT;

///

TVS = array[0..0] of V_STRUCT;
TPS = array[0..0] of P_STRUCT;
TTS = array[0..0] of T_STRUCT;
PVS = ^TVS;
PPS = ^TPS;
PTS = ^TTS;

DEVICE_RES = record
pv : PVS;
//...
pP : PPS;
//...
pT : PTS;
end;

var
AllDeviceRes : array[0..MAX_M_NUM-1] of DEVICE_RES;

Function M_OneV(DevId : DeviceID_t): V_STRUCT;
begin
Result := AllDeviceRes[DevId.m_s8ID].pv[DevId.m_s16ID];
end;

function M_OneP(DevId : DeviceID_t):P_STRUCT;
begin
Result := AllDeviceRes[DevId.m_s8ID].pp[DevId.m_s16ID];
end;

function M_OneT(DevId : DeviceID_t):T_STRUCT;
begin
Result := AllDeviceRes[DevId.m_s8ID].pT[DevId.m_s16ID];
end;

procedure CloseDeviceOK(pDevice : PDeviceID_t);
var
pOneT : PT_STRUCT;
begin
{bOpenFlag不知道是啥
if pDevice^.m_s16Main = DEVMAIN_PORT then
M_OneP(pDevice).bOpenFlag := False;
//.....
}
end;

procedure FreeOneDevRes(ID : Integer);
begin
{P不知道是啥
if (AllDeviceRes[ID].p <> nil) then
FreeMem(AllDeviceRes[ID].pv);
FillMemory(@AllDeviceRes[ID], sizeof (DEVICE_RES), 0);
}
end;
 
谢谢武稀松大侠解答,正在测试中,如有不明之处还望多多指教。
 
后退
顶部