-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
114 lines (99 loc) · 2.62 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
#include <iostream>
#include <fstream>
#include "visitInfo.pb.h"
enum OS
{
LINUX = 0,
MACOS = 1,
WINDOWS = 2,
NULL_OS = 3
};
#if defined(_WIN32)
#define PLATFORM_NAME OS::WINDOWS // Windows
#elif defined(_WIN64)
#define PLATFORM_NAME OS::WINDOWS // Windows
#elif defined(__linux__)
#define PLATFORM_NAME OS::LINUX // Debian, Ubuntu, Gentoo, Fedora, openSUSE, RedHat, Centos and other
#elif defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin)
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR == 1
#define PLATFORM_NAME OS::NULL_OS // Apple iOS
#elif TARGET_OS_IPHONE == 1
#define PLATFORM_NAME OS::NULL_OS // Apple iOS
#elif TARGET_OS_MAC == 1
#define PLATFORM_NAME OS::MACOS // Apple OSX
#endif
#else
#define PLATFORM_NAME OS::NULL_OS
#endif
using namespace::std;
void fillTheVisit(auth::AuthUser* user)
{
cout<< "Enter name of person: ";
getline(cin, *user->mutable_name());
cout<<"Enter browser: ";
getline(cin, *user->mutable_browser());
while(true)
{
cout<<"Enter ip (or blank to finish): ";
string ipes;
getline(cin, ipes);
if(ipes.empty())
{
break;
}
user->add_ip(ipes);
switch (PLATFORM_NAME) {
case OS::WINDOWS:
user->set_system(auth::AuthUser::WINDOWS);
break;
case OS::LINUX:
user->set_system(auth::AuthUser::LINUX);
break;
case OS::MACOS:
user->set_system(auth::AuthUser::MACOS);
break;
case OS::NULL_OS:
user->set_system(auth::AuthUser::NULL_OS);
break;
default:
user->set_system(auth::AuthUser::NULL_OS);
break;
}
}
}
// Main function: Reads the entire auth list from a file,
// adds one user based on user input, then writes it back out to the same
// file.
int main(int argc, char* argv[]) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
// if (argc != 2) {
// cerr << "Usage: " << argv[0] << " AUTH_FILE" << endl;
// return -1;
// }
auth::AuthCheck auth_check;
{
// Read the existing auth list.
fstream input(argv[1], ios::in | ios::binary);
if (!input) {
cout << argv[1] << ": File not found. Creating a new file." << endl;
}
else if (!auth_check.ParseFromIstream(&input)) {
cerr << "Failed to parse auth list." << endl;
return -1;
}
}
// Add an address.
fillTheVisit(auth_check.add_users());
{
// Write the new auth list back to disk.
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!auth_check.SerializeToOstream(&output)) {
cerr << "Failed to write auth list." << endl;
return -1;
}
}
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return 0;
}