?? utils.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 = ">";// ">"
char* tag_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 + -