Computer Chess Club Archives


Search

Terms

Messages

Subject: sigh, bugs fixed (I think)

Author: Anthony Cozzie

Date: 07:23:04 12/08/03

Go up one level in this thread


#include "stdio.h"
#include "dirent.h"
#include "sys/stat.h"
#include "ctype.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], *ws;
  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)
  {
    for(ws = buffer; isspace(*ws); ws++) ;

    if(     sscanf(ws, "wtm: Lost in %*d: %d", &t) == 1)
      wlosses += t;
    else if(sscanf(ws, "btm: Lost in %*d: %d", &t) == 1)
      blosses += t;
    else if(sscanf(ws, "wtm: Draws: %d", &t) == 1)
      wdraws += t;
    else if(sscanf(ws, "btm: Draws: %d", &t) == 1)
      bdraws += t;
    else if(sscanf(ws, "wtm: Mate in %*d: %d", &t) == 1)
      wwins += t;
    else if(sscanf(ws, "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;
}

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
	    && namelist[n]->d_name[strlen(tbi.id)] == '.'
	    && strcmp(namelist[n]->d_name + strlen(namelist[n]->d_name) - 3, "tbs") !=
0)
      tbi.size += s.st_size;
    free(namelist[n]);
  }
  free(namelist);
}

int main(int argc, char* argv[])
{
  list<tbinfo> tbi;
  char tbsdir[512];
  float tot = 0;
  float size;
  long long bsize = 0;

  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");
    printf("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++)
    { (*i).size = 0; find_tbs_size(*i, argv[1]); }

  printf("%-6s %7s %8s %8s  %7s %14s %10s %14s\n", "id", "pct", "wpct", "bpct",
	 "size", "bytes", "cuml", "bytes");
  for(list<tbinfo>::iterator i = tbi.begin(); i != tbi.end(); i++) {
    size = (float)(((double)(*i).size) / 0x40000000);
    printf("%-6s %6.2f%%  %6.2f%%  %6.2f%%  %5.2fGB %14llu %8.2fGB %14llu\n",
	   (*i).id, (*i).pct * 100.0f, (*i).wpct * 100.0f, (*i).bpct * 100.0f, size,
(*i).size,
	   tot += size, bsize += (*i).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.