⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parse.c

📁 UNIX/LINUX平台下面SMS网管原代码
💻 C
字号:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <libxml/xmlmemory.h>#include <libxml/parser.h>#include <iconv.h>#include "apgloble.h"#include "etc.h"extern char etcfile[128];int convert (unsigned char *in, unsigned char *out);/*将UTF8转为ISO8859*/int convert (unsigned char *in, unsigned char *out){ 	unsigned char *p, *q;        p = in;        q = out;        while(*p)        {                if(*p < 0x7f)                {                        *q++ = *p++ ;                } 		else                {                        *q = (*p<<6) | (*(p+1) & 0x3f);                        p+=2;                        q++;                }        }        *q = 0x00;	return 0;}/****************************************  *解析doc节点值				*  *输入:doc		文档指针	*  *     cur		当前节点指针	*  *     host_field	tag名称		*  *输出:tag_value	tag值		*  ****************************************/void parseTxt(xmlDocPtr doc, xmlNodePtr cur, char *host_field, char *tag_value){	xmlChar *value;	xmlNodePtr gRsp, gAc;	int    IsFound = 0;	char   outbuf[1024];	cur = cur->xmlChildrenNode;	while(cur != NULL)	{		if(cur-> type != XML_ELEMENT_NODE)		{ 			cur = cur->next;			continue;		}		if(!xmlStrcmp(cur->name, (const xmlChar*)host_field))		{			fprintf(stdout, "cur->name = [%s]\n", cur->name);			value = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);			convert((unsigned char *)value, (unsigned char *)tag_value);			xmlFree(value);			break;		}		if(!xmlStrncmp(cur->name, (const xmlChar*)"gRsp", 4))		{			gRsp = cur->xmlChildrenNode;			while( gRsp != NULL )			{				if(gRsp->type != XML_ELEMENT_NODE)				{ 					gRsp = gRsp->next;					continue;				}				if(!xmlStrcmp(gRsp->name,(const xmlChar*)host_field))				{					value = xmlNodeListGetString(doc, gRsp->xmlChildrenNode, 1);					memset(outbuf, 0, sizeof(outbuf));					u2g(value,strlen(value),outbuf,sizeof(outbuf));					memcpy(tag_value, outbuf, strlen(outbuf));					IsFound = 1;					xmlFree(value);					break;				}				gRsp = gRsp->next;			}//end inner while		}//end if				if(!xmlStrncmp(cur->name, (const xmlChar*)"gAc", 3))		{			gAc = cur->xmlChildrenNode;			while( gAc != NULL )			{				if(gAc->type != XML_ELEMENT_NODE)				{ 					gAc = gAc->next;					continue;				}				if(!xmlStrcmp(gAc->name,(const xmlChar*)host_field))				{					value = xmlNodeListGetString(doc, gAc->xmlChildrenNode, 1);					memset(outbuf, 0, sizeof(outbuf));					u2g(value,strlen(value),outbuf,sizeof(outbuf));					memcpy(tag_value, outbuf, strlen(outbuf));					IsFound = 1;					xmlFree(value);					break;				}				gAc = gAc->next;			}//end inner while		}//end if				if( IsFound ) break;		cur = cur->next;	}//end external while	return;}int parseMoreTxt(FILE *fp, int *count, xmlDocPtr doc, xmlNodePtr cur, char *key){	char		buffer[512 + 1];	char		outbuf[1024], colsbuf[1024];	char		line[4096 + 1];	xmlChar 	*value;	xmlNodePtr 	gRows, rows, cols;	int 		i, j, ItemNo, lineno = 0;	char 		tmp[1024];	char 		bitmaps[1024], rspn_field[512];	gRows = cur->xmlChildrenNode;	while(gRows != NULL)	{		if(gRows-> type != XML_ELEMENT_NODE)		{ 			gRows = gRows->next;			continue;		}		/***		if(!xmlStrcmp(gRows->name, (const xmlChar*)"gCols"))		{			cols = gRows->xmlChildrenNode;						memset(colsbuf, 0, sizeof(colsbuf));			while(cols != NULL)			{				if(cols->type != XML_ELEMENT_NODE)				{ 					cols = cols->next;					continue;				}							value = xmlNodeListGetString(doc, cols->xmlChildrenNode, 1);				memset(outbuf, 0, sizeof(outbuf));				u2g(value,strlen(value),outbuf,sizeof(outbuf));				strcat(colsbuf, (const char *)outbuf);				strcat(colsbuf, (const char *)"|");								xmlFree(value);				cols = cols->next;			}			fprintf(fp, "%s\n", colsbuf);		}**/		if(!xmlStrcmp(gRows->name, (const xmlChar*)"gRows"))		{			rows = gRows->xmlChildrenNode;						while(rows != NULL)			{				if(rows->type != XML_ELEMENT_NODE)				{ 					rows = rows->next;					continue;				}							if(!xmlStrcmp(rows->name,(const xmlChar*)"row"))				{					value = xmlNodeListGetString(doc, rows->xmlChildrenNode, 1);					memset(outbuf, 0, sizeof(outbuf));					u2g(value,strlen(value),outbuf,sizeof(outbuf));					memset(line, 0, sizeof(line));					memcpy(line, outbuf, strlen(outbuf));										xmlFree(value);					memset(tmp, 0, sizeof(tmp));					for(j = 0; j < strlen(line); j++)					{						if( line[j] == ',' )						{							tmp[j] = '|';						}						else						{							tmp[j] = line[j];						}					}					tmp[strlen(tmp)] = '|';					++lineno;						fprintf(fp,"%s\n", tmp);										rows = rows->next;				}			}		}		gRows = gRows->next;	}		*count = lineno;	return 0;} int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen){	iconv_t cd;	int rc;	char **pin = &inbuf;	char **pout = &outbuf;	cd = iconv_open(to_charset,from_charset);	if (cd==0) return -1;	memset(outbuf,0,outlen);	if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;	iconv_close(cd);	return 0;}int u2g(char *inbuf,int inlen,char *outbuf,int outlen){	return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);}int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen){	return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -