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

  1. /*  $Id$
  2.  *  
  3.  *  File    list.c
  4.  *  Part of    ChessBase utilities file format (CBUFF)
  5.  *  Author    Anjo Anjewierden, anjo@swi.psy.uva.nl
  6.  *  Purpose    List utility
  7.  *  Works with    GNU CC 2.4.5
  8.  *  
  9.  *  Notice    Copyright (c) 1993  Anjo Anjewierden
  10.  *  
  11.  *  History    22/09/93  (Created)
  12.  *          08/11/93  (Last modified)
  13.  */ 
  14.  
  15.  
  16. /*------------------------------------------------------------
  17.  *  Directives
  18.  *------------------------------------------------------------*/
  19.  
  20. #include "cbuff.h"
  21.  
  22.  
  23. char *        UTILITY_NAME = "List utility";
  24. char *        UTILITY_VERSION = "1.1.0";
  25.  
  26. void        helpUtility(FILE *fd);
  27.  
  28. static void    setDefaultListUtility(char *name, int value);
  29. static void    defaultListUtility(FILE *fd);
  30.  
  31.  
  32. /*------------------------------------------------------------
  33.  *  Formats
  34.  *------------------------------------------------------------*/
  35.  
  36. struct field
  37. { char *    name;            /* Name of the field */
  38.   int        width;            /* Width in characters */
  39.   char *    format;            /* Style to print */
  40.   int        padding;        /* Number of padding characters */
  41.   bool        print;            /* Print or not? */
  42. } ListFields[] =
  43. { { "number",       6, "%%%dld",       1, TRUE },
  44.   { "players",    25, "%%-%d.%ds",    1, TRUE },
  45.   { "white",      15, "%%-%d.%ds",    1, FALSE },
  46.   { "elowhite",    4, "%%-%d.%dd",    1, FALSE },
  47.   { "black",      15, "%%-%d.%ds",    1, FALSE },
  48.   { "eloblack",    4, "%%-%d.%dd",    1, FALSE },
  49.   { "source",     25, "%%-%d.%ds",    1, TRUE },
  50.   { "place",      15, "%%-%d.%ds",    1, FALSE },
  51.   { "event",      15, "%%-%d.%ds",    1, FALSE },
  52.   { "round",       2, "%%%dd",        1, FALSE },
  53.   { "annotator",  15, "%%-%d.%ds",    1, FALSE },
  54.   { "eco",         6, "%%-%d.%ds",    1, FALSE },
  55.   { "ecomajor",    3, "%%-%d.%ds",    1, FALSE },
  56.   { "year",        4, "%%-%d.%dd",    1, TRUE },
  57.   { "result",      4, "%%-%d.%ds",    1, TRUE },
  58.   { "moves",       3, "%%%dd",        1, TRUE },
  59.   { "deleted",     1, "%%c",          0, TRUE },
  60.   { "marked",      1, "%%c",          0, TRUE },
  61.   { "position",    1, "%%c",          0, TRUE },
  62.   { "comments",    1, "%%c",          0, TRUE },
  63.   { "variations",  1, "%%c",          0, TRUE },
  64.   { "flags2",      1, "%%c",          0, TRUE },
  65.   { (char *) NULL, 0, (char *) NULL,  0, 0 }
  66. };
  67.  
  68.  
  69. /*------------------------------------------------------------
  70.  *  List utility
  71.  *------------------------------------------------------------*/
  72.  
  73. void
  74. listUtility(Option opt)
  75. { Game g = newGame();
  76.   CBase cb = opt->database;
  77.   FILE *fd = opt->output;
  78.   long from = opt->from;
  79.   long to = (opt->to < getNoGamesCBase(cb) ? opt->to : getNoGamesCBase(cb));
  80.   long n;
  81.  
  82.   reportCBase(cb, stderr);
  83.  
  84.   n = to-from+1;
  85.   if (n < 0)
  86.     return;
  87.   
  88.   for (n=from; n<=to; n++)
  89.   { environmentError(cb, g, n);
  90.     initialiseGame(g, n, cb);
  91.     if (foundError())
  92.     { reportError(stderr);
  93.       continue;
  94.     }
  95.     if (checkHeaderGame(g) == FALSE)
  96.     { reportError(stderr);
  97.       continue;
  98.     }
  99.     listGame(g, fd);
  100.     if (foundError())
  101.       reportError(stderr);
  102.   }
  103.  
  104.   freeGame(g);
  105. }
  106.  
  107.  
  108. /*------------------------------------------------------------
  109.  *  Listing game header
  110.  *------------------------------------------------------------*/
  111.  
  112. void
  113. listGame(Game g, FILE *fd)
  114. { int i;
  115.   struct field *f;
  116.   char format[MAX_NAME_SIZE+1];
  117.   char entry[MAX_NAME_SIZE+1];
  118.  
  119.   for (f=ListFields; f->name; f++)
  120.   { if (f->print == FALSE)
  121.       continue;
  122.     sprintf(format, f->format, f->width, f->width);
  123.     if (strcmp(f->name, "number") == 0)
  124.       fprintf(fd, format, getNumberGame(g)); else
  125.     if (strcmp(f->name, "players") == 0)
  126.       fprintf(fd, format, stringChessSymbol(getPlayersGame(g),entry)); else
  127.     if (strcmp(f->name, "white") == 0)
  128.       fprintf(fd, format, getWhiteGame(g)); else
  129.     if (strcmp(f->name, "elowhite") == 0)
  130.       fprintf(fd, format, getEloWhiteGame(g)); else
  131.     if (strcmp(f->name, "black") == 0)
  132.       fprintf(fd, format, getBlackGame(g)); else
  133.     if (strcmp(f->name, "eloblack") == 0)
  134.       fprintf(fd, format, getEloBlackGame(g)); else
  135.     if (strcmp(f->name, "source") == 0)
  136.       fprintf(fd, format, stringChessSymbol(getSourceGame(g),entry)); else
  137.     if (strcmp(f->name, "place") == 0)
  138.       fprintf(fd, format, getPlaceGame(g)); else
  139.     if (strcmp(f->name, "round") == 0)
  140.       fprintf(fd, format, getRoundGame(g)); else
  141.     if (strcmp(f->name, "event") == 0)
  142.       fprintf(fd, format, getEventGame(g)); else
  143.     if (strcmp(f->name, "annotator") == 0)
  144.       fprintf(fd, format, getAnnotatorGame(g)); else
  145.     if (strcmp(f->name, "eco") == 0)
  146.       fprintf(fd, format, (getEcoGame(g) || "")); else
  147.     if (strcmp(f->name, "ecomajor") == 0)
  148.       fprintf(fd, format, (getEcoMajorGame(g) || "")); else
  149.     if (strcmp(f->name, "year") == 0)
  150.       fprintf(fd, format, getYearGame(g)); else
  151.     if (strcmp(f->name, "result") == 0)
  152.     { Result r;
  153.  
  154.       r = getResultGame(g);
  155.       fprintf(fd, format, (r == NO_RESULT ? "No" : chessSymbol(r)));
  156.     } else
  157.     if (strcmp(f->name, "moves") == 0)
  158.       fprintf(fd, format, getMovesGame(g)); else
  159.     if (strcmp(f->name, "deleted") == 0)
  160.       fprintf(fd, format, deletedGameP(g) ? 'D' : ' '); else
  161.     if (strcmp(f->name, "marked") == 0)
  162.       fprintf(fd, format, markedGameP(g) ? 'M' : ' '); else
  163.     if (strcmp(f->name, "position") == 0)
  164.       fprintf(fd, format, fullGameP(g) ? ' ' : 'P'); else
  165.     if (strcmp(f->name, "comments") == 0)
  166.       fprintf(fd, format, containsCommentsGameP(g) ? 'C' : ' '); else
  167.     if (strcmp(f->name, "variations") == 0)
  168.       fprintf(fd, format, containsVariationsGameP(g) ? 'V' : ' '); else
  169.     if (strcmp(f->name, "flags2") == 0)
  170.       fprintf(fd, format, flags2Bit6GameP(g) ? '1' : '0'); else
  171.     { fprintf(stderr, "Internal error: Field named %s (listGame)\n", f->name);
  172.       exit(1);
  173.     }
  174.     for (i=0; i<f->padding; i++)
  175.       fprintf(fd, " ");
  176.   }
  177.   fprintf(fd, "\n");
  178. }
  179.  
  180.  
  181. /*------------------------------------------------------------
  182.  *  Main
  183.  *------------------------------------------------------------*/
  184.  
  185. int
  186. main(int argc, char *argv[])
  187. { int i;
  188.   Option options = newOption();
  189.  
  190.   initChessSymbols();
  191.  
  192.   for (i=1; i<argc; i++)
  193.   {
  194.     if (strhead(argv[i], "-annotator"))    /* -annotator */
  195.     { ++i;
  196.       setDefaultListUtility("annotator", intArgument(argv[i],"-annotator",argc,i));
  197.       continue;
  198.     }
  199.  
  200.     if (strhead(argv[i], "-b"))        /* -black */
  201.     { ++i;
  202.       setDefaultListUtility("black", intArgument(argv[i],"-black",argc,i));
  203.       continue;
  204.     }
  205.  
  206.     if (strhead(argv[i], "-comments"))    /* -comments */
  207.     { ++i;
  208.       setDefaultListUtility("comments", intArgument(argv[i],"-comments",argc,i));
  209.       continue;
  210.     }
  211.  
  212.     if (strhead(argv[i], "-def"))    /* -defaults */
  213.     { FILE *fd;
  214.  
  215.       ++i;
  216.       fd = fileArgument(argv[i], "-defaults", argc, i, "r");
  217.       defaultListUtility(fd);
  218.       fclose(fd);
  219.       continue;
  220.     }
  221.  
  222.     if (strhead(argv[i], "-del"))    /* -deleted */
  223.     { ++i;
  224.       setDefaultListUtility("deleted", intArgument(argv[i],"-deleted",argc,i));
  225.       continue;
  226.     }
  227.  
  228.     if (strhead(argv[i], "-eco"))    /* -eco */
  229.     { ++i;
  230.       setDefaultListUtility("eco", intArgument(argv[i],"-eco",argc,i));
  231.       continue;
  232.     }
  233.  
  234.     if (strhead(argv[i], "-ecom"))    /* -ecomajor */
  235.     { ++i;
  236.       setDefaultListUtility("ecomajor", intArgument(argv[i],"-ecomajor",argc,i));
  237.       continue;
  238.     }
  239.  
  240.     if (strhead(argv[i], "-elob"))    /* -eloblack */
  241.     { ++i;
  242.       setDefaultListUtility("eloblack", intArgument(argv[i],"-eloblack",argc,i));
  243.       continue;
  244.     }
  245.  
  246.     if (strhead(argv[i], "-elow"))    /* -elowhite */
  247.     { ++i;
  248.       setDefaultListUtility("elowhite", intArgument(argv[i],"-elowhite",argc,i));
  249.       continue;
  250.     }
  251.  
  252.     if (strhead(argv[i], "-ev"))    /* -event */
  253.     { ++i;
  254.       setDefaultListUtility("event", intArgument(argv[i],"-event",argc,i));
  255.       continue;
  256.     }
  257.  
  258.     if (strhead(argv[i], "-fl"))    /* -flags2 */
  259.     { ++i;
  260.       setDefaultListUtility("flags2", intArgument(argv[i],"-flags2",argc,i));
  261.       continue;
  262.     }
  263.  
  264.     if (strhead(argv[i], "-ma"))    /* -marked */
  265.     { ++i;
  266.       setDefaultListUtility("marked", intArgument(argv[i],"-marked",argc,i));
  267.       continue;
  268.     }
  269.  
  270.     if (strhead(argv[i], "-mo"))    /* -moves */
  271.     { ++i;
  272.       setDefaultListUtility("moves", intArgument(argv[i],"-moves",argc,i));
  273.       continue;
  274.     }
  275.  
  276.     if (strhead(argv[i], "-n"))        /* -number */
  277.     { ++i;
  278.       setDefaultListUtility("number", intArgument(argv[i],"-number",argc,i));
  279.       continue;
  280.     }
  281.  
  282.     if (strhead(argv[i], "-o"))        /* -output */
  283.     { ++i;
  284.       if (options->output != stdout)
  285.           fclose(options->output);
  286.       options->output = fileArgument(argv[i], "-output", argc, i, "w");
  287.       continue;
  288.     }
  289.  
  290.     if (strhead(argv[i], "-plac"))    /* -place */
  291.     { ++i;
  292.       setDefaultListUtility("place", intArgument(argv[i],"-place",argc,i));
  293.       continue;
  294.     }
  295.  
  296.     if (strhead(argv[i], "-play"))    /* -players */
  297.     { ++i;
  298.       setDefaultListUtility("players", intArgument(argv[i],"-players",argc,i));
  299.       continue;
  300.     }
  301.  
  302.     if (strhead(argv[i], "-po"))    /* -position */
  303.     { ++i;
  304.       setDefaultListUtility("position", intArgument(argv[i],"-position",argc,i));
  305.       continue;
  306.     }
  307.  
  308.     if (strhead(argv[i], "-re"))    /* -result */
  309.     { ++i;
  310.       setDefaultListUtility("result", intArgument(argv[i],"-result",argc,i));
  311.       continue;
  312.     }
  313.  
  314.     if (strhead(argv[i], "-ro"))    /* -round */
  315.     { ++i;
  316.       setDefaultListUtility("round", intArgument(argv[i],"-round",argc,i));
  317.       continue;
  318.     }
  319.  
  320.     if (strhead(argv[i], "-so"))    /* -source */
  321.     { ++i;
  322.       setDefaultListUtility("source", intArgument(argv[i],"-source",argc,i));
  323.       continue;
  324.     }
  325.  
  326.     if (strhead(argv[i], "-va"))    /* -variations */
  327.     { ++i;
  328.       setDefaultListUtility("variations", intArgument(argv[i],"-variations",argc,i));
  329.       continue;
  330.     }
  331.  
  332.     if (strhead(argv[i], "-w"))        /* -white */
  333.     { ++i;
  334.       setDefaultListUtility("white", intArgument(argv[i],"-white",argc,i));
  335.       continue;
  336.     }
  337.  
  338.     if (strhead(argv[i], "-y"))        /* -year */
  339.     { ++i;
  340.       setDefaultListUtility("year", intArgument(argv[i],"-year",argc,i));
  341.       continue;
  342.     }
  343.  
  344.     if (strhead(argv[i], "-"))
  345.     { int n;
  346.  
  347.       n = genericOption(options, argv, argc, i);
  348.       if (n == 0)
  349.       { fprintf(stderr, "Fatal: Unknown command %s\n", argv[i]);
  350.     fprintf(stderr, "Do ``%s -help'' or see the documentation\n", argv[0]);
  351.     exit(1);
  352.       }
  353.       i = n;
  354.       continue;
  355.     }
  356.  
  357.     setCurrentCBase(argv[i], "-database", argc, i);
  358.     options->database = CurrentBase;
  359.     listUtility(options);
  360.     freeCBase(options->database);
  361.     options->database = (CBase) NULL;
  362.   }
  363.  
  364.   if (options->database)
  365.   { listUtility(options);
  366.     freeCBase(options->database);
  367.   }
  368.  
  369.   exit(0);
  370. }
  371.  
  372.  
  373. /*------------------------------------------------------------
  374.  *  Defaults
  375.  *------------------------------------------------------------*/
  376.  
  377. static void
  378. setDefaultListUtility(char *name, int value)
  379. { struct field *f;
  380.  
  381.   if (strcmp(name, "*") == 0)
  382.   { for (f=ListFields; f->name; f++)
  383.     { if (value)
  384.       { f->width = value;
  385.     f->print = TRUE;
  386.       } else
  387.     f->print = FALSE;
  388.     }
  389.     return;
  390.   }
  391.  
  392.   for (f=ListFields; f->name; f++)
  393.   { if (strcmp(f->name, name) == 0)
  394.     { if (value)
  395.       { f->width = value;
  396.     f->print = TRUE;
  397.       } else
  398.     f->print = FALSE;
  399.       return;
  400.     }
  401.   }
  402.   fprintf(stderr, "Warning: No default called list.%s\n", name);
  403. }
  404.  
  405.  
  406. static void
  407. defaultListUtility(FILE *fd)
  408. { char line[MAX_LINE_SIZE+1];
  409.  
  410.   while (fgets(line, MAX_LINE_SIZE, fd))
  411.   { if (strhead(line, "list."))
  412.     { char name[MAX_NAME_SIZE+1];
  413.       int value;
  414.  
  415.       sscanf(line, "list.%s %d\n", name, &value);
  416.       setDefaultListUtility(name, value);
  417.     }
  418.   }
  419. }
  420.  
  421.  
  422. /*------------------------------------------------------------
  423.  *  Help
  424.  *------------------------------------------------------------*/
  425.  
  426. void
  427. helpUtility(FILE *fd)
  428. { helpCBUFF(fd);
  429.   fprintf(fd, "List utility options (argument 0 turns off a field)\n");
  430.   fprintf(fd, "-annotator n  Annotator of the game (15; not printed)\n");
  431.   fprintf(fd, "-black n      Name of Black (15; not printed)\n");
  432.   fprintf(fd, "-comments n   If game contains comments (1; prints 'C')\n");
  433.   fprintf(fd, "-deleted n    If game is deleted (1; prints 'D')\n");
  434.   fprintf(fd, "-eco n        ECO code for the game (6; not printed)\n");
  435.   fprintf(fd, "-ecomajor n   ECO major (3; not printed)\n");
  436.   fprintf(fd, "-eloblack n   Rating of Black (4; not printed)\n");
  437.   fprintf(fd, "-elowhite n   Rating of White (4; not printed)\n");
  438.   fprintf(fd, "-event n      Event in which game was played (not implemented)\n");
  439.   fprintf(fd, "-flags2 n     If game contains flag (1; prints '1' or '0')\n");
  440.   fprintf(fd, "-marked n     If game is marked (1; prints 'M')\n");
  441.   fprintf(fd, "-moves n      Number of moves in game (3 default)\n");
  442.   fprintf(fd, "-number n     Game number (6 default)\n");
  443.   fprintf(fd, "-place n      Place where game was played (not implemented)\n");
  444.   fprintf(fd, "-players n    Player names (25 default)\n");
  445.   fprintf(fd, "-position n   If game starts from a position (1; prints 'P')\n");
  446.   fprintf(fd, "-result n     Result of the game (4 default)\n");
  447.   fprintf(fd, "-round n      Round games was played (2; not printed)\n");
  448.   fprintf(fd, "-source n     Source of the game (25 default)\n");
  449.   fprintf(fd, "-variations n If game contains variations (1; prints 'V')\n");
  450.   fprintf(fd, "-white n      Name of White (15; not printed)\n");
  451.   fprintf(fd, "-year n       Year game was played (4 default)\n");
  452. }
  453.