LCOV - code coverage report
Current view: top level - open-adventure - cheat.c (source / functions) Hit Total Coverage
Test: advent.info Lines: 48 48 100.0 %
Date: 2018-03-12 03:52:32 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * 'cheat' is a tool for generating save game files to test states that ought
       3             :  * not happen. It leverages chunks of advent, mostly initialize() and
       4             :  * savefile(), so we know we're always outputing save files that advent
       5             :  * can import.
       6             :  *
       7             :  * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
       8             :  * Copyright (c) 2017 by Eric S. Raymond
       9             :  * SPDX-License-Identifier: BSD-2-clause
      10             :  */
      11             : #include <getopt.h>
      12             : #include <stdlib.h>
      13             : #include <stdio.h>
      14             : #include <stdbool.h>
      15             : #include "advent.h"
      16             : 
      17          10 : int main(int argc, char *argv[])
      18             : {
      19             :     int ch;
      20          10 :     char *savefilename = NULL;
      21          10 :     int version = 0;
      22          10 :     FILE *fp = NULL;
      23             : 
      24             :     // Initialize game variables
      25          10 :     initialise();
      26             : 
      27             :     /* we're generating a saved game, so saved once by default,
      28             :      * unless overridden with command-line options below.
      29             :      */
      30          10 :     game.saved = 1;
      31             : 
      32             :     /*  Options. */
      33          10 :     const char* opts = "d:l:s:t:v:o:";
      34          10 :     const char* usage = "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename \n"
      35             :                         "        -d number of deaths. Signed integer.\n"
      36             :                         "        -l lifetime of lamp in turns. Signed integer.\n"
      37             :                         "        -s number of saves. Signed integer.\n"
      38             :                         "        -t number of turns. Signed integer.\n"
      39             :                         "        -v version number of save format.\n"
      40             :                         "        -o required. File name of save game to write.\n";
      41             : 
      42          34 :     while ((ch = getopt(argc, argv, opts)) != EOF) {
      43          15 :         switch (ch) {
      44           3 :         case 'd':
      45           3 :             game.numdie = (turn_t)atoi(optarg);
      46           3 :             printf("cheat: game.numdie = %ld\n", game.numdie);
      47           3 :             break;
      48           1 :         case 'l':
      49           1 :             game.limit = (turn_t)atoi(optarg);
      50           1 :             printf("cheat: game.limit = %ld\n", game.limit);
      51           1 :             break;
      52           1 :         case 's':
      53           1 :             game.saved = (long)atoi(optarg);
      54           1 :             printf("cheat: game.saved = %ld\n", game.saved);
      55           1 :             break;
      56           1 :         case 't':
      57           1 :             game.turns = (turn_t)atoi(optarg);
      58           1 :             printf("cheat: game.turns = %ld\n", game.turns);
      59           1 :             break;
      60           1 :         case 'v':
      61           1 :             version = atoi(optarg);
      62           1 :             printf("cheat: version = %d\n", version);
      63           1 :             break;
      64           7 :         case 'o':
      65           7 :             savefilename = optarg;
      66           7 :             break;
      67           1 :         default:
      68           1 :             fprintf(stderr,
      69             :                     usage, argv[0]);
      70           1 :             exit(EXIT_FAILURE);
      71             :             break;
      72             :         }
      73             :     }
      74             : 
      75             :     // Save filename required; the point of cheat is to generate save file
      76           9 :     if (savefilename == NULL) {
      77           2 :         fprintf(stderr,
      78             :                 usage, argv[0]);
      79           2 :         fprintf(stderr,
      80             :                 "ERROR: filename required\n");
      81           2 :         exit(EXIT_FAILURE);
      82             :     }
      83             : 
      84           7 :     fp = fopen(savefilename, WRITE_MODE);
      85           7 :     if (fp == NULL) {
      86           1 :         fprintf(stderr,
      87             :                 "Can't open file %s. Exiting.\n", savefilename);
      88           1 :         exit(EXIT_FAILURE);
      89             :     }
      90             : 
      91           6 :     savefile(fp, version);
      92             : 
      93           6 :     fclose(fp);
      94             : 
      95           6 :     printf("cheat: %s created.\n", savefilename);
      96             : 
      97           6 :     return EXIT_SUCCESS;
      98             : }

Generated by: LCOV version 1.13