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

Generated by: LCOV version 1.13