forked from Vicshann/Common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileStream.h
146 lines (134 loc) · 4.61 KB
/
FileStream.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once
/*
Copyright (c) 2018 Victor Sheinmann, [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef FileStrH
#define FileStrH
#include <Windows.h>
////////////#include <stdlib.h>
#include "MiniString.h"
//===========================================================================================================
class CFileStr
{
HANDLE hFile;
UINT64 SLength;
//----------------------
void Initialize(void)
{
this->hFile = INVALID_HANDLE_VALUE;
this->SLength = 0;
}
//----------------------
public:
void sAssign(const CMiniStr &str)
{
this->cAssign(str.c_str(), str.Length());
}
//----------------------
void cAssign(const char* str, int Len)
{
this->Clear();
this->cAppend(str, Len);
}
//----------------------
void iAssign(const int val)
{
this->Clear();
this->iAppend(val);
}
//----------------------
void iAssign(const unsigned long val)
{
this->Clear();
this->iAppend(val);
}
//----------------------
void sAppend(const CMiniStr &str)
{
this->cAppend(str.c_str(), str.Length());
}
//----------------------
void cAppend(const char* str, UINT Len)
{
DWORD Result;
WriteFile(this->hFile,str,Len,&Result,NULL);
this->SLength += Result;
}
//----------------------
void iAppend(const int val)
{
UINT len = 0;
char Tmpb[64];
char* res = DecNumToStrS(val, Tmpb, &len);
this->cAppend(res,len);
}
//----------------------
LPSTR iAppend(const unsigned long val)
{
int len = 0;
char Tmpb[64];
DecNumToStrU(val, Tmpb, &len);
this->cAppend((LPSTR)&Tmpb,len);
}
//----------------------
public:
//---------------------- // Instead of lambdas
CFileStr(void){this->Initialize();}
//----------------------
~CFileStr()
{
if(this->hFile != INVALID_HANDLE_VALUE)CloseHandle(this->hFile);
}
//----------------------
void Clear(void)
{
SetFilePointer(this->hFile,0,NULL,FILE_BEGIN);
SetEndOfFile(this->hFile);
this->SLength = 0;
}
//----------------------
UINT Length(void) const {return this->SLength;}
//----------------------
void operator = (const CMiniStr &str){this->sAssign(str);}
//----------------------
// void operator = (const LPSTR str){this->cAssign(str,lstrlen(str));}
//----------------------
CFileStr& operator += (const CMiniStr &str){this->sAppend(str);return *this;}
//----------------------
// CFileStr& operator += (const LPSTR str){this->cAppend(str,lstrlen(str));return *this;}
//----------------------
CFileStr& operator += (int val){this->iAppend(val);return *this;}
//----------------------
CFileStr& operator += (unsigned long val){this->iAppend(val);return *this;}
//----------------------
const CFileStr& AddChars(char val, int Count=1) // return CONST?
{
BYTE sadd[256]; // A little buffer :)
int left = (Count > sizeof(sadd))?(sizeof(sadd)):(Count);
memset(&sadd,val,left);
for(;Count > 0;Count-=left)
{
left = (Count > sizeof(sadd))?(sizeof(sadd)):(Count);
this->cAppend((LPSTR)&sadd,left); // Do not check if 'sadd' is empty?
}
return *this;
}
//----------------------
bool SetFile(PVOID FileName)
{
if(!((PBYTE)FileName)[1])this->hFile = CreateFileW((PWSTR)FileName,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
else this->hFile = CreateFileA((LPSTR)FileName,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
if(this->hFile == INVALID_HANDLE_VALUE)return false;
return true;
}
};
//-----------------------------------------------------------------------------
//===========================================================================================================
#endif