summaryrefslogtreecommitdiff
path: root/support/dktools/dk4opt.h
blob: ef9dd297f8d9d73ea3d8fd978f710d48a7c5c6a2 (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4opt.ctr
*/

/*
Copyright (C) 2015-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef DK4OPT_H_INCLUDED
/** Avoid multiple inclusions. */
#define DK4OPT_H_INCLUDED 1


#line 10 "dk4opt.ctr"

/**	@file
	Process command line option in simple programs.
	You can specify a set of allowed options including the option
	arguments types. After processing command line arguments you
	can simply check which options were used and obtain option
	argument values.
*/


#ifndef DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif

#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif

#ifndef DK4ERROR_H_INCLUDED
#include "dk4error.h"
#endif

/**	Option specification for application module.
*/
typedef struct {
  dkChar	 c;		/**< Short option character. */
  const dkChar	*longopt;	/**< Long option string. */
  int		 argtype;	/**< Argument type. */
} dk4_option_specification_t;

/**	Option values can be different types.
*/
typedef	union	{
  dkChar			*t;	/**< String value. */
  double			 d;	/**< Double value. */
  dk4_im_t			 i;	/**< Integer value. */
  dk4_um_t			 u;	/**< Unsigned integer value. */
  size_t			 s;	/**< Size value. */
  int				 b;	/**< Boolean value. */
} dk4_option_value_t;

/**	Option with value.
*/
typedef struct {
  dk4_option_specification_t	spec;	/**< Option specification. */
  dk4_option_value_t 		val;	/**< Option value. */
  int				found;	/**< Flag: Option found in cmd line. */
} dk4_option_t;


/**	Argument types.
*/
enum {
			/**	No argument expected.
			*/
DK4_OPT_ARG_NONE	=	1,

			/**	Boolean value expected.
			*/
DK4_OPT_ARG_BOOL ,

			/**	Size specification expected.
			*/
DK4_OPT_ARG_SIZE ,

			/**	Signed integer value expected.
			*/
DK4_OPT_ARG_INT ,

			/**	Unsigned integer value expected.
			*/
DK4_OPT_ARG_UNSIGNED ,

			/**	Double value expected.
			*/
DK4_OPT_ARG_DOUBLE ,

			/**	String value expected.
			*/
DK4_OPT_ARG_STRING
};



#ifdef __cplusplus
extern "C" {
#endif

/**	Create option object.
	CRT on Windows: Optional.
	@param	spec	Specifications for the option.
	@param	erp	Error report, may be NULL.
	@return	Pointer to new option object on success, NULL on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if spec is NULL,
	- DK4_E_SYNTAX<br>
	  if neither short nor long option is defined in spec,
	- DK4_E_MEMORY<br>
	  if the memory allocation failed.
*/
dk4_option_t *
dk4opt_open(const dk4_option_specification_t *spec, dk4_er_t *erp);

/**	Retrieve boolean value (as int) from option.
	CRT on Windows: Optional.
	@param	dptr	Pointer to destination variable.
	@param	optptr	Option to retrieve value from.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dptr or optptr is NULL or optptr is configured for another data
	  type,
	- DK4_E_NOT_FOUND<br>
	  if the option was not specified on the command line,
	- DK4_E_SYNTAX<br>
	  if a string is stored not representing a boolean or the option was
	  configured with an incompatible type.
*/
int
dk4opt_get_bool(int *dptr, dk4_option_t *optptr, dk4_er_t *erp);

/**	Retrieve size_t value from option.
	CRT on Windows: Optional.
	@param	dptr	Pointer to destination variable.
	@param	optptr	Option to retrieve value from.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dptr or optptr is NULL or optptr is configured for another data
	  type,
	- DK4_E_NOT_FOUND<br>
	  if the option was not specified on the command line,
	- DK4_E_SYNTAX<br>
	  if the option was configured for an incompatible type or the option
	  argument string can not be converted to a size_t,
	- DK4_E_MATH_OVERFLOW<br>
	  if the option was configured for another numeric type and conversion
	  results in an overflow.
*/
int
dk4opt_get_size(size_t *dptr, dk4_option_t *optptr, dk4_er_t *erp);

/**	Retrieve integer value (dk4_im_t) from option.
	CRT on Windows: Optional.
	@param	dptr	Pointer to destination variable.
	@param	optptr	Option to retrieve value from.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dptr or optptr is NULL or optptr is configured for another data
	  type,
	- DK4_E_NOT_FOUND<br>
	  if the option was not specified on the command line,
	- DK4_E_SYNTAX<br>
	  if the option was configured for an incompatible type or the option
	  argument string does not represent an integer number,
	- DK4_E_MATH_OVERFLOW<br>
	  if the option was configured for another numeric type and conversion
	  results in an overflow.
*/
int
dk4opt_get_int(dk4_im_t *dptr, dk4_option_t *optptr, dk4_er_t *erp);

/**	Retrieve unsigned integer value (dk4_um_t) from option.
	CRT on Windows: Optional.
	@param	dptr	Pointer to destination variable.
	@param	optptr	Option to retrieve value from.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dptr or optptr is NULL or optptr is configured for another data
	  type,
	- DK4_E_NOT_FOUND<br>
	  if the option was not specified on the command line,
	- DK4_E_SYNTAX<br>
	  if the option was configured for an incompatible data type or the
	  option string does not represent an integer number,
	- DK4_E_MATH_OVERFLOW<br>
	  if the option was configured for an incompatible numeric type and a
	  negative value is stored.
*/
int
dk4opt_get_unsigned(dk4_um_t *dptr, dk4_option_t *optptr, dk4_er_t *erp);

/**	Retrieve double value from option.
	CRT on Windows: Optional.
	@param	dptr	Pointer to destination variable.
	@param	optptr	Option to retrieve value from.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dptr or optptr is NULL or optptr is configured for another data
	  type,
	- DK4_E_NOT_FOUND<br>
	  if the option was not specified on the command line,
	- DK4_E_SYNTAX<br>
	  if the option was configured for an incompatible data type or the
	  string stored as option argument does not represent a double number.
*/
int
dk4opt_get_double(double *dptr, dk4_option_t *optptr, dk4_er_t *erp);

/**	Retrieve string value from option.
	CRT on Windows: Optional, disabling CRT reduces functionality
	as we can not convert double values to strings.
	@param	dptr	Pointer to destination buffer.
	@param	szdptr	Size of destination buffer (number of dkChar).
	@param	optptr	Option to retrieve value from.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if dptr or optptr is NULL or optptr is configured for another data
	  type,
	- DK4_E_NOT_FOUND<br>
	  if the option was not specified on the command line.
	- DK4_E_SYNTAX<br>
	  if the option was configured for an incompatible data type,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if the destination buffer is too small to hold a conversion result,
	- DK4_E_NOT_SUPPORTED<br>
	  if the option was configured for the double data type and the use of
	  sprintf() was denied during setup.
*/
int
dk4opt_get_string(
  dkChar *dptr, size_t szdptr, dk4_option_t *optptr, dk4_er_t *erp
);

/**	Destroy option object, release memory.
	CRT on Windows: Optional.
	@param	optptr	Option object to destroy.
*/
void
dk4opt_close(dk4_option_t *optptr);

/**	Compare option against other option or char or string.
	CRT on Windows: Optional.
	@param	l	Left pointer, always an option.
	@param	r	Right pointer, type depends on cr.
	@param	cr	Comparison criteria:
			0=option/option by short character,
			1=option/character,
			2=option/option by long option text,
			3=option/text.
	@return	Comparison result.
*/
int
dk4option_compare(const void *l, const void *r, int cr);

/**	Process command line options provided to the main function.
	@param	optptr		Options array.
	@param	szoptptr	Number of elements in array.
	@param	argc		Number of command line arguments.
	@param	argv		Command line arguments array.
	@param	filenames	Pointer array to store file names.
	@param	nfilenames	Elements in array (in: size, out: used).
	@param	progname	Flag: Argument 0 is program name.
	@param	logstderr	Flag: Logging to stderr allowed.
	@param	erp		Error report, may be NULL.
	@return	1 on success, 0 on error.
*/
int
dk4opt_process_argv(
  dk4_option_t		 *optptr,
  size_t		  szoptptr,
  int			  argc,
  dkChar		 *argv[],
  dkChar		**filenames,
  size_t		 *nfilenames,
  int			  progname,
  int			  logstderr,
  dk4_er_t		 *erp
);

#ifdef __cplusplus
}
#endif


#endif