partially working menu
[fceu.git] / drivers / common / args.c
CommitLineData
c62d2810 1/* FCE Ultra - NES/Famicom Emulator
2 *
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Ben Parnell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/****************************************************************/
22/* FCE Ultra */
23/* */
24/* This file contains code for parsing command-line */
25/* options. */
26/* */
27/****************************************************************/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include "../../types.h"
34#include "args.h"
35
b2b95d2e 36static int ParseEA(int x, int argc, char *argv[], ARGPSTRUCT *argsps)
c62d2810 37{
b2b95d2e 38 int y=0,ret=0;
c62d2810 39
40 do
41 {
42 if(!argsps[y].name)
43 {
b2b95d2e 44 ret = ParseEA(x,argc,argv,(void *)argsps[y].var);
c62d2810 45 y++;
46 continue;
47 }
48 if(!strcmp(argv[x],argsps[y].name)) // A match.
49 {
50 if(argsps[y].subs)
51 {
52 if((x+1)>=argc)
b2b95d2e 53 return 0;
c62d2810 54 if(argsps[y].substype&0x8000)
55 {
56 *(int *)argsps[y].subs&=~(argsps[y].substype&(~0x8000));
57 *(int *)argsps[y].subs|=atoi(argv[x+1])?(argsps[y].substype&(~0x8000)):0;
58 }
59 else
60 switch(argsps[y].substype&(~0x4000))
61 {
62 case 0: // Integer
63 *(int *)argsps[y].subs=atoi(argv[x+1]);
64 break;
65 case 1: // String
66 if(argsps[y].substype&0x4000)
67 {
68 if(*(char **)argsps[y].subs)
69 free(*(char **)argsps[y].subs);
70 if(!( *(char **)argsps[y].subs=malloc(strlen(argv[x+1])+1) ))
71 break;
b2b95d2e 72 }
c62d2810 73 strcpy(*(char **)argsps[y].subs,argv[x+1]);
74 break;
75 }
b2b95d2e 76 ret=2;
c62d2810 77 }
b2b95d2e 78 else if(argsps[y].var)
79 {
c62d2810 80 *argsps[y].var=1;
b2b95d2e 81 ret=1;
82 }
c62d2810 83 }
84 y++;
85 } while(argsps[y].var || argsps[y].subs);
b2b95d2e 86 return ret;
c62d2810 87}
88
b2b95d2e 89/* returns 1 if last arg was usccessfully parsed */
90int ParseArguments(int argc, char *argv[], ARGPSTRUCT *argsps)
c62d2810 91{
b2b95d2e 92 int x, ret=0;
c62d2810 93
94 for(x=0;x<argc;x++)
b2b95d2e 95 {
96 ret = ParseEA(x,argc,argv,argsps);
97 if (ret == 2) x++;
98 }
99
100 return ret;
c62d2810 101}
102