+/* download the file if it is missing */
+void zeRace_download_file(char *file)
+{
+ IPaddress ip;
+ TCPsocket tcpsock;
+ char request[1024];
+ char response[10240],*tmp;
+ int len,result;
+ FILE *fic;
+ struct stat buf;
+
+ if (stat(file,&buf)<0)
+ {
+ printf("downloading file \"%s\" : ",file);
+ fflush(stdout);
+
+ if (SDLNet_ResolveHost(&ip,"royale.zerezo.com",80)==-1)
+ {
+ fprintf(stderr,"SDLNet_ResolveHost: %s\n",SDLNet_GetError());
+ return;
+ }
+
+ tcpsock=SDLNet_TCP_Open(&ip);
+ if (!tcpsock)
+ {
+ fprintf(stderr,"SDLNet_TCP_Open: %s\n",SDLNet_GetError());
+ return;
+ }
+
+ sprintf(request,
+ "GET /zerace/%s HTTP/1.0\n"
+ "Host: royale.zerezo.com\n"
+ "User-Agent: zeRace " VERSION "\n"
+ "\n",file);
+ len=strlen(request);
+ result=SDLNet_TCP_Send(tcpsock,request,len);
+ if (result<len)
+ fprintf(stderr,"SDLNet_TCP_Send: %s\n",SDLNet_GetError());
+ else
+ {
+ if ((fic=fopen(file,"wt"))==NULL)
+ {
+ fprintf(stderr,"can't create \"%s\" file\n",file);
+ zeRace_exit();
+ }
+ len=SDLNet_TCP_Recv(tcpsock,response,10240);
+ tmp=response;
+ while (*tmp!='\n' || *(tmp+1)!='\r') tmp++;
+ tmp+=3;
+ fwrite(tmp,1,len+response-tmp,fic);
+ while ((len=SDLNet_TCP_Recv(tcpsock,response,10240))) fwrite(response,1,len,fic);
+ fclose(fic);
+ printf("done\n");
+ }
+
+ SDLNet_TCP_Close(tcpsock);
+ }
+}
+
+