This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.cpp
194 lines (178 loc) · 5.91 KB
/
Buffer.cpp
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 <iostream>
#include <fstream>
#include <iomanip>
#include "Buffer.h"
void Buffer::display()
// Checks to make sure that there is something in the buffer to be printed thne prints it as long as there is something to to print
// and it is within the bounds of the viewable area of the file
{
uint64_t currentLineNumber = m_topLineNum;
if (!m_bufferData.empty())
{
for (currentLineNumber; currentLineNumber < (m_topLineNum + m_viewableLines); currentLineNumber++)
{
if ((currentLineNumber - 1) < m_bufferData.size())
{
std::cout << std::setw(3) << currentLineNumber << " " << m_bufferData[currentLineNumber - 1] << std::endl;
}
}
}
}
void Buffer::lastPage()
{
if ((m_topLineNum - m_viewableLines) > 0)
{
m_topLineNum -= m_viewableLines;
}
else
{
m_topLineNum = m_bufferData.size() - (m_bufferData.size() % m_viewableLines);
}
}
void Buffer::nextPage()
{
if ((m_topLineNum + m_viewableLines) < m_bufferData.size())
{
m_topLineNum += m_viewableLines;
}
else
{
m_topLineNum = 1;
}
}
void Buffer::openLastFile()
{
if (!m_history.empty())
{
openFile(m_history[m_history.size() - 1]);
m_history.erase(m_history.begin() + (m_history.size() - 1));
}
else
{
m_BufferErrorMessage = "File history empty";
}
}
void Buffer::openLink(int32_t linkNumber)
{
if ((linkNumber >= 1 && linkNumber <= m_linkFileNames.size()) && !m_linkFileNames.empty())
{
linkNumber -= 1; // Converts number provided to index for accessing file name in vector
m_history.push_back(m_currentFileName);
openFile(m_linkFileNames[linkNumber]);
}
else if (m_linkFileNames.empty())
{
m_BufferErrorMessage = "This file has no links.";
}
else if (linkNumber < 1 || linkNumber > m_linkFileNames.size())
{
m_BufferErrorMessage = "Invalid link number. Valid Range: 1-" + std::to_string(m_linkFileNames.size());
}
}
void Buffer::printError(std::ostream &out)
// Only prints errors if there is one present, if the error message string is empty, nothing is done
{
if (!m_BufferErrorMessage.empty())
{
out << "Buffer Error: " + m_BufferErrorMessage << std::endl;
m_BufferErrorMessage.clear();
}
if (!m_UIErrorMessage.empty())
{
out << "UI Error: " + m_UIErrorMessage << std::endl;
m_UIErrorMessage.clear();
}
}
void Buffer::openFile(std::string fileName)
{
std::ifstream infile(fileName);
if (!infile.fail())
{
m_bufferData.clear(); // Makes sure the buffer is clear before reading a new file into it
m_linkFileNames.clear(); // Makes sure there are no file names currently stored before reading in the new file
m_topLineNum = 1; // sets the top line back to 1 so that the file is properly displayed next time the buffer is printed
m_currentFileName = fileName; // file name stored for later use if go command is entered
std::string currentWord;
std::string currentLine;
while (infile >> currentWord)
{
if (currentWord == "<a")
{
std::string fileName;
infile >> fileName;
infile >> currentWord;
currentWord.erase(currentWord.begin() + (currentWord.size() - 1)); // removes '>' from the end of the file name
m_linkFileNames.push_back(fileName);
currentWord = "<" + currentWord + ">[" + std::to_string(m_linkFileNames.size()) + "]";
}
else if (currentWord == "<br>")
{
m_bufferData.push_back(currentLine);
currentLine.clear();
continue;
}
else if (currentWord == "<p>")
{
if (!currentLine.empty())
{
m_bufferData.push_back(currentLine);
currentLine.clear();
}
m_bufferData.push_back(currentLine);
continue;
}
if ((currentLine.size() + currentWord.size() + 1) < m_charsPerLine)
// Plus 1 because of the space character that is going to be added to separate the current word from the next one
{
if (infile.get() == '\n')
{
currentLine += currentWord;
m_bufferData.push_back(currentLine);
currentLine.clear();
continue;
}
else
{
currentLine += currentWord;
currentLine += " ";
}
}
else
{
if (infile.get() == '\n')
{
currentLine += currentWord;
m_bufferData.push_back(currentLine);
currentLine.clear();
continue;
}
else
{
m_bufferData.push_back(currentLine);
currentLine.clear();
currentLine += currentWord;
currentLine += " ";
}
}
if (infile.peek() == ' ') // checks for tab character
{
currentLine.push_back(' ');
}
}
m_bufferData.push_back(currentLine); // if a file doesn't have a \n at the end of its last line, this pushes that line on to the data vector
infile.close();
}
else
{
m_BufferErrorMessage = "File \"" + fileName + "\" failed to open.";
}
}
void Buffer::setUIError(std::string errorMessage)
{
m_UIErrorMessage = errorMessage;
}
void Buffer::setViewableArea(uint32_t verticalLines, uint32_t horizontalCharacters)
{
m_viewableLines = verticalLines;
m_charsPerLine = horizontalCharacters;
}