-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.cpp
More file actions
72 lines (61 loc) · 2.27 KB
/
files.cpp
File metadata and controls
72 lines (61 loc) · 2.27 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
#include "files.hpp"
namespace rcl{
void load_file(std::vector<std::string>& buffer,const std::string& filename){
std::fstream file(filename,std::fstream::in);
while (!file.eof()){
std::string line;
std::getline(file,line);
if (line.length()!=0) buffer.push_back(line);
}
return;
}
void load_file(std::vector<unsigned char>& buffer, const std::string& filename){
std::fstream file(filename,std::fstream::in|std::fstream::binary);
file.seekg(0,std::fstream::end);
size_t size = file.tellg();
buffer.resize(size);
file.seekg(0);
file.read(reinterpret_cast<char*>(&buffer[0]),size);
return;
}
void save_file(const std::string& filename,const std::vector<unsigned char>& buffer){
std::fstream file(filename,std::fstream::out|std::fstream::binary);
file.write(reinterpret_cast<const char*>(&buffer[0]),buffer.size());
return;
}
void save_file(const std::string& filename,const std::vector<std::string>& buffer){
std::fstream file(filename,std::fstream::out|std::fstream::binary);
for (unsigned int idx=0;idx<buffer.size();idx++)
file << buffer[idx] << std::endl;
return;
}
void append_to_file(const std::string& filename,std::vector<unsigned char>& buffer){
std::fstream file(filename,std::fstream::binary|std::fstream::app);
file.write(reinterpret_cast<char*>(&buffer[0]),buffer.size());
return;
}
bool check_file(const std::string& filename){
return std::ifstream(filename).good();
}
void save_lock(const std::string& filename){
const unsigned char locksig[8]={0x2c,0xe0,0x8a,0x10,0x3f,0x85,0x20,0xb1};
std::fstream file(filename,std::fstream::out|std::fstream::binary);
file.write(reinterpret_cast<const char*>(locksig),8);
return;
}
bool is_lock(const std::string& filename){
const unsigned char locksig[8]={0x2c,0xe0,0x8a,0x10,0x3f,0x85,0x20,0xb1};
char lockread[8];
std::ifstream file(filename,std::fstream::in|std::fstream::binary);
if (!file.good()) return false;
file.read(lockread,8);
if (!file.good()) return false;
if (reinterpret_cast<const unsigned long long int&>(*lockread)!=reinterpret_cast<const unsigned long long int&>(*locksig)) return false;
return true;
}
std::vector<unsigned char> load_file(const std::string& filename){
std::vector<unsigned char> ans;
load_file(ans,filename);
return ans;
}
}