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

  1. /*  $Id$
  2.  *  
  3.  *  File    latex.c
  4.  *  Part of    ChessBase utilities file format (CBUFF)
  5.  *  Author    Anjo Anjewierden, anjo@swi.psy.uva.nl
  6.  *  Purpose    LaTeX utility
  7.  *  Works with    GNU CC 2.4.5
  8.  *  
  9.  *  Notice    Copyright (c) 1993  Anjo Anjewierden
  10.  *  
  11.  *  History    05/10/93  (Created)
  12.  *          15/10/93  (Last modified)
  13.  */ 
  14.  
  15.  
  16. /*------------------------------------------------------------
  17.  *  Directives
  18.  *------------------------------------------------------------*/
  19.  
  20. #include "cbuff.h"
  21.  
  22. char *        UTILITY_NAME = "LaTeX utility";
  23. char *        UTILITY_VERSION = "1.1.0";
  24.  
  25. void        helpUtility(FILE *fd);
  26.  
  27. static void    latexGame(Game g, FILE *fd, int);
  28. static void    startLatexGame(Game, TextBuffer);
  29. static void    endLatexGame(Game, TextBuffer);
  30.  
  31.  
  32.  
  33. /*------------------------------------------------------------
  34.  *  Verify utility
  35.  *------------------------------------------------------------*/
  36.  
  37. void
  38. latexUtility(Option opt)
  39. { Game g = newGame();
  40.   CBase cb = opt->database;
  41.   long from = opt->from;
  42.   long to = (opt->to < getNoGamesCBase(cb) ? opt->to : getNoGamesCBase(cb));
  43.   long n;
  44.  
  45.   reportCBase(cb, stderr);
  46.  
  47.   n = to-from+1;
  48.   if (n < 0)
  49.     return;
  50.   
  51.   for (n=from; n<=to; n++)
  52.   { environmentError(cb, g, n);
  53.     initialiseGame(g, n, cb);
  54.     if (foundError())
  55.     { reportError(stderr);
  56.       continue;
  57.     }
  58.     latexGame(g, opt->output, opt->notation);
  59.     if (foundError())
  60.       reportError(stderr);
  61.   }
  62.  
  63.   freeGame(g);
  64. }
  65.  
  66.  
  67. /*------------------------------------------------------------
  68.  *  Printing games in informant style
  69.  *------------------------------------------------------------*/
  70.  
  71. static void
  72. latexGame(Game g, FILE *fd, int notation)
  73. { TextBuffer tb;
  74.   Position pos;
  75.  
  76.   tb = newTextBuffer(fd, 75);
  77.  
  78.   startLatexGame(g, tb);
  79.  
  80.   pos = newPosition();
  81.   if (!fullGameP(g))
  82.   { copyPosition(pos, g->position);
  83.     diagramPosition(pos, tb);
  84.   }
  85.  
  86.   printMovesGame(g, getFirstMoveGame(g), pos, 0, 0, notation, tb);
  87.  
  88.   endLatexGame(g, tb);
  89.  
  90.   freeTextBuffer(tb);
  91. }
  92.  
  93.  
  94. /*------------------------------------------------------------
  95.  *  Printing the header and the trailer of an informant game
  96.  *------------------------------------------------------------
  97.  *
  98.  *  This prints the header of a game in the style of the Chess
  99.  *  Informant.  It assumes LaTeX macros called ``informantgame'' and
  100.  *  ``informantmainline'' have been defined.
  101.  */
  102.  
  103. static void
  104. startLatexGame(Game g, TextBuffer tb)
  105. { char *eco = getEcoGame(g);
  106.   char *white = getWhiteGame(g);
  107.   char *black = getBlackGame(g);
  108.   char *source = getSourceGame(g);
  109.   int year = getYearGame(g);
  110.   char *result;
  111.   char *annotator = "";
  112.   char *reference = "";
  113.  
  114.   if (eco == NULL) eco = "";
  115.   if (white == NULL) white = "White (NN)";
  116.   if (black == NULL) black = "Black (NN)";
  117.   switch (getResultGame(g))
  118.   { case WHITE_WINS:  result = chessSymbol(WHITE_WINS); break;
  119.     case DRAW:        result = chessSymbol(DRAW); break;
  120.     case BLACK_WINS:  result = chessSymbol(BLACK_WINS); break;
  121.     default:          result = "Line";
  122.   }
  123.  
  124.   formatRawTextBuffer(tb, "\n\\informantgame{%s}{%s}{%s}{%s}{%s %d}",
  125.               eco, reference, white, black, source, year);
  126.   formatRawTextBuffer(tb, "\\begin{informantmainline}{%s}{%s}",
  127.               result, annotator);
  128. }
  129.  
  130.  
  131. static void
  132. endLatexGame(Game g, TextBuffer tb)
  133. { formatRawTextBuffer(tb, "\\end{informantmainline}");
  134. }
  135.  
  136.  
  137. /*------------------------------------------------------------
  138.  *  Main
  139.  *------------------------------------------------------------*/
  140.  
  141. int
  142. main(int argc, char *argv[])
  143. { int i;
  144.   Option options = newOption();
  145.  
  146.   initChessSymbols();
  147.  
  148.   for (i=1; i<argc; i++)
  149.   {
  150.     if (strhead(argv[i], "-insert"))    /* -insert */
  151.     { FILE *fd;
  152.  
  153.       ++i;
  154.       fd = fileArgument(argv[i], "-insert", argc, i, "r");
  155.       { char line[MAX_LINE_SIZE+1];
  156.  
  157.     while (fgets(line, MAX_LINE_SIZE, fd))
  158.       fputs(line, options->output);
  159.       }
  160.       fclose(fd);
  161.       continue;
  162.     }
  163.  
  164.     if (strhead(argv[i], "-"))
  165.     { int n;
  166.  
  167.       n = genericOption(options, argv, argc, i);
  168.       if (n == 0)
  169.       { fprintf(stderr, "Fatal: Unknown command %s\n", argv[i]);
  170.     fprintf(stderr, "Do ``%s -help'' or see the documentation\n", argv[0]);
  171.     exit(1);
  172.       }
  173.       i = n;
  174.       continue;
  175.     }
  176.  
  177.     setCurrentCBase(argv[i], "-database", argc, i);
  178.     options->database = CurrentBase;
  179.     latexUtility(options);
  180.     freeCBase(options->database);
  181.     options->database = (CBase) NULL;
  182.   }
  183.  
  184.   if (options->database)
  185.   { latexUtility(options);
  186.     freeCBase(options->database);
  187.   }
  188.  
  189.   exit(0);
  190. }
  191.  
  192.  
  193. /*------------------------------------------------------------
  194.  *  Help
  195.  *------------------------------------------------------------*/
  196.  
  197. void
  198. helpUtility(FILE *fd)
  199. { helpCBUFF(fd);
  200.   fprintf(fd, "%s options:\n", UTILITY_NAME);
  201.   fprintf(fd, "-insert file  Insert contents of file here\n");
  202. }
  203.