-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.c
More file actions
144 lines (123 loc) · 2.52 KB
/
utilities.c
File metadata and controls
144 lines (123 loc) · 2.52 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
#include "utilities.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* EStatus_ToString[] =
{
"Success",
"General error",
"Calculation error",
"Overflow",
"Divide by zero",
"IO error",
"Bad input error: the input should be an integer number",
"Bad operation input errror: the operation inputted should be from numbers 1-11",
"Bad base input errror: the base inputted should be from numbers 1-4",
"Null reference error",
"Operation not valid error"
};
int ischarinbase(char c, enum EBase base)
{
switch(base)
{
case EBase_Binary:
{
if(c == '0' || c == '1')
{
return 1;
}
} break;
case EBase_Octal:
{
if(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7')
{
return 1;
}
} break;
case EBase_Decimal:
{
if(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9')
{
return 1;
}
} break;
case EBase_Hexadecimal:
{
if(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == 'a' || c == 'A' || c == 'b' || c == 'B' || c == 'c' || c == 'C' || c == 'd' || c == 'D' || c == 'e' || c == 'E' || c == 'f' || c == 'F')
{
return 1;
}
} break;
default:
{
break;
}
}
return 0;
}
int isinteger(char* s, enum EBase base)
{
int i = 0; //index
while(s[i] != '\0' && s[i] != '\n')
{
if(i == 0 && base == EBase_Decimal)
{
if(!ischarinbase(s[i], base) && s[i] != '-')
{
return 0;
}
}
else
{
if(!ischarinbase(s[i], base))
{
return 0;
}
}
i++;
}
return 1;
}
// on error return 0
char int_to_char(int a)
{
if (0 <= a && a <= 9)
{
return a + '0';
}
else if (10 <= a && a <= 15)
{
return a - 10 + 'a';
}
return 0;
}
char* int_to_base_string(int a, enum EBase base)
{
char* str_temp = malloc(sizeof_inbits(int) + 1);
memset(str_temp, '\0', sizeof_inbits(int) + 1);
char* str = malloc(sizeof_inbits(int) + 1);
memset(str, '\0', sizeof_inbits(int) + 1);
int i = 0;
while(a != 0)
{
str_temp[i] = int_to_char(a % base);
a = a / base;
i++;
}
i = 0;
size_t len = strlen(str_temp);
while(str_temp[i] != '\0')
{
str[len - i - 1] = str_temp[i];
i++;
}
free(str_temp);
return str;
}
int handle_error(enum EStatus s)
{
char* err_string = (EStatus_Success <= s && s < EStatus_NumOfErrors) ? EStatus_ToString[s] : "Unkown error";
printf("Error accured:\nError value: %i\n%s\n", s, err_string);
return 0;
}