]> icculus.org git repositories - btb/d2x.git/blob - misc/args.c
Enable global structs for mine saving functions
[btb/d2x.git] / misc / args.c
1 /* $Id: args.c,v 1.15 2004-12-01 12:48:13 btb 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.15 2004-12-01 12:48:13 btb Exp $";
27 #endif
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <physfs.h>
33
34 #include "args.h"
35 #include "u_mem.h"
36 #include "strio.h"
37 #include "strutil.h"
38
39 #define MAX_ARGS 100
40
41 int Num_args=0;
42 char * Args[MAX_ARGS];
43
44 int FindArg(char *s)
45 {
46         int i;
47
48         for (i=0; i<Num_args; i++ )
49                 if (! stricmp( Args[i], s))
50                         return i;
51
52         return 0;
53 }
54
55 int FindResArg(char *prefix, int *sw, int *sh)
56 {
57         int i;
58         int w, h;
59         char *endptr;
60         int prefixlen = strlen(prefix);
61
62         for (i = 0; i < Num_args; ++i)
63                 if (Args[i][0] == '-' && !strnicmp(Args[i] + 1, prefix, prefixlen))
64                 {
65                         w = strtol(Args[i] + 1 + prefixlen, &endptr, 10);
66                         if (w > 0 && endptr && endptr[0] == 'x')
67                         {
68                                 h = strtol(endptr + 1, &endptr, 10);
69                                 if (h > 0 && endptr[0] == '\0')
70                                 {
71                                         *sw = w;
72                                         *sh = h;
73                                         return i;
74                                 }
75                         }
76                 }
77
78         return 0;
79
80 }
81
82 void args_exit(void)
83 {
84         int i;
85         for (i=0; i< Num_args; i++ )
86                 d_free(Args[i]);
87 }
88
89 void InitArgs( int argc,char **argv )
90 {
91         int i;
92         
93         Num_args=0;
94         
95         for (i=0; i<argc; i++ )
96                 Args[Num_args++] = d_strdup( argv[i] );
97         
98         
99         for (i=0; i< Num_args; i++ ) {
100                 if ( Args[i][0] == '-' )
101                         strlwr( Args[i]  );  // Convert all args to lowercase
102         }
103         AppendArgs();
104
105         atexit(args_exit);
106 }
107
108 void AppendArgs(void)
109 {
110         PHYSFS_file *f;
111         char *line,*word;
112         int i;
113         
114         if((i=FindArg("-ini")))
115                 f = PHYSFS_openRead(Args[i+1]);
116         else
117                 f = PHYSFS_openRead("d2x.ini");
118         
119         if(f) {
120                 while(!PHYSFS_eof(f) && Num_args < MAX_ARGS)
121                 {
122                         line=fgets_unlimited(f);
123                         word=splitword(line,' ');
124                         
125                         Args[Num_args++] = d_strdup(word);
126                         
127                         if(line)
128                                 Args[Num_args++] = d_strdup(line);
129                         
130                         d_free(line); d_free(word);
131                 }
132                 PHYSFS_close(f);
133         }
134 }