parser.c

来自「mgcp协议源代码和测试程序,还有一个编译器」· C语言 代码 · 共 641 行 · 第 1/2 页

C
641
字号
/******************************************************************************
  Copyright(C) 2005,2006 Frank ZHANG

  All Rights Reserved.
    
  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
  more details.
    
  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  Place, Suite 330, Boston, MA 02111-1307 USA.
 
 ******************************************************************************
*      Authors                   :  Frank ZHANG (openmgcp@gmail.com)
*      Description               :  
*
*
*      Date of creation          :  04/10/2005
*
*
*      History                   :
*      2005/04/10 Frank ZHANG    : - Creation
******************************************************************************/
#include <stdlib.h>
#include <string.h>

#include "typedef.h"
#include "debg.h"
#include "parser.h"

/***************************************************************************
 * Function          : CtxInitial
 *
 * Description       : Initialize the ctx
 *                                        
 * Input parameters  : ctx - pointer to ctx
 *                     pData - pointer to data header
 *                     iLen - length of the data
 *
 * Output parameters : ctx - ctx point to the data buffer
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/10       : Creation
 *
 * Date              : April 10 2005, Frank Zhang
 **************************************************************************/
void CtxInitial(TCtx *ctx, char *pData, int iLen)
{
  Assert(ctx);
  Assert(pData);
  ctx->pHead = pData;
  ctx->pTail = pData + iLen;
  ctx->pCur = pData;
  SListInit(&ctx->MemList);
}
/***************************************************************************
 * Function          : CtxInitial
 *
 * Description       : Free all the memories which pointer stored in the 
 *                     memory list in ctx and initalize ctx
 *                                        
 * Input parameters  : ctx - pointer ot ctx
 *
 * Output parameters : ctx - all memories in memlist freed
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/10       : Creation
 *
 * Date              : April 10 2005, Frank Zhang
 **************************************************************************/
void CtxFree(TCtx *ctx)
{
  SListFreeAll(&ctx->MemList);
  ctx->pCur = NULL;
  ctx->pHead = NULL;
  ctx->pTail = NULL;
}
/***************************************************************************
 * Function          : AbnfAlloc
 *
 * Description       : Allocate a memory and store its pointer into ctx
 *                     
 * Input parameters  : ctx - pointer to ctx
 *                     size - memory size to be allocated
 *
 * Output parameters : ctx - a new memory pointer appended into ctx
 *
 * Return value      : The pointer to allocated memory, if fail to allocate 
 *                     return NULL
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/10       : Creation
 *
 * Date              : April 10 2005, Frank Zhang
 **************************************************************************/
void* AbnfAlloc(TCtx *ctx, size_t size)
{
  void *p = calloc(1, size);
  Assert(ctx);
   Assert(p);
//   memset(p, 0, size);
  if (p != NULL)
    SListAppend(&ctx->MemList, p);

  return p;
}
/***************************************************************************
 * Function          : AbnfAlloc
 *
 * Description       : Reallocate a memory and store its pointer into ctx, if
 *                     memory reallocate succeed, change the memory pointer in
 *                     ctx to be the new or add it, if fail to reallocate, 
 *                     remain the pointer unchanged
 *                     
 * Input parameters  : ctx - pointer to ctx
 *                     pData - pointer to original allocated memory
 *                     size - memory size to be reallocated
 *
 * Output parameters : ctx - original memory pointer changed to be the new 
 *                           allocated memory pointer
 *
 * Return value      : The pointer to reallocated memory, if fail to allocate 
 *                     return NULL
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/10       : Creation
 *
 * Date              : April 10 2005, Frank Zhang
 **************************************************************************/
void* AbnfReAlloc(TCtx *ctx, void *pData, size_t size)
{
  void *p = NULL;
  TSListNode* pNode ;

   Assert(ctx);
  
  if (pData == NULL) /* Original pointer is null, allocate new */
  {
    p = malloc(size);
	memset(p, 0, size);
    if (p != NULL)
      SListAppend(&ctx->MemList, p);

    return p;
  }
  
  /* Reallocate the original pointer */
  /* Find the list node store the original pointer */
  pNode = SListFindByPointer(&ctx->MemList, pData);
    
  if (size == 0) /* Size is 0, original pointer is not NULL, free it */
  {
    free(pData);

    /* No need to delete the node, only change the data to be NULL */
    if (pNode != NULL)
      pNode->data = NULL;

    return NULL;
  }
  else /* Size is not 0, original pointer is not NULL, reallocate it */
  {
    p = realloc(pData,size);

    if (p != NULL) /* Reallocate succeed */
    {
      if (pNode != NULL)
        pNode->data = p;
      else
        SListAppend(&ctx->MemList, p);
    }

    return p;
  }
}
/***************************************************************************
 * Function          : AbnfDetachMem
 *
 * Description       : Detach the allocated memory pointer stored in the memeory
 *                     list of the CTX
 *                     
 * Input parameters  : ctx - pointer to ctx
 *                     pMemPointer - pointer of the memory te be detached
 *
 * Output parameters : ctx - memory list changed
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/08/23       : Creation
 *
 * Date              : Aug 23 2005, Frank Zhang
 **************************************************************************/
void AbnfDetachMem(TCtx *ctx, void *pMemPointer)
{
   Assert(ctx);
  SListDelNodeByData(&ctx->MemList, pMemPointer);
}

/***************************************************************************
 * Function          : ListNewNode
 *
 * Description       : Add a new node into list and intialize the content of
 *                     the new node to be zero filled
 *                     
 * Input parameters  : ctx - pointer to ctx
 *                     list - pointer to list into which a new node added
 *                     size - size of the node in list
 *
 * Output parameters : ctx - memory pointer stored in list changed
 *                     list - a new node added
 *
 * Return value      : If new node add successfully, return the pointer to the 
 *                     new node; if fail to add new node return NULL
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/10       : Creation
 *
 * Date              : April 10 2005, Frank Zhang
 **************************************************************************/
void* ListNewNode(TCtx *ctx, TList *list, size_t size)
{
  char *pOffsetPnt = NULL;
  void *p;

   Assert(ctx);
   Assert(list);
  
  p = AbnfReAlloc(ctx, list->pNode, size*((size_t)list->iNum+1));
  Assert(p);

  /* Reallocate memory for list successfully */
  if (p != NULL)
  {
    list->pNode = p;
    pOffsetPnt = (char*)list->pNode + size*((size_t)list->iNum);
    memset(pOffsetPnt, 0, size);
    list->iNum++;
  }
  
  return pOffsetPnt;
}
/***************************************************************************
 * Function          : ListRemoveLast
 *
 * Description       : Remove the last node in the list, only decrease the
 *                     node number, not realy free it because ctx recorder
 *                     the dynamic memory pointer allocated
 *                     
 * Input parameters  : list - pointer to list
 *                     
 * Output parameters : list - last node removed if exist
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/10       : Creation
 *
 * Date              : April 10 2005, Frank Zhang
 **************************************************************************/
void ListRemoveLast(TList *list)
{
  Assert(list);
  if (list->iNum > 0)
    list->iNum--;
}
/***************************************************************************
 * Function          : ParseString
 *
 * Description       : Parse a string repetition in abnf message
 *                     
 * Input parameters  : ctx - pointer to decode context
 *                     iRepmin - min repeat of string
 *                     iRepmax - max repeat of string
 *                     string - string to be parsed
 *
 * Output parameters : ctx - if succeed to parse, ctx changed
 *
 * Return value      : FAIL - fail to parse string
 *                     OK - succeed to parse string
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/10       : Creation
 *
 * Date              : April 10 2005, Frank Zhang
 **************************************************************************/
int ParseString(TCtx *ctx, int iRepmin, int iRepmax, char *string)
{
  int iCount = 0;
  size_t iLen = strlen(string);

  Assert(ctx);
  
  while (iCount < iRepmax)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?