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

  1. /*  $Id$
  2.  *  
  3.  *  File    cbuff.c
  4.  *  Part of    ChessBase utilities file format (CBUFF)
  5.  *  Author    Anjo Anjewierden, anjo@swi.psy.uva.nl
  6.  *        Horst Aurisch, aurisch@informatik.uni-bonn.de
  7.  *  Purpose    Main program
  8.  *  Works with    GNU CC 2.4.5
  9.  *  
  10.  *  Notice    Copyright (c) 1993  Anjo Anjewierden
  11.  *  
  12.  *  History    08/06/93  (Created)
  13.  *          10/11/93  (Last modified)
  14.  */ 
  15.  
  16.  
  17. /*------------------------------------------------------------
  18.  *  Directives
  19.  *------------------------------------------------------------*/
  20.  
  21. #include "cbuff.h"
  22.  
  23.  
  24. /*------------------------------------------------------------
  25.  *  Version information
  26.  *------------------------------------------------------------*/
  27.  
  28. #define VERSION        "CBUFF 0.9.0"
  29. #define VERSION_DATE    "November, 1993"
  30. #define CVS_VERSION    "$Revision: 1.1.1.1 $"
  31.  
  32.  
  33. /*------------------------------------------------------------
  34.  *  Flags and global variables
  35.  *------------------------------------------------------------*/
  36.  
  37. char *        NullString = "";
  38.  
  39. CBase        CurrentBase = NULL;
  40.  
  41.  
  42. /*------------------------------------------------------------
  43.  *  Argument passing
  44.  *------------------------------------------------------------*/
  45.  
  46. void
  47. setCurrentCBase(char *name, char *command, int argc, int arg)
  48. { if (argc <= arg)
  49.   { fprintf(stderr, "Fatal: Argument required for %s\n", command);
  50.     exit(1);
  51.   }
  52.   if (strcmp(name, "-") == 0)
  53.   { if (CurrentBase == NULL)
  54.     { fprintf(stderr, "Fatal: No current database\n");
  55.       exit(1);
  56.     }
  57.     return;
  58.   }
  59.   if (CurrentBase)
  60.     freeCBase(CurrentBase);
  61.   if ((CurrentBase = newCBase(name, "r")) == NULL)
  62.   { fprintf(stderr, "Fatal: Cannot open ChessBase file %s\n", name);
  63.     exit(1);
  64.   }
  65. }
  66.  
  67.  
  68. CBase
  69. databaseArgument(char *name, char *command, int argc, int arg)
  70. { CBase cb;
  71.  
  72.   if (argc <= arg)
  73.   { fprintf(stderr, "Fatal: Argument required for %s\n", command);
  74.     exit(1);
  75.   }
  76.   if ((cb = newCBase(name, "r")) == NULL)
  77.   { fprintf(stderr, "Fatal: Cannot open ChessBase file %s\n", name);
  78.     exit(1);
  79.   }
  80.   return cb;
  81. }
  82.  
  83.  
  84. CBase
  85. createBaseArgument(char *name, char *command, int argc, int arg)
  86. { CBase cb;
  87.  
  88.   if (argc <= arg)
  89.   { fprintf(stderr, "Fatal: Argument required for %s\n", command);
  90.     exit(1);
  91.   }
  92.   if ((cb = newCBase(name, "c")) == NULL)
  93.   { reportError(stderr);
  94.     exit(1);
  95.   }
  96.   return cb;
  97. }
  98.  
  99.  
  100. CBase
  101. appendBaseArgument(char *name, char *command, int argc, int arg)
  102. { CBase cb;
  103.  
  104.   if (argc <= arg)
  105.   { fprintf(stderr, "Fatal: Argument required for %s\n", command);
  106.     exit(1);
  107.   }
  108.   if ((cb = newCBase(name, "a")) == NULL)
  109.   { reportError(stderr);
  110.     exit(1);
  111.   }
  112.   return cb;
  113. }
  114.  
  115.  
  116. void
  117. checkCurrentBase(CBase cb, char *command)
  118. { if (cb == NULL)
  119.   { fprintf(stderr, "Fatal: No current database for %s\n", command);
  120.     exit(1);
  121.   }
  122. }
  123.  
  124.  
  125. long
  126. longArgument(char *val, char *command, int argc, int arg)
  127. { long l;
  128.  
  129.   if (argc <= arg)
  130.   { fprintf(stderr, "Fatal: Argument required for %s\n", command);
  131.     exit(1);
  132.   }
  133.   
  134.   if ((l=atol(val)) < 0)
  135.   { fprintf(stderr, "Warning: %s %s not a number\n", command, val);
  136.     return 1;
  137.   }
  138.   return l;
  139. }
  140.  
  141.  
  142. int
  143. intArgument(char *val, char *command, int argc, int arg)
  144. { int i;
  145.  
  146.   if (argc <= arg)
  147.   { fprintf(stderr, "Fatal: Argument required for %s\n", command);
  148.     exit(1);
  149.   }
  150.   
  151.   if ((i=atoi(val)) < 0)
  152.   { fprintf(stderr, "Warning: %s %s not a number\n", command, val);
  153.     return 1;
  154.   }
  155.   return i;
  156. }
  157.  
  158.  
  159. FILE *
  160. fileArgument(char *val, char *command, int argc, int arg, char *args)
  161. { FILE *fd;
  162.  
  163.   if (argc <= arg)
  164.   { fprintf(stderr, "Fatal: Argument required for %s\n", command);
  165.     exit(1);
  166.   }
  167.   
  168.   if ((fd=fopenCbuffFile(val, args)) == NULL)
  169.   { fprintf(stderr, "Fatal: Cannot open file %s\n", val);
  170.     return NULL;
  171.   }
  172.   return fd;
  173. }
  174.  
  175.  
  176. char *
  177. stringArgument(char *val, char *command, int argc, int arg)
  178. { if (argc <= arg)
  179.   { fprintf(stderr, "Fatal: Argument required for %s\n", command);
  180.     exit(1);
  181.   }
  182.   
  183.   return val;
  184. }
  185.  
  186.  
  187. /*------------------------------------------------------------
  188.  *  Local functions
  189.  *------------------------------------------------------------*/
  190.  
  191. void
  192. versionCBUFF(FILE *fd)
  193. { fprintf(fd, "Welcome to ChessBase utilities file format (CBUFF).\n");
  194.   fprintf(fd, "There is absolutely no warranty for CBUFF.\n");
  195.   fprintf(fd, "%s %s, based on %s.\nCopyright (c) 1993 Anjo Anjewierden\n",
  196.       UTILITY_NAME, UTILITY_VERSION, VERSION);
  197. }
  198.  
  199.  
  200. void
  201. helpCBUFF(FILE *fd)
  202. { versionCBUFF(fd);\
  203.   fprintf(fd, "\n");
  204.   fprintf(fd, "Generic options of all CBUFF utilities:\n");
  205.   fprintf(fd, "-append db    Output database (for appending)\n");
  206.   fprintf(fd, "-database db  Make database current\n");
  207.   fprintf(fd, "-dump db      Output database (for creating)\n");
  208.   fprintf(fd, "-from n       Only consider games from n\n");
  209.   fprintf(fd, "-help         Prints help\n");
  210.   fprintf(fd, "-language s   Prints piece names in language s\n");
  211.   fprintf(fd, "-long         Long algebraic notation\n");
  212.   fprintf(fd, "-output file  Specifies default output file\n");
  213.   fprintf(fd, "-short        Short algebraic notation (default)\n");
  214.   fprintf(fd, "-symbols file File maps chess symbols onto strings\n");
  215.   fprintf(fd, "-to n         Only consider games to n\n");
  216.   fprintf(fd, "-verbose      Prints additional data (mainly for debugging)\n");
  217.   fprintf(fd, "-version      Prints version information\n");
  218.   fprintf(fd, "\n");
  219. }    
  220.  
  221.  
  222. /*------------------------------------------------------------
  223.  *  Support functions
  224.  *------------------------------------------------------------*/
  225.  
  226. bool
  227. strhead(char *s, char *head)
  228. { if (strncmp(s, head, strlen(head)) == 0)
  229.     return TRUE;
  230.   return FALSE;
  231. }
  232.