home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / CBUFF09.ZIP / SRC.ZIP / NAMES.C < prev    next >
C/C++ Source or Header  |  1993-11-16  |  5KB  |  207 lines

  1. /*  $Id$
  2.  *  
  3.  *  File    names.c
  4.  *  Part of    ChessBase utilities file format (CBUFF)
  5.  *  Author    Anjo Anjewierden, anjo@swi.psy.uva.nl
  6.  *  Purpose    Names utility
  7.  *  Works with    GNU CC 2.4.5
  8.  *  
  9.  *  Notice    Copyright (c) 1993  Anjo Anjewierden
  10.  *  
  11.  *  History    14/11/93  (Created)
  12.  *          14/11/93  (Last modified)
  13.  */ 
  14.  
  15.  
  16. /*------------------------------------------------------------
  17.  *  Directives
  18.  *------------------------------------------------------------*/
  19.  
  20. #include "cbuff.h"
  21.  
  22.  
  23. char *        UTILITY_NAME = "Names utility";
  24. char *        UTILITY_VERSION = "1.0.0";
  25.  
  26. void        helpUtility(FILE *fd);
  27.  
  28. FILE *        PlayersFile = NULL;
  29. FILE *        NamesFile = NULL;
  30. FILE *        SourceFile = NULL;
  31. FILE *        PlaceFile = NULL;
  32.  
  33. StringTable    PlayersTable = NULL;
  34. StringTable    NamesTable = NULL;
  35. StringTable    SourceTable = NULL;
  36. StringTable    PlaceTable = NULL;
  37.  
  38.  
  39. /*------------------------------------------------------------
  40.  *  Names utility
  41.  *------------------------------------------------------------*/
  42.  
  43. void
  44. namesUtility(Option opt)
  45. { Game g = newGame();
  46.   CBase cb = opt->database;
  47.   long from = opt->from;
  48.   long to = (opt->to < getNoGamesCBase(cb) ? opt->to : getNoGamesCBase(cb));
  49.   long n;
  50.  
  51.   reportCBase(cb, stderr);
  52.  
  53.   if (PlayersFile) PlayersTable = newStringTable(1000);
  54.   if (NamesFile)  NamesTable = newStringTable(1000);
  55.   if (SourceFile)  SourceTable = newStringTable(1000);
  56.   if (PlaceFile)   PlaceTable = newStringTable(1000);
  57.  
  58.   n = to-from+1;
  59.   if (n < 0)
  60.     return;
  61.   
  62.   for (n=from; n<=to; n++)
  63.   { environmentError(cb, g, n);
  64.     initialiseGame(g, n, cb);
  65.     if (foundError())
  66.     { reportError(stderr);
  67.       continue;
  68.     }
  69.  
  70.     if (NamesFile)
  71.     { char *white;
  72.       char *black;
  73.       if (!containsPlayerNamesGameP(g))
  74.     continue;
  75.  
  76.       white = getWhiteGame(g);
  77.       if (white == NULL)
  78.       { fprintf(stderr, "Players not extracted: %s\n", getPlayersGame(g));
  79.     continue;
  80.       }
  81.       incStringTable(NamesTable, white);
  82.       black = getBlackGame(g);
  83.       incStringTable(NamesTable, black);
  84.     }
  85.  
  86.     if (PlayersFile)
  87.       incStringTable(PlayersTable, getPlayersGame(g));
  88.  
  89.     if (SourceFile)
  90.       incStringTable(SourceTable, getSourceGame(g));
  91.     
  92.     if (PlaceFile)
  93.       incStringTable(PlaceTable, getPlaceGame(g));
  94.   }
  95.  
  96.   if (NamesFile)
  97.   { printStringTable(NamesTable, "%4ld", NamesFile);
  98.     freeStringTable(NamesTable);
  99.     fclose(NamesFile);
  100.     NamesFile = NULL;
  101.   }
  102.  
  103.   if (PlayersFile)
  104.   { printStringTable(PlayersTable, "%4ld", PlayersFile);
  105.     freeStringTable(PlayersTable);
  106.     fclose(PlayersFile);
  107.     PlayersFile = NULL;
  108.   }
  109.  
  110.   if (PlaceFile)
  111.   { printStringTable(PlaceTable, "%4ld", PlaceFile);
  112.     freeStringTable(PlaceTable);
  113.     fclose(PlaceFile);
  114.     PlaceFile = NULL;
  115.   }
  116.  
  117.   if (SourceFile)
  118.   { printStringTable(SourceTable, "%4ld", SourceFile);
  119.     freeStringTable(SourceTable);
  120.     fclose(SourceFile);
  121.     SourceFile = NULL;
  122.   }
  123.  
  124.   freeGame(g);
  125. }
  126.  
  127.  
  128. /*------------------------------------------------------------
  129.  *  Main
  130.  *------------------------------------------------------------*/
  131.  
  132. int
  133. main(int argc, char *argv[])
  134. { int i;
  135.   Option options = newOption();
  136.  
  137.   initChessSymbols();
  138.  
  139.   for (i=1; i<argc; i++)
  140.   {
  141.     if (strhead(argv[i], "-names"))    /* -names */
  142.     { ++i;
  143.       NamesFile = fileArgument(argv[i], "-names", argc, i, "w");
  144.       continue;
  145.     }
  146.  
  147.     if (strhead(argv[i], "-players"))    /* -players */
  148.     { ++i;
  149.       PlayersFile = fileArgument(argv[i], "-players", argc, i, "w");
  150.       continue;
  151.     }
  152.  
  153.     if (strhead(argv[i], "-place"))    /* -place */
  154.     { ++i;
  155.       PlaceFile = fileArgument(argv[i], "-place", argc, i, "w");
  156.       continue;
  157.     }
  158.  
  159.     if (strhead(argv[i], "-source"))    /* -source */
  160.     { ++i;
  161.       SourceFile = fileArgument(argv[i], "-source", argc, i, "w");
  162.       continue;
  163.     }
  164.  
  165.     if (strhead(argv[i], "-"))
  166.     { int n;
  167.  
  168.       n = genericOption(options, argv, argc, i);
  169.       if (n == 0)
  170.       { fprintf(stderr, "Fatal: Unknown command %s\n", argv[i]);
  171.     fprintf(stderr, "Do ``%s -help'' or see the documentation\n", argv[0]);
  172.     exit(1);
  173.       }
  174.       i = n;
  175.       continue;
  176.     }
  177.  
  178.     setCurrentCBase(argv[i], "-database", argc, i);
  179.     options->database = CurrentBase;
  180.     namesUtility(options);
  181.     freeCBase(options->database);
  182.     options->database = (CBase) NULL;
  183.   }
  184.  
  185.   if (options->database)
  186.   { namesUtility(options);
  187.     freeCBase(options->database);
  188.   }
  189.  
  190.   return 0;
  191. }
  192.  
  193.  
  194. /*------------------------------------------------------------
  195.  *  Help
  196.  *------------------------------------------------------------*/
  197.  
  198. void
  199. helpUtility(FILE *fd)
  200. { helpCBUFF(fd);
  201.   fprintf(fd, "%s options:\n", UTILITY_NAME);
  202.   fprintf(fd, "-names file   Generates player names in file (Black, White)\n");
  203.   fprintf(fd, "-place file   Generates place (city) in file\n");
  204.   fprintf(fd, "-players file Generates players field in file\n");
  205.   fprintf(fd, "-source file  Generates source field in file\n");
  206. }
  207.