-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cc
More file actions
82 lines (62 loc) · 1.97 KB
/
File.cc
File metadata and controls
82 lines (62 loc) · 1.97 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
73
74
75
76
77
78
79
80
81
82
/*******************************************************************************
Author: Alexey Frolov (alexwin32@mail.ru)
This software is distributed freely under the terms of the MIT License.
See "LICENSE" or "http://copyfree.org/content/standard/licenses/mit/license.txt".
*******************************************************************************/
#include <File.h>
#include <limits>
#ifndef WIN32
#include <codecvt>
#endif
#include <stdint.h>
#if defined(_MSC_VER) && (_MSC_VER == 1800)
#define noexcept throw()
#endif
namespace File
{
class Guard
{
private:
FILE *file = NULL;
public:
Guard(const Guard&) = delete;
Guard &operator=(const Guard&) = delete;
Guard(FILE *File) : file(File){}
FILE *Get() const noexcept {return file;}
~Guard(){fclose(file);}
};
static FILE * Open(const std::wstring &Path, const std::wstring &&Mode)
{
FILE *outFile;
#ifdef WIN32
if(_wfopen_s(&outFile, Path.c_str(), Mode.c_str()))
throw IOException(L"Fail to open" + Path);
#else
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
if(fopen(converter.to_bytes(Path),"r"))
throw IOException(L"Fail to open" + Path);
#endif
return outFile;
}
Data Read(const std::wstring &Path)
{
File::Guard file = Open(Path, L"rb");
fseek(file.Get(), 0, SEEK_END);
int64_t size = ftell(file.Get());
fseek(file.Get(), 0, SEEK_SET);
if(size > std::numeric_limits<Data::size_type>::max())
throw IOException(L"File is too big");
Data data(size);
size_t rSize = fread(&data[0], sizeof(Data::value_type), size, file.Get());
if(rSize != size)
throw IOException(L"Cant read from file");
return data;
}
void Rewrite(const std::wstring &Path, const Data &FileData)
{
File::Guard file = Open(Path, L"wb");
size_t nWrite = fwrite(&FileData[0], sizeof(Data::value_type), FileData.size(), file.Get());
if(nWrite != FileData.size())
throw IOException(L"Cant write to file");
}
}