-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSched.cpp
1441 lines (1220 loc) · 45.3 KB
/
Sched.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
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Julian Day 0 = Monday, January 1, 4713 BC, proleptic Julian calendar
() Displaying, (case 1) (for now) will be the only time the data is written to file. Should update schedule also update the file?
(*) See if this will run on a windows environment (runs on linux and mac)
char *ctime(const time_t *time);
This returns a pointer to a string of the form day month year hours:minutes:seconds year\n\0.
* Idea to keep extra features hidden from stdout and instead written in the readme
* Probably need to store date info in case exported to file, wait a day, then import to program. Probably means we need timestamp of file modification
* Maybe export to file occasionally -> seems like a bad idea, just interact when the user is requesting, but could be explored
* Sched.cpp to warn you x days before things like your car insurance expires (invisible option, but in that case, would need to export extra information to file)
Try, Catch block to prompt import of schedule.txt automatically so users can just run Sched without supplying command-line arguments
Function to calculate number of days between two given dates
Function to display calendar visual generated from parsing schedule
Function to output availability generated from parsing schedule in format: Monday: x:xx to x:xx free, Tuesday: x:xx to x:xx free, etc.
Allow multiple numbers in priority
Take care of nested parentheses
?Allow nested (()) in priority
Option to append to description
Option to prepend to priority
Make option to insert at specific spot
Separate things which are separated by newlines or make option to organize things with number priority. Give everything a sorting priority based on block it is located in
Swap option using line numbers?
Need to check against bad inputs - letters other than 'd' for deleting delete 0th item? numbers greater than size of arr give segfault
Options to return to main menu
[Fixed Issues]
(#) Empty parens with no description causes an error, along with some other errors observed in files in the development directory
(#) Be careful where isNumber() is used, because isNumber(100) would currently return false
(#) Make automated tester audit.cpp to check if two schedule files only differ by date in priority
(#) Problem with numDays when updateSched() makes a priority negative
(#) Fix logic with inputting ^# case and ^#^ case
(#) Backwards apostrophe (on tilde key top left) on main menu input causes program to infinitely loop, printing out main menu and default case -> this is because we read in expecting int
(#) Moving item from position 13 to 11 caused a duplication and a deletion. If you move into a position above, you need to delete the original pos + 1
(#) Moving ^ item caused a priority to mess up. Went from (*0*) to (*0)
(#) Need to convert priority type if priority was updated, but sort was not requested
(#) Needs to take white spaces in input task descriptions,
(#) Importing and exporting is adding new linebreaks 7 newline difference
(#) Make option to move
(#) Spacing seems off when displaying line numbers
(#) Needs to deal with ?() case -> Just add a prefix to everything
(#) Needs to have option to add a prefix to something
(#) Needs to correctly print dates when showing item numbers
(#) Needs to be able to import file
(#) Priorities other than numbers (ASAP) etc
(#) Need to be able to have priorities that contain a number somewhere (assume there is only 1)
(#) Edit priorities option
(#) Take timestamp from when user makes a relevant request (to add, display or update)
(#) Needs file to store tasks,
(#) Needs to update days remaining on display
(#) Make option to display line numbers for editing
(#) If there is newlines, and we haven't seen anything relevant yet, ignore it
(#) Do not input newlines before header into file ? currently doing this
*/
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>
#include <ctime>
#include <stdlib.h>
#include <math.h>
#include <fstream>
using namespace std;
struct widget_t {
double x;
double y;
int count;
};
double logBase10(double x)
{
if(x == 0)
return 0;
else
return log10(x);
} // logBase10
class Element
{
public:
string taskDescr;
int numDays;
bool hasNumPriority; // this means update() will subtract from numDays
bool hasCharNumPriority; // ^# case, should also have numPriority. Special case of numPriority
bool hasStringNumPriority; // ^#^ case will have this, numPriority, and not CharNumPriority
bool newLine;
bool isHeader;
string priority;
int breakPos;
string prefix;
// nonintuitive, but can be updated later
Element() // by default, assume everything has pure number priority
{
hasNumPriority = true;
hasCharNumPriority = false;
hasStringNumPriority = false;
prefix = "";
newLine = false;
isHeader = false;
}
Element(double d)
{
newLine = true;
isHeader = false;
hasNumPriority = false;
}
Element(string s) // ^# case. Will print out the priority description first, then the number part of the priority
{
hasNumPriority = true;
hasCharNumPriority = true;
hasStringNumPriority = false;
prefix = "";
newLine = false;
isHeader = false;
}
Element(int a) // pure string case
{
hasNumPriority = false;
hasCharNumPriority = false;
hasStringNumPriority = false;
prefix = "";
newLine = false;
isHeader = false;
}
Element(bool b, int breakPos) // ^#^ case
{
hasNumPriority = true;
hasCharNumPriority = false;
hasStringNumPriority = true;
this->breakPos = breakPos;
prefix = "";
newLine = false;
isHeader = false;
}
void convertPureInt()
{
this->hasNumPriority = true;
this->hasCharNumPriority = false;
this->hasStringNumPriority = false;
this->isHeader = false;
this->newLine = false;
} // convertPureInt()
void convertPureString()
{
this->hasNumPriority = false;
this->hasCharNumPriority = false;
this->hasStringNumPriority = false;
this->isHeader = false;
this->newLine = false;
} // convertPureString()
void convertCharNum()
{
this->hasNumPriority = true;
this->hasCharNumPriority = true;
this->hasStringNumPriority = false;
this->isHeader = false;
this->newLine = false;
} // convertCharNum()
void convertStringNum()
{
this->hasNumPriority = true;
this->hasCharNumPriority = false;
this->hasStringNumPriority = true;
this->isHeader = false;
this->newLine = false;
} // convertStringNum()
bool isNewLine()
{
return this->newLine;
}
bool isHeaderFun()
{
return this->isHeader;
}
void print()
{
if(this->newLine)
{
cout << "\n";
}
else if(this->isHeader)
{
cout << this->taskDescr << endl;
}
else if(!hasNumPriority && !hasCharNumPriority && !hasStringNumPriority) // pure string case
{
cout << this->prefix << "(" << this->priority << ") " << this->taskDescr << endl;
return;
}
else if(hasNumPriority && !hasCharNumPriority && !hasStringNumPriority) // pure int case
{
cout << this->prefix << "(" << this->numDays << ") " << this->taskDescr << endl;
return;
}
else if(hasNumPriority && hasCharNumPriority && !hasStringNumPriority)
{
cout << this->prefix << "(" << this->priority << this->numDays << ") " << this->taskDescr << endl;
return;
}
else
cout << this->prefix << "(" << (this->priority).substr(0,this->breakPos) << this->numDays << (this->priority).substr(this->breakPos) << ") " << this->taskDescr << endl;
} // print()
}; // Class Element
int computejdn(int day, int month, int year)
{
int a = floor((14 - month) / 12);
int y = year + 4800 - a;
int m = month + (12*a) - 3;
return (day + floor( ( (153 * m) + 2) / 5 ) + (365*y) + floor(y/4) -
floor(y/100) + floor(y/400) - 32045);
} // computejdn()
vector<Element> arr;
// subtracts diff from all integer priorities, returns the diff
int updateSched(int diff)
{
for(int i = 0; i < arr.size(); i++)
{
if(arr[i].hasNumPriority)
arr[i].numDays -= diff;
} // for
return diff;
} // updateSched()
// https://stackoverflow.com/questions/4654636/how-to-determine-if-a-string-is-a-number-with-c
// negative numbers return false
bool isNumber(string s)
{
if(s == "00")
return false;
std::string::const_iterator it = s.begin();
while (it != s.end() && isdigit(*it)) ++it;
return !s.empty() && it == s.end();
} // isNumber()
Element *ep;
Element e;
// returns a non-negative number if a number is found in string s, otherwise -1
int numberFound(string s)
{
int i;
bool numberFound = false;
string temp = "";
int pos;
for(i = 0; i < s.length(); i++)
{
if(isNumber(s.substr(i,1)))
{
numberFound = true;
//pos = i;
break;
}
} // for
if(numberFound)
{
while(i < s.length() && isNumber(s.substr(i,1)))
{
temp += s.substr(i,1);
i++;
} // while
if(temp == "00" || s.find("-0") != std::string::npos) // if the pure number part is 00 or -0 is found, do not treat this as a valid int
return -1;
/*if(pos-1 >= 0 && s.substr(pos-1,1) == "-") // don't really want to return a negative number here
{
temp = "-" + temp;
}*/
stringstream geek(temp); // convert string to int
geek >> i;
//cout << i << endl;
return i;
} // if
return -1;
} // numberFound()
bool isChar(char c)
{
if(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9')
return true;
return false;
} // isChar()
time_t now;
tm *ltm;
int dd;
int mm;
int yyyy;
int currentDay;
int currentMonth;
int currentYear;
void importFile(string fileName)
{
string inputLine;
ifstream infile;
int oldJulNum;
//infile.open ("scheddata.txt");
infile.open(fileName);
int pos;
int newLineCtr = 0;
bool headerSeen = false;
bool relevantElementSeen = false;
bool oldFile = false;
int dateDiff;
while(!(infile.eof()))
{
getline(infile,inputLine);
//if(infile.eof())
// break;
//cout << inputLine << endl;
if(inputLine == "*******************SCHEDULE*************************")
{
getline(infile,inputLine); // get the next line, which should contain the julian number
string oldJulStr = "";
int p = 0;
while(isChar(inputLine[p]))
{
oldJulStr += inputLine[p];
p++;
} // while
stringstream geek(oldJulStr); // convert string to int
geek >> oldJulNum; // oldJulNum now needs to be tested against current date
now = time(0); // get current time
ltm = localtime(&now);
currentDay = ltm->tm_mday; // These are set when main program is run
currentMonth = ltm->tm_mon + 1;
currentYear = ltm->tm_year + 1900;
int currentDate = computejdn(currentDay, currentMonth, currentYear);
dateDiff = currentDate - oldJulNum;
if(dateDiff > 0) // need to be careful about changing time zones when this is caclulated
oldFile = true;
} // if this is a file that was created by this program
if(relevantElementSeen && inputLine.find("\n") && inputLine.length() == 0) // a line just containing a new line, we want to keep formatting
{
newLineCtr++;
ep = new Element(2.0);
e = *ep;
arr.push_back(e);
} // if
else if(inputLine.substr(0,1).compare("~") == 0 || inputLine.substr(0,1).compare("[") == 0) // found header
{
relevantElementSeen = true;
if(!headerSeen)
{
//cout << newLineCtr << " New lines seen before first header" << endl;
headerSeen = true;
}
ep = new Element();
e = *ep;
e.isHeader = true;
e.hasNumPriority = false;
e.taskDescr = inputLine;
arr.push_back(e);
}
else if(( (pos = inputLine.find("(") ) != std::string::npos) && pos == 0) // we are at the first important line of the file
{ // need to see if priority is an integer, make a new element object, and assign priority and description. Note that items will be sorted.
// could also take care of case where items are not sorted
// need to take care of ^# case
relevantElementSeen = true;
string temp;
string prefix = inputLine.substr(0,pos);
int j = pos + 1;
bool isNumberPriority = true; // default, assume the full priority is a pure number
while(inputLine.substr(j,1).compare(")") != 0) // give temp the value of the string inside (...) AKA the priority
{
temp += inputLine.substr(j,1);
j++;
} // while
if(temp == "" || !isNumber(temp)) // check if priority is a pure number
isNumberPriority = false;
if(isNumberPriority) // if priority is a pure number
{
stringstream geek(temp); // convert string to int
int x; // ""
geek >> x;
// create new element with this priority and the description found in the rest of inputLine
ep = new Element();
e = *ep;
if(inputLine.length() >= j+2) // taking care of case where there is no task description (4 more cases of this follow in this function)
e.taskDescr = inputLine.substr(j+2); // +2 because it is on ')', and the next char is a space
else
e.taskDescr = "";
e.numDays = x;
//cout << "pushback" << endl;
//arr.push_back(e);
} // if
else if(temp != "" && isNumber(temp.substr(1,temp.length() - 1)) && temp != "-0") // ^# case. Exclude -0
{
// create new element with ^# priority and the description found in the rest of inputline
if(temp.substr(0,1) == "-") // we have a pure negative number
{
stringstream geek(temp);
int y;
geek >> y;
ep = new Element();
e = *ep;
if(inputLine.length() >= j+2) // taking care of case where there is no task description (4 more cases of this follow in this function)
e.taskDescr = inputLine.substr(j+2); // +2 because it is on ')', and the next char is a space
else
e.taskDescr = "";
e.numDays = y;
}
else // we have a non-negative number
{
stringstream geek(temp.substr(1,temp.length() - 1));
int x;
geek >> x;
ep = new Element("s");
e = *ep;
if(inputLine.length() >= j+2) // taking care of case where there is no task description (4 more cases of this follow in this function)
e.taskDescr = inputLine.substr(j+2); // +2 because it is on ')', and the next char is a space
else
e.taskDescr = "";
e.numDays = x;
e.priority = temp.substr(0,1);
//cout << "pushback1" << endl;
//arr.push_back(e);
}
} // else if
else if(temp != "" && (numberFound(temp) + 1)) // ^#^ case. Number found somewhere in priority, not ^# case. NumberFound returns -1 if no number found, +1 is 0 = false, quick maffs. We also want 0 to count as finding a number
{
//cout << temp << endl;
char strDaysTemp[2000];
string strDays;
int pos;
int digits;
ep = new Element(true, 0);
e = *ep;
if(inputLine.length() >= j+2) // taking care of case where there is no task description (4 more cases of this follow in this function)
e.taskDescr = inputLine.substr(j+2); // +2 because it is on ')', and the next char is a space
else
e.taskDescr = "";
e.numDays = numberFound(temp);
sprintf(strDaysTemp, "%d", e.numDays); // convert int to string
strDays = (string)strDaysTemp;
pos = temp.find(strDays); // find the position of the integer in the priority string
//cout << "pos is " << pos << endl;
digits = floor(logBase10(e.numDays)) + 1;
//cout << "digits is " << digits << endl;
e.priority = temp.erase(pos,digits); // delete integer in the string
//cout << temp << endl;
e.breakPos = pos; // position needs to be saved, so we can print it correctly later
//cout << "pushback2" << endl;
//arr.push_back(e);
//cout << e.priority << endl;
//cout << arr.size() << endl;
//cout << pos << endl;
if(pos > 0 && pos-1 < e.priority.length() && e.priority[pos-1] == '-') // need to extract the negative
{
e.numDays *= -1;
e.breakPos -= 1;
e.priority = e.priority.erase(pos-1, 1);
} // if -number case
} // else if
else // pure string priority
{
// create new element with other priority and the description found in the rest of inputLine
ep = new Element(1);
e = *ep;
if(inputLine.length() >= j+2) // taking care of case where there is no task description (4 more cases of this follow in this function)
e.taskDescr = inputLine.substr(j+2); // +2 because it is on ')', and the next char is a space
else
e.taskDescr = "";
e.priority = temp;
//cout << "pushback3" << endl;
//arr.push_back(e);
} // else
e.prefix = prefix;
arr.push_back(e);
} // if
} // while
infile.close();
//cout << "Priority of element 3 is: " << arr[3].priority << endl; // has the right priority
//cout << "Counted " << newLineCtr << " new lines" << endl;
cout << "Finished file import" << endl;
if(oldFile) // prompt user to update the schedule if old file input
{
int choice;
cout << "This file is " << dateDiff << " days old." << endl;
cout << "Would you like to update the priorities?" << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
cin >> choice;
if (choice == 1)
{
updateSched(dateDiff);
cout << "Updated the priorities." << endl;
}
}
} // importFile
// Displays the item numbers
void displayItemNumbers()
{
// need to update the number of days each time its printed if the day has changed
now = time(0); // get current time
ltm = localtime(&now); // How exactly does this work?
dd = ltm->tm_mday;
mm = ltm->tm_mon + 1;
yyyy = ltm->tm_year + 1900;
if(currentDay != dd || currentMonth != mm || currentYear != yyyy)
{
updateSched(computejdn(dd, mm, yyyy) - computejdn(currentDay, currentMonth, currentYear));
currentDay = dd;
currentMonth = mm;
currentYear = yyyy;
}
cout << "\n\n\n\n" << "*******************SCHEDULE*************************\n\n" << endl;
int i;
for(i = 0; i < arr.size(); i++)
{
cout << i << ": "; // print number, then call print function
arr[i].print();
}
cout << "\n\n\n\n\n\n\n\n" << endl;
//cout << i << endl;
//cout << "The last element in the array is ";
//arr[i-1].print();
//cout << arr[i-1].isNewLine() << endl;
} // displayItemNumbers()
// Update everything, then insert, respecting order of numbers
void insertNumPriority(Element e)
{
int inserted = 0;
if(!arr.empty())
{
for(int i = 0; i < arr.size(); i++)
{
if(arr[i].hasNumPriority && arr[i].numDays > e.numDays) // if tempdays casted to an int is bigger than numDays, place element here (before)
{
arr.insert(arr.begin() + i, e);// place here and move everything else back one
inserted = 1;
break;
}
}
}
else
{
arr.push_back(e);
inserted = 1;
}
if(inserted == 0)
arr.push_back(e);
}
// add ^#^ element to its appropriate spot based on numDays
void addStrNumElement(string priority, int numDays, string descr, int breakPos)
{
//ep =
} // addStrNumElement()
// add ^# element to its appropriate spot based on numDays
void addCharNumElement(string priority, int numDays, string descr)
{
ep = new Element("s");
e = *ep;
e.priority = priority;
e.taskDescr = descr;
e.numDays = numDays;
insertNumPriority(e);
} // addCharNumElement()
// Currently adds a pure string element to the top of the schedule
void addCharElement(string priority, string descr)
{
ep = new Element(1);
e = *ep;
e.priority = priority;
e.taskDescr = descr;
arr.insert(arr.begin(), e);
} // addCharElement()
void refreshSchedule()
{
now = time(0); // get current time
ltm = localtime(&now);
dd = ltm->tm_mday;
mm = ltm->tm_mon + 1;
yyyy = ltm->tm_year + 1900;
if(currentDay != dd || currentMonth != mm || currentYear != yyyy)
{
updateSched(computejdn(dd, mm, yyyy) - computejdn(currentDay, currentMonth, currentYear));
currentDay = dd;
currentMonth = mm;
currentYear = yyyy;
} // if
} // refreshSchedule()
int main(int argc, char* argv[])
{
/*cout << argv[0] << endl;
cout << argv[1] << endl;
cout << argc << endl;
cout << argv[1] << endl;
cout << (string)argv[1] << endl;*/
bool importedFile = false;
if(argc == 2) // if a file was provided, import it
{
importFile((string)argv[1]);
importedFile = true;
}
//vector<string> arr; // array of user's to-do items
// can calculate the day that this started running, then keep track of this day and update as required, save into file
//before shutdown. Will be a glitch between days, but when printing out, it should be fine because it will update
//time_t now;
//tm *ltm;
now = time(0); // get current time
ltm = localtime(&now);
currentDay = ltm->tm_mday; // These are set when main program is run
currentMonth = ltm->tm_mon + 1;
currentYear = ltm->tm_year + 1900;
ofstream outputFile;
while (1){ // main loop of program
// do everything after this
string commandStr = ""; // store user input
int command;
int dueDate;
string dueDateStr;
string taskDescr;
string fullTaskDescr;
string element;
string dinput;
string uinput;
//int dd;
//int mm;
//int yyyy;
int DD;
int MM;
int YYYY;
string numDays;
bool leadingzero;
int a;
int y;
int m;
int jdnstart;
int jdnend;
char temp[2000];
int inserted;
bool deleted;
bool charnum;
int curJulianDay;
int updateDays;
int itemNum;
int newLineCtr;
string restDescr;
bool firstHeader;
// while the command is not a number, display main menu and ask for input
while(!isNumber(commandStr))
{
cout << "\nMain Menu\n1) Display Schedule\n2) Add Item\n3) Remove Item\n4) Update Item\n5) Update Schedule\n6) Add/Change Prefix\n7) Calculate Julian Difference\n8) Add or Remove Linebreak" << endl;
cin >> commandStr;
if(!isNumber(commandStr))
cout << "Error: Please supply a correct integer between 1 and 8.\n" << endl;
} // while
//cout << "\nMain Menu\n1) Display Schedule\n2) Add Item\n3) Remove Item\n4) Update Item\n5) Update Schedule\n6) Add/Change Prefix\n7) Calculate Julian Difference\n8) Add or Remove Linebreak" << endl;
//cin >> command;
// convert command into an int
stringstream geek(commandStr); // convert string to int
geek >> command;
switch ( command )
{
case 1:
// printing it out (for now) will be the only time the data is written to file
// need to update the number of days each time its printed if the day has changed
now = time(0); // get current time
ltm = localtime(&now); // How exactly does this work?
dd = ltm->tm_mday; // lower case is set when any relevant command is called
mm = ltm->tm_mon + 1;
yyyy = ltm->tm_year + 1900;
if(currentDay != dd || currentMonth != mm || currentYear != yyyy)
{
updateSched(computejdn(dd, mm, yyyy) - computejdn(currentDay, currentMonth, currentYear));
currentDay = dd;
currentMonth = mm;
currentYear = yyyy;
}
outputFile.open("schedule.txt");
cout << "\n\n\n\n" << "*******************SCHEDULE*************************" << "\n" << (curJulianDay = computejdn(currentDay,currentMonth,currentYear)) << /*"\n" // have a new line and then the date?*/ " (" << currentMonth << "/" << currentDay << "/" << currentYear << ")" << "\n\n" << endl; // for the legacy. Change this and next line for desired date display
outputFile /*<< "\n\n\n\n" */<< "*******************SCHEDULE*************************" << "\n" << curJulianDay << /*"\n" // have a new line and then the date?*/ " (" << currentMonth << "/" << currentDay << "/" << currentYear << ")" << "\n" << endl; // change this line for desired date display (in file)
//if(!importedFile) // We want to control how the schedule is formatted, so we don't need this right now
// outputFile << "\n\n" << endl; // these two are extra newlines if file was imported
newLineCtr = 0;
firstHeader = false;
for(int i = 0; i < arr.size(); i++)
{
if(arr[i].isNewLine())
{
cout << "\n";
outputFile << endl;
newLineCtr++;
//if(!firstHeader)
//cout << "Printed " << newLineCtr << " newlines before first header" << endl;
} // if
else if(arr[i].isHeaderFun())
{
cout << arr[i].taskDescr << endl;
outputFile << "\n" << arr[i].taskDescr; //<< endl; // try printing newlines first to avoid having an extra line at bottom
firstHeader = true;
}
else if(arr[i].hasStringNumPriority && arr[i].hasNumPriority) // ^#^ case
{
cout << arr[i].prefix << "(" << (arr[i].priority).substr(0,arr[i].breakPos) << arr[i].numDays << (arr[i].priority).substr(arr[i].breakPos) << ") " << arr[i].taskDescr << endl; // print priority, number of days, then task description in proper format
outputFile << "\n" << arr[i].prefix << "(" << (arr[i].priority).substr(0,arr[i].breakPos) << arr[i].numDays << (arr[i].priority).substr(arr[i].breakPos) << ") " << arr[i].taskDescr; // << endl;
//cout << "BreakPos is " << arr[i].breakPos << endl;
//cout << "priority is " << arr[i].priority << endl;
} // else if
else if(arr[i].hasCharNumPriority && arr[i].hasNumPriority) // ^# case **can make this come after the current else if and only check if it has CharNumPriority
{
cout << arr[i].prefix << "(" << arr[i].priority << arr[i].numDays << ") " << arr[i].taskDescr << endl; // print priority, number of days, then task description in proper format
outputFile << "\n" << arr[i].prefix << "(" << arr[i].priority << arr[i].numDays << ") " << arr[i].taskDescr; // << endl;
} // else if
else if(!arr[i].hasNumPriority)
{
cout << arr[i].prefix << "(" << arr[i].priority << ") " << arr[i].taskDescr << endl; // pure string case. print priority then task description in proper format
outputFile << "\n" << arr[i].prefix << "(" << arr[i].priority << ") " << arr[i].taskDescr; // << endl;
} // else if
else
{
cout << arr[i].prefix << "(" << arr[i].numDays << ") " << arr[i].taskDescr << endl; // pure number case. print number of days, then task description in proper format
outputFile << "\n" << arr[i].prefix << "(" << arr[i].numDays << ") " << arr[i].taskDescr; // << endl;
} // else
} // for
cout << "\n\n\n\n\n\n\n\n" << endl;
//outputFile << "\n\n\n\n\n\n\n\n" << endl; dont think this is needed
outputFile.close();
//cout << "Exported " << newLineCtr << " new lines" << endl;
//cout << "size after the print: " << arr.size() << endl;
break;
case 2:
cout << "Enter Description of Task" << endl;
cin >> taskDescr; // get the line up to a whitespace
getline(cin, restDescr); // get the rest of the line
taskDescr = taskDescr + restDescr; // concatenate together
cout << "Input due date of task in format DDMMYYYY or a desription of the priority" << endl;
cin >> dueDateStr;
getline(cin, restDescr);
dueDateStr = dueDateStr + restDescr;
now = time(0); // get current time
ltm = localtime(&now); // How exactly does this work?
dd = ltm->tm_mday;
mm = ltm->tm_mon + 1;
yyyy = ltm->tm_year + 1900;
// RACE CONDITION POTENTIAL if this runs before item was inserted -> actually only the date when calculations happen is important
// this makes everything in terms of the date we just calculated
// Dates are determined by moment of user action
if(currentDay != dd || currentMonth != mm || currentYear != yyyy)
{
updateSched(computejdn(dd, mm, yyyy) - computejdn(currentDay, currentMonth, currentYear));
currentDay = dd;
currentMonth = mm;
currentYear = yyyy;
} // ehhh just set current times when starts, and update as you go through? if nothing entered, update doesnt do anything -> fine
charnum = false;
// dueDate is a string, check if it is a ^# or pure string case (that is, if it is not a pure number)
if(!isNumber(dueDateStr))
{
//cout << "determined its not a number" << endl;
charnum = true;
// dueDate is not a string, so check if it is ^# case. If not, it is a pure string
if(dueDateStr.length() > 1 && isNumber(dueDateStr.substr(1))) // ^# case // but ^# case wouldn't be a due date, it would be a different priority
{
//cout << "mistakenly thought it was a number" << endl;
// convert dueDateStr.substr(1) into an int, assign this to a new element's numdays, assign dueDateStr[0] to e's priority, and rest to descr
stringstream geek(dueDateStr.substr(1)); // convert string to int
geek >> dueDate; // dueDate actually contains what should be numDays
addCharNumElement(dueDateStr.substr(0,1), dueDate, taskDescr);
cout << "Element added\n" << endl;
break; // breaks out of this insertion request
}
else if(numberFound(dueDateStr)+1) // ^#^ case
{
// extract number and string // NOTE: dueDateStr is everything that would show up in (...)
char strDaysTemp[2000];
string strDays;
int pos;
int digits;
ep = new Element(true, 0);
e = *ep;
e.taskDescr = taskDescr;
e.numDays = numberFound(dueDateStr);
sprintf(strDaysTemp, "%d", e.numDays); // convert int to string
strDays = (string)strDaysTemp;
pos = dueDateStr.find(strDays); // find the position of the integer in the priority string
//cout << "pos is " << pos << endl;
digits = floor(logBase10(e.numDays)) + 1;
//cout << "digits is " << digits << endl;
e.priority = dueDateStr.erase(pos,digits); // delete integer in the string
//cout << temp << endl;
e.breakPos = pos; // position needs to be saved, so we can print it correctly later
//cout << "pushback2" << endl;
//arr.push_back(e);
//cout << e.priority << endl;
//cout << arr.size() << endl;
//cout << pos << endl;
if(pos > 0 && pos-1 < e.priority.length() && e.priority[pos-1] == '-') // need to extract the negative
{
e.numDays *= -1;
e.breakPos -= 1;
e.priority = e.priority.erase(pos-1, 1);
} // if -number case
insertNumPriority(e);
cout << "Element added\n" << endl;
break;
} // else if
else // pure string case
{
addCharElement(dueDateStr, taskDescr);
cout << "Element added\n" << endl;
break;
}
} // if
// convert dueDateStr to int if we didnt partially already above in the ^# case
if(!charnum){
stringstream geek(dueDateStr); // convert string to int
geek >> dueDate;
}
// convert current time to julian day
jdnstart = computejdn(dd, mm, yyyy);
DD = dueDate / 1000000; // uppercase is due date, calculated when provided
MM = (dueDate % 1000000) / 10000;
YYYY = dueDate % 10000;
jdnend = computejdn(DD, MM, YYYY);
sprintf(temp, "%d", jdnend - jdnstart); // convert int to string
numDays = (string)temp; // temp is char*, needs to be casted to string
element = "(" + numDays + ") " + taskDescr;
ep = new Element();
e = *ep;
e.taskDescr = taskDescr;
e.numDays = jdnend - jdnstart;
if(charnum) // ^# case
{
e.priority = dueDateStr.substr(0,1);
e.hasCharNumPriority = true;
}
//arr.push_back(element);
inserted = 0;
if(!arr.empty())
{
for(int i = 0; i < arr.size(); i++)
{
// for each string in arr, extract the nuDays
/*string tempdays = "";
int j = 1;
while(arr[i].substr(j,1).compare(")") != 0)
{
tempdays += arr[i].substr(j,1);
j++;
}
stringstream geek(tempdays); // convert string to int
int x; // ""
geek >> x; // ""
cout << x << endl;*/
if(arr[i].hasNumPriority && arr[i].numDays > jdnend - jdnstart) // if tempdays casted to an int is bigger than numDays, place element here (before)
{
arr.insert(arr.begin() + i, e);// place here and move everything else back one
inserted = 1;
cout << "Element added\n" << endl;
break;
}
}
}
else
{
//cout << "pushback4" << endl;
arr.push_back(e);
inserted = 1;
cout << "Element added\n" << endl;
}
if(inserted == 0)
//cout << "pushback5" << endl;
arr.push_back(e);
cout << "Element added\n" << endl;
//sort(arr.begin(), arr.end());
break;
case 3:
// Ask user what item he/she wants removed, and remove it
deleted = false;
while(deleted == false)
{
cout << "Which item number would you like deleted? (Enter 'd' to display item numbers)" << endl;
cin >> dinput;
while(dinput == "d")
{
displayItemNumbers();
cout << "Which item number would you like deleted? (Enter 'd' to display item numbers)" << endl;
cin >> dinput;
} // while