📄 chtmlfile.cpp
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <Utility.h>
#include "Stru.h"
#include "Chtmlfile.h"
#define ID_IMGHTTP "<img src=\"http://"
#define ID_IMG "<img src=\"/"
#define ID_IMG_STD "<img src=\""
CHtmlFile::CHtmlFile()
{
m_szbuffer=new char[1024*1024*4];
assert(m_szbuffer!=NULL);
}
CHtmlFile::~CHtmlFile()
{
delete m_szbuffer;
}
void CHtmlFile::replaceDir(char *szurl,char *olddir,char *newdir)
{
char *p;
int iszlen=strlen(szurl);
if(memcmp(szurl,olddir,strlen(olddir))==0)
p=szurl;
else
{
p=strstr(szurl,olddir);
if(p==NULL)
return;
}
char *ps=p+strlen(olddir);
char *pd=p+strlen(newdir);
int isz=iszlen+p-szurl-strlen(olddir);
if(isz>0)
memmove(pd,ps,isz);
memcpy(p,newdir,strlen(newdir));
szurl[iszlen+strlen(newdir)-strlen(olddir)]=0;
return;
}
char * CHtmlFile::getnewHtml(char *pval,int &inlen,char *olddir,char *newdir)
{
int offset=0;
int newdirlen=strlen(newdir);
int olddirlen=strlen(olddir);
int i;
for(i=0;i<inlen-olddirlen+1;)
{
if(pval[i]==*olddir)
{
if(memcmp(pval,olddir,olddirlen)==0)
{
memcpy(m_szbuffer+offset,newdir,newdirlen);
offset+=newdirlen;
i+=olddirlen;
continue;
}
}
m_szbuffer[offset]=pval[i];
offset++;
i++;
}
for(;i<inlen;i++)
{
m_szbuffer[offset]=pval[i];
offset++;
}
inlen=offset;
return m_szbuffer;
}
char * CHtmlFile::repImg(char *pval,int &inlen,char *roothost)
{
int offset=0;
int i;
char *p;
char *p2;
char root[16];
char host[128];
p=strchr(roothost+1,'/');
int len=p-roothost;
memcpy(root,p,len);
root[len]=0;
p2=strchr(p+1,'/');
len=p2-p;
memcpy(host,p,len);
host[len]=0;
int lenhost=strlen(host);
for(i=0; i<inlen - lenhost+1; )
{
if(pval[i]=='<' && pval[i+1]=='i')
{
if(memcmp(pval,ID_IMG,strlen(ID_IMG))==0)
{
memcpy(m_szbuffer+offset,ID_IMG_STD,strlen(ID_IMG_STD));
offset+=strlen(ID_IMG_STD);
memcpy(m_szbuffer+offset,root,strlen(root));
offset+=strlen(root);
memcpy(m_szbuffer+offset,root,strlen(host));
offset+=strlen(host);
i+=strlen(ID_IMG);
continue;
}
else if(memcmp(pval,ID_IMGHTTP,strlen(ID_IMGHTTP))==0)
{
memcpy(m_szbuffer+offset,ID_IMG_STD,strlen(ID_IMG_STD));
offset+=strlen(ID_IMG_STD);
memcpy(m_szbuffer+offset,root,strlen(root));
offset+=strlen(root);
i+=strlen(ID_IMGHTTP);
continue;
}
}
m_szbuffer[offset]=pval[i];
offset++;
i++;
}
for(;i<inlen;i++)
{
m_szbuffer[offset]=pval[i];
offset++;
}
inlen=offset;
return m_szbuffer;
}
void CHtmlFile::writeFile(char *szindir,char *pval,int len)
{
char szdir[512];
char szbuf[512];
int idirs=0;
char *pone=strchr(szindir+1,'/');
while (pone!=NULL)
{
idirs++;
pone++;
pone=strchr(pone+1,'/');
}
idirs;
//printf("dir=[%s] idirs=[%d] \n",szindir,idirs);
strcpy(szbuf,szindir);
strcpy(szdir,"/");
pone=strtok(szbuf,"/");
for(int i=0;i<idirs;i++)
{
strcat(szdir,pone);
//printf("szdir=[%s]\n",szdir);
if(access(szdir,R_OK|W_OK)==-1)
{
//printf("create dir [%s]\n",szdir);
mkdir(szdir,0755);
}
else
{
//printf("dir existed [%s]\n",szdir);
}
strcat(szdir,"/");
pone=strtok(NULL,"/");
}
//printf("filename=[%s]\n",pone);
strcat(szdir,pone);
printf("to write file[%s] len=[%d]\n",szdir,len);
TLog::toLog("(%d)(%s) \n",len,szdir);
FILE *fp;
fp=fopen(szdir,"w");
if(fp==NULL)
return;
fwrite(pval,len,1,fp);
fclose(fp);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -