forked from vencabkk/opencl-resizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO.cpp
More file actions
executable file
·65 lines (54 loc) · 1.62 KB
/
IO.cpp
File metadata and controls
executable file
·65 lines (54 loc) · 1.62 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
#include "IO.h"
#include <string>
#include <vector>
#include <cassert>
#if defined(__APPLE__) || defined(__MACOSX)
#include <fstream>
#elif defined(WIN32)
#include <windows.h>
#endif
bool IO::readBinaryFile(const std::string& fileName, std::vector<unsigned char>& data)
{
#if defined(WIN32)
DWORD dwBytesRead = 0;
HANDLE hFile = CreateFile(fileName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
unsigned long size = GetFileSize(hFile, NULL);
data.resize(size);
return ReadFile(hFile, (char*)(&(data)[0]), size, &dwBytesRead, NULL);
#elif defined(__APPLE__) || defined(__MACOSX)
FILE* file = std::fopen(fileName.c_str(), "rb");
if (file)
{
fseek(file, 0, SEEK_END);
long size = ftell(file);
rewind(file);
data.resize(size);
fread((char*)(&(data)[0]), 1, size, file);
fclose(file);
return true;
}
return false;
#endif
}
bool IO::writeBinaryFile(const std::string& fileName, const std::vector<unsigned char>& data, unsigned size)
{
unsigned long length (data.size());
if (size > 0)
{
length = size;
}
#if defined(WIN32)
DWORD dwBytesWritten = 0;
HANDLE hFile = CreateFile(fileName.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
return WriteFile(hFile, (char*)(&(data)[0]), length, &dwBytesWritten, NULL);
#elif defined(__APPLE__) || defined(__MACOSX)
FILE* file = std::fopen(fileName.c_str(), "wb");
if (file)
{
fwrite((char*)(&(data)[0]), 1, length, file);
fclose(file);
return true;
}
return false;
#endif
}