|
1
2
3
4
5
6
|
#include "auxiliar.h"
char* substring(char *string, uint8_t start, uint8_t end)
{
uint8_t size = end - start;
char *substr;
|
|
7
|
substr = (char*) malloc(sizeof(char)*(size + 1));
|
|
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
|
memset(substr,0x00,size+1);
strncpy(substr,string+start,size);
return substr;
}
int next_occurrence(char *string, char character)
{
int i;
int pos = -1;
for(i = 0; i < strlen(string); i++)
{
if(string[i] == character)
{
pos = i;
break;
}
}
return pos;
}
int num_occurrences(char *string, char character)
{
int i;
int count = 0;
for(i = 0; i < strlen(string); i++)
{
if(string[i] == character)
{
count++;
}
}
if(!count)
{
return -1;
}
return count;
}
void error()
{
printf("error");
exit(-1);
}
char** comma_separate(char *input)
{
int i;
int next_pos = 0;
int pos = 0;
int input_num = num_occurrences(input,',');
if(input_num <= 0)
{
return NULL;
}
char **inputs;
|
|
63
|
inputs = (char**) malloc(sizeof(char*) * (input_num+1));
|
|
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
|
for(i = 0; i < input_num; i++)
{
next_pos += next_occurrence(input+pos,',');
if(next_pos == -1)
{
return NULL;
}
inputs[i] = substring(input,pos,next_pos);
next_pos++;
pos = next_pos;
}
return inputs;
}
void free_mem(char **inputs, int num_inputs)
{
int i;
for(i = 0; i < num_inputs; i++)
{
free(inputs[i]);
}
free(inputs);
}
int last_occurrence(char *string, char character)
{
int i;
int pos = -1;
for(i = 0; i < strlen(string); i++)
{
if(string[i] == character)
{
pos = i;
}
}
return pos;
}
void normalize(char **inputs, int num_inputs)
{
int i;
int j;
int length;
for(i = 0; i < num_inputs; i++)
{
length = strlen(inputs[i]);
for(j = 0; j < length; j++)
{
if(inputs[i][j] == '0')
{
inputs[i][j] = 0;
}
else
{
inputs[i][j] = 1;
}
}
}
|
|
123
124
125
126
127
128
|
}
int bin2dec(char *bin)
{
int i;
int num = 0;
|
|
129
|
int factor = 1;
|
|
130
131
|
int length = strlen(bin);
int pos = length - 1;
|
|
132
|
char *aux = (char*) malloc(sizeof(char) * (length+1));
|
|
133
|
memset(aux,0x00,length+1);
|
|
134
|
strcpy(aux,bin);
|
|
135
136
137
138
139
|
if(aux[0] == '1')
{
reverse_two_complement(aux);
factor = -1;
}
|
|
140
141
142
143
144
|
normalize(&aux,1);
for(i = 0; i < length; i++)
{
if(aux[pos--])
{
|
|
145
|
num += 0x1 << i;
|
|
146
147
|
}
}
|
|
148
|
num *= factor;
|
|
149
150
151
152
|
free(aux);
return num;
}
|
|
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
int bin2udec(char *bin)
{
int i;
int num = 0;
int length = strlen(bin);
int pos = length - 1;
char *aux = (char*) malloc(sizeof(char) * (length+1));
memset(aux,0x00,length+1);
strcpy(aux,bin);
normalize(&aux,1);
for(i = 0; i < length; i++)
{
if(aux[pos--])
{
num += 0x1 << i;
}
}
free(aux);
return num;
}
|
|
174
175
|
char* dec2bin(int dec, int length)
{
|
|
176
|
char *bin = (char*) malloc(sizeof(char) * (length + 1));
|
|
177
|
memset(bin,0x00,length+1);
|
|
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
int pos = length-1;
int i;
for(i = 0; i < length; i++)
{
if(dec & 0x1)
{
bin[pos--] = '1';
}
else
{
bin[pos--] = '0';
}
dec >>= 1;
}
return bin;
|
|
193
194
195
196
197
|
}
void expand_input(char **input, int required_length)
{
|
|
198
199
|
char *new_input = (char*) malloc(sizeof(char)*(required_length + 1));
memset(new_input,0x00,required_length + 1);
|
|
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
memset(new_input,'0',required_length+1);
int offset = required_length - strlen(*input);
strcpy(new_input + offset,*input);
free(*input);
*input = new_input;
}
int str2int(char *string)
{
int i;
int num = 0;
int mul = 1;
for(i = 0; i < strlen(string); i++)
{
num += mul*(string[i] - 48);
mul *= 10;
}
return num;
}
|
|
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
void reverse_two_complement(char *bin)
{
int i;
for(i = 0; i < strlen(bin); i++)
{
if(bin[i] == '0')
{
bin[i] = '1';
}
else
{
bin[i] = '0';
}
}
char *bin_added = dec2bin(bin2dec(bin)+1,strlen(bin));
strcpy(bin,bin_added);
free(bin_added);
|
|
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
}
int count_bits(char *input)
{
int i;
int count = 0;
int input_length = strlen(input);
for(i = 0; i < input_length; i++)
{
if(input[i] == '1')
{
count++;
}
}
return count;
|
|
252
|
}
|