Dec 25, 2010 at 10:16pm UTC
do not respond to this do not respond to this
#include "stdafx.h"
#include <string.h>
# 1 define VALID
#define INVALID 0
#define D_A 0
#define D_B 1
#define ARR_SIZE 25
typedef struct{
char cmdArr[ARR_SIZE];
bool AorB;
}cmd_s;
typedef struct{
char cmdArr[ARR_SIZE];
int size;
}cmd3_s;
/* Global Variables */
cmd_s Cmd;
cmd3_s Cmd3;
void fillArr(char destArr[], const char sourceArr[], const int size)
{
memset(destArr, 0, ARR_SIZE);
memcpy(destArr, sourceArr, size);
}
bool isCmd(const char * cmd_t, const int size)
{
if(strncmp(Cmd.cmdArr, cmd_t, size) == 0)
{
return 1;
}
return 0;
}
int checkCmd(bool drction, char * cmd_t, int size, int *statePtr, int dest_state)
{
if(Cmd.AorB == drction )
{
if(isCmd(cmd_t, size))
{
*statePtr = dest_state;
return VALID;
}
}
*statePtr = 1;
return INVALID;
}
void saveCmd3(const char sourceArr[], const int size)
{
fillArr(Cmd3.cmdArr, sourceArr, size);
Cmd3.size = size - 1; //don't count the '/0'
}
int checkState3(int *statePtr)
{
int status = (checkCmd(D_A,
"LIST", sizeof("LIST"),
statePtr, 4));
saveCmd3("LIST", sizeof("LIST"));
// other possible commands ...
if(status == INVALID)
{
status = (checkCmd(D_A,
"EXIT ", sizeof("EXIT "),
statePtr, 5));
}
return status;
}
int getCmdLen()
{
int i=0;
int cnt=0;
for(i=0; i<ARR_SIZE; i++)
{
if(Cmd.cmdArr[i] != '\0')
cnt++;
}
return cnt;
}
int checkCh(char ch)
{
if((ch >='a') && (ch <='z'))
return VALID;
if((ch >='0') && (ch <='9'))
return VALID;
return INVALID;
}
int checkState4(int *statePtr)
{
int cmdSize = getCmdLen();
int cmd3Sise = Cmd3.size;
int SecondIdx = cmdSize - cmd3Sise;
int i;
*statePtr = 1;
if(cmdSize < cmd3Sise*2 + 2)
return INVALID;
if(strncmp(&Cmd.cmdArr[0], Cmd3.cmdArr, cmd3Sise) != 0)
return INVALID;
if(strncmp(&Cmd.cmdArr[SecondIdx], Cmd3.cmdArr, cmd3Sise) != 0)
return INVALID;
// check the msg
for(i = cmd3Sise+1; i<= SecondIdx-2 ; i++ )
{
if(checkCh(Cmd.cmdArr[i]) == INVALID)
return INVALID;
}
*statePtr = 3;
return VALID;
}
int handleCmd()
{
static int stat_state = 1;
switch(stat_state)
{
case 1:
return (checkCmd(D_A,
"OPEN_CONNECTION", sizeof("OPEN_CONNECTION"),
&stat_state, 2));
case 2:
return (checkCmd(D_B,
"OPEN_CONNECTION_ACKED", sizeof("OPEN_CONNECTION_ACKED"),
&stat_state, 3));
case 3:return (checkState3(&stat_state));
case 4:return (checkState4(&stat_state));
case 5:
return (checkCmd(D_B,
"EXIT_ACKED", sizeof("EXIT_ACKED"),
&stat_state, 1));
default: return INVALID;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
// init
memset(&Cmd, 0, sizeof(Cmd) );
memset(&Cmd3, 0, sizeof(Cmd3));
Cmd.AorB = D_A;
fillArr(Cmd.cmdArr, "OPEN_CONNECTION", sizeof("OPEN_CONNECTION"));
handleCmd();
Cmd.AorB = D_B;
fillArr(Cmd.cmdArr, "OPEN_CONNECTION_ACKED", sizeof("OPEN_CONNECTION_ACKED"));
handleCmd();
Cmd.AorB = D_A;
fillArr(Cmd.cmdArr, "LIST", sizeof("LIST"));
handleCmd();
Cmd.AorB = D_B;
fillArr(Cmd.cmdArr, "LIST 6575 LIST", sizeof("LIST 6575 LIST"));
handleCmd();
Last edited on Dec 25, 2010 at 10:20pm UTC