aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Liodden <eriklio@stud.ntnu.no>2018-01-18 16:25:56 +0100
committerErik Liodden <eriklio@stud.ntnu.no>2018-01-18 16:25:56 +0100
commit01fd9c052eedfe6554a764d068fe24a99a7ec580 (patch)
tree5e789a6bbf4467d42a2e3e7bd75395d1ff1f4974
parent17e5240104ee0bd43019be26cd05938c7421d23e (diff)
downloadalgdat-master.tar.gz
add io.h and io.cHEADmaster
i will try to collect all io based stuff in io.h and io.c. this way i won't have to reinvent the wheel every time.
-rw-r--r--Makefile4
-rw-r--r--io.c46
-rw-r--r--io.h14
-rw-r--r--oving1.c15
-rw-r--r--oving2.c17
5 files changed, 80 insertions, 16 deletions
diff --git a/Makefile b/Makefile
index d2262b0..2b4168d 100644
--- a/Makefile
+++ b/Makefile
@@ -6,9 +6,9 @@ AR=ar
PROG= oving1 oving2
-LIB_OBJS=stack.o queue.o llist.o misc.o sort.o heap.o
+LIB_OBJS=stack.o queue.o llist.o io.o misc.o sort.o heap.o
LIB_FILE=libalgdat.a
-LIB_H=structure.h misc.h sort.h
+LIB_H=structure.h misc.h io.h sort.h
LIBS = $(LIB_FILE)
LIB_PATH=/usr/local/lib
diff --git a/io.c b/io.c
new file mode 100644
index 0000000..4642285
--- /dev/null
+++ b/io.c
@@ -0,0 +1,46 @@
+/*
+ * io.c
+ * implementation of basic io operations
+ *
+ * Erik Liodden
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include "io.h"
+
+/* read_lines: return an array of strings (char *) where each string is a line
+ * of the input file stream fp.
+ */
+char **read_lines(size_t *n_lines, FILE *fp)
+{
+ char *line = NULL;
+ size_t linecap = 0;
+ size_t next = 4;
+ char **lines;
+ ssize_t linelen;
+
+ *n_lines = 0;
+ lines = malloc(next * sizeof(char *));
+ while ((linelen = getline(&line, &linecap, fp)) > 0) {
+ (*n_lines)++;
+ if (*n_lines == next) {
+ next *= 1.618;
+ lines = realloc(lines, next * sizeof(char *));
+ }
+ *(lines + (*n_lines-1)) = strndup(line, linelen);
+ }
+ return lines;
+}
+
+/* dispose_read_lines: delete the lines created by read_lines()
+ */
+void dispose_read_lines(char **lines, size_t n_lines)
+{
+ size_t i;
+
+ for (i = 0; i < n_lines; i++)
+ free(*(lines + i));
+ free(lines);
+}
diff --git a/io.h b/io.h
new file mode 100644
index 0000000..02ac3b5
--- /dev/null
+++ b/io.h
@@ -0,0 +1,14 @@
+/*
+ * io.h
+ * decleration of basic io operations
+ *
+ * Erik Liodden
+ */
+
+#ifndef IO_H
+#define IO_H
+
+char **read_lines(size_t *n_lines, FILE *fp);
+void dispose_read_lines(char **lines, size_t n_lines);
+
+#endif /* IO_H */
diff --git a/oving1.c b/oving1.c
index c504803..55e9ac6 100644
--- a/oving1.c
+++ b/oving1.c
@@ -8,20 +8,20 @@
#include <stdio.h>
#include <stdlib.h>
#include "structure.h"
-
-#define BUF_SIZE 1024
+#include "io.h"
int main(void)
{
int num, max;
- char buf[BUF_SIZE];
+ size_t len, i;
+ char **lines;
struct llist l;
struct llist_node *node;
- llist_init(&l);
- /*read from stdin to buffer */
- while (fgets(buf, BUF_SIZE, stdin)) {
- num = atoi(buf);
+ llist_init(&l);
+ lines = read_lines(&len, stdin);
+ for (i = 0; i < len; i++) {
+ num = atoi(*(lines + i));
node = llist_create_node(sizeof(int), NULL);
llist_node_set_elem(node, &num);
llist_insert_end(&l, node);
@@ -38,6 +38,7 @@ int main(void)
printf("%d\n", max);
/* free memory allocated by the list */
+ dispose_read_lines(lines, len);
llist_dispose(&l);
return 0;
}
diff --git a/oving2.c b/oving2.c
index 2e48b3a..f949609 100644
--- a/oving2.c
+++ b/oving2.c
@@ -9,6 +9,7 @@
#include <stdlib.h>
#include <string.h>
#include "structure.h"
+#include "io.h"
struct node {
char c;
@@ -33,17 +34,18 @@ void read_input(struct heap *h)
{
struct queue q;
struct node node;
- char *buf, *str, *token;
- size_t len;
+ char *str, *token;
+ size_t len, i;
+ char **lines;
heap_init(h, sizeof(struct queue), &free_q, &comp_q);
- while (!feof(stdin)) {
- buf = fgetln(stdin, &len);
- if (len < 4)
+ lines = read_lines(&len, stdin);
+ for (i = 0; i < len; i++) {
+ if (strlen(*(lines + i)) < 4)
continue;
queue_init(&q, sizeof(struct node), NULL);
- node.c = buf[0];
- str = strndup(buf + 2, len - 2);
+ node.c = **(lines + i);
+ str = strdup(*(lines + i) + 2);
token = strtok(str, ",");
while(token != NULL) {
node.pos = atoi(token);
@@ -53,6 +55,7 @@ void read_input(struct heap *h)
free(str);
heap_max_heap_insert(h, &q);
}
+ dispose_read_lines(lines, len);
}
void merge(struct queue *q, struct queue *p1, struct queue *p2)