Refactor string building and fix reader
This commit is contained in:
parent
f36156d44a
commit
16aeaf6c37
@ -81,7 +81,8 @@ int main(int argc, char *argv[]) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
char input[__UINT8_MAX__] = ""; ///< An array to save the input to
|
char * input = ""; ///< An array to save the input to
|
||||||
|
size_t input_len;
|
||||||
int point_num = 0; ///< Save the number of points
|
int point_num = 0; ///< Save the number of points
|
||||||
node_t * head = NULL;
|
node_t * head = NULL;
|
||||||
|
|
||||||
@ -89,7 +90,8 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
node_t * current = head;
|
node_t * current = head;
|
||||||
|
|
||||||
while(fgets(input, __INT8_MAX__, stdin) != NULL) { ///< Read line by line
|
while(getline(&input, &input_len, stdin) > 0) { ///< Read line by line
|
||||||
|
fprintf(stderr, "pid: %d \n%s\n", getpid(), input);
|
||||||
/// Split the input by whitespace as delimiter
|
/// Split the input by whitespace as delimiter
|
||||||
char *x = strtok(input, " ");
|
char *x = strtok(input, " ");
|
||||||
char *y = strtok(NULL, " ");
|
char *y = strtok(NULL, " ");
|
||||||
@ -107,11 +109,6 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(feof(stdin) == 0) {
|
|
||||||
fprintf(stderr, "ERROR: An error interrupted the read\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(point_num == 1)
|
if(point_num == 1)
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
@ -169,30 +166,22 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
float x_mean = get_x_mean(head);
|
float x_mean = get_x_mean(head);
|
||||||
|
|
||||||
node_t * first_part = malloc(sizeof(node_t));
|
|
||||||
int first_part_len = 1;
|
|
||||||
|
|
||||||
node_t * second_part = malloc(sizeof(node_t));
|
|
||||||
int second_part_len = 1;
|
|
||||||
|
|
||||||
node_t * first_part_buff = first_part;
|
|
||||||
node_t * second_part_buff = second_part;
|
|
||||||
|
|
||||||
/// Separate the lists based on the mean
|
/// Separate the lists based on the mean
|
||||||
current = head;
|
current = head;
|
||||||
|
|
||||||
|
char * first_part = malloc(point_num * __UINT8_MAX__);
|
||||||
|
char * second_part = malloc(point_num * __UINT8_MAX__);
|
||||||
|
|
||||||
while(current -> next != NULL) {
|
while(current -> next != NULL) {
|
||||||
|
char point[__UINT8_MAX__];
|
||||||
if(current -> points[0] <= x_mean) {
|
if(current -> points[0] <= x_mean) {
|
||||||
first_part_buff -> points[0] = current -> points[0];
|
/// Setup string for first child pipe
|
||||||
first_part_buff -> points[1] = current -> points[1];
|
sprintf(point, "%f %f\n", current -> points[0], current -> points[1]);
|
||||||
first_part_buff -> next = malloc(sizeof(node_t));
|
strcat(first_part, point);
|
||||||
first_part_buff = first_part_buff -> next;
|
|
||||||
first_part_len ++;
|
|
||||||
} else {
|
} else {
|
||||||
second_part_buff -> points[0] = current -> points[0];
|
/// Setup string for second child pipe
|
||||||
second_part_buff -> points[1] = current -> points[1];
|
sprintf(point, "%f %f\n", current -> points[0], current -> points[1]);
|
||||||
second_part_buff -> next = malloc(sizeof(node_t));
|
strcat(second_part, point);
|
||||||
second_part_buff = second_part_buff -> next;
|
|
||||||
second_part_len ++;
|
|
||||||
}
|
}
|
||||||
current = current -> next;
|
current = current -> next;
|
||||||
}
|
}
|
||||||
@ -202,24 +191,13 @@ int main(int argc, char *argv[]) {
|
|||||||
* Send first half to first child
|
* Send first half to first child
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
/// Setup string for first child pipe
|
|
||||||
current = first_part;
|
|
||||||
char first_part_string[first_part_len][__UINT8_MAX__];
|
|
||||||
int i = 0;
|
|
||||||
while(current -> next != NULL) {
|
|
||||||
sprintf(first_part_string[i], "%f %f\n", current -> points[0], current -> points[1]);
|
|
||||||
i++;
|
|
||||||
current = current -> next;
|
|
||||||
}
|
|
||||||
first_part_string[first_part_len][0] = EOF;
|
|
||||||
|
|
||||||
if(dup2(in_pipe_a[1], STDIN_FILENO) != STDIN_FILENO) {
|
if(dup2(in_pipe_a[1], STDIN_FILENO) != STDIN_FILENO) {
|
||||||
fprintf(stderr, "ERROR: Failed duplicating STDIN\n");
|
fprintf(stderr, "ERROR: Failed duplicating STDIN\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write to pipe
|
/// Write to pipe
|
||||||
if (write(in_pipe_a[1], first_part_string, first_part_len * __UINT8_MAX__) < 0) {
|
if (write(in_pipe_a[1], first_part, strlen(first_part)) < 0) {
|
||||||
fprintf(stderr, "ERROR: Failed writing to child\n");
|
fprintf(stderr, "ERROR: Failed writing to child\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -230,24 +208,13 @@ int main(int argc, char *argv[]) {
|
|||||||
* Send second half to second child
|
* Send second half to second child
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
/// Setup string for second child pipe
|
|
||||||
current = second_part;
|
|
||||||
char second_part_string[second_part_len + 1][__UINT8_MAX__];
|
|
||||||
i = 0;
|
|
||||||
while(current -> next != NULL) {
|
|
||||||
sprintf(second_part_string[i], "%f %f\n", current -> points[0], current -> points[1]);
|
|
||||||
i++;
|
|
||||||
current = current -> next;
|
|
||||||
}
|
|
||||||
second_part_string[second_part_len][0] = EOF;
|
|
||||||
|
|
||||||
if(dup2(in_pipe_b[1], STDIN_FILENO) != STDIN_FILENO) {
|
if(dup2(in_pipe_b[1], STDIN_FILENO) != STDIN_FILENO) {
|
||||||
fprintf(stderr, "ERROR: Failed duplicating STDIN\n");
|
fprintf(stderr, "ERROR: Failed duplicating STDIN\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write to pipe
|
/// Write to pipe
|
||||||
if (write(in_pipe_b[1], second_part_string, second_part_len * __UINT8_MAX__) < 0) {
|
if (write(in_pipe_b[1], second_part, strlen(second_part)) < 0) {
|
||||||
fprintf(stderr, "ERROR: Failed writing to child\n");
|
fprintf(stderr, "ERROR: Failed writing to child\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -262,9 +229,7 @@ int main(int argc, char *argv[]) {
|
|||||||
/// Free the memory
|
/// Free the memory
|
||||||
free(head);
|
free(head);
|
||||||
free(first_part);
|
free(first_part);
|
||||||
free(first_part_buff);
|
|
||||||
free(second_part);
|
free(second_part);
|
||||||
free(second_part_buff);
|
|
||||||
free(current);
|
free(current);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user