-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverse.c
140 lines (116 loc) · 3.18 KB
/
reverse.c
1
2
3
4
5
6
7
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
63
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 1024
struct lines {
char *string;
struct lines *prev;
struct lines *next;
};
/*Function to read lines from the input stream to the doubly linked list*/
int read_lines(struct lines **line, FILE *stream) {
char tmp[MAXLEN];
struct lines *lp;
while (fgets(tmp, MAXLEN, stream) != NULL) {
if (*line == NULL) {
if ((*line = lp = malloc(sizeof(struct lines))) == NULL) {
fprintf(stderr, "ERROR: could not allocate memory\n");
return -1;
}
lp->prev = lp->next = NULL;
} else {
if ((lp->next = malloc(sizeof(struct lines))) == NULL) {
fprintf(stderr, "ERROR: could not allocate memory\n");
return -1;
}
lp->next->prev = lp;
lp = lp->next;
lp->next = NULL;
}
if ((lp->string = malloc(strlen(tmp) + 1)) == NULL) {
fprintf(stderr, "ERROR: could not allocate memory\n");
return -1;
}
strcpy(lp->string, tmp);
}
return 0;
}
/*Function to write lines from doubly linked list in reverse order to the output stream*/
void write_lines(struct lines *line, FILE *stream) {
struct lines *lp;
lp = line;
/*Going to the end of the doubly linked list*/
while (lp->next != NULL) {
lp = lp->next;
}
/*Writing the doubly linked list in reverse order to the output stream*/
while (lp != NULL) {
fprintf(stream, "%s", lp->string);
lp = lp->prev;
}
}
/*Function to free the allocated doubly linked list*/
void delete_lines(struct lines *line) {
struct lines *lp;
lp = line;
while (lp != NULL) {
line = lp->next;
free(lp->string);
free(lp);
lp = line;
}
}
int main(int argc, char **argv) {
/*IF argc = 1 -> read from stdin and write to stdout*/
/*IF argc = 2 -> user gives inputfile which prints to screen <stdout>*/
/*IF argc = 3 -> same thing as 2, but writes to provided file instead of stdout*/
/*IF ELSE be mad and crash*/
struct lines *line = NULL;
if (argc == 1) {
printf("Input your text: \n");
if (read_lines(&line, stdin) == -1) {
exit(1);
}
printf("\nData from stdin in reversed order:\n");
write_lines(line, stdout);
} else if (argc == 2) {
FILE *input;
if ((input = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "Error: cannot open file '%s'\n", argv[1]);
exit(1);
}
printf("Data from file '%s' in reversed order:\n", argv[1]);
if (read_lines(&line, input) == -1) {
exit(1);
}
write_lines(line, stdout);
fclose(input);
} else if (argc == 3) {
if (strcmp(argv[1], argv[2]) == 0) {
fprintf(stderr, "Input and output file must differ\n");
exit(1);
}
FILE *input;
FILE *output;
if ((input = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "Error: cannot open file '%s'\n", argv[1]);
exit(1);
}
if (read_lines(&line, input) == -1) {
exit(1);
}
if ((output = fopen(argv[2], "w")) == NULL) {
fprintf(stderr, "Error: cannot open file '%s'\n", argv[1]);
exit(1);
}
write_lines(line, output);
printf("Data from file '%s' writed to the file '%s' in reversed order\n", argv[1], argv[2]);
fclose(input);
fclose(output);
} else {
fprintf(stderr, "Wrong amount of arguments!\n Usage: reverse <optional inputfile.txt> <optional outputfile.txt>\n");
exit(1);
}
delete_lines(line);
return 0;
}