-
Notifications
You must be signed in to change notification settings - Fork 0
/
converts.c
34 lines (32 loc) · 920 Bytes
/
converts.c
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
#include <stdint.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "converts.h" /* Include converts.h library */
/* Check for return value of errno and convert str to decimal */
int64_t
any_to_dec(char *str, c_rgb_t type) {
int64_t col_num = -1;
char *endptr = NULL;
switch (type) {
case DECIMAL:
col_num = strtol(str, &endptr, 10);
break;
case HEXADECIMAL:
col_num = strtol(str, &endptr, 16);
break;
case OCTAL:
col_num = strtol(str, &endptr, 8);
break;
case BINARY:
col_num = strtol(str, &endptr, 2);
break;
default:
break;
}
if (errno == EINVAL || str == endptr) {
fprintf(stderr, "Error occured when converting %s to the type %d\n", str, type);
return -1;
}
return col_num;
}