用户:Antigng-bot/interwikilinkfix

struct problemlist
{
	char title[100];
	struct problemlist *next;
};
struct problemlist *query()
{
	FILE *f,*g;
	char line[1000]={0},title[100]={0};
	const char *match="ns=\"0\" title=\"";
	const int length=strlen(match);
	register int i,j,k;
	struct problemlist *pre,*temp,*head=0;
	get("https://zh.wikipedia.org/w/api.php?action=query&list=search&srnamespace=0&format=xml&srlimit=500&srsearch=insource%3A%2F%5C%5B%5C%5B%5C%3A%5Cen%5C%3A%2F","a.txt",1);
	f=fopen("a.txt","r+");
	g=fopen("b.txt","w+");
	do
	{
		fgets(line,1000,f);
	}while(line[0]!='<'&&!feof(f));
	while(!feof(f))
	{
		i=0;
		while(line[i])
		{
			for(j=0;j<length;j++)
			{
				if(line[i+j]!=match[j]) break;
			}
			if(j==length)
			{
				
				for(k=0;line[i+j+k]!='\"'&&line[i+j+k]!=0;k++)
				{
					title[k]=line[i+j+k];
				}
				title[k]=0;
		//		printf("%s\n",title); 
				if(line[i+j+k]=='\"')
				{
					if(!head)
				  {
					    pre=temp=(struct problemlist *)malloc(sizeof(struct problemlist));
					    head=temp;
				   }
				   else
			     {
					     pre=temp;
					     temp=(struct problemlist *)malloc(sizeof(struct problemlist));
					     pre->next=temp;
				   }
				   strcpy(temp->title,title);
				   temp->next=0;
				   fprintf(g,"%s\n",title);
				}
			}
			i++;
		}
		fgets(line,1000,f);
	}
	fclose(f);
	fclose(g);
	return head;
}

int proceed(struct problemlist* p,struct problemlist **new,int depth)
{
	struct problemlist *head;
	FILE *f,*debug;
	register int count=0,i=0,error=0,j,k,l,cl,flag,status=0,todo=0;
	char c[1000],line[1000],tt[200],*newtext,temp[20],ch,chinese[500]={0},foreign[500]={0},link_en[1000]={0};
	char *match="[[:en:";
	int length=strlen(match);
	if(!p) return -1;
	newtext=(char *)malloc(1024*1024);
	head=p;
	for(count=0;count<depth&&head;count++,head=head->next)
	{
		strcpy(c,"https://zh.wikipedia.org/w/index.php?action=raw&title=");
		URLEncode(head->title,strlen(head->title),tt,200);
		strcat(c,tt);
		get(c,"text.txt",0);
	 //printf("mark 1:\n");
		f=fopen("text.txt","r+");
		flag=0;
		while(!feof(f))
    {
  	  ch=fgetc(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) continue;
  status=0;todo=0;error=0;cl=0;
//  printf("mark 2:\n");
	while(!feof(f))
	{
		
		fgets(line,1000,f);
	//	printf("%s\n",line);
		i=0;
		while(line[i])
		{
		 switch(line[i])
		 {
		 	case '{':status++;break;
		 	case '}': status--;break;
		 }
			if(line[i]==match[0]&&status<2)
			{
			//	printf("preok\n");
				for(j=0;j<length;j++)
				{
					if(line[i+j]!=match[j]) break;
				}
				
				if(j==length) 
				{
				//	printf("ok\n");
					i=i+j;
					foreign[0]=0;chinese[0]=0;
					k=0;l=0;
					while(line[i]&&line[i]!='|'&&line[i]!=']')
					{
						foreign[k]=line[i];
						i++;k++;
						foreign[k]=0;
					}
					switch(line[i])
					{
						case 0:error=1;break;
						case ']':if(line[i+1]!=']') 
						         {
						         	error=1;
						         	break;
					           }
					           else
					           {
					           	i+=2;
											break;					           	
										 }
					  case '|':	i++;
                      while(line[i]&&line[i]!=']')
						          {
						          	chinese[l]=line[i];
						          	i++;l++;
						          	chinese[l]=0;
											}	
											switch(line[i])
											{
												case 0:error=1;break;
												case ']':if(line[i+1]!=']')
												         {
												         	error=1;
												         	break;
																 }
																 else
																 {
																 	i+=2;
																 }
											}			 
					}
					if(error) break;
					if(!foreign[0])
					{
						error=1;
						break;
					}
					if(!chinese[0])
					{
						strcpy(chinese,foreign);
					}
					if(!todo) todo=1;
					sprintf(link_en,"{{link-en|%s|%s}}",chinese,foreign);
					strcat(newtext,link_en);
					cl+=strlen(link_en);
					continue;
				}
			}
		
			newtext[cl]=line[i];
			i++;
			cl++;newtext[cl]=0;
			
		}
	}
	newtext[cl]=0;
	if(error) continue;
	

	if(todo) edit(head->title,newtext);
		
		
	}
	free(newtext);
	
	fclose(f);
	return 0;
}