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
|
#ifndef BASE64_HELPER_HPP
#define BASE64_HELPER_HPP
#include <fstream>
#include <iostream>
using std::numeric_limits;
using std::streamsize;
constexpr size_t MAX_STREAM_SIZE = numeric_limits<streamsize>::max();
inline const size_t& SizeOfFile(const char* fileName)
{
std::fstream MyFile(fileName, std::ios::in | std::ios::binary | std::ios::ate);
std::streampos Sizer;
if (MyFile.is_open())
{
Sizer = MyFile.tellg();
const size_t size = Sizer;
MyFile.close();
return size;
}
else
{
std::cerr << "Unable to open file: " << fileName;
return 0;
}
}
inline char* ReadTextFromFile(const char* fileName, char* buffer)
{
std::ifstream File(fileName);
if (!File.is_open())
{
std::cerr << "Unable to open file for reading: " << fileName;
}
File >> buffer;
File.close();
return buffer;
}
inline char* ReadFileAsBinary(const char* fileName, char* buffer, const size_t& size)
{
try
{
std::fstream File(fileName,std::ios::in| std::ios::binary);
if (!File.is_open())
{
std::cerr << "Could not open file for binary input: " << fileName;
return buffer;
}
delete[] buffer;
buffer = nullptr;
buffer = new char[size];
File.read(buffer, size);
File.close();
return buffer;
}
catch (const std::exception& ex)
{
std::cerr << "Exception during binary file input. " << fileName
<< " was not successfully streamed to binary. " <<
ex.what();
return nullptr;
}
}
inline bool WriteFileFromBinary(const char* fileName, const char* buffer, std::string fileContents)
{
try
{
std::fstream File(fileName, std::ios::binary | std::ios::out);
if (!File.is_open())
{
std::cerr << "Could not open file for binary output: " << fileName;
}
buffer = nullptr;
buffer = fileContents.c_str();
size_t NewSize = fileContents.size() - 1;
File.write(buffer, NewSize);
File.close();
return true;
}
catch (const std::exception& ex)
{
std::cerr << "Exception during binary file input. " << fileName
<< " was not successfully streamed to binary. " <<
ex.what();
}
return false;
}
inline bool WriteTextToFile(const char* fileName, std::string fileContents)
{
std::ofstream File(fileName);
if (!File.is_open())
{
std::cerr << "Unable to open the file: " << fileName << std::endl;
return false;
}
File << fileContents;
File.close();
return false;
}
inline void Prompts(const char* Prompt)
{
std::cout << Prompt;
}
inline const char* InputFileName(const char* Prompt)
{
char* FileName = nullptr;
FileName = new char[101];
Prompts(Prompt);
std::cout.flush();
std::cin >> FileName;
while (!std::cin)
{
std::cout << Prompt << std::endl;
std::cin.clear();
std::cin.ignore(MAX_STREAM_SIZE, '\n');
std::cin >> FileName;
}
return FileName;
}
#endif
|