-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.h
95 lines (81 loc) · 1.87 KB
/
file.h
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
#ifndef NAIVELEX_FILE_H
#define NAIVELEX_FILE_H
#include <deque>
#include <fstream>
#include <utility>
#include <cassert>
#include "token.h"
class FileWrapper {
private:
std::string fileName_;
std::ifstream sourceFile_;
std::deque<char> buffer_;
bool eof_;
size_t line_;
size_t column_;
size_t count_;
void read()
{
if(!eof_)
{
auto c = sourceFile_.get();
buffer_.push_back(c);
sourceFile_.peek();
eof_ = sourceFile_.eof();
}
}
public:
explicit FileWrapper(std::string fileName)
{
fileName_ = std::move(fileName);
line_ = 1;
column_ = 0;
count_ = 0;
eof_ = false;
sourceFile_.open(fileName_);
assert(!sourceFile_.fail());
this->read();
}
char getNextChar()
{
if(this->eof())
return '\0';
if(buffer_.empty())
this->read();
auto c = buffer_.front();
buffer_.pop_front();
// Update line_ and column_
if(c == '\n')
{
line_++;
column_ = 0;
}
else
column_++;
count_++;
return c;
}
char peekChar(size_t const offset)
{
while(!sourceFile_.eof() && buffer_.size() < offset)
{
this->read();
}
if(buffer_.size() < offset)
throw std::out_of_range("Early EOF. Offset out of range.");
return buffer_.at(offset - 1);
}
void eatChars(size_t const num)
{
for(size_t i = 0; i < num; i++)
getNextChar();
}
bool eof() { return eof_ && buffer_.empty(); }
size_t getCount() { return count_; }
size_t getColumn() { return column_; }
size_t getLineCount() { return line_; }
std::string getName() { return fileName_; }
friend std::ostream& operator<<(std::ostream& os, FileWrapper & fileWrapper);
TokenLoc getLocation();
};
#endif //NAIVELEX_FILE_H