1 /*
2 
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public License
5 as published by the Free Software Foundation; either version 2
6 of the License, or (at your option) any later version.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 
12 See the GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 
18 	$Id$
19 */
20 
21 #include "defs.h"
22 
23 #define MAX_TOKEN	1024
24 
25 static char	token[MAX_TOKEN];
26 
27 /*
28 ==============
29 Parse
30 
31 Parse a token out of a string
32 ==============
33 */
Parse(char * data,qbool newline)34 char *Parse (char *data, qbool newline)
35 {
36 	unsigned char c;
37 	int		len;
38 
39 	len = 0;
40 	token[0] = 0;
41 
42 	if (!data)
43 		return NULL;
44 
45 	// skip whitespace
46 	while (true)
47 	{
48 		while ( (c = *data) == ' ' || c == '\t' || (newline && (c == '\r' || c == '\n')))
49 			data++;
50 
51 		if (c == 0)
52 			return NULL;			// end of file;
53 
54 		// skip # comments
55 		if (c=='#')
56 		{
57 			while (*data && *data != '\n')
58 				data++;
59 		}
60 		else
61 			break;
62 	}
63 
64 	// handle quoted strings specially
65 	if (c == '\"')
66 	{
67 		data++;
68 		while (len < MAX_TOKEN-1)
69 		{
70 			c = *data++;
71 			if (c=='\"' || !c)
72 			{
73 				token[len] = 0;
74 				if (!c)
75 					data--;
76 				return data;
77 			}
78 			token[len] = c;
79 			len++;
80 		}
81 	}
82 
83 	// parse a regular word
84 	do
85 	{
86 		token[len] = c;
87 		data++;
88 		len++;
89 		if (len >= MAX_TOKEN-1)
90 			break;
91 		c = *data;
92 	}
93 	while (c && c != ' ' && c != '\t' && c != '\n' && c != '\r' && c != '#');
94 
95 	token[len] = 0;
96 	return data;
97 }
98 
99 extern param_t params[];
100 
101 enum {D_COMMON, D_CONVERT, D_MARGE};
ReadIni(char * buf)102 void ReadIni(char *buf)
103 {
104 	param_t	*param;
105 	int dest = D_COMMON;
106 	int job = D_COMMON;
107 
108 	if (!(CheckParm("-m") || CheckParm("-marge")))
109 	{
110 		if (!(CheckParm("-debug") || CheckParm("-log"))
111 		        || CheckParm("-c") || CheckParm("-convert"))
112 			job = D_CONVERT;
113 		else
114 			job = D_COMMON;
115 	}
116 	else job = D_MARGE;
117 
118 	while (buf)
119 	{
120 		buf = Parse(buf, true);
121 
122 		if (!token[0])
123 			return;
124 
125 		if (token[0] == '[')
126 		{
127 			if (!strcasecmp(token, "[marge]"))
128 				dest = D_MARGE;
129 			else if (!strcasecmp(token, "[convert]"))
130 				dest = D_CONVERT;
131 			else if (!strcasecmp(token, "[common]"))
132 				dest = D_COMMON;
133 			goto finish;
134 		}
135 
136 		if (dest != D_COMMON && dest != job)
137 			goto finish;
138 
139 		// settings
140 		for (param = params; param->name; param++)
141 		{
142 			if (!strcasecmp(token, param->name+1) || (param->shname && !strcasecmp(param->shname+1, token)))
143 			{
144 				if (!CheckParm(param->name) && (!param->shname || !CheckParm(param->shname)))
145 				{
146 					// if --option is set in cmdline ignore this option
147 					if (CheckParm(va("-%s", param->name)) || (param->shname && CheckParm(va("-%s", param->shname))))
148 						goto finish;
149 
150 					AddParm(va("-%s",token));
151 					if (param->type & (TYPE_S | TYPE_I))
152 					{
153 						buf = Parse(buf, false);
154 						AddParm(token);
155 					}
156 				}
157 
158 				goto finish;
159 			}
160 		}
161 
162 		// parse argv will scream if it doesn't know it, we don't do it here.
163 		AddParm(va("-%s", token));
164 finish:
165 		// skip line
166 		while (*buf && *buf != '\n')
167 			buf++;
168 	}
169 }
170