/
main.cpp
137 lines (113 loc) · 2.83 KB
/
main.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
//------------------------------------------------------------------------------
// main.cpp
//
// CSED353
//
// Paul Schreiner
// TODO: Add your name here.
//------------------------------------------------------------------------------
//
#include "Event.h"
#include "Calendar.h"
#include "Servermode.h"
#include <iostream>
#include <string.h>
#include <fstream>
#include <unistd.h>
#include <cstdlib>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
using std::cout;
using std::endl;
using std::list;
using std::string;
using std::ifstream;
//--------------------------------------------------------------------------
// Main Function
//
int main(int argc, char *argv[])
{
if (argc > 2)
{
cout << "Usage: Start with ./cnw [filename] to load calendar "
<< "or ./cnw to make new calendar." << endl;
}
// New Calendar is created.
if (argc == 1)
{
tm* begin1 = new tm;
begin1->tm_hour = 9;
begin1->tm_min = 20;
begin1->tm_mday = 10;
begin1->tm_mon = 6 - 1;
begin1->tm_year = 2015 - 1900;
Event* event1 = new Event("Presentation", begin1, 30);
list<Event*> calendar_queue;
Calendar calendar(calendar_queue);
calendar.addEvent(event1);
calendar.setConnectionState(false);
calendar.run();
return SUCCESS;
}
if (strcmp("-s", argv[1]) == 0)
{
//Server mode
cout << "You've entered server mode!" << endl;
runServer();
return SUCCESS;
}
// Else load calendar
string filename = argv[1];
ifstream file;
file.open(filename.c_str(), std::ios::in | std::ios::binary);
if (!file.is_open())
{
std::cout << "Error: file " << filename << " cannot be opened.\n";
return ERROR;
}
char check_input[50];
for (int i = 0; i < 50; i++)
check_input[i] = 0;
file.read(check_input, 4);
if (!(strcmp(check_input, "CNW\0") == 0))
{
cout << "File corrupted!" << endl;
return ERROR;
}
int num_events;
file.read((char*) &num_events, sizeof(int));
list<Event*> calendar_queue;
string event_name;
short duration;
for (int i = 0; i < num_events; i++)
{
Event* event = new Event();
tm* time = new tm();
int tm_data;
getline(file, event_name, '\0');
event->setName(event_name);
file.read((char*) &tm_data, sizeof(int));
time->tm_mday = tm_data;
file.read((char*) &tm_data, sizeof(int));
time->tm_mon = tm_data;
file.read((char*) &tm_data, sizeof(int));
time->tm_year = tm_data;
file.read((char*) &tm_data, sizeof(int));
time->tm_hour = tm_data;
file.read((char*) &tm_data, sizeof(int));
time->tm_min = tm_data;
file.read((char*) &tm_data, sizeof(int));
time->tm_sec = tm_data;
file.read((char*) &duration, sizeof(short));
event->setStartTime(time);
event->setDuration(duration);
calendar_queue.push_back(event);
}
Calendar calendar(calendar_queue);
calendar.setConnectionState(false);
calendar.run();
return SUCCESS;
}