Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
239 lines (173 sloc) 8.06 KB
/*****************************************************************************/
/* */
/* Tetristats 1.00 */
/* Copyright (c) 2001-2017, Frederic Cambus */
/* https://github.com/fcambus/tetristats */
/* */
/* Last Updated: 2017-02-07 */
/* */
/* Tetristats is released under the BSD 2-Clause license. */
/* See LICENSE file for details. */
/* */
/*****************************************************************************/
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
FILE *input_file;
char *input_file_buffer;
int input_file_size;
struct stat input_file_stat;
FILE *output_file;
time_t output_file_time;
struct tx_scoreline {
char type;
char name[31];
unsigned long int score;
char inuse;
} tx_scoreline;
struct jx_scoreline {
char *type;
char *score;
char *name;
} *jx_scoreline;
int jx_lines;
int jx_tabspaces;
int loop;
/*****************************************************************************/
/* Create Output File */
/*****************************************************************************/
void create_output_file(char *output_file_name) {
output_file=fopen(output_file_name,"w");
if (output_file==NULL) {
printf("ERROR: Cannot create file %s\n\n",output_file_name);
exit(-1);
}
fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n",output_file);
fputs("<HTML>\n<HEAD>\n",output_file);
fputs("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; CHARSET=iso-8859-1\">\n",output_file);
fputs("<TITLE>Tetrinet Statistics</TITLE>",output_file);
fputs("<LINK REL=\"stylesheet\" HREF=\"tetristats.css\" TYPE=\"text/css\">",output_file);
fputs("</HEAD>\n",output_file);
fputs("<CENTER>\n<TABLE BORDER=\"3\">",output_file);
fputs("<TR CLASS=\"TITLE\"><TD WIDTH=\"5%\">&nbsp;Rank&nbsp;<TD WIDTH=\"65%\">&nbsp;Name<TD WIDTH=\"15%\"ALIGN=\"center\">&nbsp;Category<TD WIDTH=\"15%\"ALIGN=\"right\">Score&nbsp;</TD></TR>\n",output_file);
}
/*****************************************************************************/
/* Main */
/*****************************************************************************/
int main (int argc, char *argv[]) {
printf("-------------------------------------------------------------------------------\n");
printf(" Tetristats 1.00 (c) by Frederic Cambus 2001-2017\n");
printf("-------------------------------------------------------------------------------\n\n");
if (argc!=4) {
printf("USAGE: tetristats servertype inputfile outputfile\n\n");
printf("SERVERTYPE: -tx (for Tetrinetx)\n -jx (for JetriX)\n\n");
printf("EXAMPLES: tetristats -tx game.winlist tetristats.html\n tetristats -jx default.winlist tetristats.html\n\n");
exit(-1);
}
if ((strcmp(argv[1],"-jx")!=0) & (strcmp(argv[1],"-tx")!=0)) {
printf("ERROR: servertype must be either -tx (for Tetrinetx) or -jx (for JetriX)\n\n");
exit(-1);
}
/*****************************************************************************/
/* Load Input File */
/*****************************************************************************/
stat (argv[2], &input_file_stat);
input_file_size=input_file_stat.st_size;
input_file_buffer= (char *) malloc(input_file_size);
if (input_file_buffer==NULL) {
printf("ERROR: Cannot allocate memory\n\n");
exit(-1);
}
input_file=fopen(argv[2],"rb");
if (input_file==NULL) {
printf("ERROR: Cannot open file %s\n\n",argv[2]);
exit(-1);
}
fread(input_file_buffer,input_file_size,1,input_file);
fclose(input_file);
/*****************************************************************************/
/* Tetrinetx */
/*****************************************************************************/
if (strcmp(argv[1],"-tx")==0) {
if ((input_file_size==0) || (input_file_size%40!=0)) {
printf("ERROR: %s is not a Tetrinetx score file\n\n",argv[2]);
exit(-1);
}
create_output_file(argv[3]);
for (loop=0;loop<(input_file_size/40);loop++) {
memcpy(&tx_scoreline,input_file_buffer+(loop*40),40);
if ( tx_scoreline.type=='\0') {
break;
}
fprintf(output_file,"<TR CLASS=\"COLOR\"><TD>&nbsp;%i</TD><TD>&nbsp;%s</TD><TD ALIGN=\"center\">",loop+1,tx_scoreline.name);
switch (tx_scoreline.type) {
case 'p': fputs("Player",output_file);
break;
case 't': fputs("Team",output_file);
break;
default:
break;
}
fprintf(output_file,"</TD><TD ALIGN=\"right\">%lu&nbsp;</TD></TR>\n",tx_scoreline.score);
}
}
/*****************************************************************************/
/* Jetrix */
/*****************************************************************************/
if (strcmp(argv[1],"-jx")==0) {
for (loop=0;loop<input_file_size;loop++) {
switch (input_file_buffer[loop]) {
case '\t': jx_tabspaces++;
break;
case '\n': jx_lines++;
break;
default:
break;
}
}
if ((jx_lines==0) || (jx_tabspaces/jx_lines!=2)) {
printf("ERROR: %s is not a JetriX score file\n\n",argv[2]);
exit(-1);
}
create_output_file(argv[3]);
jx_scoreline=malloc(jx_lines);
if (jx_scoreline==NULL) {
printf("ERROR: Cannot allocate memory\n\n");
exit (-1);
}
jx_scoreline[0].type=strtok(input_file_buffer,"\t");
jx_scoreline[0].score=strtok(NULL,"\t");
jx_scoreline[0].name=strtok(NULL,"\t\n");
for (loop=1;loop<jx_lines;loop++) {
jx_scoreline[loop].type=strtok(NULL,"\t");
jx_scoreline[loop].score=strtok(NULL,"\t");
jx_scoreline[loop].name=strtok(NULL,"\t\n");
}
for (loop=0;loop<jx_lines;loop++) {
if (strcmp(jx_scoreline[loop].type,"p")==0) {
jx_scoreline[loop].type="Player";
}
if (strcmp(jx_scoreline[loop].type,"t")==0) {
jx_scoreline[loop].type="Team";
}
fprintf(output_file,"<TR CLASS=\"COLOR\"><TD>&nbsp;%i</TD><TD>&nbsp;%s</TD><TD ALIGN=\"center\">%s</TD><TD ALIGN=\"right\">%s&nbsp;</TD></TR>\n",loop+1,jx_scoreline[loop].name,jx_scoreline[loop].type,jx_scoreline[loop].score);
}
free(jx_scoreline);
}
/*****************************************************************************/
/* Output File Footer */
/*****************************************************************************/
output_file_time = time((time_t *)NULL);
fputs("\n</TABLE>\n<BR>",output_file);
fprintf(output_file,"Created on %s with <A HREF=\"https://github.com/fcambus/tetristats\">Tetristats</A> 1.00 - (c) by Frederic Cambus 2001-2014\n",ctime(&output_file_time));
fputs("</CENTER>\n</BODY>\n</HTML>\n",output_file);
/*****************************************************************************/
/* Terminate Program */
/*****************************************************************************/
fclose(output_file);
free(input_file_buffer);
printf("Successfully created file %s\n\n",argv[3]);
return (0);
}