-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.cpp
More file actions
313 lines (232 loc) · 6.91 KB
/
Copy pathentry.cpp
File metadata and controls
313 lines (232 loc) · 6.91 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "entry.h"
#include "jourminal.h"
#include "user.h"
#include <cstdio>
#include <ncurses.h>
#include <stdlib.h>
vector<Entry*> Entry::entries;
int Entry::lastId;
Entry* Entry::activeEntry;
bool Entry::creating = false;
using namespace std;
void Entry::OpenFile (string filepath) {
int key; //character we will store into from the keyboard
int row = 1;
int col = 0;
initscr(); //initialize the screen
noecho();
nodelay(stdscr,FALSE);
keypad(stdscr,TRUE);
clear();
refresh();
int open = scr_restore(filepath.c_str());
refresh();
while ((key = getch()) != '~'){
if (key == KEY_DOWN){
move(++row, col);
} else if (key == KEY_UP){
if (row > 0){
move(--row, col);
}
} else if (key == KEY_LEFT){
if (col > 0){
move(row, --col);
}
} else if (key == KEY_RIGHT){
if(col < COLS){
move(row, ++col);
}
} else if (key == 127){ //backspace
if(col > 0){
col--;
} else{
if (row > 0){
col = COLS;
row--;
}
}
move(row, col);
delch();
} else if (key == '\n'){ //enter
row++;
col = 0;
move(row, col);
} else if (key == '$'){ //shift alt S (save)
int save = scr_dump(filepath.c_str());
endwin();
return;
} else{
col++;
addch(key);
}
move(0, COLS - 8);
clrtoeol(); // clear line
printw("%d , %d", row, col);
move(row, col);
refresh();
}
endwin();
}
Entry::Entry (int _id, int _jourminalId, string _title,
int _authorId, string _filename, time_t _dateCreated) {
User* u = User::FindUserById(_authorId);
this->id = _id;
this->title = _title;
this->author = u;
this->filename = _filename;
this->dateCreated = _dateCreated;
this->jourminal = Jourminal::FindJourminalById(_jourminalId);
this->jourminal->AddEntry(this);
Entry::entries.push_back(this);
}
Entry::Entry (string _title) {
std::string::iterator end_pos = std::remove(_title.begin(), _title.end(), ' ');
_title.erase(end_pos, _title.end());
this->id = ++Entry::lastId;
this->title = _title;
this->author = User::activeUser;
srand(time(0));
this->filename = to_string(rand()%1000000) +this->title;
this->dateCreated = time(0);
this->jourminal = Jourminal::activeJourminal;
this->jourminal->AddEntry(this);
ofstream file;
Entry::entries.push_back(this);
ostringstream oss;
oss << this->dateCreated;
file.open ("entry.dbjml", ios::in | ios::app);
file << this->id << "||" << jourminal->GetId() << "||" << this->title << "||"
<< this->author->GetId() << "||" << this->filename << "||" << oss.str() << "||" << "\n";
file.close();
}
int Entry::GetId() {
return this->id;
}
string Entry::GetTitle() {
return this->title;
}
User* Entry::GetAuthor() {
return this->author;
}
string Entry::GetFilename() {
return this->filename;
}
time_t Entry::GetDateCreated() {
return this->dateCreated;
}
Jourminal* Entry::GetJourminal() {
return this->jourminal;
}
bool Entry::DataFileExists() {
ifstream file("entry.dbjml");
return file.good();
}
void Entry::CreateDataFile() {
ofstream file;
file.open ("entry.dbjml");
file.close();
cout << "-- Entry database file created." << endl;
}
void Entry::ScanEntryDatabase() {
cout << "- Scanning Entry database." << endl;
string line;
ifstream file ("entry.dbjml");
if (file.is_open()) {
while (getline (file,line)) {
std::string s = line;
std::string delimiter = "||";
vector<string> entryData;
size_t pos = 0;
string token;
string::size_type sz;
while ((pos = s.find(delimiter)) != std::string::npos) {
token = s.substr(0, pos);
s.erase(0, pos + delimiter.length());
entryData.push_back(token);
}
Entry::lastId = stoi (entryData[0],&sz);
Entry * entryToAdd = new Entry(Entry::lastId,
stoi(entryData[1]),
entryData[2],
stoi(entryData[3]),
entryData[4],
(time_t)stoi(entryData[5]));
}
file.close();
}
cout << "-- Scanning is complete." << endl;
}
void Entry::CreateEntry() {
string title;
cout << "Creating new entry." << endl;
cout << "New entry title: ";
cin.ignore(1, '\n');
getline(cin, title);
Entry *e = new Entry(title);
cout << "New entry created!" << endl;
Entry::creating = false;
}
void Entry::ConfigureEntryDatabase() {
cout << "- Configuring entry database." << endl;
if (Entry::DataFileExists()) {
cout << "-- Database file found." << endl;
} else {
cout << "-- Database file not found. Creating one now." << endl;
Entry::CreateDataFile();
}
Entry::ScanEntryDatabase();
}
void Entry::OpenActive() {
cout << "You are in: " << Jourminal::activeJourminal->GetTitle() <<"/" <<
Entry::activeEntry->GetTitle() << endl;
string action;
cout << "Type <d> to delete, <o> to read/edit, and <b> to go back" << endl;
cout << "Command: ";
cin >> action;
if (action.compare("b")==0) {
Entry::activeEntry = nullptr;
} else if (action.compare("o")==0) {
OpenFile(Entry::activeEntry->GetFilepath());
//Entry::OpenFile("test.jml");
}
else if (action.compare("d")==0) {
Entry::DeleteActive();
}
else {
cout<< "Sorry. Command not understood" << endl;
}
}
void Entry::DeleteActive() {
cout << Entry::entries.size() << endl;
Jourminal::activeJourminal->RemoveEntry(Entry::activeEntry);
for (uint i = 0; i< Entry::entries.size(); i++) {
if (Entry::activeEntry == Entry::entries[i]) {
Entry::entries.erase(Entry::entries.begin() + i);
}
}
ostringstream oss;
oss << "entries/" << Entry::activeEntry->GetFilename() << ".jml";
remove(oss.str().c_str());
delete Entry::activeEntry;
Entry::activeEntry = nullptr;
Entry::RewriteEntries();
}
string Entry::GetFilepath() {
return "entries/" + Entry::activeEntry->GetFilename() + ".jml";
}
void Entry::RewriteEntries() {
std::ofstream ofs;
ofs.open("entry.dbjml", std::ofstream::out | std::ofstream::trunc);
ofs.close();
ofstream file;
file.open ("entry.dbjml", ios::in | ios::app);
for (uint i =0; i< Entry::entries.size(); i++) {
Entry*e = Entry::entries[i];
ostringstream oss;
oss << e->GetDateCreated();
file << e->GetId() << "||" << e->GetJourminal()->GetId()
<< "||" << e->GetTitle() << "||" << e->GetAuthor()->GetId()
<< "||" << e->GetFilename() << "||" << oss.str() << "||" << "\n";
}
file.close();
}