|
|
@ -40,140 +40,6 @@ unit_static void retreat_pointer(cbuf_handle_t cbuf) { |
|
|
|
cbuf->tail = (cbuf->tail + 1) % cbuf->max; |
|
|
|
} |
|
|
|
|
|
|
|
// Compares the buffers cbuf1 and cbuf2. Elements present on cbuf1 that do not
|
|
|
|
// exist on cbuf2 are added to the array add2, elements present on cbuf2 that do
|
|
|
|
// not exist on cbuf1 are added to the array add1.
|
|
|
|
// Both buffers must be ordered on the same way!
|
|
|
|
unit_static void diff_bufs(cbuf_handle_t cbuf1, cbuf_handle_t cbuf2, |
|
|
|
char ***add1, char ***add2) { |
|
|
|
assert(cbuf1 && cbuf2 && |
|
|
|
(circ_buf_element_size(cbuf1) == circ_buf_element_size(cbuf2)) && |
|
|
|
(circ_buf_capacity(cbuf1) == circ_buf_capacity(cbuf2))); |
|
|
|
|
|
|
|
// Initializes the diff arrays
|
|
|
|
(*add1) = (char **) malloc(circ_buf_capacity(cbuf2) * sizeof(char *)); |
|
|
|
(*add2) = (char **) malloc(circ_buf_capacity(cbuf1) * sizeof(char *)); |
|
|
|
|
|
|
|
char *curr_str1 = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
char *curr_str2 = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
/*uint8_t diff_array_index = 0;
|
|
|
|
for (uint8_t i = 0; i < circ_buf_size(cbuf1); ++i) { |
|
|
|
// Reads current element of cbuf1
|
|
|
|
circ_buf_read(cbuf1, i, curr_str1); |
|
|
|
bool element_exists = false; |
|
|
|
|
|
|
|
for (uint8_t j = 0; j < circ_buf_size(cbuf2); ++j) { |
|
|
|
circ_buf_read(cbuf2, j, curr_str2); |
|
|
|
// Checks against cbuf2 elements
|
|
|
|
if (!strcmp(curr_str2, curr_str1)) { |
|
|
|
element_exists = true; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!element_exists) { |
|
|
|
(*add2)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
strcpy((*add2)[diff_array_index], curr_str1); |
|
|
|
++diff_array_index; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
(*add1)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
strcpy((*add1)[diff_array_index], EOB); |
|
|
|
|
|
|
|
diff_array_index = 0; |
|
|
|
for (uint8_t i = 0; i < circ_buf_size(cbuf2); ++i) { |
|
|
|
// Reads current element of cbuf2
|
|
|
|
circ_buf_read(cbuf2, i, curr_str2); |
|
|
|
bool element_exists = false; |
|
|
|
|
|
|
|
for (uint8_t j = 0; j < circ_buf_size(cbuf1); ++j) { |
|
|
|
circ_buf_read(cbuf1, j, curr_str1); |
|
|
|
// Checks against cbuf1 elements
|
|
|
|
if (!strcmp(curr_str1, curr_str2)) { |
|
|
|
element_exists = true; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!element_exists) { |
|
|
|
(*add1)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
strcpy((*add1)[diff_array_index], curr_str2); |
|
|
|
++diff_array_index; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
(*add2)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
strcpy((*add2)[diff_array_index], EOB);*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t cbuf1_idx = 0, cbuf2_idx = 0, add1_arr_idx = 0, add2_arr_idx = 0; |
|
|
|
while ((cbuf1_idx < circ_buf_size(cbuf1)) && |
|
|
|
(cbuf2_idx < circ_buf_size(cbuf2))) { |
|
|
|
circ_buf_read(cbuf1, cbuf1_idx, curr_str1); |
|
|
|
circ_buf_read(cbuf2, cbuf2_idx, curr_str2); |
|
|
|
|
|
|
|
int strcmp_res = strcmp(curr_str1, curr_str2); |
|
|
|
if (!strcmp_res) { |
|
|
|
++cbuf1_idx; |
|
|
|
++cbuf2_idx; |
|
|
|
} else { // TODO: change the inner comparisons (strtok etc)
|
|
|
|
if (strcmp_res < 0) { |
|
|
|
(*add2)[add2_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
strcpy((*add2)[add2_arr_idx], curr_str1); |
|
|
|
++add2_arr_idx; |
|
|
|
++cbuf1_idx; |
|
|
|
} |
|
|
|
else if (strcmp_res > 0) { |
|
|
|
(*add1)[add1_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
strcpy((*add1)[add1_arr_idx], curr_str2); |
|
|
|
++add1_arr_idx; |
|
|
|
++cbuf2_idx; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
while (cbuf1_idx < circ_buf_size(cbuf1)) { |
|
|
|
(*add2)[add2_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
circ_buf_read(cbuf1, cbuf1_idx, curr_str1); |
|
|
|
strcpy((*add2)[add2_arr_idx], curr_str1); |
|
|
|
++add2_arr_idx; |
|
|
|
++cbuf1_idx; |
|
|
|
} |
|
|
|
|
|
|
|
while (cbuf2_idx < circ_buf_size(cbuf2)) { |
|
|
|
(*add1)[add1_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
circ_buf_read(cbuf2, cbuf2_idx, curr_str2); |
|
|
|
strcpy((*add1)[add1_arr_idx], curr_str2); |
|
|
|
++add1_arr_idx; |
|
|
|
++cbuf2_idx; |
|
|
|
} |
|
|
|
|
|
|
|
(*add1)[add1_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
strcpy((*add1)[add1_arr_idx], EOB); |
|
|
|
|
|
|
|
(*add2)[add2_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
strcpy((*add2)[add2_arr_idx], EOB); |
|
|
|
|
|
|
|
free(curr_str1); |
|
|
|
free(curr_str2); |
|
|
|
|
|
|
|
/*uint8_t i = 0;
|
|
|
|
printf("add1:\n"); |
|
|
|
while (strcmp((*add1)[i], EOB)) { |
|
|
|
printf("%s\n", (*add1)[i]); |
|
|
|
++i; |
|
|
|
} |
|
|
|
|
|
|
|
i = 0; |
|
|
|
printf("add2:\n"); |
|
|
|
while (strcmp((*add2)[i], EOB)) { |
|
|
|
printf("%s\n", (*add2)[i]); |
|
|
|
++i; |
|
|
|
}*/ |
|
|
|
} |
|
|
|
|
|
|
|
// APIs
|
|
|
|
|
|
|
|
cbuf_handle_t circ_buf_init(char **buffer, size_t size, size_t element_size) { |
|
|
@ -303,6 +169,136 @@ int circ_buf_read(cbuf_handle_t cbuf, size_t position, char *data) { |
|
|
|
return r; |
|
|
|
} |
|
|
|
|
|
|
|
void diff_bufs(cbuf_handle_t cbuf1, cbuf_handle_t cbuf2, |
|
|
|
char ***add1, char ***add2) { |
|
|
|
assert(cbuf1 && cbuf2 && |
|
|
|
(circ_buf_element_size(cbuf1) == circ_buf_element_size(cbuf2)) && |
|
|
|
(circ_buf_capacity(cbuf1) == circ_buf_capacity(cbuf2))); |
|
|
|
|
|
|
|
// Initializes the diff arrays
|
|
|
|
(*add1) = (char **) malloc(circ_buf_capacity(cbuf2) * sizeof(char *)); |
|
|
|
(*add2) = (char **) malloc(circ_buf_capacity(cbuf1) * sizeof(char *)); |
|
|
|
|
|
|
|
char *curr_str1 = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
char *curr_str2 = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
/*uint8_t diff_array_index = 0;
|
|
|
|
for (uint8_t i = 0; i < circ_buf_size(cbuf1); ++i) { |
|
|
|
// Reads current element of cbuf1
|
|
|
|
circ_buf_read(cbuf1, i, curr_str1); |
|
|
|
bool element_exists = false; |
|
|
|
|
|
|
|
for (uint8_t j = 0; j < circ_buf_size(cbuf2); ++j) { |
|
|
|
circ_buf_read(cbuf2, j, curr_str2); |
|
|
|
// Checks against cbuf2 elements
|
|
|
|
if (!strcmp(curr_str2, curr_str1)) { |
|
|
|
element_exists = true; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!element_exists) { |
|
|
|
(*add2)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
strcpy((*add2)[diff_array_index], curr_str1); |
|
|
|
++diff_array_index; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
(*add1)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
strcpy((*add1)[diff_array_index], EOB); |
|
|
|
|
|
|
|
diff_array_index = 0; |
|
|
|
for (uint8_t i = 0; i < circ_buf_size(cbuf2); ++i) { |
|
|
|
// Reads current element of cbuf2
|
|
|
|
circ_buf_read(cbuf2, i, curr_str2); |
|
|
|
bool element_exists = false; |
|
|
|
|
|
|
|
for (uint8_t j = 0; j < circ_buf_size(cbuf1); ++j) { |
|
|
|
circ_buf_read(cbuf1, j, curr_str1); |
|
|
|
// Checks against cbuf1 elements
|
|
|
|
if (!strcmp(curr_str1, curr_str2)) { |
|
|
|
element_exists = true; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!element_exists) { |
|
|
|
(*add1)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
strcpy((*add1)[diff_array_index], curr_str2); |
|
|
|
++diff_array_index; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
(*add2)[diff_array_index] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
strcpy((*add2)[diff_array_index], EOB);*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t cbuf1_idx = 0, cbuf2_idx = 0, add1_arr_idx = 0, add2_arr_idx = 0; |
|
|
|
while ((cbuf1_idx < circ_buf_size(cbuf1)) && |
|
|
|
(cbuf2_idx < circ_buf_size(cbuf2))) { |
|
|
|
circ_buf_read(cbuf1, cbuf1_idx, curr_str1); |
|
|
|
circ_buf_read(cbuf2, cbuf2_idx, curr_str2); |
|
|
|
|
|
|
|
int strcmp_res = strcmp(curr_str1, curr_str2); |
|
|
|
if (!strcmp_res) { |
|
|
|
++cbuf1_idx; |
|
|
|
++cbuf2_idx; |
|
|
|
} else { // TODO: change the inner comparisons (strtok etc)
|
|
|
|
if (strcmp_res < 0) { |
|
|
|
(*add2)[add2_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
strcpy((*add2)[add2_arr_idx], curr_str1); |
|
|
|
++add2_arr_idx; |
|
|
|
++cbuf1_idx; |
|
|
|
} |
|
|
|
else if (strcmp_res > 0) { |
|
|
|
(*add1)[add1_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
strcpy((*add1)[add1_arr_idx], curr_str2); |
|
|
|
++add1_arr_idx; |
|
|
|
++cbuf2_idx; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
while (cbuf1_idx < circ_buf_size(cbuf1)) { |
|
|
|
(*add2)[add2_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
circ_buf_read(cbuf1, cbuf1_idx, curr_str1); |
|
|
|
strcpy((*add2)[add2_arr_idx], curr_str1); |
|
|
|
++add2_arr_idx; |
|
|
|
++cbuf1_idx; |
|
|
|
} |
|
|
|
|
|
|
|
while (cbuf2_idx < circ_buf_size(cbuf2)) { |
|
|
|
(*add1)[add1_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
circ_buf_read(cbuf2, cbuf2_idx, curr_str2); |
|
|
|
strcpy((*add1)[add1_arr_idx], curr_str2); |
|
|
|
++add1_arr_idx; |
|
|
|
++cbuf2_idx; |
|
|
|
} |
|
|
|
|
|
|
|
(*add1)[add1_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf1) * sizeof(char)); |
|
|
|
strcpy((*add1)[add1_arr_idx], EOB); |
|
|
|
|
|
|
|
(*add2)[add2_arr_idx] = (char*) malloc(circ_buf_element_size(cbuf2) * sizeof(char)); |
|
|
|
strcpy((*add2)[add2_arr_idx], EOB); |
|
|
|
|
|
|
|
free(curr_str1); |
|
|
|
free(curr_str2); |
|
|
|
|
|
|
|
/*uint8_t i = 0;
|
|
|
|
printf("add1:\n"); |
|
|
|
while (strcmp((*add1)[i], EOB)) { |
|
|
|
printf("%s\n", (*add1)[i]); |
|
|
|
++i; |
|
|
|
} |
|
|
|
|
|
|
|
i = 0; |
|
|
|
printf("add2:\n"); |
|
|
|
while (strcmp((*add2)[i], EOB)) { |
|
|
|
printf("%s\n", (*add2)[i]); |
|
|
|
++i; |
|
|
|
}*/ |
|
|
|
} |
|
|
|
|
|
|
|
int circ_buf_serialize(cbuf_handle_t cbuf, char **serialized) { |
|
|
|
char *temp = (char*) malloc(circ_buf_element_size(cbuf) * sizeof(char)); |
|
|
|
const char separator[2] = "\r"; |
|
|
|