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

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 20:04:22

编译命令

点击(此处)折叠或打开

  1. cc -I /home/eas/src/libxml/libxml2-2.6.30/include/ -I /home/eas/src/libiconv/libiconv-1.13.1/include/ -L/home/eas/src/libxml/libxml2-2.6.30/.libs -L/home/eas/src/libiconv/libiconv-1.13.1/lib/.libs/ -lxml2 -liconv -lz -lpthread -lm -o paixu paixu.c


点击(此处)折叠或打开

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <string.h>
  4. #include    <math.h>
  5. #include    <ctype.h>
  6. #include    <sys/stat.h>
  7. #include    <unistd.h>
  8. #include    <stdbool.h>
  9. #include    <sys/timeb.h>
  10. #include    <time.h>

  11. #include    <libxml/parser.h>                /* LIBXML2 Header File */
  12. #include    <libxml/tree.h>                    /* LIBXML2 Header File */
  13. #include     <libxml/xmlmemory.h>            /* LIBXML2 Header File */
  14. #include    <libxml/xpath.h>                /* LIBXML2 Header File */
  15. #include    <iconv.h>                        /* LIBXML2 Header File */

  16. #define FMLFLD_NOTARRAY 0 /* 非数组(用于下标参数) */
  17. #define FMLFLD_OPTIONAL 0 /* 可选域 */
  18. #define FMLFLD_REQUIRED 1 /* 必需域 */
  19. #define    SYM_FML_FS    0x40    /* 域分隔符0x1E*/
  20. #define    SYM_FML_NV    ':'     /* 名值分隔符*/
  21. #define    SYM_FML_AN    '#'     /* 数组下标起始符*/
  22. #define RC_SUCC 0
  23. #define RC_NFND -1
  24. #define RC_NSPC -2

  25. #define SZ_DATA_BUS (32*1024)            /* 数据总线宽度 */
  26. /*typedef char T_DATABUS[SZ_DATA_BUS];*/    /* 数据总线 */
  27. typedef char buf[SZ_DATA_BUS] T_DATABUS;

  28. void FmlDataClear(char *    cFmlData);
  29. char *FmlFieldSeek(const char *cFmlData, const char *cFieldName,int    iIndex);
  30. int    FmlFieldPut(char *cFmlData,int iBufSize,char *cFieldName,int iIndex,char *cFieldValue);
  31. int FmlFieldGet(const char *cFmlData,const char *cFieldName,int    iIndex,    char *cFieldValue,int iValueSize);
  32. int    FmlFieldNum(const char *cFmlData, const char *cFieldName, int *iNum);

  33. int CodeConvert(char* from_charset, char* to_charset, char* inbuf,size_t inlen, char* outbuf, size_t outlen);
  34. char* u2g(char *inbuf);
  35. char* g2u(char *inbuf);
  36. char * StrTrim(char * pstr);
  37. int    GetDateTime(char * cDate, char * cTime);
  38. int JugeFileExist(char *lFileName);

  39. int XmlSetPropByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number, const xmlChar *name, const xmlChar *value, const char * encoding);
  40. int XmlNodeSetContentByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number, const xmlChar *content, const char * encoding);
  41. xmlNodePtr GetNodeByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number);
  42. int XmlNodeSortByProp(const char * filename, const xmlChar * XPath, const xmlChar * propname);

  43. int main(int argc, char **argv)
  44. {
  45.     if(argc < 2)
  46.     {
  47.         printf("参数个数不对,应为2个!\n");
  48.         return -1;
  49.     }
  50.     
  51.     char filename[1024];
  52.     memset(filename, 0x00, sizeof(filename));
  53.     strcpy(filename, argv[1]);
  54.     StrTrim(filename);
  55.     
  56.     char sParsefile[1024];
  57.     memset(sParsefile, 0x00, sizeof(sParsefile));
  58.     sprintf(sParsefile, "%s%s%s", getenv("HOME"), "/src/xml_fun/", filename);
  59.     StrTrim(sParsefile);
  60.     
  61.     printf("*************************************************************\n");
  62.     int iRet = JugeFileExist(sParsefile);
  63.     if(iRet != RC_SUCC)
  64.     {
  65.         printf("文件[%s]不存在\n", sParsefile);
  66.         return -1;
  67.     }    
  68.     FILE *    fp = NULL;
  69.     if((fp = fopen(sParsefile, "r")) == NULL)
  70.     {
  71.         printf("打开dmz文件[%s]失败\n", sParsefile);
  72.         return -1;
  73.     }
  74.     char sXmlBuf[1024*1000*8];    /*从报文文件中读取的内容*/
  75.     char sChildBuf[10240];        /*存放一次读取的文件内容*/
  76.     memset(sXmlBuf, 0x00, sizeof(sXmlBuf));
  77.     memset(sChildBuf, 0x00, sizeof(sChildBuf));
  78.     while(NULL != fgets(sChildBuf, sizeof(sChildBuf), fp))
  79.     {
  80.         strcat(sXmlBuf, sChildBuf);
  81.         memset(sChildBuf, 0, sizeof(sChildBuf));
  82.     }
  83.     int iXmlByteNum = strlen(sXmlBuf);
  84.     printf("读取文件[%s],文件长度=[%d]\n", sParsefile, iXmlByteNum);    
  85.     printf("文件数据如下:\n[%s]", sXmlBuf);    
  86.     printf("*************************************************************\n");

  87.     XmlNodeSortByProp(sParsefile, BAD_CAST "/request/balance_query/row", BAD_CAST "id");

  88.     printf("*************************************************************\n");
  89.     
  90.     return 0;
  91. }


  92. int XmlNodeSortByProp(const char * filename, const xmlChar * XPath, const xmlChar * propname)
  93. {
  94.     xmlDocPtr doc = xmlReadFile(filename,"GBK",XML_PARSE_NOBLANKS);
  95.     if (NULL == doc)
  96.     {
  97.         printf("未指定文件名\n");
  98.         return -1;
  99.     }

  100.     xmlXPathContextPtr context = xmlXPathNewContext(doc);
  101.     xmlXPathObjectPtr resultObject = xmlXPathEval(XPath, context);
  102.     xmlNodeSetPtr nodeSetVal = resultObject->nodesetval;
  103.     
  104.     int i = -1, j = -1;
  105.     xmlChar * AttrValue1, *AttrValue2;
  106.     xmlNodePtr tempnode = NULL;
  107.     int resultNum = nodeSetVal->nodeNr;
  108.     for(i=0; i<resultNum-1; i++)
  109.     {
  110.         for(j=i+1; j<resultNum; j++)
  111.         {
  112.             AttrValue1 = xmlGetProp(nodeSetVal->nodeTab[i], propname);
  113.             AttrValue2 = xmlGetProp(nodeSetVal->nodeTab[j], propname);
  114.             if(xmlStrcmp(AttrValue1, AttrValue2) > 0)/*交换位置*/
  115.             {
  116.                 tempnode = nodeSetVal->nodeTab[i];
  117.                 nodeSetVal->nodeTab[i] = nodeSetVal->nodeTab[j];
  118.                 nodeSetVal->nodeTab[j] = tempnode;
  119.             }
  120.             if(AttrValue1 != NULL)
  121.             {
  122.                 xmlFree(AttrValue1);
  123.                 AttrValue1 = NULL;
  124.             }
  125.             if(AttrValue2 != NULL)
  126.             {
  127.                 xmlFree(AttrValue2);
  128.                 AttrValue2 = NULL;
  129.             }        
  130.         }
  131.     }
  132.     
  133.     xmlNodePtr parentnode = GetNodeByXpath(doc, XPath, 0)->parent;
  134.     for(i=0; i<resultNum; i++)
  135.     {
  136.         xmlReplaceNode(nodeSetVal->nodeTab[i], NULL);
  137.         /*xmlUnlinkNode(nodeSetVal->nodeTab[i]);*/
  138.     }
  139.     for(i=0; i<resultNum; i++)
  140.     {
  141.         xmlAddChild(parentnode, nodeSetVal->nodeTab[i]);
  142.     }
  143.     
  144.     int iRet = remove(filename);
  145.     if (iRet != 0)
  146.     {
  147.         printf("remove 失败!\n");
  148.     }
  149.     iRet = xmlSaveFormatFileEnc(filename, doc, "GB2312", 1);
  150.     if (iRet != 0)
  151.     {
  152.        printf("xml文件已创建失败!\n");
  153.     }
  154.     printf("排序已成功!\n");
  155.     xmlXPathFreeObject(resultObject);
  156.     xmlXPathFreeContext(context);
  157.     xmlFreeDoc(doc);
  158.     
  159.     return 0;
  160. }


  161. /*
  162. void xmlXPathFreeNodeSet(xmlNodeSetPtr obj);
  163. xmlXPathObjectPtr xmlXPathObjectCopy(xmlXPathObjectPtr val);
  164. void xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj);
  165. int xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2);

  166. xmlNodeSetPtr xmlXPathNodeSetCreate(xmlNodePtr val);
  167. */


  168. /*************************************************************
  169.  * 函 数 名:XmlSetPropByXpath()
  170.  * 功能描述:将val_str设置到XML文档的xpath节点中
  171.  * 输入参数:doc - XML文档指针
  172.  *          encode - 0:utf8 1:gbk
  173.  *          xpath - XML文档路径,如"/root/abc"
  174.  *          val_str - UTF-8编码的值
  175.  *          number - 如果该XML文档中有多个xpath,给第几个xpath设置,0表示第一个节点
  176.  * 输出参数:无
  177.  * 返 回:0 - 成功 ;-1 - 失败
  178.  * 说 明:如果encode为0不需要转码,如果encode为1,说明是中文GBK编码,需要转换成UTF8
  179.  ************************************************************/
  180. int XmlSetPropByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number, const xmlChar *name, const xmlChar *value, const char * encoding)
  181. {
  182.     int encodingflag = 1;
  183.     if((strncasecmp(encoding, "GBK", strlen("GBK")) == 0)||(strncasecmp(encoding, "GB2312", strlen("GB2312")) == 0)) /*需要转换为utf-8*/
  184.     {
  185.         encodingflag = 1;
  186.     }
  187.     else
  188.     {
  189.         encodingflag = 0;
  190.     }
  191.     
  192.     xmlXPathContextPtr context = NULL;
  193.     xmlXPathObjectPtr resultObject = NULL;

  194.     context = xmlXPathNewContext(doc);
  195.     resultObject = xmlXPathEval(xpath, context);
  196.     /*number介于0~cur->nodesetval->nodeNr-1之间*/
  197.     int resultNum = resultObject->nodesetval->nodeNr;
  198.     if(resultNum<1 || number>=resultNum || number < 0)
  199.     {
  200.         xmlXPathFreeObject(resultObject);
  201.         xmlXPathFreeContext(context);
  202.         printf("指定xpath的节点不存在!\n");
  203.         return -1;
  204.     }
  205.     xmlNodePtr node = NULL;
  206.     node = resultObject->nodesetval->nodeTab[number];
  207.     if(node == NULL)
  208.     {
  209.         xmlXPathFreeObject(resultObject);
  210.         xmlXPathFreeContext(context);
  211.         printf("指定xpath的节点不存在!\n");
  212.         return -1;
  213.     }
  214.     xmlChar *AttrName = NULL;
  215.     xmlChar *AttrValue = NULL;
  216.     if((name != NULL)&&(value != NULL)) /*均非空*/
  217.     {
  218.         if(encodingflag == 1) /*需要转换为国标码*/
  219.         {
  220.             AttrName = (xmlChar *)g2u((char *)name);
  221.             AttrValue = (xmlChar *)g2u((char *)value);
  222.             if(AttrValue != NULL)
  223.                 xmlSetProp(node,AttrName,AttrValue);
  224.         }
  225.         else /*其他编码*/
  226.         {
  227.             xmlSetProp(node,name,value);
  228.         }
  229.     }
  230.     else if((name != NULL)&&(value == NULL)) /*值为空*/
  231.     {
  232.         if(encodingflag == 1) /*需要转换为国标码*/
  233.         {
  234.             AttrName = (xmlChar *)g2u((char *)name);
  235.             xmlSetProp(node, AttrName, "");
  236.         }
  237.         else /*其他编码*/
  238.         {
  239.             xmlSetProp(node, name, "");
  240.         }
  241.     }

  242.     xmlXPathFreeObject(resultObject);
  243.     xmlXPathFreeContext(context);
  244.     if(AttrName != NULL)
  245.     {
  246.         xmlFree(AttrName);
  247.         AttrName = NULL;
  248.     }
  249.     if(AttrValue != NULL)
  250.     {
  251.         xmlFree(AttrValue);
  252.         AttrValue = NULL;
  253.     }
  254.     return 0;
  255. }


  256. /*************************************************************
  257.  * 函 数 名:XmlNodeSetContentByXpath()
  258.  * 功能描述:将content设置到XML文档的xpath节点中
  259.  * 输入参数:doc - XML文档指针
  260.  *          encode - val_str所采用的编码方式
  261.  *          xpath - XML文档路径,如"/root/abc"
  262.  *          val_str - UTF-8编码的值
  263.  *          number - 如果该XML文档中有多个xpath,给第几个xpath设置,0表示第一个节点
  264.  * 输出参数:无
  265.  * 返 回:0 - 成功 ;-1 - 失败
  266.  * 说 明:如果encode为0不需要转码,如果encode为1,说明是中文GBK编码,需要转换成UTF8
  267.  ************************************************************/
  268. int XmlNodeSetContentByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number, const xmlChar *content, const char * encoding)
  269. {
  270.     int encodingflag = -1;
  271.     if((strncasecmp(encoding, "GBK", strlen("GBK")) == 0)||(strncasecmp(encoding, "GB2312", strlen("GB2312")) == 0)) /*国标码需要转换为utf-8*/
  272.     {
  273.         encodingflag = 1;
  274.     }
  275.     else
  276.     {
  277.         encodingflag = 0;
  278.     }
  279.     
  280.     xmlXPathContextPtr context = NULL;
  281.     xmlXPathObjectPtr resultObject = NULL;

  282.     context = xmlXPathNewContext(doc);
  283.     resultObject = xmlXPathEval(xpath, context);
  284.     xmlNodeSetPtr nodesetVal = resultObject->nodesetval; /*节点集合指针*/
  285.     /*number介于0~cur->nodesetval->nodeNr-1之间*/
  286.     int resultNum = nodesetVal->nodeNr;
  287.     if(resultNum<1 || number>=resultNum || number < 0)
  288.     {
  289.         xmlXPathFreeObject(resultObject);
  290.         xmlXPathFreeContext(context);
  291.         printf("指定xpath的节点不存在!\n");
  292.         return -1;
  293.     }
  294.     xmlNodePtr node = NULL;
  295.     /*node = (nodesetVal->nodeTab)[number];*/
  296.     node = *((nodesetVal->nodeTab)+number);
  297.     if(node == NULL)
  298.     {
  299.         xmlXPathFreeObject(resultObject);
  300.         xmlXPathFreeContext(context);
  301.         printf("指定xpath的节点不存在!\n");
  302.         return -1;
  303.     }
  304.     xmlChar *out = NULL;
  305.     if(content != NULL) /*空值*/
  306.     {
  307.         if(encodingflag == 1) /*需要转换为utf-8*/
  308.         {
  309.             out = (xmlChar *)g2u((char *)content);
  310.             if(out != NULL)
  311.                 xmlNodeSetContent(node,out);
  312.         }
  313.         else /*其他编码*/
  314.         {
  315.             xmlNodeSetContent(node,content);
  316.         }
  317.     }
  318.     else
  319.     {
  320.         xmlNodeSetContent(node,"");
  321.     }

  322.     xmlXPathFreeObject(resultObject);
  323.     xmlXPathFreeContext(context);
  324.     if(out != NULL)
  325.     {
  326.         xmlFree(out);
  327.         out = NULL;
  328.     }
  329.     return 0;
  330. }


  331. /*************************************************************
  332.  * 函 数 名:GetNodeByXpath()
  333.  * 功能描述: 根据xpath查询指定节点
  334.  * 输入参数:doc        - 文档
  335.  *             xpath        - 节点路径"/request/balance/row"
  336.  *             number    - 序号"/request/balance/row...row...row"
  337.  * 输出参数:
  338.  * 返 回:xmlNode - 成功 ;NULL - 失败
  339.  * 说 明:按序号查找节点
  340.  ************************************************************/
  341. xmlNodePtr GetNodeByXpath(const xmlDocPtr doc, const xmlChar *xpath, const int number)
  342. {
  343.     xmlXPathContextPtr context = NULL;
  344.     xmlXPathObjectPtr resultObject = NULL;

  345.     context = xmlXPathNewContext(doc);
  346.     resultObject = xmlXPathEval(xpath,context);
  347.     /*number介于0~cur->nodesetval->nodeNr-1之间*/
  348.     int resultNum = resultObject->nodesetval->nodeNr;
  349.     if(resultNum<1 || number>=resultNum || number < 0)
  350.     {
  351.         xmlXPathFreeObject(resultObject);
  352.         xmlXPathFreeContext(context);
  353.         printf("指定xpath的节点不存在!\n");
  354.         return (NULL);
  355.     }
  356.     xmlNodePtr node;
  357.     node = resultObject->nodesetval->nodeTab[number];

  358.     xmlXPathFreeObject(resultObject);
  359.     xmlXPathFreeContext(context);

  360.     return (node);
  361. }


  362. /***********************************************************
  363. * 函 数 名:GetDateTime()
  364. * 功能描述: 分别获取系统日期时间
  365. * 输入参数:
  366. *            cDate-日期接收缓冲区;
  367. *            cTime-日期接收缓冲区;
  368. * 输出参数:cDate
  369. * 返 回:0 成功
  370. * 流程描述:
  371. * 说明: 返回日期格式:YYYYMMDD
  372. * 修改记录:
  373. * [修改人] [日期] - [描述]
  374. ***********************************************************/
  375. int    GetDateTime(char * cDate, char * cTime)
  376. {
  377.     struct tm *    cur;
  378.     time_t    timep;

  379.     struct timeb time_msec;
  380.     unsigned short msec;

  381.     time(&timep);
  382.     cur = localtime(&timep);

  383.     ftime(&time_msec);
  384.     msec=time_msec.millitm;

  385.     sprintf(cDate,"%04d%02d%02d",cur->tm_year+1900,cur->tm_mon+1,cur->tm_mday);
  386.     cDate[8]=0;
  387.     sprintf(cTime,"%02d%02d%02d%03d",cur->tm_hour,cur->tm_min,cur->tm_sec,msec);
  388.     cTime[9]=0;

  389.     return 0;
  390. }


  391. int CodeConvert(char* from_charset, char* to_charset, char* inbuf,size_t inlen, char* outbuf, size_t outlen)
  392. {
  393.     iconv_t cd;
  394.     char** pin = &inbuf;
  395.     char** pout = &outbuf;
  396.     cd = iconv_open(to_charset,from_charset);
  397.     if(cd == (iconv_t)-1)
  398.         return -1;
  399.     memset(outbuf,0,outlen);
  400.     if(iconv(cd,(char**)pin,(size_t *)&inlen,(char**)pout,(size_t*)&outlen) == -1)
  401.         return -1;
  402.     iconv_close(cd);
  403.     return 0;
  404. }

  405. /*************************************************************
  406.  * 函 数 名:u2g()
  407.  * 功能描述: UTF-8码转成GBK码
  408.  * 输入参数:inbuf    - 转入字符
  409.  * 输出参数:
  410.  * 返 回:szOut - 成功; NULL - 失败
  411.  * 说 明:成功则返回一个动态分配的char*变量,需要在使用
  412.  *             完毕后手动free,失败返回NULL,使用有风险,调用需谨慎
  413.  ************************************************************/
  414. char* u2g(char *inbuf)
  415. {
  416.     size_t nOutLen = 0;
  417.     char* szOut = NULL;
  418.     if(!strlen(inbuf))
  419.         return NULL;
  420.     nOutLen = 2 * strlen(inbuf) - 1;
  421.     szOut = (char*)malloc(nOutLen);
  422.     if (-1 == CodeConvert("UTF-8","GBK",inbuf,strlen(inbuf),szOut,nOutLen))
  423.     {
  424.         free(szOut);
  425.         szOut = NULL;
  426.     }
  427.     return szOut;
  428. }

  429. /*************************************************************
  430.  * 函 数 名:g2u()
  431.  * 功能描述: GBK码转成UTF-8码
  432.  * 输入参数:inbuf    - 转入字符
  433.  * 输出参数:
  434.  * 返 回:szOut - 成功; NULL - 失败
  435.  * 说 明:成功则返回一个动态分配的char*变量,需要在使用
  436.  *             完毕后手动free,失败返回NULL,使用有风险,调用需谨慎
  437.  ************************************************************/
  438. char* g2u(char *inbuf)
  439. {
  440.     size_t nOutLen = 0;
  441.     char* szOut = NULL;
  442.     if(!strlen(inbuf))
  443.         return NULL;
  444.     nOutLen = 2 * strlen(inbuf) - 1;
  445.     szOut = (char*)malloc(nOutLen);
  446.     if (-1 == CodeConvert("GBK","UTF-8",inbuf,strlen(inbuf),szOut,nOutLen))
  447.     {
  448.         free(szOut);
  449.         szOut = NULL;
  450.     }
  451.     return szOut;
  452. }

  453. /*FmlDataClear 函数功能:清空数据总线函数*/
  454. void FmlDataClear(char *    cFmlData)
  455. {
  456.     cFmlData[0] = 0;
  457. }

  458. /*FmlFieldErase函数:擦除总线数据中的指定域,没有这个域就不擦除*/
  459. void    FmlFieldErase(char *cFmlData, const char *cFieldName,int iIndex)
  460. {
  461.     char    *p, *q;
  462.     if ( ( p = FmlFieldSeek( cFmlData, cFieldName, iIndex ) ) == NULL )
  463.         return; /*p指向指定的域名的起始地址*/
  464.     if ( ( q = strchr( p, SYM_FML_FS ) ) == NULL ) /*SYM_FML_FS:域分割符^^*/
  465.         q = p + strlen( p );
  466.     else
  467.         q ++; /*q指向域尾*/
  468.     strcpy( p, q ); /*p到q之间的被跳过*/
  469. }

  470. /*FmlFieldSeek函数功能:检索总线中指定域函数,返回指定域的起始地址*/
  471. char *FmlFieldSeek(const char *cFmlData, const char *cFieldName,int    iIndex)
  472. {
  473.     char    *p = (char *) cFmlData;
  474.     char    *n, *q, *v;
  475.     int    r;

  476.     while ( ( n = strchr( p, SYM_FML_FS ) ) != NULL )/*SYM_FML_FS:域分割符^^*/
  477.     {    
  478.         *n = 0; /*n指向下一个域分割符^^,并截断*/
  479.         q = strchr( p, SYM_FML_NV ); /*SYM_FML_NV:名值分割符:*/
  480.         *n = SYM_FML_FS; /*恢复域分割符,重新连上总线*/
  481.         if ( q != NULL )
  482.         {
  483.             *q = 0; /*q指向名值分割符:,并截断*/
  484.             if ( ( v = strchr( p, SYM_FML_AN ) ) != NULL ) /*0下标时,v=NULL*/
  485.                 *v = 0; /*v指向数组下标起始符#,并截断*/
  486.             if ( ( r = strcmp( p, cFieldName ) ) == 0 ) /*比较p指向的是不是待检索的域名*/
  487.             {
  488.                 if ( v == NULL && iIndex != FMLFLD_NOTARRAY || v != NULL && atoi( v + 1 ) != iIndex )
  489.                     r = -1; /*对下标值的检查,出错时r=-1*/
  490.             }
  491.             if ( v != NULL )
  492.                 *v = SYM_FML_AN; /*重新连上总线*/
  493.             *q = SYM_FML_NV; /*重新连上总线*/
  494.             if ( r == 0 ) /*域名匹配,下标也正确时,查找完毕*/
  495.                 return p; /*返回域名起始地址*/
  496.         }
  497.         p = n + 1; /*p指向下一个域名起始地址,继续循环查找*/
  498.     }
  499.     return NULL;
  500. }

  501. /*FmlFieldPut() 功能描述: 为总线中指定域赋值函数*/
  502. int    FmlFieldPut(char *cFmlData,int iBufSize,char *cFieldName,int iIndex,char *cFieldValue)
  503. {
  504.     int    s, l;
  505.     char    a[12];

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

  507.     if ( iIndex == FMLFLD_NOTARRAY ) /*FMLFLD_NOTARRAY=0,判断是否是0下标*/
  508.         s = 0; /*0下标时,没有数组下标起始符#,也没有小标值0的显示*/
  509.     else
  510.         s = snprintf( a, sizeof( a ), "%d", iIndex ) + 1; /*下标的长度+#号码长度*/
  511.     if ( ( l = strlen( cFmlData ) ) + strlen( cFieldName ) + s + strlen( cFieldValue ) + 2 > iBufSize )
  512.         return RC_NSPC; /*判断是否溢出,其中2=域分隔符+'\0'*/
  513.     if ( l > 0 && cFmlData[l-1] != SYM_FML_FS ) /*SYM_FML_FS域分割符^^,原串没有域分割符时*/
  514.     {
  515.         cFmlData[l] = SYM_FML_FS; /*原串尾加上域分隔符*/
  516.         l ++; /*原串长+1*/
  517.     }

  518.     if ( s != 0 ) /*非0下标时*/
  519.         /*a中保存的是下标值*/
  520.         sprintf( cFmlData + l, "%s%c%s%c%s%c", cFieldName, SYM_FML_AN, a, SYM_FML_NV, cFieldValue, SYM_FML_FS );
  521.     else /*0下标时*/
  522.         sprintf( cFmlData + l, "%s%c%s%c", cFieldName, SYM_FML_NV,
  523.         cFieldValue, SYM_FML_FS );

  524.     return RC_SUCC;
  525. }

  526. /*FmlFieldGet() 功能描述: 从总线中取指定域值函数*/
  527. int FmlFieldGet(const char *cFmlData,const char *cFieldName,int    iIndex,    char *cFieldValue,int iValueSize)
  528. {
  529.     char    *p, *q, *v;
  530.     int    l;
  531.     
  532.     if ( ( p = FmlFieldSeek( cFmlData, cFieldName, iIndex ) ) == NULL )
  533.         return RC_NFND; /*p指向指定域名的起始地址*/
  534.     q = strchr( p, SYM_FML_NV ) + 1; /*SYM_FML_NV名值分隔符:,q指向域值的起始地址*/
  535.     if ( ( v = strchr( q, SYM_FML_FS ) ) == NULL ) /*SYM_FML_FS域分隔符^^,v指向域值的尾部地址*/
  536.         l = strlen( q ); /*没有串尾符^^的长度*/
  537.     else
  538.         l = v - q; /*l=域值的长度*/
  539.     if ( l >= iValueSize ) /*要放域值的空间不能小于域值的长度*/
  540.         return RC_NSPC;
  541.     memcpy( cFieldValue, q, l );
  542.     cFieldValue[l] = 0; /*域值的地址加字符串的结束符*/
  543.     return RC_SUCC;
  544. }

  545. /*FmlFieldNum() 功能描述: 统计总线中重复域的个数函数*/
  546. int    FmlFieldNum(const char *cFmlData, const char *cFieldName, int *iNum)
  547. {
  548.     char    *p = (char *) cFmlData;
  549.     char    *n, *q, *v;
  550.     int    r;
  551.     int    num,flag;

  552.     num=0;
  553.     flag=0;

  554.     while ( ( n = strchr( p, SYM_FML_FS ) ) != NULL )
  555.     {
  556.         *n = 0;
  557.         q = strchr( p, SYM_FML_NV );
  558.         *n = SYM_FML_FS;
  559.         if ( q != NULL )
  560.         {
  561.             *q = 0;
  562.             if ( ( v = strchr( p, SYM_FML_AN ) ) != NULL )
  563.                 *v = 0;
  564.             else
  565.             {
  566.                 *iNum=0;
  567.                 if( v != NULL)
  568.                     *v=SYM_FML_AN;
  569.                 *q=SYM_FML_NV;
  570.                 p = n + 1;
  571.                 continue;
  572.             }
  573.             if ( ( r = strcmp( p, cFieldName ) ) == 0 )
  574.             {
  575.                 num++;
  576.                 flag=1;
  577.             }
  578.                 *v=SYM_FML_AN;
  579.                 *q=SYM_FML_NV;
  580.         }
  581.         p = n + 1;
  582.     }
  583.     *iNum=num;
  584.     if(flag == 1)
  585.         return RC_SUCC;
  586.     else
  587.         return RC_NFND;
  588. }

  589. char * StrTrim(char * pstr)
  590. {
  591.     if(NULL == pstr)
  592.     {
  593.         return NULL;
  594.     }
  595.     char * phead = pstr;
  596.     char * ptail = pstr + strlen(pstr) - 1;
  597.     while((*phead == ' ')&&(phead < ptail)) phead++;
  598.     while((*ptail == ' ')&&(ptail > phead)) ptail--;
  599.     *(ptail + 1) = '\0';
  600.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  601.     return pstr;
  602. }

  603. /***********************************************************
  604.  * 函 数: JugeFileExist()
  605.  * 功能描述: 判断文件是否存在
  606.  * 输入参数:lFilename    -    长文件名
  607.  * 返 回:0-文件存在;1-文件不存在
  608.  * 流程描述:
  609.  *    说 明:
  610.  * 修改记录:
  611.  * [修改人] [日期][描述]
  612. ***********************************************************/
  613. int JugeFileExist(char *lFileName)
  614. {
  615.     struct stat statbuf;
  616.     memset(&statbuf,0,sizeof(statbuf));
  617.     stat(lFileName,&statbuf);
  618.     if(statbuf.st_mode & S_IFREG)
  619.         return 0;
  620.     else
  621.         return 1;
  622. }

阅读(1776) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~