Advertisement
Guest User

Source file

a guest
Jan 16th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1.  
  2. #include "stdio.h"
  3. #include "string.h"
  4. #include "stdlib.h"
  5. #include "Command_Parser.h"
  6.  
  7. static cmdTableStruct cmdTable[]=
  8. {
  9.  
  10.  { 1123,(myStruct (*)(paramHandler_type *pHandler)) setParams,(int(*)(int y,...)) printParams },
  11.  //more command in same manner
  12. };
  13.  
  14. #define MSG_GET_CMD_TABLE_MAX   (sizeof(cmdTable)/sizeof(cmdTableStruct))
  15.  
  16. paramHandler_type paramHandler;
  17.  
  18.  
  19. int parseCommand( char *cmdBuffer)
  20. {
  21.     int commandType;
  22.  
  23.     cmdTableStruct *commandTable=NULL;
  24.  
  25.     void *genericMessageStruct;
  26.  
  27.  
  28.     printf("parseCommand : %s\n",cmdBuffer);
  29.  
  30.     memset(&paramHandler,0,sizeof(paramHandler_type));
  31.  
  32.     if(!(genericMessageStruct = getCommandInfo(cmdBuffer,&commandType, &paramHandler)))
  33.     {
  34.         return COMMAND_RESULT_ERROR;
  35.     }
  36.  
  37.     if((commandTable = findCommandFromTable(commandType)) == NULL)
  38.     {
  39.         return COMMAND_RESULT_ERROR;
  40.     }
  41.  
  42.     commandTable->process_command(commandType,genericMessageStruct);
  43.  
  44.     return COMMAND_RESULT_OK;
  45. }
  46.  
  47. cmdTableStruct* findCommandFromTable(int id)
  48. {
  49.     int loop_i;
  50.     cmdTableStruct * findTbl=NULL;
  51.  
  52.     for(loop_i=0; loop_i < MSG_GET_CMD_TABLE_MAX; loop_i++)
  53.     {
  54.         findTbl = &cmdTable[loop_i];
  55.         if( findTbl->commandCode == id)
  56.             return findTbl;
  57.     }
  58.  
  59.     return (NULL);
  60. }
  61.  
  62. void *getCommandInfo(char *str, int *commandType, paramHandler_type *pHandler)
  63. {
  64.     int i=0,j=0;
  65.     int commandCode;
  66.     char *token = NULL;
  67.     char *delimiter=" ,";
  68.     void *genericMessageStruct;
  69.  
  70.     token = strtok(str,delimiter);
  71.  
  72.     commandCode = atoi(token);
  73.  
  74.     *commandType = commandCode;
  75.  
  76.     printf("In getCommandInfo called :%d \n",commandCode);
  77.  
  78.     for(i=0;i<MSG_GET_CMD_TABLE_MAX;i++)
  79.     {
  80.         if(cmdTable[i].commandCode == commandCode)
  81.         {
  82.             printf("In getCommandInfo: found command\n");
  83.  
  84.             genericMessageStruct = cmdTable[i].getCommandParamStructure(pHandler);
  85.             while(token != NULL)
  86.             {
  87.                 token = strtok (NULL, " ,");
  88.                 switch(pHandler->type[j])
  89.                 {
  90.                     case SUPPORTED_FIELD_TYPE_INT:
  91.                     printf("arg%d = %d\n",j,(int)atoi(token));
  92.                     *((int*)pHandler[j].paramValue) = (int)atoi(token);
  93.                     break;
  94.                     default:
  95.                     break;
  96.                 }
  97.                 j++;
  98.             }
  99.             break;
  100.         }
  101.     }
  102.  
  103.     return genericMessageStruct;
  104. }
  105.  
  106. myStruct *setParams(paramHandler_type *pHandler)
  107. {
  108.     int i=0;
  109.     myStruct *my_param_struct;
  110.     printf("setParams called\n");
  111.     my_param_struct = malloc(sizeof(myStruct));
  112.  
  113.     pHandler->paramValue[i]=(void*)&my_param_struct->ID;
  114.     pHandler->type[i]=SUPPORTED_FIELD_TYPE_INT;
  115.  
  116.     i++;
  117.     pHandler->paramValue[i]=(void*)&my_param_struct->ch;
  118.     pHandler->type[i]=SUPPORTED_FIELD_TYPE_INT;
  119.  
  120.     i++;
  121.     pHandler->numofElements=i;
  122.  
  123.     return my_param_struct;
  124. }
  125.  
  126.  
  127. int printParams(int commandType,myStruct *param )
  128. {
  129.    printf("ID :%d ch : %d\n",param->ID,param->ch);
  130.  
  131.    free(param);
  132.  
  133.    return COMMAND_RESULT_OK;
  134. }
  135.  
  136.  
  137. int main(int argc,char *argv[])
  138. {
  139.     char buffer[]="1123,13,46";
  140.     parseCommand(buffer);
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement