Menu

[09bd5b]: / src / optarg.c  Maximize  Restore  History

Download this file

423 lines (363 with data), 12.3 kB

  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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/***************************************************************************
* Copyright (C) 2008-2010 by Matthias Ihrke *
* mihrke@uni-goettingen.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "optarg.h"
#include "helper.h"
#include <string.h>
#include <math.h>
#include <stdlib.h>
/** \brief print an optarglist.
*/
void optarglist_print( OptArgList *list, FILE *out ){
int i;
fprintf( out, "OptArgList '%p' with %i items:\n", list, list->nargs );
for( i=0; i<list->nargs; i++ ){
fprintf( out, " %i: %s(%s) is %s: ", i+1, list->args[i].key, list->args[i].type,
list->args[i].scalar?"scalar":"pointer");
if( list->args[i].scalar ){
fprintf( out, "%f\n", list->args[i].data_scalar );
} else {
fprintf( out, "%p\n", list->args[i].data_ptr );
}
}
}
/**\cond PRIVATE
*/
OptArgList* optarglist_hidden( char *format, va_list ap ){
char *key, *type;
char *tmp;
char *fmt;
int nargs, i;
OptArgList *L;
nargs=0;
tmp=format;
/* count '=' */
while( (tmp=strchr( tmp, '=' ))!=NULL ){
tmp++;
nargs++;
}
if( sizeof(char*)!=sizeof(void*) ||
sizeof(double*)!=sizeof(void*) ){
errprintf("System Error! For this function to work, all pointers must be of the same size!\n");
return NULL;
}
L = (OptArgList*)malloc( sizeof( OptArgList ) );
L->nargs=nargs;
L->args=(OptArg*)malloc( nargs*sizeof( OptArg ) );
dprintf("Found %i args\n", nargs );
/* parse key=type information from format */
fmt = (char*)malloc( (strlen( format )+1)*sizeof(char) );
strcpy( fmt, format );
key=fmt;
type=fmt;
for( i=0; i<nargs; i++ ){
if( (tmp=strchr( key, '=' )) ){
*tmp='\0';
}
dprintf("key=%s\n", key);
strcpy( L->args[i].key, key );
string_strip_blanks( L->args[i].key );
type=tmp+1;
if( (tmp=strchr( type, ',' ) ) ){
*tmp='\0';
key = tmp+1;
}
dprintf("type=%s\n", type );
strcpy( L->args[i].type, type );
string_strip_blanks( L->args[i].type );
}
dprintf("Parsing done\n");
for (i = 0; i < nargs; i++){
if( !strchr( L->args[i].type, '*' ) ){ /* it's scalar */
L->args[i].scalar=TRUE;
if( !strcmp( L->args[i].type, "char" ) ){
L->args[i].data_scalar = (double) va_arg (ap, int );
} else if( !strcmp( L->args[i].type, "short" ) ){
L->args[i].data_scalar = (double) va_arg (ap, int );
} else if( !strcmp( L->args[i].type, "int" ) ){
L->args[i].data_scalar = (double)va_arg (ap, int );
} else if( !strcmp( L->args[i].type, "long" ) ){
L->args[i].data_scalar = (double) va_arg (ap, long );
} else if( !strcmp( L->args[i].type, "float" ) ){
L->args[i].data_scalar = (double) va_arg (ap, double );
} else if( !strcmp( L->args[i].type, "double" ) ){
L->args[i].data_scalar = (double) va_arg (ap, double );
} else {
errprintf("Sorry, do not know scalar data type '%s'\n"
"This function is probably going to break because of this.\n"
"Please correct the error and restart.\n",
L->args[i].type );
break;
}
} else {
dprintf("There is a pointer in the %i'th argument\n", i+1 );
L->args[i].scalar=FALSE;
L->args[i].data_ptr = (void*)va_arg(ap, void *);
dprintf("got a pointer: %p\n", L->args[i].data_ptr );
}
}
free( fmt );
return L;
}
/**\endcond */
/**\brief Creates an OptArgList including a optarglist_free arg.
The Format string has the following convention:
<pre>
key1=double*,key2=void*,key3=int,key=double,...
</pre>
If you have at least one asterisk (*) in the specification, then
the corresponding argument needs to be a pointer.
Internally, all pointer types are \c void* and stored along
with the given specification.
Be VERY careful to typecast all variables to match the type you indicated.
For example, if you have a float-variable and pass it as double, you need to
typecast to (double).
Only "basic" scalar values (without asterisk) are
supported:
<ul>
<li> char
<li> short
<li> int
<li> long
<li> float
<li> double
</ul>
Blanks/Newlines etc are stripped from key and type, so
<pre>
key = double , key2=void*
</pre>
are ok.
*/
OptArgList* optarglisttmp( char *format, ... ){
va_list ap;
va_start (ap, format ); /* Initialize the argument list. */
OptArgList *o=optarglist_hidden( format, ap );
va_end(ap);
OptArg f;
sprintf(f.key, "optarglist_free");
f.scalar=TRUE;
sprintf(f.type, "int");
f.data_ptr=NULL;
f.data_scalar=1.0;
OptArgList *no=optarglist_append_arg( o, &f );
optarglist_free( o );
return no;
}
/** \brief Creates an OptArgList out of one or many arguments.
The Format string has the following convention:
<pre>
key1=double*,key2=void*,key3=int,key=double,...
</pre>
If you have at least one asterisk (*) in the specification, then
the corresponding argument needs to be a pointer.
Internally, all pointer types are \c void* and stored along
with the given specification.
Be VERY careful to typecast all variables to match the type you indicated.
For example, if you have a float-variable and pass it as double, you need to
typecast to (double).
Only "basic" scalar values (without asterisk) are
supported:
<ul>
<li> char
<li> short
<li> int
<li> long
<li> float
<li> double
</ul>
Blanks/Newlines etc are stripped from key and type, so
<pre>
key = double , key2=void*
</pre>
are ok.
*/
OptArgList* optarglist( char *format, ... ){
va_list ap;
va_start (ap, format ); /* Initialize the argument list. */
OptArgList *o=optarglist_hidden( format, ap );
va_end(ap);
return o;
}
/** \brief free an optarglist.
does not free any data_ptr.
*/
void optarglist_free( OptArgList *list ){
free( list->args );
free( list );
}
/** \brief Return the value of the scalar argument.
or NaN if not available.
Check returned value with isnan()!
*/
double optarglist_scalar_by_key( OptArgList *list, const char *key ){
OptArg *arg;
arg = optarglist_optarg_by_key( list, key );
if( !arg ){
errprintf("There is no key: '%s'\n", key );
return NAN;
}
if( !arg->scalar ){
errprintf("You requested a scalar from a pointer argument\nYou get NaN instead :-)\n");
return NAN;
}
dprintf("arg->data_scalar=%f\n", arg->data_scalar );
return arg->data_scalar;
}
/** \brief Return a pointer to the data_ptr field of the OptArg struct.
with the key from the
list. NULL if no such key has been found.
*/
void* optarglist_ptr_by_key ( OptArgList *list, const char *key ){
OptArg *arg;
arg = optarglist_optarg_by_key( list, key );
if( !arg ) return NULL;
if( arg->scalar ){
errprintf("You requested a data-pointer from a scalar argument\nYou get NULL instead :-)\n");
return NULL;
}
return arg->data_ptr;
}
/** \brief Return a pointer to an OptArg struct with the key from the
list.
NULL if no such key has been found.
*/
OptArg* optarglist_optarg_by_key( OptArgList *list, const char *key ){
int i;
if( !list ) return NULL;
for( i=0; i<list->nargs; i++ ){
if( !strcmp( list->args[i].key, key ) )
return &(list->args[i]);
}
return NULL;
}
/** \brief return TRUE if key is found in list, else FALSE.
*/
bool optarglist_has_key( OptArgList *list, const char *key ){
int i;
if( !list ) return FALSE;
for( i=0; i<list->nargs; i++ ){
if( !strcmp( list->args[i].key, key ) )
return TRUE;
}
return FALSE;
}
/** \brief create a new optarglist that is the former optarglist with the new arg appended.
The caller is responsible for freeing the old list.
\param list the pointer to the Optarglist
\return the new list (the caller is responsible for freeing the old one)
*/
OptArgList* optarglist_append_arg ( OptArgList *list, OptArg *arg ){
OptArgList *new;
new = (OptArgList*)malloc( sizeof( OptArgList ) );
new->nargs=list->nargs+1;
new->args=(OptArg*)malloc( new->nargs*sizeof( OptArg ) );
memcpy( new->args, list->args, list->nargs * sizeof( OptArg ) );
memcpy( &(new->args[new->nargs-1]), arg, sizeof( OptArg ) );
return new;
}
/**\brief delete an argument from the argument list.
The function returns a new OptArgList containing
which is the old list minus the argument. The arguments
copied!
The caller is responsible for freeing the old list.
\param list the list
\param arg the argument to delete
\return the new list (the caller is responsible for freeing the old one)
*/
OptArgList* optarglist_delete_arg ( OptArgList *list, OptArg *arg ){
int idx=-1;
int i,j;
for( i=0; i<list->nargs; i++ ){
if( &(list->args[i])==arg ){
idx=i;
}
}
if( idx<0 ){
warnprintf("list did not contain the optional argument\n");
return;
}
OptArgList *L = (OptArgList*)malloc( sizeof( OptArgList ) );
L->nargs=list->nargs-1;
L->args=(OptArg*)malloc( L->nargs*sizeof( OptArg ) );
for( i=0,j=0; i<list->nargs; i++ ){
if( i==idx ) continue;
memcpy( &(L->args[i]), &(list->args[i]), sizeof(OptArg) );
j++;
}
return L;
}
/** \brief remove the optarglist_free-flag from the list.
This function is only interesting for you, if you want to write an
own function that takes an OptArgList* as an argument.
The function removes the optarglist_free-flag from the list
and returns a new list without the flag. The old list is free'd
in case that the optarglist_free argument is found.
The removedflag-flag is set to FALSE if it is the same list,
to TRUE if it is a new (truncated) list.
Usage is as follows:
\code
void testfct( OptArgList *opts ){
// overwrite the pointer
bool freeflag_removed=FALSE;
opts=optarglist_remove_freeflag( opts, &freeflag_removed );
// do some stuff including passing the argument to some other function
call_other_function( opts );
// clean up in case the flag was indeed removed
if( freeflag_removed )
optarglist_free( opts );
\endcode
\param list the list
\param removedflag is set to FALSE if it is the same list,
to TRUE if it is a new (truncated) list.
\return a pointer to the new (or the old) list
*/
OptArgList* optarglist_remove_freeflag( OptArgList *list, bool *removedflag ){
OptArg *freearg;
OptArgList *nlist=list;
*removedflag=FALSE;
if( optarglist_has_key( list, "optarglist_free" ) ){
freearg=optarglist_optarg_by_key( list, "optarglist_free" );
nlist=optarglist_delete_arg( list, freearg );
optarglist_free( list );
*removedflag=TRUE;
}
return nlist;
}
/** \brief create a new OptArg* containing a scalar value.
*/
OptArg* optarg_scalar( const char *key, double scalar ){
OptArg *p = (OptArg*)malloc(sizeof(OptArg) );
strncpy( p->key, key, MAX_LABEL_LENGTH );
p->scalar = TRUE;
p->data_scalar = scalar;
sprintf( p->type, "double" );
p->data_ptr = NULL;
return p;
}
/** \brief create a new OptArg* containing a pointer.
*/
OptArg* optarg_ptr ( const char *key, void *ptr ){
OptArg *p = (OptArg*)malloc(sizeof(OptArg) );
strncpy( p->key, key, MAX_LABEL_LENGTH );
p->scalar = FALSE;
p->data_ptr = ptr;
sprintf( p->type, "void*" );
p->data_scalar = 0.0/0.0;
return p;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.