Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
unix
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
tu-wien
unix
Commits
16aeaf6c
Commit
16aeaf6c
authored
Dec 16, 2018
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor string building and fix reader
parent
f36156d4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
52 deletions
+17
-52
cpair/cpair.c
cpair/cpair.c
+17
-52
No files found.
cpair/cpair.c
View file @
16aeaf6c
...
@@ -81,7 +81,8 @@ int main(int argc, char *argv[]) {
...
@@ -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[]) {
...
@@ -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[]) {
...
@@ -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[]) {
...
@@ -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[]) {
...
@@ -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[]) {
...
@@ -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[]) {
...
@@ -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
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment