Author: Anthony Cozzie
Date: 07:12:37 12/07/03
The new version computes winning % for white & black, and should find the size &
cumulative size of the tbs as well (your directory structure is not quite the
same as mine, but it should still work). The program is run a little
differently now though.
-------------------------------------------------------------------------
#include "stdio.h"
#include "dirent.h"
#include "sys/stat.h"
#include <list>
using namespace std;
struct tbinfo
{
float pct;
float wpct;
float bpct;
long long size;
char id[24];
bool operator<(struct tbinfo &a) {
if(strlen(id) != strlen(a.id))
return strlen(id) < strlen(a.id);
else
return pct < a.pct;
}
};
typedef struct tbinfo tbinfo;
//Parses a tbs file into the relevant statistics.
//returns 1 on error, 0 on success. Currently assumes
//all TBs have less than 2^31 wins/draws/losses etc.
int parse_tbs_file(tbinfo &ti, const char *fname)
{
char buffer[1024];
FILE *fp;
double wwins = 0, wdraws = 0, wlosses = 0, bwins = 0, bdraws = 0, blosses = 0;
int t;
if(!(fp = fopen(fname, "r"))) return 1;
while(fgets(buffer, sizeof(buffer), fp) != NULL)
{
if( sscanf(buffer, "wtm: Lost in %*d: %d", &t) == 1)
wlosses += t;
else if(sscanf(buffer, "btm: Lost in %*d: %d", &t) == 1)
blosses += t;
else if(sscanf(buffer, "wtm: Draws: %d", &t) == 1)
wdraws += t;
else if(sscanf(buffer, "btm: Draws: %d", &t) == 1)
bdraws += t;
else if(sscanf(buffer, "wtm: Mate in %*d: %d", &t) == 1)
wwins += t;
else if(sscanf(buffer, "btm: Mate in %*d: %d", &t) == 1)
bwins += t;
}
fclose(fp);
ti.wpct = (float)((wwins + wdraws / 2) / (wwins + wdraws + wlosses));
ti.bpct = (float)((bwins + bdraws / 2) / (bwins + bdraws + blosses));
wwins += blosses; wdraws += bdraws; wlosses += bwins;
ti.pct = (float)((wwins + wdraws / 2) / (wwins + wdraws + wlosses));
ti.size = 0;
//occaisionally side with more pieces loses
if(ti.pct < 0.5)
ti.pct = 1 - ti.pct;
return 0;
}
int parse_tbs_directory(list<tbinfo> &tbi, const char *dname)
{
char fname_buffer[1024];
tbinfo ti;
struct dirent *de;
DIR *d;
if(!(d = opendir(dname))) return 1;
while((de = readdir(d)) != NULL) {
if(strcmp(".", de->d_name) == 0 || strcmp("..", de->d_name) == 0)
continue;
sscanf(de->d_name, "%[^.]", ti.id);
sprintf(fname_buffer, "%s/%s", dname, de->d_name);
parse_tbs_file(ti, fname_buffer);
tbi.push_back(ti);
}
closedir(d);
return 0;
}
//note that this will add the size of the "TBS" file as well,
//but its a pretty small error 10KB v 1GB :)
void find_tbs_size(tbinfo &tbi, const char *dir)
{
char fname_buffer[1024];
struct dirent **namelist;
struct stat s;
int n;
if((n = scandir(dir, &namelist, 0, alphasort)) < 0)
return; //error
while(n--) {
if(strcmp(namelist[n]->d_name, ".") == 0 || strcmp(namelist[n]->d_name,
"..") == 0)
{ free(namelist[n]); continue; }
sprintf(fname_buffer, "%s/%s", dir, namelist[n]->d_name);
stat(fname_buffer, &s);
if(S_ISDIR(s.st_mode))
find_tbs_size(tbi, fname_buffer);
else if(strncmp(tbi.id, namelist[n]->d_name, strlen(tbi.id)) == 0)
tbi.size += s.st_size;
free(namelist[n]);
}
free(namelist);
}
int main(int argc, char* argv[])
{
list<tbinfo> tbi;
char tbsdir[512];
double tot = 0, size;
if(argc < 2) {
printf("Usage: %s [tablebase root directory]\n", argv[0]);
printf("The program assumes all tablebases are in some directory structure,
and that\n"
"there is a directory called \"tbs\" that contains all the tbs files.\n");
exit(-1);
}
sprintf(tbsdir, "%s/%s", argv[1], "tbs");
tbi.clear();
parse_tbs_directory(tbi, tbsdir);
tbi.sort();
for(list<tbinfo>::iterator i = tbi.begin(); i != tbi.end(); i++)
find_tbs_size(*i, argv[1]);
printf("%-6s %-7s %-7s %-7s %-9s %-5s\n", "id", "pct", "wpct", "bpct",
"size", "cuml");
for(list<tbinfo>::iterator i = tbi.begin(); i != tbi.end(); i++) {
size = (float)(((double)(*i).size) / 0x40000000);
printf("%-6s %5.2f%% %5.2f%% %5.2f%% %5.2fGB %7.2fGB\n", (*i).id,
(*i).pct * 100.0f,
(*i).wpct * 100.0f, (*i).bpct * 100.0f, size, tot += size);
}
return 0;
}
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.