Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1184974
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 16:11:27


点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>

  4. #define FMLFLD_NOTARRAY 0 /* 非数组(用于下标参数) */
  5. #define FMLFLD_OPTIONAL 0 /* 可选域 */
  6. #define FMLFLD_REQUIRED 1 /* 必需域 */
  7. #define    SYM_FML_FS    0x40    /* 域分隔符0x1E*/
  8. #define    SYM_FML_NV    ':'     /* 名值分隔符*/
  9. #define    SYM_FML_AN    '#'     /* 数组下标起始符*/
  10. #define RC_SUCC 0
  11. #define RC_NFND -1
  12. #define RC_NSPC -2

  13. #define SZ_DATA_BUS (32*1024) /* 数据总线宽度 */
  14. typedef char T_DATABUS[SZ_DATA_BUS]; /* 数据总线 */

  15. void FmlDataClear(char *    cFmlData);
  16. char *FmlFieldSeek(const char *cFmlData, const char *cFieldName,int    iIndex);
  17. int    FmlFieldPut(char *cFmlData,int iBufSize,char *cFieldName,int iIndex,char *cFieldValue);
  18. int FmlFieldGet(const char *cFmlData,const char *cFieldName,int    iIndex,    char *cFieldValue,int iValueSize);
  19. int    FmlFieldNum(const char *cFmlData, const char *cFieldName, int *iNum);

  20. /*FmlDataClear 函数功能:清空数据总线函数*/
  21. void FmlDataClear(char *    cFmlData)
  22. {
  23.     cFmlData[0] = 0;
  24. }

  25. /*FmlFieldErase函数:擦除总线数据中的指定域,没有这个域就不擦除*/
  26. void    FmlFieldErase(char *cFmlData, const char *cFieldName,int iIndex)
  27. {
  28.     char    *p, *q;
  29.     if ( ( p = FmlFieldSeek( cFmlData, cFieldName, iIndex ) ) == NULL )
  30.         return; /*p指向指定的域名的起始地址*/
  31.     if ( ( q = strchr( p, SYM_FML_FS ) ) == NULL ) /*SYM_FML_FS:域分割符^^*/
  32.         q = p + strlen( p );
  33.     else
  34.         q ++; /*q指向域尾*/
  35.     strcpy( p, q ); /*p到q之间的被跳过*/
  36. }

  37. /*FmlFieldSeek函数功能:检索总线中指定域函数,返回指定域的起始地址*/
  38. char *FmlFieldSeek(const char *cFmlData, const char *cFieldName,int    iIndex)
  39. {
  40.     char    *p = (char *) cFmlData;
  41.     char    *n, *q, *v;
  42.     int    r;

  43.     while ( ( n = strchr( p, SYM_FML_FS ) ) != NULL )/*SYM_FML_FS:域分割符^^*/
  44.     {    
  45.         *n = 0; /*n指向下一个域分割符^^,并截断*/
  46.         q = strchr( p, SYM_FML_NV ); /*SYM_FML_NV:名值分割符:*/
  47.         *n = SYM_FML_FS; /*恢复域分割符,重新连上总线*/
  48.         if ( q != NULL )
  49.         {
  50.             *q = 0; /*q指向名值分割符:,并截断*/
  51.             if ( ( v = strchr( p, SYM_FML_AN ) ) != NULL ) /*0下标时,v=NULL*/
  52.                 *v = 0; /*v指向数组下标起始符#,并截断*/
  53.             if ( ( r = strcmp( p, cFieldName ) ) == 0 ) /*比较p指向的是不是待检索的域名*/
  54.             {
  55.                 if ( v == NULL && iIndex != FMLFLD_NOTARRAY || v != NULL && atoi( v + 1 ) != iIndex )
  56.                     r = -1; /*对下标值的检查,出错时r=-1*/
  57.             }
  58.             if ( v != NULL )
  59.                 *v = SYM_FML_AN; /*重新连上总线*/
  60.             *q = SYM_FML_NV; /*重新连上总线*/
  61.             if ( r == 0 ) /*域名匹配,下标也正确时,查找完毕*/
  62.                 return p; /*返回域名起始地址*/
  63.         }
  64.         p = n + 1; /*p指向下一个域名起始地址,继续循环查找*/
  65.     }
  66.     return NULL;
  67. }


  68. /*FmlFieldPut() 功能描述: 为总线中指定域赋值函数*/
  69. int    FmlFieldPut(char *cFmlData,int iBufSize,char *cFieldName,int iIndex,char *cFieldValue)
  70. {
  71.     int    s, l;
  72.     char    a[12];

  73.     FmlFieldErase( cFmlData, cFieldName, iIndex ); /*擦除指定域名域值*/

  74.     if ( iIndex == FMLFLD_NOTARRAY ) /*FMLFLD_NOTARRAY=0,判断是否是0下标*/
  75.         s = 0; /*0下标时,没有数组下标起始符#,也没有小标值0的显示*/
  76.     else
  77.         s = snprintf( a, sizeof( a ), "%d", iIndex ) + 1; /*下标的长度+#号码长度*/
  78.     if ( ( l = strlen( cFmlData ) ) + strlen( cFieldName ) + s + strlen( cFieldValue ) + 2 > iBufSize )
  79.         return RC_NSPC; /*判断是否溢出,其中2=域分隔符+'\0'*/
  80.     if ( l > 0 && cFmlData[l-1] != SYM_FML_FS ) /*SYM_FML_FS域分割符^^,原串没有域分割符时*/
  81.     {
  82.         cFmlData[l] = SYM_FML_FS; /*原串尾加上域分隔符*/
  83.         l ++; /*原串长+1*/
  84.     }

  85.     if ( s != 0 ) /*非0下标时*/
  86.         /*a中保存的是下标值*/
  87.         sprintf( cFmlData + l, "%s%c%s%c%s%c", cFieldName, SYM_FML_AN, a, SYM_FML_NV, cFieldValue, SYM_FML_FS );
  88.     else /*0下标时*/
  89.         sprintf( cFmlData + l, "%s%c%s%c", cFieldName, SYM_FML_NV,
  90.         cFieldValue, SYM_FML_FS );

  91.     return RC_SUCC;
  92. }


  93. /*FmlFieldGet() 功能描述: 从总线中取指定域值函数*/
  94. int FmlFieldGet(const char *cFmlData,const char *cFieldName,int    iIndex,    char *cFieldValue,int iValueSize)
  95. {
  96.     char    *p, *q, *v;
  97.     int    l;
  98.     
  99.     if ( ( p = FmlFieldSeek( cFmlData, cFieldName, iIndex ) ) == NULL )
  100.         return RC_NFND; /*p指向指定域名的起始地址*/
  101.     q = strchr( p, SYM_FML_NV ) + 1; /*SYM_FML_NV名值分隔符:,q指向域值的起始地址*/
  102.     if ( ( v = strchr( q, SYM_FML_FS ) ) == NULL ) /*SYM_FML_FS域分隔符^^,v指向域值的尾部地址*/
  103.         l = strlen( q ); /*没有串尾符^^的长度*/
  104.     else
  105.         l = v - q; /*l=域值的长度*/
  106.     if ( l >= iValueSize ) /*要放域值的空间不能小于域值的长度*/
  107.         return RC_NSPC;
  108.     memcpy( cFieldValue, q, l );
  109.     cFieldValue[l] = 0; /*域值的地址加字符串的结束符*/
  110.     return RC_SUCC;
  111. }


  112. /*FmlFieldNum() 功能描述: 统计总线中重复域的个数函数*/
  113. int    FmlFieldNum(const char *cFmlData, const char *cFieldName, int *iNum)
  114. {
  115.     char    *p = (char *) cFmlData;
  116.     char    *n, *q, *v;
  117.     int    r;
  118.     int    num,flag;

  119.     num=0;
  120.     flag=0;

  121.     while ( ( n = strchr( p, SYM_FML_FS ) ) != NULL )
  122.     {
  123.         *n = 0;
  124.         q = strchr( p, SYM_FML_NV );
  125.         *n = SYM_FML_FS;
  126.         if ( q != NULL )
  127.         {
  128.             *q = 0;
  129.             if ( ( v = strchr( p, SYM_FML_AN ) ) != NULL )
  130.                 *v = 0;
  131.             else
  132.             {
  133.                 *iNum=0;
  134.                 if( v != NULL)
  135.                     *v=SYM_FML_AN;
  136.                 *q=SYM_FML_NV;
  137.                 p = n + 1;
  138.                 continue;
  139.             }
  140.             if ( ( r = strcmp( p, cFieldName ) ) == 0 )
  141.             {
  142.                 num++;
  143.                 flag=1;
  144.             }
  145.                 *v=SYM_FML_AN;
  146.                 *q=SYM_FML_NV;
  147.         }
  148.         p = n + 1;
  149.     }
  150.     *iNum=num;
  151.     if(flag == 1)
  152.         return RC_SUCC;
  153.     else
  154.         return RC_NFND;
  155. }


  156. int main(int argc, char ** argv)
  157. {

  158.     int length = sizeof(T_DATABUS);
  159.     printf("length = [%d]\n", length);
  160.     
  161.     char    sMesg[256];
  162.     char    seqno_0[128];
  163.     char    seqno_1[128];
  164.     char    seqno_2[128];
  165.     char    seqno_3[128];
  166.     char    name[128];
  167.     int        ret = -1;
  168.     int        count = -1;
  169.     T_DATABUS tDataBus; /*内部数据总线*/

  170.     memset(sMesg, 0, sizeof(sMesg));
  171.     memset(seqno_0, 0, sizeof(seqno_0));
  172.     memset(seqno_1, 0, sizeof(seqno_1));
  173.     memset(seqno_2, 0, sizeof(seqno_2));
  174.     memset(seqno_3, 0, sizeof(seqno_3));
  175.     memset(name, 0, sizeof(name));

  176.     FmlDataClear(tDataBus);
  177.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "retmsg", 0, "取平台流水号失败");
  178.     printf("tdataBus = [%s]\n", tDataBus);
  179.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "wangxiancai", 0, "");
  180.     printf("tdataBus = [%s]\n", tDataBus);
  181.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "retcode", 0, "999999");
  182.     printf("tdataBus = [%s]\n", tDataBus);
  183.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 0, "000000");
  184.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 1, "111111");
  185.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 2, "222222");
  186.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 3, "333333");
  187.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 4, "444444");
  188.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 5, "222222");
  189.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 6, "666666");
  190.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 7, "777777");
  191.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 8, "888888");
  192.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 9, "999999");
  193.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 20, "aaaaa");
  194.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "seqno", 30000, "bbbbb");
  195.     ret=FmlFieldGet(tDataBus, "retmsg", 0, sMesg, sizeof(sMesg));
  196.     printf("sMesg = [%s]\n", sMesg);
  197.     /*ret = FmlFieldGet(tDataBus, "seqno", 0, seqno_0, sizeof(seqno_0));*/
  198.     ret = FmlFieldGet(tDataBus, "seqno", 1, seqno_1, sizeof(seqno_1));
  199.     ret = FmlFieldGet(tDataBus, "seqno", 10, seqno_2, sizeof(seqno_2));
  200.     
  201.     ret = FmlFieldNum(tDataBus, "seqno", &count);
  202.     
  203.     printf("seqno_0 = [%s]\n", seqno_0);
  204.     printf("seqno_1 = [%s]\n", seqno_1);
  205.     printf("seqno_2 = [%s]\n", seqno_2);
  206.     printf("count(seqno) = [%d]\n",count);
  207.     
  208.     FmlFieldPut(tDataBus, SZ_DATA_BUS, "retmsg", 0, "第二次向databus中put retmsg");
  209.     printf("tdataBus = [%s]\n", tDataBus);
  210.     ret=FmlFieldGet(tDataBus, "wangxiancai", 2, name, sizeof(name));
  211.     printf("name = [%s]\n", name);
  212.     memset(name, 0, sizeof(name));
  213.     ret=FmlFieldGet(tDataBus, "wangxiancai111", 0, name, sizeof(name));
  214.     printf("ret = [%d]\n", ret);
  215.     printf("name = [%s]\n", name);
  216.     return 0;
  217. }

阅读(380) | 评论(0) | 转发(0) |
0

上一篇:assert函数

下一篇:dup2函数

给主人留下些什么吧!~~