]> icculus.org git repositories - btb/d2x.git/blob - misc/args.c
actually use PHYSFSX_openRead/WriteBuffered
[btb/d2x.git] / misc / args.c
1 /* $Id: args.c,v 1.17 2005-02-25 05:20:36 chris Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Functions for accessing arguments.
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #ifdef RCS
26 static char rcsid[] = "$Id: args.c,v 1.17 2005-02-25 05:20:36 chris Exp $";
27 #endif
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "physfsx.h"
33 #include "args.h"
34 #include "u_mem.h"
35 #include "strio.h"
36 #include "strutil.h"
37
38 #define MAX_ARGS 100
39
40 int Num_args=0;
41 char * Args[MAX_ARGS];
42
43 int FindArg(char *s)
44 {
45         int i;
46
47         for (i=0; i<Num_args; i++ )
48                 if (! stricmp( Args[i], s))
49                         return i;
50
51         return 0;
52 }
53
54 int FindResArg(char *prefix, int *sw, int *sh)
55 {
56         int i;
57         int w, h;
58         char *endptr;
59         int prefixlen = strlen(prefix);
60
61         for (i = 0; i < Num_args; ++i)
62                 if (Args[i][0] == '-' && !strnicmp(Args[i] + 1, prefix, prefixlen))
63                 {
64                         w = strtol(Args[i] + 1 + prefixlen, &endptr, 10);
65                         if (w > 0 && endptr && endptr[0] == 'x')
66                         {
67                                 h = strtol(endptr + 1, &endptr, 10);
68                                 if (h > 0 && endptr[0] == '\0')
69                                 {
70                                         *sw = w;
71                                         *sh = h;
72                                         return i;
73                                 }
74                         }
75                 }
76
77         return 0;
78
79 }
80
81 void args_exit(void)
82 {
83         int i;
84         for (i=0; i< Num_args; i++ )
85                 d_free(Args[i]);
86 }
87
88 void InitArgs( int argc,char **argv )
89 {
90         int i;
91         
92         Num_args=0;
93         
94         for (i=0; i<argc; i++ )
95                 Args[Num_args++] = d_strdup( argv[i] );
96         
97         
98         for (i=0; i< Num_args; i++ ) {
99                 if ( Args[i][0] == '-' )
100                         strlwr( Args[i]  );  // Convert all args to lowercase
101         }
102         AppendArgs();
103
104         atexit(args_exit);
105 }
106
107 void AppendArgs(void)
108 {
109         PHYSFS_file *f;
110         char *line,*word;
111         int i;
112         
113         if((i=FindArg("-ini")))
114                 f = PHYSFSX_openReadBuffered(Args[i+1]);
115         else
116                 f = PHYSFSX_openReadBuffered("d2x.ini");
117         
118         if(f) {
119                 while(!PHYSFS_eof(f) && Num_args < MAX_ARGS)
120                 {
121                         line=fgets_unlimited(f);
122                         word=splitword(line,' ');
123                         
124                         Args[Num_args++] = d_strdup(word);
125                         
126                         if(line)
127                                 Args[Num_args++] = d_strdup(line);
128                         
129                         d_free(line); d_free(word);
130                 }
131                 PHYSFS_close(f);
132         }
133 }