File: cdk_params.c

package info (click to toggle)
libcdk5 5.0.20050424-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,504 kB
  • ctags: 2,620
  • sloc: ansic: 29,645; sh: 4,283; makefile: 634; cpp: 42; sed: 40
file content (247 lines) | stat: -rw-r--r-- 4,470 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <cdk_int.h>

/*
 * Useful functions for command-line parsing.
 *
 * $Author: tom $
 * $Date: 2003/11/29 16:03:15 $
 * $Revision: 1.5 $
 */

#define OPTION_ON	((char *)1)
#define OPTION_OFF	((char *)0)

static void usage (char **argv,
		   CDK_PARAMS * params,
		   char *options)
{
   int n;
   char *str;

   fprintf (stderr, "Usage: %s [options]\n\nOptions:\n", baseName (argv[0]));

   for (n = 1; n < MAX_CDK_PARAMS; ++n)
   {
      if (n != ':'
	  && (str = strchr (options, n)) != 0)
      {
	 int value = (str[1] == ':');
	 fprintf (stderr, "  -%c", n);
	 if (value)
	 {
	    fprintf (stderr, " (%s)\n",
		     (params->allParams[n]
		      ? params->allParams[n]
		      : "not set"));
	 }
	 else
	 {
	    fprintf (stderr, " (%s)\n",
		     (params->allParams[n]
		      ? "set"
		      : "not set"));
	 }
      }
   }
   exit (EXIT_FAILURE);
}

static int CDKparseSize (char *string, int fullSize)
{
   int result;
   if (strcmp (string, "FULL") == 0)
   {
      result = fullSize;
   }
   else
   {
      result = (int)atoi (string);
   }
   return result;
}

/*
 * Parse the string as one of CDK's positioning keywords, or an actual
 * position.
 */
int CDKparsePosition (char *string)
{
   int result;

   if (string == 0)
   {
      result = NONE;
   }
   else if (strcmp (string, "TOP") == 0)
   {
      result = TOP;
   }
   else if (strcmp (string, "BOTTOM") == 0)
   {
      result = BOTTOM;
   }
   else if (strcmp (string, "LEFT") == 0)
   {
      result = LEFT;
   }
   else if (strcmp (string, "RIGHT") == 0)
   {
      result = RIGHT;
   }
   else if (strcmp (string, "CENTER") == 0)
   {
      result = CENTER;
   }
   else
   {
      result = (int)atoi (string);
   }
   return result;
}

/*
 * Parse the given argc/argv command-line, with the options passed to
 * getopt()'s 3rd parameter.
 */
void CDKparseParams (int argc,
		     char **argv,
		     CDK_PARAMS * params,
		     char *options)
{
   int code;
   char *str;

   memset (params, 0, sizeof (*params));
   params->Box = TRUE;

   while ((code = getopt (argc, argv, options)) != EOF)
   {
      if (code == '?' || (str = strchr (options, code)) == 0)
      {
	 usage (argv, params, options);
      }
      else
      {
	 params->allParams[code] = OPTION_ON;
	 if (str[1] == ':')
	 {
	    params->allParams[code] = optarg;
	 }
	 switch (code)
	 {
	 case 'W':
	    params->wValue = CDKparseSize (optarg, FULL);
	    break;

	 case 'H':
	    params->hValue = CDKparseSize (optarg, FULL);
	    break;

	 case 'X':
	    params->xValue = CDKparsePosition (optarg);
	    break;

	 case 'Y':
	    params->yValue = CDKparsePosition (optarg);
	    break;

	 case 'N':
	    params->Box = FALSE;
	    break;

	 case 'S':
	    params->Shadow = TRUE;
	    break;
	 }
      }
   }
}

/*
 * Retrieve a numeric option-value, default=0.
 */
int CDKparamNumber (CDK_PARAMS * params, int option)
{
   return CDKparamNumber2 (params, option, 0);
}

/*
 * Retrieve a numeric option-value, given default.
 */
int CDKparamNumber2 (CDK_PARAMS * params, int option, int missing)
{
   return CDKparamValue (params, option, missing);
}

/*
 * Retrieve the string value of an option, default=0.
 */
char *CDKparamString (CDK_PARAMS * params, int option)
{
   return CDKparamString2 (params, option, 0);
}

/*
 * Retrieve the string value of an option, with default for missing value.
 */
char *CDKparamString2 (CDK_PARAMS * params, int option, char *missing)
{
   char *value = ((option > 0 && option < MAX_CDK_PARAMS)
		  ? params->allParams[option]
		  : 0);
   if (value == 0)
      value = missing;
   return value;
}

/*
 * Retrieve an integer (or boolean) option value from the parsed command-line.
 * (prefer: CDKparamNumber).
 */
int CDKparamValue (CDK_PARAMS * params, int option, int missing)
{
   int result;
   char *value = CDKparamString (params, option);

   if (value == 0)
   {
      result = missing;
   }
   else if (strchr (CDK_CLI_PARAMS, option) != 0)
   {
      switch (option)
      {
      case 'H':
	 result = params->hValue;
	 break;
      case 'W':
	 result = params->wValue;
	 break;
      case 'X':
	 result = params->xValue;
	 break;
      case 'Y':
	 result = params->yValue;
	 break;
      case 'N':
	 result = params->Box;
	 break;
      case 'S':
	 result = params->Shadow;
	 break;
      default:
	 result = missing;
	 break;
      }
   }
   else if (value == OPTION_ON)
   {
      result = 1;
   }
   else
   {
      result = atoi (value);
   }

   return result;
}