用户:Antigng/troubleshooting

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include "network.h"
#include "convert.h"
CRITICAL_SECTION cs;
CRITICAL_SECTION tcs;
CRITICAL_SECTION hcs;
CRITICAL_SECTION fcs;
char *matchstring=NULL;
int matchlength=0;
int *nextm=NULL;
static int kmpini()
{
	int i,j;
	nextm=(int *)malloc(matchlength*sizeof(int));
	i=0;
	nextm[0]=j=-1;
	while(i<matchlength-1)
	{
		if(j==-1)
		{
			j=0;
			i++;
			nextm[i]=0;
		}
		else if(matchstring[i]==matchstring[j])
		{
			i++;j++;
			nextm[i]=j;
		}
		else j=nextm[j];
	}
	return 0;
}
static int kmp(HTTP h)
{
	char ch;
	int i=0,j=0,todo=0,exit=0;
	ch=hgetc(h);
	do
	{
		while(i<matchlength)
		{
			if(i==-1)
			{
				ch=hgetc(h);
				if(heof(h))
				{
					exit=1;
					break;
				}
				i=0;
			}
			else if(ch==matchstring[i])
			{
				i++;
				ch=hgetc(h);
				if(heof(h))
				{
					exit=1;
					break;
				}
			}            
			else 
			{
				if(nextm[i]==-1)
				{
					i=-1;
				}
				else
				{
					j=i-nextm[i];
					i=i-j;
				}
			}      
		}
		if(i==matchlength) 
		{
			todo=1;
			i=0;
		}
	}while(!exit);
	return todo;
}
static int getrandomhtml(char *pageid,char *title)
{
	char tt[8192]={0};
	char url[8192]={0};
	HTTP h;
	URLEncode(title,strlen(title),tt,4096);
	sprintf(url,"/wiki/%s",tt);
	h=hopen();
	get(url,8888,0,h);
	skipresponseheader(h);
	if(kmp(h))
	{
		FILE *fp=NULL;
		char name[1024]={0};
		char ch=0;
		hrewind(h);
		sprintf(name,"%s.txt",pageid);
		fp=fopen(name,"w+");
		while(!heof(h))
		{
			ch=hgetc(h);
			fputc(ch,fp);
		}
		fclose(fp);
		hclose(h);
		return 1;
	}
	else
	{
		hclose(h);
		return 0;
	}
}
static int getrandomtitle()
{
	HTTP h;
	const char *mtc[]={"id","title"};
	char *mtv[2];
	char id[128]={0},title[1024]={0};
	char line[1024]={0};
	mtv[0]=id;
	mtv[1]=title;
	h=hopen();
	if(get("/w/api.php?action=query&format=xml&list=random&rnnamespace=0&rnfilterredir=nonredirects",8888,0,h))
	{
		return -1;
	}
	skipresponseheader(h);
	while(!heof(h))
	{
		if(xmlparsetag(h,line)==XML_HAS_VALUE)
		{
			if(!strcmp(line,"page"))
			{
				xmlparsearg(h,2,mtc,mtv);
				break;
			}
		}
	}
	hclose(h);
	if(id[0]&&title[0])
	{
		int count=0;
		for(count=0;title[count];count++)
		{
			if(title[count]==' ') title[count]='_';
		}
		if(getrandomhtml(id,title))
		{
			printf("Queried %s, error found.\n",id);
		}
		else
		{
			printf("Queried %s, no error.\n",id);
		}
		return 1;
	}
	return 0;
}
int main(void)
{
	matchstring=G2U("来自Wikipedia");
	matchlength=strlen(matchstring);
	buckini(20);
	kmpini();
	while(1)
	{
		getrandomtitle();
		Sleep(1000*10);
	}
	return 0;
}