tarot_status.c 4.84 KB
Newer Older
Vivien Kraus's avatar
Vivien Kraus committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * tarot_status.c
 *
 * Copyright (C) 2019 Vivien Kraus <vivien@planete-kraus.eu>
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <tarot.h>
#include "gettext.h"
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include "xalloc.h"
#include <assert.h>

#define _(String) gettext (String)
#define N_(String) String

static TarotGame *read_game ();

int
main (int argc, char *argv[])
{
  TarotGame *game = NULL;
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);
  if (tarot_init (LOCALEDIR) != 0)
    {
      fprintf (stderr, _ ("ERROR: could not initialize libtarot.\n"));
    }
  if (argc != 2)
    {
      fprintf (stderr, _ ("Usage:\n\
- tarot-status --help\n\
- tarot-status --version\n\
- tarot-status next < GAME\n\
- tarot-status at-end < GAME\n\
- tarot-status score < GAME\n\
Please run tarot-status --help.\n"));
      return EXIT_FAILURE;
    }
  if (strcmp (argv[1], _ ("--help")) == 0)
    {
      printf (_ ("Usage: tarot-status [OPTION]... ([COMMAND] < GAME)?\n\
Print some status information of GAME.  Options are:\n\
- --help: print this message and exit\n\
- --version: print program version and exit\n\
\n\
Commands:\n\
- next: print the number of the next player.  The number of a player\n\
    is the number of players that have made a bid before this player in\n\
    the bidding phase;\n\
- at-end: exit sucessfully if the game is finished, or exit with code\n\
    1 if the game is not finished;\n\
- score: for a finished game, print the score, in the order of player\n\
     numbers, one line for each player.\n"));
      return EXIT_SUCCESS;
    }
  if (strcmp (argv[1], _ ("--version")) == 0)
    {
      printf (_ ("%s (libtarot %s)\n\
\n\
Copyright © 2019 Vivien Kraus\nThis is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"),
	      "tarot-status",
	      tarot_libtarot_version ());
      return EXIT_SUCCESS;
    }
  game = read_game ();
  if (strcmp (argv[1], _ ("next")) == 0)
    {
      TarotPlayer next;
      if (tarot_game_get_next (game, &next) == TAROT_GAME_OK)
	{
	  printf ("%u\n", next);
	  return EXIT_SUCCESS;
	}
      else
	{
	  fprintf (stderr, _ ("Error: all players have played in this game.\n"));
	  return EXIT_FAILURE;
	}
    }
  if (strcmp (argv[1], _ ("at-end")) == 0)
    {
      size_t n_scores;
      if (tarot_game_get_scores (game, &n_scores, 0, 0, NULL) == TAROT_GAME_OK)
	{
	  return EXIT_SUCCESS;
	}
      return EXIT_FAILURE;
    }
  if (strcmp (argv[1], _ ("score")) == 0)
    {
      size_t n_scores;
      int i;
      int *scores;
      if (tarot_game_get_scores_alloc (game, &n_scores, &scores) != TAROT_GAME_OK)
	{
	  fprintf (stderr, _ ("Error: this game is not finished yet.\n"));
	  return EXIT_FAILURE;
	}
      for (i = 0; i < n_scores; i++)
	{
	  printf ("%d\n", scores[i]);
	}
      return EXIT_SUCCESS;
    }
  fprintf (stderr, _ ("Error: unknown command '%s'.\n"), argv[1]);
  return EXIT_FAILURE;
}

static char *read_stdin (void);

static TarotGame *
read_game (void)
{
  TarotGame *ret = tarot_game_alloc ();
  char *input = read_stdin ();
  if (tarot_game_load_from_xml (ret, input) == TAROT_GAME_OK)
    {
      return ret;
    }
  fprintf (stderr, _ ("Error: invalid game.\n"));
  abort ();
}

struct buffer;
typedef struct buffer buffer;
static buffer *buffer_alloc (void);
static void buffer_push_back (buffer *b, char c);
static char *buffer_data (buffer *b); /* NOT nul-terminated */

static char *
read_stdin (void)
{
  buffer *buffer = buffer_alloc ();
  int c;
  while ((c = getchar ()) != EOF)
    {
      unsigned int u = c;
      buffer_push_back (buffer, (unsigned char) u);
    }
  buffer_push_back (buffer, '\0');
  return buffer_data (buffer);
}

struct buffer
{
  size_t allocated;
  size_t used;
  char *data;
};

static buffer *
buffer_alloc (void)
{
  buffer *ret = xmalloc (sizeof (buffer));
  ret->allocated = 1;
  ret->used = 0;
  ret->data = xmalloc (1);
  return ret;
}

static void
buffer_push_back (buffer *b, char c)
{
  if (b->used >= b->allocated)
    {
      b->allocated *= 2;
      b->data = xrealloc (b->data, b->allocated);
    }
  assert (b->used < b->allocated);
  b->data[b->used++] = c;
}

static char *
buffer_data (buffer *b)
{
  return b->data;
}