Repository navigation
Expand file tree
/
Copy pathbuffered-input.cc
More file actions
194 lines (162 loc) · 4.53 KB
/
Copy pathbuffered-input.cc
File metadata and controls
194 lines (162 loc) · 4.53 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "buffered-input.h"
#include <ios>
#include <iostream>
#include <fstream>
#include <vector>
// Index into buffer indicating current reading position
unsigned long buffer_pos = 0;
int src_line_no = 1;
int src_col_no = 1;
// Buffer to store all characters from source fil
std::vector<unsigned char> input_buffer;
// Line n stores the position at that point
std::vector<int> line_positions;
/*
* Given a file name, copy its contents into a char buffer
*
* Function opens and closes the file based on the given name
* -1 is returned if file cannot be opened
*
* Buffer is a global variable of type std::vector<char>
*
* @param file_name Name of file to be opened
* @return -1 if file cannot be opened, 0 otherwise
*/
int buffer_init(const char* file_name)
{
// open file in binary and set the position of stream to end of file
std::ifstream file(file_name, std::ios::binary | std::ios::ate);
// file can't open
if (!file)
{
std::cout << "Unable to open file: " << file_name << '\n';
return -1;
}
// use position of stream at end of file to set buffer size
std::streamsize size = file.tellg();
input_buffer.resize(size);
// start reading from beginning of file
file.seekg(0, std::ios::beg);
// file.read expects char* while input_buffer uses unsigned char*
file.read(reinterpret_cast<char*>(input_buffer.data()), size);
file.close();
// Associate 0th & 1st line with starting reading position
line_positions.push_back(0);
line_positions.push_back(0);
++src_line_no;
for (size_t i = 0; i < input_buffer.size(); i++)
{
if (input_buffer[i] == '\n')
{
// Associate line number with reading position
line_positions.push_back(buffer_pos);
++src_line_no;
}
++buffer_pos;
}
// Reset values
buffer_pos = 0;
src_line_no = 1;
src_col_no = 1;
return 0;
}
/*
* Check if current reading position is eof
*
* @return true if it is, false if it isn't
*/
bool buffer_eof()
{
// is position past end of buffer array?
return buffer_pos > input_buffer.size();
}
/*
* Return current char at reading position and increment reading position by 1
* Also increments line and column number accordingly
*
* @param c Character to be returned
* @return -1 if end of buffer
*/
int buffer_get_next_char(char& c)
{
if (buffer_eof())
{
return -1;
}
c = input_buffer[buffer_pos];
// if new line character previously detected
if (input_buffer[buffer_pos] == '\n')
{
++src_line_no;
src_col_no = 1;
}
else
{
++src_col_no;
}
++buffer_pos;
return 0;
}
/*
* Shift 1 backwards in buffer, decrementing line and col number accordingly
*
* @return -1 if attempting to go backwards at start of buffer
*/
int buffer_back_char()
{
// can't go backwards
if (buffer_pos == 0)
{
return -1;
}
--buffer_pos;
// if at beginning of line, adjust col and line number accordingly
if (src_col_no == 1)
{
// decrement line before performing further operations
--src_line_no;
// get starting buffer position of line we were just at
int old_line_pos = line_positions[src_line_no + 1];
// get starting buffer position of line we decremented to
int new_line_pos = line_positions[src_line_no];
// update column using the starting buffer position of both lines
src_col_no = old_line_pos - new_line_pos;
}
else
{
--src_col_no;
}
return 0;
}
/*
* Given a line number, return the string at that line
*
* @param line_no Line number as int
* @param line Line associated with line number
* @return -1 if invalid line number
*/
int get_src_line(int line_no, std::string& line)
{
// is a valid line number supplied?
if (line_no >= (int) line_positions.size() || line_no <= 0)
{
return -1;
}
int line_pos = line_positions[line_no];
int next_line_pos;
// is this the last line in the file?
if (line_no + 1 >= (long) line_positions.size())
{
// then calculate the line position after it to be the total number of lines
next_line_pos = input_buffer.size();
}
else
{
// if not, then get the position of the next line in the file
next_line_pos = line_positions[line_no + 1];
}
// construct the line string using the line positions
line = std::string(input_buffer.begin() + line_pos, input_buffer.begin() + next_line_pos);
return 0;
}
// int cleanup() {}