用戶:Antigng-bot/link
外觀
int proceedchild(char *title)
{
HTTP f,g;
char c[5000],tt[5000];
char buffer[5000],temp[5000];
char articlebuffer[3000];
char article[3000];
char section[3000];
char name[3000];
char ch,cht;
const char *match="http";
const int length=strlen(match);
const char *hostmatch="://zh.wikipedia.org";
const int hostmatchlength=strlen(hostmatch);
int matchpos=0;
int hostmatchpos=0;
int bufferlen=0;
int pathpos=0;
int articlepos=0;
int sectionpos=0;
int namepos=0;
struct neditargv point;
int todo=0,flag=0,status=0;
strcpy(c,"https://zh.wikipedia.org/w/index.php?action=raw&redirect=no&&title=");
URLEncode(title,strlen(title),tt,500);
strcat(c,tt);
f=hopen();
if(get(c,0,f))
{
hclose(f);
EnterCriticalSection(&tcs);
threadnumber--;
LeaveCriticalSection(&tcs);
return -1;
}
flag=0;
while(!heof(f))
{
ch=hgetc(f);
if(ch=='\r')
{
if(flag==0) flag=1;
else if(flag==2) flag=3;
}
if(ch=='\n')
{
if(flag==1) flag=2;
else if(flag==3)
{
flag=4;
break;
}
}
if(ch!='\n'&&ch!='\r') flag=0;
}
if(flag!=4)
{
EnterCriticalSection(&tcs);
threadnumber--;
LeaveCriticalSection(&tcs);
hclose(f);
return -2;
}
g=hopen();
hputs("&text=",strlen("&text="),g);
cht=ch;
ch=hgetc(f);
while(!heof(f))
{
switch(status)
{
case 1:
buffer[bufferlen]=ch;
bufferlen++;
if(ch!=match[matchpos]&&matchpos<length)
{
URLEncode(buffer,bufferlen,temp,4990);
hputs(temp,strlen(temp),g);
status=0;
buffer[0]=0;
bufferlen=0;
temp[0]=0;
matchpos=0;
}
else
{
if(matchpos<length)
{
matchpos++;
}
else
{
if(ch=='s') hostmatchpos=0;
else hostmatchpos=1;
matchpos=0;
status=2;
}
}
break;
case 2:
buffer[bufferlen]=ch;
bufferlen++;
if(ch!=hostmatch[hostmatchpos]&&hostmatchpos<hostmatchlength)
{
URLEncode(buffer,bufferlen,temp,4990);
hputs(temp,strlen(temp),g);
status=0;
buffer[0]=0;
bufferlen=0;
temp[0]=0;
hostmatchpos=0;
}
else
{
if(hostmatchpos<hostmatchlength)
{
hostmatchpos++;
}
else
{
hostmatchpos=0;
pathpos=0;
status=3;
}
}
break;
case 3:
buffer[bufferlen]=ch;
bufferlen++;
if(ch=='/')
{
if(pathpos<4)
{
URLEncode(buffer,bufferlen,temp,4990);
hputs(temp,strlen(temp),g);
status=0;
buffer[0]=0;
bufferlen=0;
temp[0]=0;
pathpos=0;
}
else
{
pathpos=0;
status=4;
articlepos=0;
articlebuffer[articlepos]=0;
}
}
else if(ch=='?'||ch==' '||ch=='&'||ch==']')
{
URLEncode(buffer,bufferlen,temp,4990);
hputs(temp,strlen(temp),g);
status=0;
buffer[0]=0;
bufferlen=0;
temp[0]=0;
pathpos=0;
}
else pathpos++;
break;
case 4:
buffer[bufferlen]=ch;
bufferlen++;
switch(ch)
{
case ' ':
status=6;
namepos=0;
break;
case '#':
status=5;
sectionpos=0;
break;
case ']':
status=7;
break;
case '?':
status=8;
break;
default:
articlebuffer[articlepos]=ch;
articlepos++;
articlebuffer[articlepos]=0;
}
break;
case 5:
buffer[bufferlen]=ch;
bufferlen++;
switch(ch)
{
case '?':
status=8;
break;
case ']':
status=7;
break;
case ' ':
status=6;
namepos=0;
break;
default:
section[sectionpos]=ch;
sectionpos++;
section[sectionpos]=0;
}
break;
case 6:
buffer[bufferlen]=ch;
bufferlen++;
switch(ch)
{
case ' ':
if(namepos)
{
name[namepos]=ch;
namepos++;
name[namepos]=0;
}
break;
case ']':
status=7;
break;
default:
name[namepos]=ch;
namepos++;
name[namepos]=0;
}
break;
case 7:
if(!articlepos||URLDecode(articlebuffer,articlepos,article,2990)==-1)
{
/*
do something
*/
}
else
{
todo=1;
if(!namepos)
{
if(!sectionpos) bufferlen=sprintf(buffer,"[[:%s]]",article);
else bufferlen=sprintf(buffer,"[[:%s#%s|%s]]",article,section,article);
}
else if(!sectionpos)
{
if(strcmp(article,name)) bufferlen=sprintf(buffer,"[[:%s|%s]]",article,name);
else bufferlen=sprintf(buffer,"[[:%s]]",name);
}
else bufferlen=sprintf(buffer,"[[:%s#%s|%s]]",article,section,name);
}
URLEncode(buffer,bufferlen,temp,4990);
hputs(temp,strlen(temp),g);
status=0;
buffer[0]=0;
bufferlen=0;
temp[0]=0;
articlepos=0;
namepos=0;
sectionpos=0;
smartURLEncode(ch,g);
break;
case 8:
switch(ch)
{
case ']':
status=7;
break;
case ' ':
status=6;
namepos=0;
break;
}
break;
default:
if(ch!='['||cht=='[') smartURLEncode(ch,g);
else
{
status=1;
buffer[0]='[';
bufferlen=1;
}
}
cht=ch;
ch=hgetc(f);
}
if(todo&&!status)
{
point.newtext=g;
point.title=tt;
point.count=0;
smartedit(&point);
}
hclose(f);
hclose(g);
EnterCriticalSection(&tcs);
threadnumber--;
LeaveCriticalSection(&tcs);
return 0;
}