-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlicense.cpp
More file actions
135 lines (105 loc) · 3.09 KB
/
license.cpp
File metadata and controls
135 lines (105 loc) · 3.09 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
#include "license.h"
License::License(const std::string& email, std::fstream* lFile, std::fstream* aFile)
{
m_Email = email;
m_lFile = lFile;
m_aFile = aFile;
std::cout << "License object created!\n";
}
License::~License()
{
m_lFile->close();
m_aFile->close();
m_lFile = nullptr;
m_aFile = nullptr;
}
void License::show_licenses()
{
if (!m_lFile->is_open()) m_lFile->open("licenses.txt", std::fstream::in | std::fstream::app);
bool present = false;
c2 = "---------------------------------------\n";
while (std::getline(*m_lFile, c1))
{
if ((pos = c1.find(':')) != std::string::npos && c1.substr(pos + 1) == m_Email)
{
if (!present) present = true;
c2 += "|";
c2 += c1.substr(0, pos);
c2 += "|";
c2 += "\n";
}
}
if (present)
c2 += "---------------------------------------\n";
else
c2 = "\nNo licenses present! Create one.\n";
reset_filepos();
std::cout << c2 << "\n";
}
std::string License::create_license()
{
if (!m_lFile->is_open()) m_lFile->open("licenses.txt", std::fstream::in | std::fstream::app);
std::string uuid = Tools::generate_uuid_v4();
*m_lFile << uuid << ":" << m_Email << "\n";
std::cout << uuid << std::endl;
m_lFile->close();
return uuid;
}
bool License::delete_license()
{
if (!m_lFile->is_open()) m_lFile->open("licenses.txt", std::fstream::in | std::fstream::app);
std::ofstream tempf;
tempf.open("temp.txt", std::ofstream::out);
if (!tempf.is_open())
{
std::cerr << "File IO is closed!";
exit(-1);
}
while (std::getline(*m_lFile, c1))
{
if ((pos = c1.find(':')) != std::string::npos && c1.substr(pos+1) == m_Email)
continue;
else
tempf << c1 << "\n";
}
// Clears bit to 'good' state after reading operation, so we can remove the file. Otherwise no perms error as file handle is open
reset_filepos();
m_lFile->close();
tempf.close();
std::remove("licenses.txt");
std::rename("temp.txt", "licenses.txt");
return true;
}
void License::validate_license()
{
if (!m_lFile->is_open()) m_lFile->open("licenses.txt", std::fstream::in | std::fstream::app);
bool found_license = false;
while (true)
{
std::cout << "\nEnter your license: ";
std::getline(std::cin, c1);
while (std::getline(*m_lFile, c1))
{
if ((pos = c1.find(':')) != std::string::npos && (c1.substr(pos + 1)) == m_Email && (c1 = c1.substr(0, pos)) == c2)
{
found_license = true;
break;
}
}
reset_filepos();
if (found_license)
{
std::cout << "Your active license found - " << c1 << "\n";
break;
}
else
{
std::cerr << "\nInvalid license.\n";
}
}
}
void License::reset_filepos(std::ios_base::iostate state, std::streamoff seekpos, std::ios_base::seekdir way)
{
m_lFile->clear(state);
m_lFile->seekg(0, std::ios::beg);
}