nSnake
A ncurses implementation of the classic Snake game
 All Data Structures Files Functions Variables Enumerations Macros Pages
arguments.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2  * nSnake - The classic snake game with ncurses. *
3  * Copyright (C) 2011-2012 Alexandre Dantas (kure) *
4  * *
5  * This file is part of nSnake. *
6  * *
7  * nSnake is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  * homepage: http://sourceforge.net/projects/nsnake/ *
21  * *
22 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
23 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <getopt.h> /* For getopt_long() */
34 
35 #include "arguments.h"
36 #include "hscores.h"
37 
38 
50 void args_handle (int argc, char* argv[])
51 {
52  static struct option options[] =
53  {
54  {"help", no_argument, NULL, 'h'},
55  {"license", no_argument, NULL, 'l'},
56  {"version", no_argument, NULL, 'v'},
57  {"reset-scores", no_argument, NULL, 'r'},
58  /* The last element must be all zeroes */
59  {0, 0, 0, 0}
60  };
61  /* The index of the current option */
62  int option_index;
63  /* The character for comparison */
64  int c = 0;
65 
66 
67  /* We keep checking the arguments untill they run out (c == -1) */
68  while (c != -1)
69  {
70  c = getopt_long (argc, argv, "hlvr", options, &option_index);
71 
72  switch (c)
73  {
74  case 'h':
75  print_help ();
76  exit (EXIT_SUCCESS);
77  break;
78 
79  case 'l':
80  print_license ();
81  exit (EXIT_SUCCESS);
82  break;
83 
84  case 'v':
85  print_version ();
86  exit (EXIT_SUCCESS);
87  break;
88 
89  case 'r':
90  // TODO TODO TODO TODO TODO TODO TODO TODO RESET SCORES
91  hscore_clean ();
92  printf ("* HighScores reseted\n");
93  exit (EXIT_SUCCESS);
94  break;
95 
96  case '?':
97  /* getopt_long() already printed an error message about
98  * unrecognized option */
99  print_usage ();
100  exit (EXIT_FAILURE);
101  break;
102 
103  case -1:
104  // There were no '-' parameters passed
105  // or all the parameters were processed
106  break;
107 
108  default:
109  print_usage ();
110  exit (EXIT_FAILURE);
111  break;
112  }
113  }
114 
115  /* Just in case the user specified more arguments (not options)
116  * than needed, you decide what to do. Here, we just ignore them */
117  while (optind < argc)
118  optind++;
119 }
120 
121 
124 void print_help ()
125 {
126  printf("nSnake Help\n");
127  printf("\n");
128  printf("Synopsis:\n");
129  printf("\tThe classic snake game. You control a snake pursuing\n");
130  printf("\tASCII-like fruits.\n");
131  printf("Controls:\n");
132  printf("\tNumbers (1~9) Changes the game speed at the main menu\n");
133  printf("\tArrow Keys, WASD Control the snake directions\n");
134  printf("\tq Quits the game at any time\n");
135  printf("\tp Pauses/Unpauses the game\n");
136  printf("Usage:\n");
137  printf("\tnsnake [-h] [-l] [-v] [-r]\n");
138  printf("Commandline arguments:\n\n");
139  printf("\t-h, --help Displays the help guidelines.\n");
140  printf("\t-l, --license Displays this program's license and warranty.\n");
141  printf("\t-v, --version Displays the version and general information.\n");
142  printf("\t-r, --reset-scores Resets all the Highscores to default.\n");
143  printf("Type 'man nsnake' for more information.\n");
144  printf("\n");
145 }
146 
147 
151 {
152  printf("nSnake - The classic snake game with ncurses.\n");
153  printf("Copyright (C) 2011-2012 Alexandre Dantas (kure)\n");
154  printf("\n");
155  printf("nSnake is free software: you can redistribute it and/or modify\n");
156  printf("it under the terms of the GNU General Public License as published by\n");
157  printf("the Free Software Foundation, either version 3 of the License, or\n");
158  printf("any later version.\n");
159  printf("\n");
160  printf("This program is distributed in the hope that it will be useful,\n");
161  printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
162  printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
163  printf("GNU General Public License for more details.\n");
164  printf("\n");
165  printf("You should have received a copy of the GNU General Public License\n");
166  printf("along with this program. If not, see <http://www.gnu.org/licenses/>.\n");
167  printf("\n");
168 }
169 
170 
173 void print_usage ()
174 {
175  printf("nSnake v"VERSION" Usage:\n");
176  printf("\tnsnake [-h] [-l] [-v] [-r]\n");
177  printf("Commandline arguments:\n\n");
178  printf("\t-h, --help Displays the help guidelines.\n");
179  printf("\t-l, --license Displays this program's license and warranty.\n");
180  printf("\t-v, --version Displays the version and general information.\n");
181  printf("\t-r, --reset-scores Resets all the Highscores to default.\n");
182  printf("\n");
183 }
184 
185 
189 {
190  printf("nSnake v"VERSION"\t("DATE")\n");
191  printf("Copyright (C) 2011-2012 Alexandre Dantas (kure)\n");
192  printf("\n");
193  printf("This program comes with ABSOLUTELY NO WARRANTY\n");
194  printf("for warranty details type 'nsnake --license'.\n");
195  printf("This is free software, and you are welcome to redistribute it\n");
196  printf("under certain conditions; type 'nsnake --license' for license details.\n");
197  printf("\n");
198  printf("Mailto: alex.dantas92@gmail.com\n");
199  printf("Homepage: http://sourceforge.net/projects/nsnake\n");
200  printf("\n");
201 }