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

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 20:08:12

编译命令

点击(此处)折叠或打开

  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 xmHasProp xmHasProp.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    <libxml/parser.h>                /* LIBXML2 Header File */
  10. #include    <libxml/tree.h>                    /* LIBXML2 Header File */
  11. #include     <libxml/xmlmemory.h>            /* LIBXML2 Header File */
  12. #include    <libxml/xpath.h>                /* LIBXML2 Header File */
  13. #include    <iconv.h>                        /* LIBXML2 Header File */

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

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

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

  30. int CodeConvert(char* from_charset, char* to_charset, char* inbuf,size_t inlen, char* outbuf, size_t outlen);
  31. char* u2g(char *inbuf);
  32. char* g2u(char *inbuf);
  33. char * StrTrim(char * pstr);
  34. int JugeFileExist(char *lFileName);

  35. int main(int argc, char **argv)
  36. {
  37.     bool bflag = true;
  38.     int i=0;
  39.     char *nodeName = NULL;
  40.     char *nodeValue = NULL;
  41.     char *eleName;
  42.     char *eleValue;
  43.     xmlChar* szAttr;
  44.     xmlAttrPtr attrPtr;

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

  94.     /* 解析文件 */
  95.     doc = xmlParseFile(sParsefile);
  96.     if (NULL == doc)
  97.     {
  98.         printf("未指定文件名\n");
  99.         return -1;
  100.     }
  101.     
  102.     /*
  103.     xmlParseFile函数等同于下面的函数
  104.     doc = xmlReadFile(pFilename,"GBK",XML_PARSE_NOBLANKS);
  105.     if (NULL == doc)
  106.     {
  107.         printf("未指定文件名\n");
  108.         return -1;
  109.     }
  110.     */

  111.     /* 确定文档根元素 */
  112.     
  113.     curNode = xmlDocGetRootElement(doc);
  114.     if (NULL == curNode)
  115.     {
  116.         xmlFreeDoc(doc);
  117.         return -1;
  118.     }
  119.     
  120.     /* xmlHasProp获取节点中某个属性的指针 */
  121.     attrPtr = xmlHasProp(curNode,BAD_CAST "req_time");
  122.     if(attrPtr != NULL)
  123.     {
  124.         //szAttr = xmlGetProp(curNode,attrPtr->name);
  125.         szAttr = xmlGetProp(curNode,"from");
  126.         //eleName = u2g((char*)attrPtr->name);
  127.         eleName = u2g("from");
  128.         eleValue = u2g((char*)szAttr);
  129.         if(eleValue != NULL)
  130.             FmlFieldPut(tDataBus, SZ_DATA_BUS, eleName, 0, eleValue);
  131.         else
  132.             FmlFieldPut(tDataBus, SZ_DATA_BUS, eleName, 0, "");
  133.         xmlFree(szAttr);
  134.         if( eleName != NULL )
  135.         {
  136.             free(eleName);
  137.             eleName = NULL;
  138.         }
  139.         if( eleValue != NULL )
  140.         {
  141.             free(eleValue);
  142.             eleValue = NULL;
  143.         }
  144.     }
  145.     else
  146.     {
  147.         printf("1111111111111111111\n");
  148.     }
  149.     printf("总线tDataBus=\n[%s]\n", tDataBus);
  150.     
  151.     attrPtr = attrPtr->prev;
  152.     if(attrPtr != NULL)
  153.     {
  154.         szAttr = xmlGetProp(curNode,attrPtr->name);
  155.         eleName = u2g((char*)attrPtr->name);
  156.         eleValue = u2g((char*)szAttr);
  157.         if(eleValue != NULL)
  158.             FmlFieldPut(tDataBus, SZ_DATA_BUS, eleName, 0, eleValue);
  159.         else
  160.             FmlFieldPut(tDataBus, SZ_DATA_BUS, eleName, 0, "");
  161.         xmlFree(szAttr);
  162.         if( eleName != NULL )
  163.         {
  164.             free(eleName);
  165.             eleName = NULL;
  166.         }
  167.         if( eleValue != NULL )
  168.         {
  169.             free(eleValue);
  170.             eleValue = NULL;
  171.         }
  172.     }
  173.     else
  174.     {
  175.         printf("22222222222222222222\n");
  176.     }
  177.     
  178.     if(bflag)
  179.     {
  180.         printf("55555555555\n");
  181.     }
  182.     else
  183.     {
  184.         printf("66666666666\n");
  185.     }
  186.     printf("总线tDataBus=\n[%s]\n", tDataBus);
  187.     
  188.     printf("***************************************************\n");
  189.     
  190.     return 0;
  191. }

  192. int CodeConvert(char* from_charset, char* to_charset, char* inbuf,size_t inlen, char* outbuf, size_t outlen)
  193. {
  194.     iconv_t cd;
  195.     char** pin = &inbuf;
  196.     char** pout = &outbuf;
  197.     cd = iconv_open(to_charset,from_charset);
  198.     if(cd == (iconv_t)-1)
  199.         return -1;
  200.     memset(outbuf,0,outlen);
  201.     if(iconv(cd,(char**)pin,(size_t *)&inlen,(char**)pout,(size_t*)&outlen) == -1)
  202.         return -1;
  203.     iconv_close(cd);
  204.     return 0;
  205. }

  206. /*************************************************************
  207.  * 函 数 名:u2g()
  208.  * 功能描述: UTF-8码转成GBK码
  209.  * 输入参数:inbuf    - 转入字符
  210.  * 输出参数:
  211.  * 返 回:szOut - 成功; NULL - 失败
  212.  * 说 明:成功则返回一个动态分配的char*变量,需要在使用
  213.  *             完毕后手动free,失败返回NULL,使用有风险,调用需谨慎
  214.  ************************************************************/
  215. char* u2g(char *inbuf)
  216. {
  217.     size_t nOutLen = 0;
  218.     char* szOut = NULL;
  219.     if(!strlen(inbuf))
  220.         return NULL;
  221.     nOutLen = 2 * strlen(inbuf) - 1;
  222.     szOut = (char*)malloc(nOutLen);
  223.     if (-1 == CodeConvert("UTF-8","GBK",inbuf,strlen(inbuf),szOut,nOutLen))
  224.     {
  225.         free(szOut);
  226.         szOut = NULL;
  227.     }
  228.     return szOut;
  229. }

  230. /*************************************************************
  231.  * 函 数 名:g2u()
  232.  * 功能描述: GBK码转成UTF-8码
  233.  * 输入参数:inbuf    - 转入字符
  234.  * 输出参数:
  235.  * 返 回:szOut - 成功; NULL - 失败
  236.  * 说 明:成功则返回一个动态分配的char*变量,需要在使用
  237.  *             完毕后手动free,失败返回NULL,使用有风险,调用需谨慎
  238.  ************************************************************/
  239. char* g2u(char *inbuf)
  240. {
  241.     size_t nOutLen = 0;
  242.     char* szOut = NULL;
  243.     if(!strlen(inbuf))
  244.         return NULL;
  245.     nOutLen = 2 * strlen(inbuf) - 1;
  246.     szOut = (char*)malloc(nOutLen);
  247.     if (-1 == CodeConvert("GBK","UTF-8",inbuf,strlen(inbuf),szOut,nOutLen))
  248.     {
  249.         free(szOut);
  250.         szOut = NULL;
  251.     }
  252.     return szOut;
  253. }

  254. /*FmlDataClear 函数功能:清空数据总线函数*/
  255. void FmlDataClear(char *    cFmlData)
  256. {
  257.     cFmlData[0] = 0;
  258. }

  259. /*FmlFieldErase函数:擦除总线数据中的指定域,没有这个域就不擦除*/
  260. void    FmlFieldErase(char *cFmlData, const char *cFieldName,int iIndex)
  261. {
  262.     char    *p, *q;
  263.     if ( ( p = FmlFieldSeek( cFmlData, cFieldName, iIndex ) ) == NULL )
  264.         return; /*p指向指定的域名的起始地址*/
  265.     if ( ( q = strchr( p, SYM_FML_FS ) ) == NULL ) /*SYM_FML_FS:域分割符^^*/
  266.         q = p + strlen( p );
  267.     else
  268.         q ++; /*q指向域尾*/
  269.     strcpy( p, q ); /*p到q之间的被跳过*/
  270. }

  271. /*FmlFieldSeek函数功能:检索总线中指定域函数,返回指定域的起始地址*/
  272. char *FmlFieldSeek(const char *cFmlData, const char *cFieldName,int    iIndex)
  273. {
  274.     char    *p = (char *) cFmlData;
  275.     char    *n, *q, *v;
  276.     int    r;

  277.     while ( ( n = strchr( p, SYM_FML_FS ) ) != NULL )/*SYM_FML_FS:域分割符^^*/
  278.     {    
  279.         *n = 0; /*n指向下一个域分割符^^,并截断*/
  280.         q = strchr( p, SYM_FML_NV ); /*SYM_FML_NV:名值分割符:*/
  281.         *n = SYM_FML_FS; /*恢复域分割符,重新连上总线*/
  282.         if ( q != NULL )
  283.         {
  284.             *q = 0; /*q指向名值分割符:,并截断*/
  285.             if ( ( v = strchr( p, SYM_FML_AN ) ) != NULL ) /*0下标时,v=NULL*/
  286.                 *v = 0; /*v指向数组下标起始符#,并截断*/
  287.             if ( ( r = strcmp( p, cFieldName ) ) == 0 ) /*比较p指向的是不是待检索的域名*/
  288.             {
  289.                 if ( v == NULL && iIndex != FMLFLD_NOTARRAY || v != NULL && atoi( v + 1 ) != iIndex )
  290.                     r = -1; /*对下标值的检查,出错时r=-1*/
  291.             }
  292.             if ( v != NULL )
  293.                 *v = SYM_FML_AN; /*重新连上总线*/
  294.             *q = SYM_FML_NV; /*重新连上总线*/
  295.             if ( r == 0 ) /*域名匹配,下标也正确时,查找完毕*/
  296.                 return p; /*返回域名起始地址*/
  297.         }
  298.         p = n + 1; /*p指向下一个域名起始地址,继续循环查找*/
  299.     }
  300.     return NULL;
  301. }

  302. /*FmlFieldPut() 功能描述: 为总线中指定域赋值函数*/
  303. int    FmlFieldPut(char *cFmlData,int iBufSize,char *cFieldName,int iIndex,char *cFieldValue)
  304. {
  305.     int    s, l;
  306.     char    a[12];

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

  308.     if ( iIndex == FMLFLD_NOTARRAY ) /*FMLFLD_NOTARRAY=0,判断是否是0下标*/
  309.         s = 0; /*0下标时,没有数组下标起始符#,也没有小标值0的显示*/
  310.     else
  311.         s = snprintf( a, sizeof( a ), "%d", iIndex ) + 1; /*下标的长度+#号码长度*/
  312.     if ( ( l = strlen( cFmlData ) ) + strlen( cFieldName ) + s + strlen( cFieldValue ) + 2 > iBufSize )
  313.         return RC_NSPC; /*判断是否溢出,其中2=域分隔符+'\0'*/
  314.     if ( l > 0 && cFmlData[l-1] != SYM_FML_FS ) /*SYM_FML_FS域分割符^^,原串没有域分割符时*/
  315.     {
  316.         cFmlData[l] = SYM_FML_FS; /*原串尾加上域分隔符*/
  317.         l ++; /*原串长+1*/
  318.     }

  319.     if ( s != 0 ) /*非0下标时*/
  320.         /*a中保存的是下标值*/
  321.         sprintf( cFmlData + l, "%s%c%s%c%s%c", cFieldName, SYM_FML_AN, a, SYM_FML_NV, cFieldValue, SYM_FML_FS );
  322.     else /*0下标时*/
  323.         sprintf( cFmlData + l, "%s%c%s%c", cFieldName, SYM_FML_NV,
  324.         cFieldValue, SYM_FML_FS );

  325.     return RC_SUCC;
  326. }

  327. /*FmlFieldGet() 功能描述: 从总线中取指定域值函数*/
  328. int FmlFieldGet(const char *cFmlData,const char *cFieldName,int    iIndex,    char *cFieldValue,int iValueSize)
  329. {
  330.     char    *p, *q, *v;
  331.     int    l;
  332.     
  333.     if ( ( p = FmlFieldSeek( cFmlData, cFieldName, iIndex ) ) == NULL )
  334.         return RC_NFND; /*p指向指定域名的起始地址*/
  335.     q = strchr( p, SYM_FML_NV ) + 1; /*SYM_FML_NV名值分隔符:,q指向域值的起始地址*/
  336.     if ( ( v = strchr( q, SYM_FML_FS ) ) == NULL ) /*SYM_FML_FS域分隔符^^,v指向域值的尾部地址*/
  337.         l = strlen( q ); /*没有串尾符^^的长度*/
  338.     else
  339.         l = v - q; /*l=域值的长度*/
  340.     if ( l >= iValueSize ) /*要放域值的空间不能小于域值的长度*/
  341.         return RC_NSPC;
  342.     memcpy( cFieldValue, q, l );
  343.     cFieldValue[l] = 0; /*域值的地址加字符串的结束符*/
  344.     return RC_SUCC;
  345. }

  346. /*FmlFieldNum() 功能描述: 统计总线中重复域的个数函数*/
  347. int    FmlFieldNum(const char *cFmlData, const char *cFieldName, int *iNum)
  348. {
  349.     char    *p = (char *) cFmlData;
  350.     char    *n, *q, *v;
  351.     int    r;
  352.     int    num,flag;

  353.     num=0;
  354.     flag=0;

  355.     while ( ( n = strchr( p, SYM_FML_FS ) ) != NULL )
  356.     {
  357.         *n = 0;
  358.         q = strchr( p, SYM_FML_NV );
  359.         *n = SYM_FML_FS;
  360.         if ( q != NULL )
  361.         {
  362.             *q = 0;
  363.             if ( ( v = strchr( p, SYM_FML_AN ) ) != NULL )
  364.                 *v = 0;
  365.             else
  366.             {
  367.                 *iNum=0;
  368.                 if( v != NULL)
  369.                     *v=SYM_FML_AN;
  370.                 *q=SYM_FML_NV;
  371.                 p = n + 1;
  372.                 continue;
  373.             }
  374.             if ( ( r = strcmp( p, cFieldName ) ) == 0 )
  375.             {
  376.                 num++;
  377.                 flag=1;
  378.             }
  379.                 *v=SYM_FML_AN;
  380.                 *q=SYM_FML_NV;
  381.         }
  382.         p = n + 1;
  383.     }
  384.     *iNum=num;
  385.     if(flag == 1)
  386.         return RC_SUCC;
  387.     else
  388.         return RC_NFND;
  389. }

  390. char * StrTrim(char * pstr)
  391. {
  392.     if(NULL == pstr)
  393.     {
  394.         return NULL;
  395.     }
  396.     char * phead = pstr;
  397.     char * ptail = pstr + strlen(pstr) - 1;
  398.     while((*phead == ' ')&&(phead < ptail)) phead++;
  399.     while((*ptail == ' ')&&(ptail > phead)) ptail--;
  400.     *(ptail + 1) = '\0';
  401.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  402.     return pstr;
  403. }

  404. /***********************************************************
  405.  * 函 数: JugeFileExist()
  406.  * 功能描述: 判断文件是否存在
  407.  * 输入参数:lFilename    -    长文件名
  408.  * 返 回:0-文件存在;1-文件不存在
  409.  * 流程描述:
  410.  *    说 明:
  411.  * 修改记录:
  412.  * [修改人] [日期][描述]
  413. ***********************************************************/
  414. int JugeFileExist(char *lFileName)
  415. {
  416.     struct stat statbuf;
  417.     memset(&statbuf,0,sizeof(statbuf));
  418.     stat(lFileName,&statbuf);
  419.     if(statbuf.st_mode & S_IFREG)
  420.         return 0;
  421.     else
  422.         return 1;
  423. }

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