-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathargs.h
50 lines (44 loc) · 1.16 KB
/
args.h
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
/*
* Functions to assist with argument handling
*
* Copyright (c) DoD HPCMP PETTT. All rights reserved.
* See LICENSE file for details.
*/
#include <stdlib.h>
#include <string.h>
/* Convert a string with/out trailing 'x' to an integer and bool (with/out the x)
* s: pointer to input string
* i: pointer to an output integer
* hasx: pointer to output boolean: true of string ends in x
*/
void atoix(const char *s, int *i, int *hasx)
{
char *ss = strdup(s);
int len = strlen(ss);
if(ss[len-1] == 'x' || ss[len-1] == 'X') {
*hasx = 1;
ss[len-1] = 0;
len--;
} else
*hasx = 0;
*i = atoi(ss);
free(ss);
}
/* Convert a string with/out trailing 'x' to a double and bool (with/out the x)
* s: pointer to input string
* d: pointer to an output integer
* hasx: pointer to output boolean: true of string ends in x
*/
void atodx(const char *s, double *d, int *hasx)
{
char *ss = strdup(s);
int len = strlen(ss);
if(ss[len-1] == 'x' || ss[len-1] == 'X') {
*hasx = 1;
ss[len-1] = 0;
len--;
} else
*hasx = 0;
*d = strtod(ss, NULL);
free(ss);
}