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

📄 utils.cpp

📁 一个C++写的RSS解析器,需要libxml2.rpm支持
💻 CPP
字号:
#include "string.h"
#include "stdio.h"
#include <ctype.h>

#include <sys/stat.h>

#include "utils.h"



#define HTML_EXT_LEN	256


void TRIM(char* a)
{ 
	int len=0; 
	while ((a)[len] && isspace((a)[len])) 
		len++; 

	memmove((a),&(a)[len], strlen(&(a)[len])+1); 
	while ((len=strlen(a))>0 && isspace((a)[len-1])) 
		(a)[len-1]='\0'; 
}

int filesize( char* path )
{
	if(path == NULL)
		return -1;

	int ret = 0;
	struct stat st;
	
    if (stat(path,&st) == -1)
	{
		return -1;
	}

    return st.st_size;
}

char* FormatHTML(char* data)
{
	if(data == NULL)
		return NULL;

	char* pBegin = NULL, *pEnd = NULL;
	char* pStart = data;
	char* pTemp = NULL;
	
	char* tag_gt = "&gt;";// ">"
	char* tag_lt = "&lt;"; //"<"
	
	int len = sizeof(data);
	int pos = 0;
	char* result = (char*)malloc(len + 1 + HTML_EXT_LEN);
	memset(result, 0, len + 1 + HTML_EXT_LEN);

	while(pStart)
	{
		pBegin = strchr(pStart, '<');
		pEnd = strchr(pStart, '>');

		if(!pBegin && !pEnd)
		{//no tag
			strcat(result, pStart);
			break;
		}else if(pBegin && !pEnd)
		{//only  '>'
			*pBegin = '\0';
			strcat(result, pStart);
			
			strcat(result, tag_gt);
			strcat(result, pBegin+1);
			
			break;
		}else if(!pBegin && pEnd)
		{//only  '<'
			*pEnd = '\0';

			strcat(result, pStart);
			strcat(result, tag_lt);
			strcat(result, pEnd+1);

			break;
		}else if(pBegin < pEnd)
		{// both '<' and '>', and '<' is before
			*pBegin = '\0';
			strcat(result, pStart);

			pTemp = strchr((pBegin + 1), '<');
			if(pTemp && pTemp < pEnd)
			{
				strcat(result, tag_lt);
				pStart = pBegin + 1;
			}
			else
				pStart = pEnd + 1;

			continue;
		}else
		{// both '<' and '>', and '>' is before
			*pEnd = '\0';
			strcat(result, tag_gt);
			strcat(result, pStart);

			pStart = pEnd + 1;
		}
	}

	if(strlen(result) == 0)
	{
		free(result);
		result = NULL;
	}

	return result;
}


char *CodeConvert(const char *inputstr, int method)
{
	int inbytesleft = 0;
	int outbytesleft = BUFSIZ;

	char outbuf[BUFSIZ];

	char *outbuf_ptr = outbuf;
	char *result = NULL;
	iconv_t conv_handle;

	if(!inputstr)
		return NULL;
	
	
	switch(method)
	{
	case METHOD_GB2312_TO_UTF8:
		conv_handle = iconv_open("UTF-8", "GB18030");
		break;
	case METHOD_UTF8_TO_GB2312:
		conv_handle = iconv_open("GB18030", "UTF-8");
		break;
	default:
		return NULL;
	}
		
	inbytesleft = strlen(inputstr);
		
	if(conv_handle == (iconv_t) -1)
	{	
		return NULL;
	}
	size_t nconv = iconv(conv_handle, (char**)&inputstr, (size_t*)&inbytesleft, &outbuf_ptr, (size_t*)&outbytesleft);
	if(nconv == (size_t) -1)
	{  	/* Not everything went right.*/
		sprintf(outbuf, ERROR_STRING_EN);
		if(iconv_close(conv_handle) != 0)
			printf("iconv_close error");

		outbuf_ptr = NULL;
		result = (char*)malloc(strlen(outbuf) + 1);
		strcpy(result, outbuf);
		return result;
	}
	
	*outbuf_ptr = '\0';
	if(iconv_close(conv_handle) != 0)
		printf("iconv_close");

	outbuf_ptr = NULL;
	result = (char*)malloc(strlen(outbuf) + 1);
	strcpy(result, outbuf);
	return result;
}

void codeConvert(char *content, int method)
{
#define OUT_SIZE 255	//这个值和数据库中t_superlink表中description字段长度一致

	if( !content )
		return;
		
	int content_left_len = strlen(content);
	
	int contentlen = strlen(content);
	
	char* out = (char*)malloc(OUT_SIZE);
	memset(out,0,OUT_SIZE);
	
	char* out_ptr = out;
	
	int out_left_len = OUT_SIZE;
	
	iconv_t conv_handle;

	switch(method)
	{
	case METHOD_UTF8_TO_GB2312:
		printf("dddd\n");
		conv_handle = iconv_open("GB18030", "utf-8");
		break;
	case METHOD_GB2312_TO_UTF8:
		conv_handle = iconv_open("utf-8", "GB18030");
		break;
	default:
		free(out);
		return;
	}
		
	if(conv_handle == (iconv_t) -1)
	{	
		return;
	}
	
	free(out);
	
	printf("3333\n");
	
	size_t nconv = iconv(conv_handle, &content, (size_t*)&content_left_len, &out_ptr, (size_t*)&out_left_len);
	printf("4444\n");
	if(nconv == (size_t) -1)
	{  	/* Not everything went right.*/
		printf("5555\n");
		if(iconv_close(conv_handle) != 0)
			printf("iconv_close error");
			
		
		memset(content, 0, strlen(content));

		
		if(out)
		{
			printf("6666\n");
			free(out);
			printf("7777\n");
		}
			
		return;
	}
	
	printf("%s\n", out);
	
	printf("9999\n");
	if(iconv_close(conv_handle) != 0)
		printf("iconv_close");
		
	int outlen = strlen(out);
	
	printf("%s\n", out);
	
	if(outlen > contentlen)
	{
		content = (char*)realloc(content, outlen+1);
	}
	
	strcpy(content, out);

	free(out);
}

int nodeNameIsExist(xmlDocPtr input,char *path)
{
	int ret=-1;
    xmlXPathContextPtr ctx=NULL;
    xmlXPathObjectPtr xpath=NULL;

    if(path==NULL||input==NULL)
    	return ret;

   	xmlXPathInit();
    ctx=xmlXPathNewContext(input);

    if((xpath=xmlXPathEvalExpression((xmlChar *)path,ctx))==NULL)
    	goto out;

    if(xmlXPathEvalPredicate(ctx,xpath)==1)
    	ret=0;

out:
   	if(xpath!=NULL)
    	xmlXPathFreeObject(xpath);

	if(ctx!=NULL)
    	xmlXPathFreeContext(ctx);

    return ret;
}


xmlNodePtr get_location (xmlDocPtr input, char *path)
{
  xmlNodePtr cur_tree_node = NULL;
  xmlXPathContextPtr ctx = NULL;
  xmlXPathObjectPtr xpath = NULL;

  if (path == NULL || input == NULL)
        return NULL;

  xmlXPathInit ();
  ctx = xmlXPathNewContext (input);

  if ((xpath = xmlXPathEvalExpression ((xmlChar *) path, ctx)) == NULL)
        goto out;

  if (xpath->nodesetval == NULL)
        goto out;

  if (xpath->nodesetval->nodeTab == NULL)
        goto out;

  cur_tree_node = xpath->nodesetval->nodeTab[0];

out:
  if (xpath != NULL)
        xmlXPathFreeObject (xpath);
  if (ctx != NULL)
        xmlXPathFreeContext (ctx);

  return cur_tree_node;
}

char*  get_locationContent (xmlDocPtr input, char *path)
{ 
  char *buf = NULL;
  xmlNodePtr cur_tree_node;
  
  if (path == NULL || input == NULL)
        return NULL;
  
  if ((cur_tree_node = get_location (input, path)) == NULL)
        return NULL;
  
  if ((buf = (char*)xmlNodeGetContent (cur_tree_node)) != NULL)
  {
        if (*buf == '\0')
        {
          free (buf);
          return NULL;
        }
  }
  return buf;
}


⌨️ 快捷键说明

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