-
Notifications
You must be signed in to change notification settings - Fork 200
/
FileHelper.h
106 lines (96 loc) · 3.81 KB
/
FileHelper.h
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
#pragma once
#include <cstdio>
#include <stdexcept>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
// give it filepaths with '/', and it will replace them with \\, if WIN32 is defined (ie, on Windows)
class FileHelper {
public:
static char *readBinary( std::string filepath, long *p_filesize ) {
std::string localPath = localizePath( filepath );
std::ifstream file( localPath.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
if(!file.is_open()) {
throw std::runtime_error("couldnt open file " + localPath);
}
*p_filesize = static_cast<long>( file.tellg() );
std::cout << " filesize " << *p_filesize << std::endl;
char *data = new char[*p_filesize];
file.seekg(0, std::ios::beg);
if(!file.read( data, *p_filesize )) {
throw std::runtime_error("failed to read from " + localPath );
}
file.close();
return data;
}
static long getFilesize( std::string filepath ) {
std::ifstream in( localizePath( filepath ).c_str(), std::ifstream::ate | std::ifstream::binary);
return static_cast<long>( in.tellg() );
}
static char *readBinaryChunk( std::string filepath, long start, long length ) {
std::string localPath = localizePath( filepath );
std::ifstream file( localPath.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
if(!file.is_open()) {
throw std::runtime_error("failed to open file: " + localPath);
}
file.seekg( start, std::ios::beg );
char *data = new char[length];
if(!file.read( data, length )) {
throw std::runtime_error("failed to read from " + localPath );
}
file.close();
return data;
}
// need to allocate targetArray yourself, beforehand
static void readBinaryChunk( char *targetArray, std::string filepath, long start, long length ) {
std::string localPath = localizePath( filepath );
std::ifstream file( localPath.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
if(!file.is_open()) {
throw std::runtime_error("failed to open file: " + localPath);
}
file.seekg( start, std::ios::beg );
// char *data = new char[length];
if(!file.read( targetArray, length )) {
throw std::runtime_error("failed to read from " + localPath );
}
file.close();
// return data;
}
static void writeBinary( std::string filepath, char*data, long filesize ) {
std::string localPath = localizePath( filepath );
std::ofstream file( localPath.c_str(), std::ios::out | std::ios::binary );
if(!file.is_open()) {
throw std::runtime_error("cannot open file " + localPath );
}
if( !file.write( (char *)data, filesize ) ) {
throw std::runtime_error("failed to write to " + localPath );
}
file.close();
}
static bool exists( const std::string filepath ) {
std::string localPath = localizePath( filepath );
std::ifstream testifstream( localPath.c_str() );
bool exists = testifstream.good();
testifstream.close();
return exists;
}
static void rename( std::string oldname, std::string newname ) {
::rename( localizePath( oldname ).c_str(), localizePath( newname ).c_str() );
}
static void remove( std::string filename ) {
::remove( localizePath( filename ).c_str() );
}
static std::string localizePath( std::string path ) {
std::replace( path.begin(), path.end(), '/', pathSeparator().c_str()[0] );
//std::cout << "localized path: " << path << std::endl;
return path;
}
static std::string pathSeparator() {
#ifdef _WIN32
return "\\";
#else
return "/";
#endif
}
};