This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
forked from BriungRi/ostep-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse.cc
129 lines (112 loc) · 2.72 KB
/
reverse.cc
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
/*
* =====================================================================================
*
* Filename: reverse.cc
*
* Description: Reverses the lines of a file
*
* Version: 1.0
* Created: 07/04/2020 14:49:10
* Revision: none
* Compiler: clang++
*
* Author: Brian Li,
* Organization:
*
* =====================================================================================
*/
#include <iostream>
#include <stdexcept>
#include <sys/stat.h>
template <class T> class Vector {
private:
// Backing array
T *arr_;
int cap_;
int len_;
public:
// Constructor
Vector() {
cap_ = 1;
len_ = 0;
// Initialize a to point to an array of length `cap`
arr_ = new T[cap_ * sizeof(T)];
}
// Inserts `v` at the end
void insert(const T &v) {
if (len_ == cap_) {
// Copy `arr` into a new array
cap_ *= 2;
T *nextArr = new T[cap_ * sizeof(T)];
for (int i = 0; i < len_; i++) {
nextArr[i] = arr_[i];
}
delete[] arr_;
arr_ = nextArr;
}
arr_[len_] = v;
len_ += 1;
}
// Gets the last element
T get() { return arr_[len_ - 1]; }
// Gets the element at index `i`
T get(const int i) {
if (i < 0 || i >= len_) {
char errMsg[100];
sprintf(errMsg, "Expected an index between 0 and %d, got %d", len_ - 1,
i);
throw std::out_of_range(errMsg);
}
return arr_[i];
}
// Returns the length of the array
const int getSize() { return len_; }
};
int main(int argc, char *argv[]) {
if (argc > 3) {
std::cerr << "usage: reverse <input> <output>\n";
return 1;
}
FILE *in = stdin;
if (argc > 1) {
char *input = argv[1];
in = fopen(input, "r");
// Verify that `input` exists
if (in == NULL) {
std::cerr << "reverse: cannot open file '" << input << "'\n";
return 1;
}
}
FILE *out = stdout;
if (argc == 3) {
char *input = argv[1];
char *output = argv[2];
// Verify that the `input` is physically different from `output`
struct stat inStat, outStat;
stat(input, &inStat);
stat(output, &outStat);
if (inStat.st_ino == outStat.st_ino) {
std::cerr << "reverse: input and output file must differ\n";
return 1;
}
out = fopen(output, "w");
// Verify that `output` exists
if (out == NULL) {
std::cerr << "reverse: cannot open file '" << output << "'\n";
return 1;
}
}
Vector<std::string> v;
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
while ((linelen = getline(&line, &linecap, in)) > 0) {
v.insert(line);
}
for (int i = v.getSize() - 1; i >= 0; i--) {
fprintf(out, "%s", v.get(i).c_str());
}
fclose(in);
fclose(out);
return 0;
}