-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cpp
More file actions
598 lines (516 loc) · 19.5 KB
/
timer.cpp
File metadata and controls
598 lines (516 loc) · 19.5 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// C++ version of timer
// Originally written in Python 3
// By Ernold "C" McPuvlist, Jan 2022
// Updated March 2025
/* Conventions used for comments: */
// Double slash are to comment the code
/* Slash/asterisk are debug stubs */
// TODO:
#include <fstream>
#include <iostream>
#include <ctime>
#include <string>
#include <iomanip>
#include <vector>
using std::cout;
using std::cerr;
using std::cin;
using std::getline;
using std::endl;
using std::string;
using std::left;
using std::right;
using std::setw;
using std::setfill;
using std::ios;
using std::fstream;
using std::ostream;
using std::ofstream;
using std::streampos;
using std::exception;
using std::bad_alloc;
using std::vector;
using std::iter_swap;
// Static constants
static const int EVENT_NAME_LENGTH = 26;
static const size_t TIMESIZE = sizeof(time_t);
static const size_t BOOLSIZE = sizeof(bool);
static const time_t NULLTIME = (time_t)NULL;
// Re-usable messages
static const char OUT_OF_RANGE[] = "Out of range";
static const char NOT_VALID_NO[] = "Not a valid number";
static const char INVALID_CHOICE[] = "Not a valid choice";
static const char INVALID_DATE[] = "Not a valid date/time";
static const char OUT_OF_MEMORY[] = "Error allocating memory for new event";
static const char BAD_TIMESTAMP[] = "Bad timestamp in data file";
// ANSI colour sequences
static const char ANSI_NORMAL[] = "\033[37;0m";
static const char ANSI_WHITE_BOLD[] = "\033[37;1m";
static const char ANSI_RED_BOLD[] = "\033[31;1m";
static const char ANSI_GREEN_BOLD[] = "\033[32;1m";
static const char ANSI_YELLOW_BOLD[] = "\033[33;1m";
// Forward declaration of global function
static void err_out(const char *);
// Class definitions
class TimeDelta {
// Class to hold the component parts of a timedelta
public:
int weeks, days, hours, mins, secs;
void make(const double tdiff) {
// Break a timedelta down into its elements
const int SECS_IN_MIN = 60;
const int SECS_IN_HOUR = 3600;
const int SECS_IN_DAY = 86400;
const int SECS_IN_WEEK = 604800;
int remaining = (int)tdiff;
weeks = remaining / SECS_IN_WEEK;
remaining = remaining % SECS_IN_WEEK;
days = remaining / SECS_IN_DAY;
remaining = remaining % SECS_IN_DAY;
hours = remaining / SECS_IN_HOUR;
remaining = remaining % SECS_IN_HOUR;
mins = remaining / SECS_IN_MIN;
secs = remaining % SECS_IN_MIN;
}
};
// TimedEvent class contains a single timed event
class TimedEvent {
public:
// Class members:
string name; // Event name
time_t start_time; // Start time
time_t end_time; // End time
bool all_day; // true if all day event (i.e. date only, time zeroed out)
// Size of a TimedEvent for serialisation purposes
static const int TIMEDEVENT_SIZE = EVENT_NAME_LENGTH + 2 * TIMESIZE + BOOLSIZE;
// Construct as empty
TimedEvent() {
clear();
}
void clear() {
// Clear all contents
name = "";
name.resize(EVENT_NAME_LENGTH);
start_time = end_time = NULLTIME;
all_day = false;
}
void get_inputs() {
// Edit a single record. Used in Add and Edit menu options
string input_buffer;
struct tm tmp_tm;
bool keep_trying = true;
const char dateformat[] = "%Y-%m-%d";
const char datetimeformat[] = "%Y-%m-%d %H:%M";
// 1. Get name
cout << "Enter event name (maximum " << EVENT_NAME_LENGTH << " characters):" << endl;
if (name[0] != '\0') // if already has a name then show it
cout << name << endl;
getline(cin, input_buffer);
if (input_buffer.length()) { // Keep previous name if nothing entered
name = input_buffer;
if (name.length() > EVENT_NAME_LENGTH) {
name.resize(EVENT_NAME_LENGTH);
err_out("Name is too long and has been truncated to:");
cerr << name << endl;
}
else
// has to be normalised to EVENT_NAME_LENGTH anyway
name.resize(EVENT_NAME_LENGTH);
}
// 2. Get all_day
keep_trying = true;
while(keep_trying) {
cout << "All day? ";
getline(cin, input_buffer);
if (toupper(input_buffer[0]) == 'Y') {
all_day = true;
keep_trying = false;
}
else if (toupper(input_buffer[0]) == 'N') {
all_day = false;
keep_trying = false;
}
else
err_out("That's wrong");
}
// 3. Get start time
keep_trying = true;
while(keep_trying) {
if(all_day) {
cout << "Enter event start date in ISO format (yyyy-mm-dd):" << endl;
getline(cin, input_buffer);
if(strptime(input_buffer.c_str(), dateformat, &tmp_tm) == NULL)
err_out(INVALID_DATE);
else {
tmp_tm.tm_hour = 0;
tmp_tm.tm_min = 0;
tmp_tm.tm_sec = 0;
tmp_tm.tm_isdst = -1;
start_time = mktime(&tmp_tm);
keep_trying = false;
}
}
else { // not all day
cout << "Enter event start date/time in ISO format (yyyy-mm-dd hh:mm):" << endl;
getline(cin, input_buffer);
if(strptime(input_buffer.c_str(), datetimeformat, &tmp_tm) == NULL)
err_out(INVALID_DATE);
else {
tmp_tm.tm_sec = 0;
tmp_tm.tm_isdst = -1;
start_time = mktime(&tmp_tm);
keep_trying = false;
}
}
}
// 4. Get end time
keep_trying = true;
while(keep_trying) {
if(all_day) {
cout << "Enter event end date in ISO format (yyyy-mm-dd):" << endl;
getline(cin, input_buffer);
if (input_buffer == "") { // No end time
end_time = NULLTIME;
keep_trying = false;
}
else if(strptime(input_buffer.c_str(), dateformat, &tmp_tm) == NULL)
err_out(INVALID_DATE);
else {
tmp_tm.tm_hour = 0;
tmp_tm.tm_min = 0;
tmp_tm.tm_sec = 0;
tmp_tm.tm_isdst = -1;
end_time = mktime(&tmp_tm);
keep_trying = false;
}
}
else { // not all day
cout << "Enter event end date/time in ISO format (yyyy-mm-dd hh:mm):" << endl;
getline(cin, input_buffer);
if (input_buffer == "") { // No end time
end_time = NULLTIME;
keep_trying = false;
}
else if(strptime(input_buffer.c_str(), datetimeformat, &tmp_tm) == NULL)
err_out(INVALID_DATE);
else {
tmp_tm.tm_sec = 0;
tmp_tm.tm_isdst = -1;
end_time = mktime(&tmp_tm);
keep_trying = false;
}
}
}
}
};
// Overload the << operator for sending a TimedEvent object
// to output stream (i.e. display a TimedEvent record)
ostream& operator<<(ostream& stream, const TimedEvent& te)
{
const time_t cur_time = time(NULL); // current time
bool is_future;
double time_interval;
struct tm *temp_tm;
char time_display_buf[EVENT_NAME_LENGTH]; // String buffer for time display
TimeDelta td;
static string date_format = "%d/%m/%Y";
static string time_format = "%H:%M";
static int date_pad_width = 10;
static int time_pad_width = 5;
// Work out the context of the time interval.
// If there is no end time and event is in the past ->
// Time interval will be the current time minus the start time
// If there is no end time and event is in the future ->
// Time interval will be the future start time minus the current time
// If there is an end time ->
// Time interval will be the end time minus the start time
if (te.end_time == (time_t)NULL) { // If there is no end time
time_interval = difftime(cur_time, te.start_time);
if (time_interval > 0) // Event is in the past
is_future = false;
else { // Event is in the future, flip interval from negative to positive
time_interval = -time_interval;
is_future = true;
}
}
else {
time_interval = difftime(te.end_time, te.start_time);
is_future = false;
}
// We should now have:
// 1. time_interval, guaranteed to be a positive value
// 2. is_future flag correctly set
// Start outputting
// Event name and start date
temp_tm = localtime(&te.start_time);
strftime(time_display_buf, EVENT_NAME_LENGTH, date_format.c_str(), temp_tm);
if (is_future)
stream << ANSI_GREEN_BOLD;
else
stream << ANSI_YELLOW_BOLD;
// Name is forced to a C string here using c_str(). For unknown reasons
// using the C++ string direct causes setw() to be ignored for strings
// that have been edited using the get_inputs method.
stream << left << setfill(' ') << setw(EVENT_NAME_LENGTH) << te.name.c_str() << " ";
stream << time_display_buf << " ";
// Only display time if not an all-day event, otherwise pad with blank space
if (te.all_day)
stream << setw(time_pad_width + 1) << "";
else {
strftime(time_display_buf, EVENT_NAME_LENGTH, time_format.c_str(), temp_tm);
stream << time_display_buf << " ";
}
// Display end date if it is not null, pad otherwise
if(te.end_time == (time_t)NULL)
stream << setw(date_pad_width + time_pad_width + 1) << "";
else {
temp_tm = localtime(&te.end_time);
strftime(time_display_buf, EVENT_NAME_LENGTH, date_format.c_str(), temp_tm);
stream << time_display_buf << " ";
// Only display time if not an all-day event, otherwise pad with blank space
if (te.all_day)
stream << setw(time_pad_width) << "";
else {
strftime(time_display_buf, EVENT_NAME_LENGTH, time_format.c_str(), temp_tm);
stream << time_display_buf;
}
}
// Display the time interval
td.make(time_interval);
stream << " " << setw(4) << right << td.weeks << " " <<
setw(2) << td.days;
if (!te.all_day)
stream << " " << setw(2) << td.hours << ":" << setw(2) << setfill('0') << td.mins;
// Reset fill to ' '
stream << setfill(' ') << "";
return stream;
}
// TimedEventArray class, based on a vector (dynamic array)
class TimedEventArray : public vector<TimedEvent> {
private:
const char DISK_FILE[10] = "timer.dat";
TimedEvent tmp_event; // temporary holding event object for marshalling prior to
// pushing to the array
public:
~TimedEventArray() {
// Erase all members
clear();
}
void new_event() {
tmp_event.clear();
tmp_event.get_inputs();
try {
push_back(tmp_event);
}
catch (bad_alloc&) {
err_out(OUT_OF_MEMORY);
}
}
// Comparison functions used by the sort_events method
static int compare_by_name(TimedEvent &te1, TimedEvent &te2) {
return te1.name.compare(te2.name);
}
static int compare_by_date(TimedEvent &te1, TimedEvent &te2) {
return difftime(te1.start_time, te2.start_time);
}
void sort_events(int(*compare_func)(TimedEvent&, TimedEvent&)) {
// Sort the event array.
// compare_func = function to use for field comparison
bool done;
for(int i = 0; i < (int)size(); i++) {
done = true;
for(iterator itj = begin(); itj < end()-i-1; itj++) {
if (compare_func(*itj, *(itj+1)) > 0) {
// swap
iter_swap(itj, itj+1);
done = false;
}
}
if (done)
break;
}
}
void disk_save() {
// Save (serialise) the event array to file stream
ofstream f;
f.open (DISK_FILE, ios::out | ios::binary);
for(iterator it = begin(); it != end(); it++) {
f.write (it->name.c_str(), EVENT_NAME_LENGTH);
f.write ((char *)(&it->start_time), TIMESIZE);
f.write ((char *)(&it->end_time), TIMESIZE);
f.write ((char *)(&it->all_day), BOOLSIZE);
}
if (f.fail())
err_out("Cannot save data to disk");
f.close();
}
void disk_load() {
fstream f;
char cbuffer [EVENT_NAME_LENGTH + 1]; /* Add a byte for terminating null if needed */
const char file_err[26] = "Failed to open data file";
streampos file_length;
f.open (DISK_FILE, ios::in | ios::binary);
if (f.fail()) {
err_out(file_err);
return;
}
// Calculate the number of event records in the file
f.seekg(0, ios::end);
file_length = f.tellg();
f.seekg(0, ios::beg);
// Reserve space in the event array for incoming records
reserve(file_length / TimedEvent::TIMEDEVENT_SIZE);
f.read (cbuffer, EVENT_NAME_LENGTH);
// Add a terminating null character in case string has max length
cbuffer[EVENT_NAME_LENGTH] = '\0';
while (!f.eof()) {
tmp_event.name = cbuffer;
tmp_event.name.resize(EVENT_NAME_LENGTH);
f.read((char *)&tmp_event.start_time, TIMESIZE);
// Test localtime() to validate the timestamp
if (localtime(&tmp_event.start_time) == NULL) {
err_out(BAD_TIMESTAMP);
break;
}
f.read((char *)&tmp_event.end_time, TIMESIZE);
if (localtime(&tmp_event.end_time) == NULL) {
err_out(BAD_TIMESTAMP);
break;
}
f.read((char *)&tmp_event.all_day, BOOLSIZE);
// Add the loaded event to the array
try {
push_back(tmp_event);
}
catch (bad_alloc&) {
cerr << OUT_OF_MEMORY << endl;
break;
}
// Next record, will raise eof when finished
f.read (cbuffer, EVENT_NAME_LENGTH);
}
f.close();
}
void printout() {
// Show all the events as a numbered list
const time_t cur_time = time(NULL); // current time
char timedisp[40]; // Output buffer for strftime
int line_no = 1;
// All header spacing takes EVENT_NAME_LENGTH into account, so
// that it can stretch accordingly
cout << endl << setfill(' ') << ANSI_WHITE_BOLD << setw(5) << "" <<
setw(EVENT_NAME_LENGTH+1) << left << "Name" << setw(17) << "Start" <<
setw(16) << "End" << ANSI_YELLOW_BOLD << " Elapsed" <<
ANSI_WHITE_BOLD << "/" << ANSI_GREEN_BOLD << "To go" << endl;
cout << ANSI_NORMAL << setw(EVENT_NAME_LENGTH + 42) << "" << "W D H:M" << endl;
cout << setfill('=') << setw(EVENT_NAME_LENGTH + 53) << "" << endl;
// Use default space fill from now on
cout << setfill(' ') << "";
for (iterator it = begin(); it != end(); it++)
cout << ANSI_NORMAL << right << setw(3) << line_no++ << ". " << *it << endl;
strftime(timedisp, 40, "%a %d %b %Y %H:%M:%S %Z", localtime(&cur_time));
cout << ANSI_NORMAL << endl << timedisp << endl;
}
};
// Data container stored on the heap
static TimedEventArray event_array;
// Function to send an error message to cerr, highlighted in red
static void err_out(const char *msg) {
cerr << ANSI_RED_BOLD << msg << ANSI_NORMAL << endl;
}
// Main
int main(int argc, char*argv[]) {
string choice;
int event_selected;
char choice_code;
bool running = true;
bool is_dirty = false; // whether edits have happened or not
// Welcome message
cout << ANSI_NORMAL << "Event Timer 1.2" << endl;
cout << "Copyright (C) Ernold C Puvlist, 2022, 2025" << endl;
// Load data from disk
event_array.disk_load();
// User menu loop
while (running) {
event_array.printout();
cout << "(A) Add, (En) Edit, (Dn) Delete, (S) Sort, (X) Exit: ";
getline(cin, choice);
choice_code = toupper(choice[0]);
switch (choice_code) {
case 'A':
event_array.new_event();
is_dirty = true;
break;
case 'E':
// Edit selected record.
// Extract numeric part of user input
try {
event_selected = stoi(choice.substr(1));
if (event_selected < 0 || event_selected > (int)event_array.size()) {
err_out(OUT_OF_RANGE);
break;
}
else {
event_array.at(event_selected - 1).get_inputs();
is_dirty = true;
}
}
catch (exception &e) {
err_out(NOT_VALID_NO);
}
break;
case 'D':
// Delete selected record
// Extract numeric part of user input
try {
event_selected = stoi(choice.substr(1));
if (event_selected < 0 || event_selected > (int)event_array.size()) {
err_out(OUT_OF_RANGE);
break;
}
cout << "Delete event no. " << event_selected << " (" << event_array.at(event_selected - 1).name << ")? ";
getline(cin, choice);
if (toupper(choice[0]) == 'Y') {
event_array.erase(event_array.begin() + event_selected - 1);
is_dirty = true;
}
else
err_out("Not deleted");
}
catch (exception &e) {
err_out(NOT_VALID_NO);
}
break;
case 'S': // Sort the events
cout << "Sort on Name (N) or Start date (D)? ";
getline(cin, choice);
switch (toupper(choice[0])) {
case 'N':
event_array.sort_events(&TimedEventArray::compare_by_name);
is_dirty = true;
break;
case 'D':
event_array.sort_events(&TimedEventArray::compare_by_date);
is_dirty = true;
break;
default:
err_out(INVALID_CHOICE);
}
break;
case 'X':
if (is_dirty) {
cout << "Data has been changed. Save the data? ";
getline(cin, choice);
if (toupper(choice[0]) == 'Y')
event_array.disk_save();
}
cout << endl << "Thank you for using Event Timer." << endl;
cout << "Now fuck off" << endl;
running = false;
break;
default:
cerr << endl;
err_out(INVALID_CHOICE);
}
}
}