-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.cpp
71 lines (58 loc) · 1.71 KB
/
Utils.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
#include "Utils.h"
#include <iostream>
namespace Logger
{
void PrintCannotOpenFile(std::wstring strFname)
{
wprintf(L"Cannot open %s file!\n", strFname.c_str());
}
void PrintErrorTransformingFile(size_t numRead, size_t numWritten)
{
std::wcout << L"Problem in transforming the file: " << numRead << L" read vs " << numWritten << L" written!\n";
}
void PrintTransformSummary(size_t blockCount, size_t blockSizeInBytes, std::wstring strFirstFile, std::wstring strSecondFile)
{
std::wcout << L"Transformed " << blockCount << L" blocks of " << blockSizeInBytes << L" bytes from " << strFirstFile << L" into " << strSecondFile << L"\n";
}
}
void FILEDeleter::operator()(FILE *pFile) const
{
if (pFile)
fclose(pFile);
}
FILE_unique_ptr make_fopen(const wchar_t* fname, const wchar_t* mode)
{
FILE *fileHandle = nullptr;
auto err = _wfopen_s(&fileHandle, fname, mode); // by default it's buffered IO, 4k buffer
if (err != 0)
{
Logger::PrintCannotOpenFile(fname);
return nullptr;
}
return FILE_unique_ptr(fileHandle);
}
FILE_shared_ptr make_fopen_shared(const wchar_t* fname, const wchar_t* mode)
{
FILE *fileHandle = nullptr;
auto err = _wfopen_s(&fileHandle, fname, mode); // by default it's buffered IO, 4k buffer
if (err != 0)
{
Logger::PrintCannotOpenFile(fname);
return nullptr;
}
return FILE_shared_ptr(fileHandle, FILEDeleter());
}
HANDLE_unique_ptr make_HANDLE_unique_ptr(HANDLE handle, std::wstring strMsg)
{
if (handle == INVALID_HANDLE_VALUE || handle == nullptr)
{
wprintf_s(L"Invalid handle value! %s\n", strMsg.c_str());
return nullptr;
}
return HANDLE_unique_ptr(handle);
}
void HANDLEDeleter::operator()(HANDLE handle)
{
if (handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
}