AMPS:字符串操作源码解读

来源:互联网 发布:德邦证券软件 编辑:程序博客网 时间:2024/06/06 17:29

  字符串处理在软件中非常普遍,AMPS使用C语言编写,所有没有像 标准C++提供的string一样的类型,所以对字符串的处理就是对char *这样的指针处理,为了处理方便,AMPS定义了自己的String类型,如下:

typedef struct _AMPSStringt_AMPSString;struct _AMPSString{unsigned char*puchData;//pointer to the message dataunsigned intunLength;//size of buffer};

并实现了对这个String的各类操作,这样,在使用者来看,就跟使用标准c++库中的string类型一样了。

下面是AMPS中String类型相关操作的源程序:

AMPS_String.h

#ifndef __HEADER_AMPS_STRING_H#define __HEADER_AMPS_STRING_H#ifdef __cplusplusextern "C" {#endif#include "AMPS_Defines.h"int String_IntToString(unsigned char* r_puchBuffer, int r_nValue, int r_nBase);int String_StringToInt(t_AMPSString* r_poAMPSString, int r_nBase);int String_CopyDuplicate(t_AMPSString* r_poAMPSStringDst, t_AMPSString* r_poAMPSStringSrc);void String_Free(t_AMPSString* r_poAMPSString);void String_Move(t_AMPSString* r_poAMPSStringSrc, t_AMPSString* r_poAMPSStringDst);int String_Set(char* r_pchSrc, t_AMPSString* poAMPSStringDst);int String_SetN(char* r_pchSrc, unsigned int r_unSrcLength, t_AMPSString* r_poAMPSStringDst);t_AMPSString* String_Alloc(void);int String_AllocBuffer(t_AMPSString* r_poAMPSString, unsigned int r_unSize);t_AMPSString* String_Duplicate(t_AMPSString* r_poAMPSString);int String_Compare(t_AMPSString* r_poAMPSString1, t_AMPSString* r_poAMPSString2);// Case insensitiveint String_CompareCase(t_AMPSString* r_poAMPSString1, t_AMPSString* r_poAMPSString2);int String_Print(int r_nTraceID, int r_nTraceLevel, char* r_pchPrefix, t_AMPSString* r_poAMPSString);int String_PrintN(int r_nTraceID, int r_nTraceLevel, char* r_pchPrefix, unsigned char* r_puchData, unsigned int r_unLength);int String_GenerateRandomString(t_AMPSString* r_poAMPSString);unsigned int String_GetOffsetOfChar(t_AMPSString* r_poAMPSString, unsigned int r_unStartOffset, unsigned char r_ucSeachChar);int String_CopySubString(t_AMPSString* r_poAMPSStringSrc, t_AMPSString* r_poAMPSStringDst, unsigned int r_unStartOffset, unsigned int r_unEndOffset);int String_AsciiDigitToNumber(char r_chDigit);char String_NumToAsciiDigit(int r_nNum);char* String_GetStringZ(t_AMPSString* r_poAMPSString);int String_CompareSubString(t_AMPSString* r_poAMPSString, t_AMPSString* poAMPSStringSubString);int String_Assign(char* r_pchSrc, t_AMPSString* r_poAMPSStringDst);int String_AssignN(char* r_pchSrc, unsigned int r_unSrcLength, t_AMPSString* r_poAMPSStringDst);unsigned long String_StringToUL(t_AMPSString* r_poAMPSString, int r_nBase);#ifdef __cplusplus}#endif#endif //__HEADER_AMPS_STRING_H

AMPS_String.c

/*****************************************************************文件名称: AMPS_String.c功能描述: 字符串操作函数库*****************************************************************/#include "AMPS_Core.h"#include "AMPS_Defines.h"#include "AMPS_MemMgt.h"#include "AMPS_Components.h"#include "AMPS_String.h"/*****************************************************************函数名称: String_CopyDuplicate功能描述: 字符串拷贝入参::      t_AMPSString* poDstString_i 目标串      t_AMPSString* poSrcString_i 源串出参:      返回值:      int*****************************************************************/int String_CopyDuplicate( t_AMPSString* poDstString_i, t_AMPSString* poSrcString_i ){    /*清空目标串*/String_Free( poDstString_i );poDstString_i->unLength = poSrcString_i->unLength;poDstString_i->puchData = AMPS_Malloc( poDstString_i->unLength );if( NULL == poDstString_i->puchData ){poDstString_i->unLength = 0;return (AMPS_ERROR_FAILURE);}memcpy( poDstString_i->puchData, poSrcString_i->puchData, poDstString_i->unLength );return (AMPS_SUCCESS);}/*****************************************************************函数名称: String_IntToString功能描述: int转换为字符串入参::     unsigned char* puchBuffer_i 目标串     int nValue_i 待转换的int值     int nBase_i  基数出参:      返回值:      int*****************************************************************/#define MAX_LENGTH  32int String_IntToString( unsigned char* puchBuffer_i, int nValue_i, int nBase_i ){int nLen = 0;unsigned char pucReverse[MAX_LENGTH+1]; // plus one for the nullunsigned char* pucTemp;    /*是否为负数标记*/char cSign = nValue_i < 0;    /*取绝对值*/nValue_i = abs( nValue_i );pucReverse[MAX_LENGTH] = 0;pucTemp = pucReverse+MAX_LENGTH;    /*从个位起,接每一位取出存放在pucTemp中,从右至左放*/do{*--pucTemp = "0123456789abcdefghijklmnopqrstuvwxyz"[nValue_i % nBase_i];nValue_i /= nBase_i;}while( nValue_i );    /*负数前面加上-*/if( cSign ){*--pucTemp = '-';}nLen = strlen((char*)pucTemp );memcpy( puchBuffer_i, pucTemp, nLen );puchBuffer_i[nLen] = '\0';return (nLen);}/*****************************************************************函数名称: String_Free功能描述: 字符串清空入参::      t_AMPSString* poString_i 待清空的字符串出参:      返回值:      void*****************************************************************/void String_Free( t_AMPSString* poString_i ){if( NULL != poString_i->puchData ){AMPS_Free( poString_i->puchData );}poString_i->puchData = NULL;poString_i->unLength = 0;}/*****************************************************************函数名称: String_Move功能描述: 字符串移动(将A的值移入B,然后A清空)入参::     t_AMPSString* poSrc_i 源串     t_AMPSString* poDest_i 目标串出参:      返回值:      void*****************************************************************/void String_Move( t_AMPSString* poSrc_i, t_AMPSString* poDest_i ){String_Free( poDest_i );poDest_i->puchData = poSrc_i->puchData;poDest_i->unLength = poSrc_i->unLength;poSrc_i->puchData = NULL;poSrc_i->unLength = 0;}/*****************************************************************函数名称: String_Alloc功能描述: 字符串分配内存并初始化入参::      void出参:      返回值:      t_AMPSString******************************************************************/t_AMPSString* String_Alloc( void ){t_AMPSString* poString = NULL;poString = AMPS_Malloc( sizeof (t_AMPSString));if( NULL != poString ){memset( poString, 0, sizeof (t_AMPSString));}return (poString);}/*****************************************************************函数名称: String_StringToInt功能描述: 字符串转为整型入参::      t_AMPSString* poString_i 待操作字符串      int nBase_i              基值出参:      返回值:      int*****************************************************************/int String_StringToInt( t_AMPSString* poString_i, int nBase_i ){int nValue = 0;unsigned int unDigitCount = 0;while( unDigitCount < poString_i->unLength ){        /*pow(x,y)表示x的y次冪*/nValue += (poString_i->puchData[poString_i->unLength - unDigitCount - 1] - '0') * (int)(pow( nBase_i, unDigitCount ) );unDigitCount++;}return (nValue);}/*****************************************************************函数名称: String_StringToInt功能描述: 字符串转为无符号长整型入参::      t_AMPSString* poString_i 待操作字符串      int nBase_i              基值出参:      返回值:      unsigned long***************************************************************/unsigned long String_StringToUL( t_AMPSString* poString_i, int nBase_i ){unsigned long ulValue = 0L;unsigned int unDigitCount = 0;while( unDigitCount < poString_i->unLength ){ulValue += (poString_i->puchData[poString_i->unLength - unDigitCount - 1] - '0') * (int)(pow( nBase_i, unDigitCount ) );unDigitCount++;}return (ulValue);}/*****************************************************************函数名称: String_StringToInt功能描述: 字符串赋值入参::      char* pcSrc_i  值      t_AMPSString* poDest_i 目标字符串出参:      返回值:      int***************************************************************/int String_Set( char* pcSrc_i, t_AMPSString* poDest_i ){int nRetVal = AMPS_SUCCESS;unsigned int unSrcLength = 0;unSrcLength = strlen( pcSrc_i );nRetVal = String_SetN( pcSrc_i, unSrcLength, poDest_i );return (nRetVal);}/*****************************************************************函数名称: String_SetN功能描述: 字符串赋值入参::      char* pcSrc_i  值      unsigned int unSrcLength 指定的长度      t_AMPSString* poDest_i 目标字符串出参:      返回值:      int***************************************************************/int String_SetN( char* pcSrc_i, unsigned int unSrcLength, t_AMPSString* poDest_i ){int nRetVal = AMPS_SUCCESS;String_Free( poDest_i );poDest_i->puchData = AMPS_Malloc( unSrcLength );if( NULL == poDest_i->puchData ){nRetVal = AMPS_ERROR_FAILURE;}else{memcpy( poDest_i->puchData, pcSrc_i, unSrcLength );poDest_i->unLength = unSrcLength;}return (nRetVal);}/*****************************************************************函数名称: String_Duplicate功能描述: 字符串赋值入参::      t_AMPSString* poString_i出参:      返回值:      t_AMPSString ****************************************************************/t_AMPSString* String_Duplicate( t_AMPSString* poString_i ){t_AMPSString* poDest = NULL;if( poString_i->puchData != NULL ){poDest = String_Alloc();if( NULL != poDest ){if( AMPS_SUCCESS != String_CopyDuplicate( poDest, poString_i ) ){AMPS_Free( poDest );poDest = NULL;}}}return (poDest);}/*****************************************************************函数名称: String_Compare功能描述: 字符串比较入参::      t_AMPSString* poString1_i 字符串1      t_AMPSString* poString2_i 字符串2出参:      返回值:      int ***************************************************************/int String_Compare( t_AMPSString* poString1_i, t_AMPSString* poString2_i ){    /*以两者中长度最小串的长度*/    unsigned int unMinLength = (poString1_i->unLength < poString2_i->unLength ? poString1_i->unLength : poString2_i->unLength);return (strncmp((char*)poString1_i->puchData, (char*)poString2_i->puchData, unMinLength ));}/*****************************************************************函数名称: String_Compare功能描述: 字符串比较,大小写不敏感入参::      t_AMPSString* poString1_i 字符串1      t_AMPSString* poString2_i 字符串2出参:      返回值:      int ***************************************************************/int String_CompareCase( t_AMPSString* poString1_i, t_AMPSString* poString2_i ){unsigned int unMinLength = (poString1_i->unLength < poString2_i->unLength ? poString1_i->unLength : poString2_i->unLength);//return (strncasecmp((char*)poString1_i->puchData, (char*)poString2_i->puchData, unMinLength ));return(AMPS_StrnCaseCMP(NULL, (char*)poString1_i->puchData, (char*)poString2_i->puchData, unMinLength));}/*****************************************************************函数名称: String_Compare功能描述: 比较子串,返回匹配的位置入参::      t_AMPSString* poString1_i 字符串      t_AMPSString* poSubString_i  子字符串出参:      返回值:      int ***************************************************************/int String_CompareSubString( t_AMPSString* poString_i, t_AMPSString* poSubString_i ){unsigned int unOffset = 0;unsigned int unMatchOffset = (unsigned int)-1;unsigned int unMaxCmpSize = 0;unMaxCmpSize = poString_i->unLength - poSubString_i->unLength + 1;for( unOffset = 0; unOffset < unMaxCmpSize; unOffset++){if( 0 == memcmp(&poString_i[unOffset], poSubString_i, poSubString_i->unLength)){unMatchOffset = unOffset;break;}}return (int)unMatchOffset;}/*****************************************************************函数名称: String_Print功能描述: 打印字符串入参::      t_AMPSString* poString1_i 字符串      t_AMPSString* poSubString_i  子字符串出参:      返回值:      int ***************************************************************/int String_Print( int nTraceId_i, int nTraceLevel_i, char* pchPrefix_i, t_AMPSString* poString_i ){return (String_PrintN( nTraceId_i, nTraceLevel_i, pchPrefix_i, poString_i->puchData, poString_i->unLength ));}/*****************************************************************函数名称: String_PrintN功能描述: 打印字符串入参::      t_AMPSString* poString1_i 字符串      t_AMPSString* poSubString_i  子字符串出参:      返回值:      int ***************************************************************/int String_PrintN( int nTraceId_i, int nTraceLevel_i, char* pchPrefix_i, unsigned char* puchData_i, unsigned int unLength_i ){char pchFormat[] = "%s(%d)%.*s\n";if( unLength_i == 0 || NULL == puchData_i ){strcpy( pchFormat, "%s%s\n" );TRACE( nTraceId_i, nTraceLevel_i, pchFormat, pchPrefix_i, "(null)" );}else{TRACE( nTraceId_i, nTraceLevel_i, pchFormat, pchPrefix_i, unLength_i, unLength_i, puchData_i );}return (AMPS_TRUE);}/*****************************************************************函数名称: String_GenerateRandomString功能描述: 生成随机字符串入参::      t_AMPSString* poStr_i出参:      t_AMPSString* poStr_i    返回值:      int ***************************************************************/int String_GenerateRandomString( t_AMPSString* poStr_i ){#define znew   (z = 36969*(z&65535)+(z>>16))#define wnew   (w = 18000*(w&65535)+(w>>16))#define MWC    ((znew<<16)+wnew)#define SHR3  (jsr ^= (jsr<<17), jsr ^= (jsr>>13), jsr ^= (jsr<<5))#define CONG  (jcong = 69069*jcong+1234567)#define FIB   ((b = a+b), (a = b-a))#define KISS  ((MWC^CONG)+SHR3)#define LFIB4 (c++, t[c] = t[c]+t[UC( c+58 )]+t[UC( c+119 )]+t[UC( c+178 )])#define SWB   (c++, bro = (x < y), t[c] = (x = t[UC( c+34 )])-(y = t[UC( c+19 )]+bro))#define UNI   (KISS*2.328306e-10)#define VNI   ((long) KISS)*4.656613e-10#define UC    (unsigned char)  /*a cast operation*/typedef unsigned long UL;/*  Global static variables: */static UL z = 362436069, w = 521288629, jsr = 123456789, jcong = 380116160;static UL t[256];static UL x = 0, y = 0, bro;static unsigned char c = 0;unsigned long nRandomVal = 0;int nOffset = 0;unsigned char puchIntString[MAX_LENGTH];int nIntStringSize = 0;int nCount = 0;for( nCount = 0; nCount < 1; nCount++ ){nRandomVal += (KISS + SWB);memset( puchIntString, 0, sizeof (puchIntString));nIntStringSize = String_IntToString( puchIntString, nRandomVal, 10 );memcpy( poStr_i->puchData+nOffset, puchIntString, nIntStringSize );nOffset += nIntStringSize;}poStr_i->unLength = nOffset;return (AMPS_SUCCESS);}/*****************************************************************函数名称: String_AllocBuffer功能描述: 字符串分配内存并初始化入参::      t_AMPSString* poString_i      unsigned int unSize_i 长度出参:         返回值:      int ***************************************************************/int String_AllocBuffer( t_AMPSString* poString_i, unsigned int unSize_i ){poString_i->puchData = AMPS_Malloc( unSize_i );if( NULL == poString_i->puchData ){return (AMPS_ERROR_FAILURE);}memset( poString_i->puchData, 0, unSize_i );poString_i->unLength = unSize_i;return (AMPS_SUCCESS);}/*****************************************************************函数名称: String_GetOffsetOfChar功能描述: 获取指定字符在字符串中的位置入参::      t_AMPSString* poString_i      unsigned int unStartOffset_i 开始位置      unsigned char ucSeachChar_i 要查找的字符      出参:          返回值:      int ***************************************************************/unsigned int String_GetOffsetOfChar( t_AMPSString* poString_i, unsigned int unStartOffset_i, unsigned char ucSeachChar_i ){unsigned int unOffset = 0;for( unOffset = unStartOffset_i; unOffset < poString_i->unLength; unOffset++ ){if( poString_i->puchData[unOffset] == ucSeachChar_i ){break;}}return (unOffset);}/*****************************************************************函数名称: String_GetOffsetOfChar功能描述: 获取指定字符在字符串中的位置入参::      t_AMPSString* poString_i      unsigned int unStartOffset_i 开始位置      unsigned char ucSeachChar_i 要查找的字符      出参:         返回值:      int ***************************************************************/int String_CopySubString( t_AMPSString* poSrcString_i, t_AMPSString* poDstString_i, unsigned int unStartOffset_i, unsigned int unEndOffset_i ){return (String_SetN((char*)&poSrcString_i->puchData[unStartOffset_i], unEndOffset_i - unStartOffset_i, poDstString_i ));}// According to RFC-2833/*****************************************************************函数名称: String_AsciiDigitToNumber功能描述: ASCII 字符转数字入参::      char cDigit_i ASCII 字符      出参:          返回值:      int ***************************************************************/int String_AsciiDigitToNumber( char cDigit_i ){int nNum = 0;if( 0 != isdigit( cDigit_i ) ){nNum = cDigit_i - '0';}else if( 0 != isalpha( cDigit_i ) ){nNum = cDigit_i - 'a' + 12;}else if( cDigit_i == '*' ){nNum = 10;}else if( cDigit_i == '#' ){nNum = 11;}//  printf("String_AsciiDigitToNumber digit = %c, number = %d\n",cDigit_i, nNum);return (nNum);}/*****************************************************************函数名称: String_NumToAsciiDigit功能描述: 数字传ASCII字符入参::      int nNum_i 数字出参:          返回值:      char ***************************************************************/char String_NumToAsciiDigit( int nNum_i ){char cDigit = 0;if( 0 <= nNum_i && 9 >= nNum_i ){cDigit = (char)(nNum_i + '0');}else if( nNum_i == 10 ){cDigit = '*';}else if( nNum_i == 11 ){cDigit = '#';}else{cDigit = (char)(nNum_i - 12 + 'a');}//printf("String_NumToAsciiDigit number = %d, digit = %d\n",nNum_i, cDigit);return (cDigit);}/**************************************************************函数名称: String_GetStringZ功能描述: 获取字符串内容入参::      t_AMPSString* poString_i出参:          返回值:      char ****************************************************************/char* String_GetStringZ( t_AMPSString* poString_i ){char* pcStr = NULL;pcStr = AMPS_Malloc( poString_i->unLength + 1 );if( NULL != pcStr ){strncpy( pcStr, (char*)poString_i->puchData, poString_i->unLength );pcStr[poString_i->unLength] = '\0';}return (pcStr);}/**************************************************************函数名称: String_Assign功能描述: 字符串赋值入参::      char* pcSrc_i      t_AMPSString* poDest_i出参:      t_AMPSString* poDest_i    返回值:      char ****************************************************************/int String_Assign( char* pcSrc_i, t_AMPSString* poDest_i ){int nRetVal = AMPS_SUCCESS;unsigned int unSrcLength = 0;unSrcLength = strlen( pcSrc_i );nRetVal = String_AssignN( pcSrc_i, unSrcLength, poDest_i );return (nRetVal);}/**************************************************************函数名称: String_AssignN功能描述: 字符串赋值(指定长度)入参::      char* pcSrc_i            内容      unsigned int unSrcLength 长度      t_AMPSString* poDest_i   字符串出参:      t_AMPSString* poDest_i    返回值:      char ****************************************************************/int String_AssignN( char* pcSrc_i, unsigned int unSrcLength, t_AMPSString* poDest_i ){int nRetVal = AMPS_SUCCESS;String_Free( poDest_i );poDest_i->puchData = (unsigned char*)pcSrc_i;poDest_i->unLength = unSrcLength;return (nRetVal);}



原创粉丝点击