Computer Chess Club Archives


Search

Terms

Messages

Subject: Bob: tbs parser written

Author: Anthony Cozzie

Date: 12:04:14 12/06/03


I didn't see the size of the TB in the TBS file, which is sort of annoying, but
this program should generate a sorted list of the 6 man tbs and their winning
percentages (assuming I understand the TBS file correctly).  Could you run it
and post the results?

anthony

------------------------------------------
#include "stdio.h"
#include "dirent.h"
#include <list>

using namespace std;

struct tbinfo
{
  float pct;
  int size;
  char id[24];

  bool operator<(struct tbinfo &a) { 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 wins = 0, draws = 0, losses = 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)
      losses += t;
    else if(sscanf(buffer, "btm: Lost in %*d: %d", &t) == 1)
      wins += t;
    else if(sscanf(buffer, "wtm: Draws: %d", &t) == 1)
      draws += t;
    else if(sscanf(buffer, "btm: Draws: %d", &t) == 1)
      draws += t;
    else if(sscanf(buffer, "wtm: Mate in %*d: %d", &t) == 1)
      wins += t;
    else if(sscanf(buffer, "btm: Mate in %*d: %d", &t) == 1)
      losses += t;
  }

  fclose(fp);

  ti.pct = (float)((wins + draws / 2) / (wins + draws + losses));

  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);
    if(strlen(ti.id) > 5) {
      sprintf(fname_buffer, "%s/%s", dname, de->d_name);
      parse_tbs_file(ti, fname_buffer);
      tbi.push_back(ti);
    }
  }
  closedir(d);

  return 0;
}

int main(int argc, char* argv[])
{
  list<tbinfo> tbi;

  if(argc < 2) { printf("Usage: %s [TBS file director]\n", argv[0]); exit(-1); }

  tbi.clear();
  parse_tbs_directory(tbi, argv[1]);
  tbi.sort();

  for(list<tbinfo>::iterator i = tbi.begin(); i != tbi.end(); i++)
    printf("%-6s %.2f%%\n", (*i).id, (*i).pct * 100.0f);

  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.