parser.c
来自「mgcp协议源代码和测试程序,还有一个编译器」· C语言 代码 · 共 641 行 · 第 1/2 页
C
641 行
{
if (strncmp(ctx->pCur, string, iLen) == 0
&& ctx->pCur+iLen <= ctx->pTail) /* Avoid curren pointer over boundary */
{
iCount++;
ctx->pCur += strlen(string);
}
else break;
}
if (iCount < iRepmin)
return FAIL;
else return OK;
}
/***************************************************************************
* Function : EncodeString
*
* Description : Encode a string repetition into abnf message
*
* Input parameters : ctx - pointer to encode context
* iRepmin - min repeat of string
* iRepmax - max repeat of string
* string - string to be encoded
*
* Output parameters : ctx - if succeed to encode string, ctx changed
*
* Return value : FAIL - fail to encode string
* OK - succeed to encode string
*
* Comments :
*
* History :
* 2005/04/10 : Creation
*
* Date : April 10 2005, Frank Zhang
**************************************************************************/
int EncodeString(TCtx *ctx, int iRep, char *string)
{
int iCount = 0;
size_t iLen = 0;
Assert(ctx);
Assert(string);
iLen = strlen(string);
while (iCount < iRep)
{
if (ctx->pCur+iLen <= ctx->pTail) /* Avoid curren pointer over boundary */
{
strncpy(ctx->pCur, string, iLen);
iCount++;
ctx->pCur += strlen(string);
}
else return FAIL;
}
return OK;
}
/***************************************************************************
* Function : ParseChar
*
* Description : Parse a char repetition in abnf message
*
* Input parameters : ctx - pointer to decode context
* iRepmin - min repeat of char repetition
* iRepmax - max repeat of char repetition
* ValBegin - min value of char
* ValEnd - max value of chat
*
* Output parameters : ctx - if succeed to parse, ctx changed
*
* Return value : FAIL - fail to parse char
* OK - succeed to parse char
*
* Comments :
*
* History :
* 2005/04/10 : Creation
*
* Date : April 10 2005, Frank Zhang
**************************************************************************/
int ParseChar(TCtx *ctx, int iRepmin, int iRepmax, unsigned char ValBegin,
unsigned char ValEnd)
{
int iCount = 0;
Assert(ctx);
while (iCount < iRepmax)
{
if (*ctx->pCur >= ValBegin
&& *ctx->pCur <= ValEnd
&& ctx->pCur < ctx->pTail) /* Avoid curren pointer over boundary */
{
iCount++;
ctx->pCur++;
}
else break;
}
if (iCount < iRepmin)
return FAIL;
else return OK;
}
/***************************************************************************
* Function : EncodeChar
*
* Description : encode a char repetition into abnf message
*
* Input parameters : ctx - pointer to encode context
* iRepmin - min repeat of char repetition
* iRepmax - max repeat of char repetition
* ValBegin - min value of char
* ValEnd - max value of chat
*
* Output parameters : ctx - if succeed to encode, ctx changed
*
* Return value : FAIL - fail to encode char
* OK - succeed to encode char
*
* Comments :
*
* History :
* 2005/04/10 : Creation
*
* Date : April 10 2005, Frank Zhang
**************************************************************************/
int EncodeChar(TCtx *ctx, int iRep, unsigned char ucValue)
{
int iCount = 0;
Assert(ctx);
while (iCount < iRep)
{
if (ctx->pCur < ctx->pTail) /* Avoid curren pointer over boundary */
{
iCount++;
*ctx->pCur = (char)ucValue; /* Only encode the lower value into ctx */
ctx->pCur++;
}
else return FAIL;
}
return OK;
}
/***************************************************************************
* Function : ParseToken
*
* Description : Parse token repetition in abnf message
*
* Input parameters : ctx - pointer to decode context
* iRepmin - min repeat of token repetition
* iRepmax - max repeat of token repetition
* decfunc - pointer to token decode function
*
* Output parameters : ctx - if succeed to parse, ctx changed
*
* Return value : FAIL - fail to parse token repetition
* OK - succeed to parse token repetition
*
* Comments :
*
* History :
* 2005/04/10 : Creation
*
* Date : April 10 2005, Frank Zhang
**************************************************************************/
int ParseToken(TCtx *ctx, int iRepmin, int iRepmax, DecodeFunc decfunc)
{
int iCount = 0;
Assert(ctx);
Assert(decfunc);
while (iCount < iRepmax)
{
if (decfunc(ctx) == OK)
iCount++;
else break;
}
if (iCount < iRepmin)
return FAIL;
else return OK;
}
/***************************************************************************
* Function : EncodeToken
*
* Description : Encode token repetition into abnf message
*
* Input parameters : ctx - pointer to encode context
* iRepmin - min repeat of token repetition
* iRepmax - max repeat of token repetition
* encfunc - pointer to token encode function
*
* Output parameters : ctx - if succeed to encode, ctx changed
*
* Return value : FAIL - fail to encode token repetition
* OK - succeed to encode token repetition
*
* Comments :
*
* History :
* 2005/04/10 : Creation
*
* Date : April 10 2005, Frank Zhang
**************************************************************************/
int EncodeToken(TCtx *ctx, int iRep, EncodeFunc encfunc)
{
int iCount = 0;
Assert(ctx);
Assert(encfunc);
while (iCount < iRep)
{
if (encfunc(ctx) == OK)
iCount++;
else return FAIL;
}
return OK;
}
/***************************************************************************
* Function : AbnfStrnClone
*
* Description : Copy n chars into a new allocate string from the src string
*
* Input parameters : ctx - pointer to ctx
* Head - pointer to header char to be copied
* Tail - pointer to tail char to be copied
*
* Output parameters : ctx - store the allocate memory pointer
*
* Return value : If succeed, return the pointer of the new string;
* If fail, return NULL
*
* Comments :
*
* History :
* 2005/04/10 : Creation
*
* Date : April 10 2005, Frank Zhang
**************************************************************************/
char* AbnfStrnClone(TCtx *ctx, char *Head, char *Tail)
{
char *pTmp = NULL;
int iLen = Tail - Head;
Assert(ctx);
Assert(Head);
Assert(Tail);
if (iLen > 0)
{
if ((pTmp = (char*)AbnfAlloc(ctx, (size_t)iLen+1)) != NULL)
{
strncpy(pTmp, Head, (size_t)iLen);
pTmp[iLen] = '\0';
}
}
return pTmp;
}
/***************************************************************************
* Function : ParseStrRule
*
* Description : Parse a StringRule repetition in abnf message
*
* Input parameters : ctx - pointer to decode context
* iRepmin - min repeat of repetition
* iRepmax - max repeat of repetition
* ppdata - pointer to a string point which pointe to
* the parse result
* decfunc - pointer of decode function
*
* Output parameters : ctx - if succeed to parse, ctx changed
* ppdata - if succeed to parse, create a new string
* and return the sting pointer, otherwise
* return NULL
*
* 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 ParseStrRule(TCtx *ctx, char **ppdata, int iRepmin, int iRepmax, DecodeFunc decfunc)
{
int iCount = 0;
/* Save the entry pointer */
char *pEntry = ctx->pCur;
Assert(ctx);
Assert(ppdata);
Assert(decfunc);
while (iCount < iRepmax)
{
if (decfunc(ctx) == OK)
iCount++;
else break;
}
if (iCount < iRepmin)
{
*ppdata = NULL;
return FAIL;
}
*ppdata= AbnfStrnClone(ctx, pEntry, ctx->pCur);
return OK;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?