-
Notifications
You must be signed in to change notification settings - Fork 5
/
file.cpp
107 lines (91 loc) · 2.62 KB
/
file.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
/*-----------------------------------------------------------------------------
File.cpp
Various useful file i/o functions.
(c) 2013 Shamus Young
-----------------------------------------------------------------------------*/
#include "master.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void FileDelete (string filename)
{
_unlink (filename.c_str ());
}
bool FileExists(string filename)
{
return exists(filename);
}
bool FileSave (string filename, const char *buf, int size)
{
std::ofstream ofs(filename.c_str(), std::ofstream::binary | std::ofstream::out | std::ofstream::trunc);
//ofs.open (filename, );
if (!ofs.is_open())
return false;
ofs.write(buf, size);
ofs.close();
return true;
}
string FileContents(string filename)
{
std::ifstream ifs(filename.c_str());
ifs.open(filename.c_str(), std::ifstream::in);
if (!ifs.is_open())
Console("File not found: %s", filename.c_str());
string contents((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
ifs.close();
return contents;
}
char* FileContentsBinary(string filename, long* size_in)
{
std::ifstream ifs;
long size;
char* buffer;
ifs.open(filename.c_str(), std::ifstream::in | std::ifstream::binary | ios::ate);
if (!ifs.is_open())
return NULL;
size = (long)ifs.tellg();
ifs.seekg(0);
buffer = (char*)malloc(size);
ifs.read(buffer, size);
ifs.close();
if (size_in)
*size_in = size;
return buffer;
}
int FileCopy(const char* from, const char* to)
{
std::ifstream initialFile(from, ios::in | ios::binary);
std::ofstream outputFile(to, ios::out | ios::binary);
//defines the size of the buffer
initialFile.seekg(0, ios::end);
long fileSize = (long)initialFile.tellg();
//Requests the buffer of the predefined size
//As long as both the input and output files are open...
if (initialFile.is_open() && outputFile.is_open()) {
short* buffer = new short[fileSize];
//Determine the file's size
//Then starts from the beginning
initialFile.seekg(0, ios::beg);
//Then read enough of the file to fill the buffer
initialFile.read((char*)buffer, fileSize);
//And then write out all that was read
outputFile.write((char*)buffer, fileSize);
delete[] buffer;
}
else if (!outputFile.is_open()) {
Console("FileCopy: Failed to open '%s'.", to);
return 0;
}
else if (!initialFile.is_open()) {
Console("FileCopy: Failed to open '%s'.", from);
return 0;
}
initialFile.close();
outputFile.close();
return 1;
}
bool FileDelete(char* name)
{
if (!_unlink(name))
return true;
return false;
}