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

  1. /*  $Id$
  2.  *  
  3.  *  File    clean.c
  4.  *  Part of    ChessBase utilities file format (CBUFF)
  5.  *  Author    Anjo Anjewierden, anjo@swi.psy.uva.nl
  6.  *  Purpose    Clean up utility
  7.  *  Works with    GNU CC 2.4.5
  8.  *  
  9.  *  Notice    Copyright (c) 1993  Anjo Anjewierden
  10.  *  
  11.  *  History    15/10/93  (Created)
  12.  *          14/11/93  (Last modified)
  13.  */ 
  14.  
  15.  
  16. /*------------------------------------------------------------
  17.  *  Directives
  18.  *------------------------------------------------------------*/
  19.  
  20. #include "cbuff.h"
  21.  
  22. char *        UTILITY_NAME = "Clean utility";
  23. char *        UTILITY_VERSION = "1.1.0";
  24.  
  25. void        helpUtility(FILE *fd);
  26.  
  27. bool        DELETE_OPTION;
  28.  
  29.  
  30. /*------------------------------------------------------------
  31.  *  Clean utility
  32.  *------------------------------------------------------------*/
  33.  
  34. void
  35. cleanUtility(Option opt)
  36. { Game g = newGame();
  37.   CBase cb = opt->database;
  38.   long from = opt->from;
  39.   long to = (opt->to < getNoGamesCBase(cb) ? opt->to : getNoGamesCBase(cb));
  40.   long n;
  41.  
  42.   reportCBase(cb, stderr);
  43.  
  44.   if (opt->dump == NULL)
  45.   { fprintf(stderr, "No output database specified (-dump or -append option)\n");
  46.     exit(1);
  47.   }
  48.  
  49.   n = to-from+1;
  50.   if (n < 0)
  51.     return;
  52.   
  53.   for (n=from; n<=to; n++)
  54.   { environmentError(cb, g, n);
  55.     initialiseGame(g, n, cb);
  56.     if (foundError())
  57.     { reportError(stderr);
  58.       continue;
  59.     }
  60.     if (DELETE_OPTION && deletedGameP(g))
  61.     { if (foundError())
  62.       { reportError(stderr);
  63.     continue;
  64.       }
  65.       fprintf(stderr, "Deleting game %6ld:  %s, %s\n",
  66.           n, getPlayersGame(g), getSourceGame(g));
  67.       deleteGameCBase(cb, n);
  68.     }
  69.     if (foundError())
  70.       reportError(stderr);
  71.   }
  72.  
  73.   exportManyCBase(opt->dump, cb, 1, getNoGamesCBase(cb));
  74.  
  75.   freeGame(g);
  76. }
  77.  
  78.  
  79. /*------------------------------------------------------------
  80.  *  Main
  81.  *------------------------------------------------------------*/
  82.  
  83. int
  84. main(int argc, char *argv[])
  85. { int i;
  86.   Option options = newOption();
  87.  
  88.   /* initChessSymbols(); */
  89.  
  90.   DELETE_OPTION = TRUE;
  91.  
  92.   for (i=1; i<argc; i++)
  93.   {
  94.     if (strhead(argv[i], "-delete"))
  95.     { DELETE_OPTION = TRUE;
  96.       continue;
  97.     }
  98.  
  99.     if (strhead(argv[i], "-delete"))
  100.     { DELETE_OPTION = FALSE;
  101.       continue;
  102.     }
  103.  
  104.     if (strhead(argv[i], "-"))
  105.     { int n;
  106.  
  107.       n = genericOption(options, argv, argc, i);
  108.       if (n == 0)
  109.       { fprintf(stderr, "Fatal: Unknown command %s\n", argv[i]);
  110.     fprintf(stderr, "Do ``%s -help'' or see the documentation\n", argv[0]);
  111.     exit(1);
  112.       }
  113.       i = n;
  114.       continue;
  115.     }
  116.  
  117.     setCurrentCBase(argv[i], "-database", argc, i);
  118.     options->database = CurrentBase;
  119.     cleanUtility(options);
  120.     freeCBase(options->database);
  121.     options->database = (CBase) NULL;
  122.   }
  123.  
  124.   if (options->database)
  125.   { cleanUtility(options);
  126.     freeCBase(options->database);
  127.   }
  128.  
  129.   exit(0);
  130. }
  131.  
  132.  
  133. /*------------------------------------------------------------
  134.  *  Help
  135.  *------------------------------------------------------------*/
  136.  
  137. void
  138. helpUtility(FILE *fd)
  139. { helpCBUFF(fd);
  140.   fprintf(fd, "%s options:\n", UTILITY_NAME);
  141.   fprintf(fd, "-nodelete     Does not remove deleted games\n");
  142.   fprintf(fd, "-delete       Removes deleted games (default)\n");
  143. }
  144.