-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileStream.h
More file actions
622 lines (548 loc) · 18.8 KB
/
FileStream.h
File metadata and controls
622 lines (548 loc) · 18.8 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#pragma once
#include <Windows.h>
#include <string>
#include <future>
#include <type_traits>
namespace System
{
namespace Enums
{
template<typename T>
inline typename std::enable_if<std::is_enum<T>::value, bool>::type Contains(T x, T testFlag)
{
typedef typename std::underlying_type<T>::type base;
return static_cast<base>(x & testFlag) == static_cast<base>(testFlag);
}
template<typename BASE, typename T>
inline typename std::enable_if<std::is_convertible<T, BASE>::value, bool>::type Contains(T x, T testFlag)
{
return static_cast<BASE>(x & testFlag) == static_cast<BASE>(testFlag);
}
};
}
#define ENUM_OPERATORS(T) \
enum class T; \
inline T operator & (T x, T y) { return static_cast<T> (static_cast<intptr_t>(x)& static_cast<intptr_t>(y)); }; \
inline T operator | (T x, T y) { return static_cast<T> (static_cast<intptr_t>(x) | static_cast<intptr_t>(y)); }; \
inline T operator ^ (T x, T y) { return static_cast<T> (static_cast<intptr_t>(x) ^ static_cast<intptr_t>(y)); }; \
inline T operator ~ (T x) { return static_cast<T> (~static_cast<intptr_t>(x)); }; \
inline T& operator &= (T& x, T y) { x = x & y; return x; }; \
inline T& operator |= (T& x, T y) { x = x | y; return x; }; \
inline T& operator ^= (T& x, T y) { x = x ^ y; return x; };
#define ENUM_FLAGS(T) ENUM_OPERATORS(T)
#define ENUM_OPERATORSEX(T, TYPE) \
enum class T: TYPE; \
inline T operator & (T x, T y) { return static_cast<T> (static_cast<TYPE>(x)& static_cast<TYPE>(y)); }; \
inline T operator | (T x, T y) { return static_cast<T> (static_cast<TYPE>(x) | static_cast<TYPE>(y)); }; \
inline T operator ^ (T x, T y) { return static_cast<T> (static_cast<TYPE>(x) ^ static_cast<TYPE>(y)); }; \
inline T operator ~ (T x) { return static_cast<T> (~static_cast<TYPE>(x)); }; \
inline T& operator &= (T& x, T y) { x = x & y; return x; }; \
inline T& operator |= (T& x, T y) { x = x | y; return x; }; \
inline T& operator ^= (T& x, T y) { x = x ^ y; return x; };
#define ENUM_FLAGSEX(T, TYPE) ENUM_OPERATORSEX(T, TYPE)
using namespace std;
namespace System
{
namespace IO
{
typedef HANDLE FileHandle;
#define Invalid_FileHandle INVALID_HANDLE_VALUE //无效文件指针
ENUM_FLAGSEX(FileAdvancedAccess, unsigned long)
/// <summary>
/// 高级文件访问权限
/// </summary>
enum class FileAdvancedAccess : unsigned long
{
//
// Standart Section
//
AccessSystemSecurity = 0x1000000, // AccessSystemAcl access type
MaximumAllowed = 0x2000000, // MaximumAllowed access type
Delete = 0x10000,
ReadControl = 0x20000,
WriteDAC = 0x40000,
WriteOwner = 0x80000,
Synchronize = 0x100000,
StandardRightsRequired = 0xF0000,
StandardRightsRead = ReadControl,
StandardRightsWrite = ReadControl,
StandardRightsExecute = ReadControl,
StandardRightsAll = 0x1F0000,
SpecificRightsAll = 0xFFFF,
File_Read_Data = 0x0001, // file & pipe
File_List_Directory = 0x0001, // directory
File_Write_Data = 0x0002, // file & pipe
File_Add_File = 0x0002, // directory
File_Append_Data = 0x0004, // file
File_Add_SubDirectory = 0x0004, // directory
File_Create_Pipe_Instance = 0x0004, // named pipe
File_Read_EA = 0x0008, // file & directory
File_Write_EA = 0x0010, // file & directory
File_Execute = 0x0020, // file
File_Traverse = 0x0020, // directory
File_Delete_Child = 0x0040, // directory
File_Read_Attributes = 0x0080, // all
File_Write_Attributes = 0x0100, // all
//
// Generic Section
//
GenericRead = 0x80000000,
GenericWrite = 0x40000000,
GenericExecute = 0x20000000,
GenericAll = 0x10000000,
Specific_Rights_All = 0x00FFFF,
File_All_Access = StandardRightsRequired | Synchronize | 0x1FF,
File_Generic__Read = StandardRightsRead | File_Read_Data | File_Read_Attributes | File_Read_EA | Synchronize,
File_Generic__Write = StandardRightsWrite | File_Write_Data | File_Write_Attributes | File_Write_EA | File_Append_Data | Synchronize,
File_Generic__ReadWrite = ReadControl | File_Read_Data | File_Write_Data | File_Read_Attributes | File_Write_Attributes | File_Read_EA | File_Write_EA | File_Append_Data | Synchronize,
File_Generic_Execute = StandardRightsExecute | File_Read_Attributes | File_Execute | Synchronize
};
ENUM_OPERATORS(FileAccess)
/// <summary>
/// 文件访问权限
/// </summary>
enum class FileAccess
{
Read,
Write,
All,
};
ENUM_FLAGSEX(FileShare, unsigned long)
/// <summary>
/// 文件共享权限
/// </summary>
enum class FileShare :unsigned long
{
/// <summary>
///
/// </summary>
None = 0x00000000,
/// <summary>
/// Enables subsequent open operations on an object to request read access.
/// Otherwise, other processes cannot open the object if they request read access.
/// If this flag is not specified, but the object has been opened for read access, the function fails.
/// </summary>
Read = 0x00000001,
/// <summary>
/// Enables subsequent open operations on an object to request write access.
/// Otherwise, other processes cannot open the object if they request write access.
/// If this flag is not specified, but the object has been opened for write access, the function fails.
/// </summary>
Write = 0x00000002,
/// <summary>
/// Enables subsequent open and write operations on an object.
/// </summary>
ReadWrite = 0x00000003,
/// <summary>
/// Enables subsequent open operations on an object to request delete access.
/// Otherwise, other processes cannot open the object if they request delete access.
/// If this flag is not specified, but the object has been opened for delete access, the function fails.
/// </summary>
Delete = 0x00000004
};
ENUM_OPERATORS(FileCreationDisposition)
/// <summary>
/// 文件创建配置
/// </summary>
enum class FileCreationDisposition
{
/// <summary>
/// Creates a new file. The function fails if a specified file exists.
/// </summary>
New = 1,
/// <summary>
/// Creates a new file, always.
/// If a file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes,
/// and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor that the SECURITY_ATTRIBUTES structure specifies.
/// </summary>
CreateAlways = 2,
/// <summary>
/// Opens a file. The function fails if the file does not exist.
/// </summary>
OpenExisting = 3,
/// <summary>
/// Opens a file, always.
/// If a file does not exist, the function creates a file as if dwCreationDisposition is CREATE_NEW.
/// </summary>
OpenAlways = 4,
/// <summary>
/// Opens a file and truncates it so that its size is 0 (zero) bytes. The function fails if the file does not exist.
/// The calling process must open the file with the GENERIC_WRITE access right.
/// </summary>
TruncateExisting = 5
};
ENUM_OPERATORS(FileMode)
/// <summary>
/// 文件模式
/// </summary>
enum class FileMode
{
CreateNew = 1,
/// <summary>
/// Creates a new file, always.
/// If a file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes,
/// and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor that the SECURITY_ATTRIBUTES structure specifies.
/// </summary>
Create = 2,
/// <summary>
/// Opens a file. The function fails if the file does not exist.
/// </summary>
Open = 3,
/// <summary>
/// Opens a file, always.
/// If a file does not exist, the function creates a file as if dwCreationDisposition is CREATE_NEW.
/// </summary>
OpenOrCreate = 4,
/// <summary>
/// Opens a file and truncates it so that its size is 0 (zero) bytes. The function fails if the file does not exist.
/// The calling process must open the file with the GENERIC_WRITE access right.
/// </summary>
Truncate = 5,
/// <summary>
/// Opens a file, always.
/// If a file does not exist, the function creates a file as if dwCreationDisposition is CREATE_NEW.
/// Then the file will be seeked to the end.
/// </summary>
Append = 6
};
ENUM_FLAGSEX(FileAttributes, unsigned long)
/// <summary>
/// 文件属性
/// </summary>
enum class FileAttributes :unsigned long
{
None = 0x0000000,
Readonly = 0x00000001,
Hidden = 0x00000002,
System = 0x00000004,
Directory = 0x00000010,
Archive = 0x00000020,
Device = 0x00000040,
Normal = 0x00000080,
Temporary = 0x00000100,
SparseFile = 0x00000200,
ReparsePoint = 0x00000400,
Compressed = 0x00000800,
Offline = 0x00001000,
NotContentIndexed = 0x00002000,
Encrypted = 0x00004000,
Write_Through = 0x80000000,
Overlapped = 0x40000000,
NoBuffering = 0x20000000,
RandomAccess = 0x10000000,
SequentialScan = 0x08000000,
DeleteOnClose = 0x04000000,
BackupSemantics = 0x02000000,
PosixSemantics = 0x01000000,
OpenReparsePoint = 0x00200000,
OpenNoRecall = 0x00100000,
FirstPipeInstance = 0x00080000
};
/// <summary>
/// 查找起始位置
/// </summary>
enum class SeekOrigin
{
Begin,
Current,
End,
};
/// <summary>
/// 文件类型
/// </summary>
enum class FileType
{
/// <summary>
/// The specified file is a character file, typically an LPT device or a console.
/// </summary>
Char =0x0002,
/// <summary>
/// The specified file is a disk file.
/// </summary>
Disk=0x0001,
/// <summary>
/// The specified file is a socket, a named pipe, or an anonymous pipe.
/// </summary>
Pipe=0x0003,
/// <summary>
/// Unused.
/// </summary>
Remote=0x8000,
Unknown=0x0000,
};
/// <summary>
/// 文件流类
/// </summary>
class FileStream
{
private:
FileHandle _fileHandle;
bool _autoClose;
wstring _path;
char* _buffer;
int _readPos;
long long _readLen;
long long _writePos;
int _bufferSize;
bool _canRead;
bool _canWrite;
bool _canSeek;
bool _isPipe;
unsigned long long _pos;
long long _appendStart;
FileType _type;
protected:
/// <summary>
/// 读取字节
/// </summary>
/// <param name="buffer">字节</param>
/// <param name="size">字节大小</param>
/// <param name="offset">字节偏移</param>
/// <param name="count">字节数</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
unsigned long ReadCore(char* buffer, unsigned long size, unsigned long offset, unsigned long count, bool nothrow = true);
/// <summary>
/// 写入字节
/// </summary>
/// <param name="buffer">字节</param>
/// <param name="size">字节大小</param>
/// <param name="offset">字节偏移</param>
/// <param name="count">字节数</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
bool WriteCore(char* buffer, unsigned long size, unsigned long offset, unsigned long count, bool nothrow = true);
/// <summary>
/// 刷新写入
/// </summary>
/// <param name="nothrow">The nothrow.</param>
void FlushWrite(bool nothrow = true);
/// <summary>
/// 刷新读取
/// </summary>
void FlushRead();
/// <summary>
/// 快进到新位置
/// </summary>
/// <param name="offset">位置偏移</param>
/// <param name="origin">位置.</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns>新位置</returns>
unsigned long long SeekCore(long long offset, SeekOrigin origin, bool nothrow = true);
/// <summary>
/// 设置文件流长度
/// </summary>
/// <param name="newLength">新长度</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
bool SetLengthCore(unsigned long long newLength, bool nothrow = true);
/// <summary>
/// 刷新
/// </summary>
void FlushCore();
public:
/// <summary>
/// 创建 <see cref="FileStream"/> 实例.
/// </summary>
FileStream();
/// <summary>
/// 创建 <see cref="FileStream"/> 实例.
/// </summary>
/// <param name="hFile">WindowsAPI 文件句柄</param>
/// <param name="autoClose">自动关闭</param>
FileStream(FileHandle hFile, bool autoClose = true);
~FileStream();
operator FileHandle();
/// <summary>
/// 打开指定文件
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="desiredAccess">期望文件访问权限</param>
/// <param name="shareMode">文件共享模式</param>
/// <param name="creationDistribution">文件创建模式</param>
/// <param name="bufferSize">缓冲大小</param>
/// <param name="nothrow">无错误捕捉</param>
/// <param name="lpSecurityAttributes">WindowsAPI 安全属性句柄</param>
/// <param name="attributes">文件属性</param>
/// <param name="templateFileHandle">WindowsAPI 临时文件句柄</param>
/// <returns>bool.</returns>
bool Open(const wchar_t* filePath, FileAdvancedAccess desiredAccess, FileShare shareMode, FileCreationDisposition creationDistribution, int bufferSize=4096, bool nothrow = true,
LPSECURITY_ATTRIBUTES lpSecurityAttributes = nullptr,
FileAttributes attributes = FileAttributes::None,
FileHandle templateFileHandle = nullptr);
/// <summary>
/// 打开指定文件
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="desiredAccess">期望文件访问权限</param>
/// <param name="shareMode">文件共享模式</param>
/// <param name="creationDistribution">文件创建模式</param>
/// <param name="bufferSize">缓冲大小</param>
/// <param name="nothrow">无错误捕捉</param>
/// <param name="lpSecurityAttributes">WindowsAPI 安全属性句柄</param>
/// <param name="attributes">文件属性</param>
/// <param name="templateFileHandle">WindowsAPI 临时文件句柄</param>
/// <returns>bool.</returns>
bool Open(const wchar_t* filePath, FileAccess desiredAccess, FileShare shareMode, FileMode mode, int bufferSize = 4096, bool nothrow = true,
LPSECURITY_ATTRIBUTES lpSecurityAttributes = nullptr,
FileAttributes attributes = FileAttributes::None,
FileHandle templateFileHandle = nullptr);
/// <summary>
/// 判断文件流是否可读
/// </summary>
bool CanRead() const;
/// <summary>
/// 判断文件流是否可写
/// </summary>
bool CanWrite() const;
/// <summary>
/// 判断文件流是否可快进
/// </summary>
bool CanSeek() const;
/// <summary>
/// 判断文件流是否处于打开状态
/// </summary>
bool IsOpen() const;
/// <summary>
/// 判断文件流是否处于关闭状态
/// </summary>
bool IsClosed() const;
/// <summary>
/// 附加WindowsAPI 文件句柄
/// </summary>
/// <param name="hFile">WindowsAPI 文件句柄</param>
/// <param name="autoClose">是否自动关闭</param>
void Attach(FileHandle hFile, bool autoClose = true);
/// <summary>
/// 分离WindowsAPI 文件句柄
/// </summary>
void Detach();
/// <summary>
/// 关闭文件流
/// </summary>
/// <param name="nothrow">无错误捕捉</param>
void Close(bool nothrow = true);
/// <summary>
/// 写入字节
/// </summary>
/// <param name="buffer">字节</param>
/// <param name="size">字节大小</param>
/// <param name="offset">字节偏移</param>
/// <param name="count">字节数</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
bool Write(char* buffer, unsigned long size, unsigned long offset, unsigned long count, bool nothrow = true);
/// <summary>
/// 写入一个字节
/// </summary>
/// <param name="byte">字节</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
bool WriteByte(int byte, bool nothrow = true);
/// <summary>
/// 读取字节
/// </summary>
/// <param name="buffer">字节</param>
/// <param name="size">字节大小</param>
/// <param name="offset">字节偏移</param>
/// <param name="count">字节数</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
unsigned long Read(char* buffer, unsigned long size, unsigned long offset, unsigned long count, bool nothrow = true);
/// <summary>
/// 读取下一个字节
/// </summary>
/// <param name="nothrow">无错误捕捉</param>
/// <returns>单字节</returns>
int ReadByte(bool nothrow = true);
/// <summary>
/// 快进到新位置
/// </summary>
/// <param name="offset">位置偏移</param>
/// <param name="origin">位置.</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns>新位置</returns>
unsigned long long Seek(long long offset, SeekOrigin origin, bool nothrow = true);
/// <summary>
/// 快进至结尾
/// </summary>
/// <returns>新位置</returns>
unsigned long long SeekToEnd();
/// <summary>
/// 退回至文件头
/// </summary>
void SeekToBegin();
/// <summary>
/// 设置文件流长度
/// </summary>
/// <param name="newLength">新长度</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
bool SetLength(unsigned long long newLength, bool nothrow = true);
/// <summary>
/// 获取文件流长度
/// </summary>
/// <param name="nothrow">无错误捕捉</param>
/// <returns>文件流长度</returns>
unsigned long long GetLength(bool nothrow = true) const;
/// <summary>
/// 获取文件流位置
/// </summary>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
unsigned long long GetPosition(bool nothrow = true) const;
/// <summary>
/// 设置文件流位置
/// </summary>
/// <param name="newPos">新位置</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
bool SetPosition(unsigned long long newPos, bool nothrow=true);
/// <summary>
/// 刷新
/// </summary>
/// <param name="nothrow">无错误捕捉</param>
/// <returns>bool.</returns>
bool Flush(bool nothrow = true);
/// <summary>
/// 刷新
/// </summary>
/// <param name="flushToDisk">刷新到硬盘</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
virtual bool Flush(bool flushToDisk, bool nothrow = true);
/// <summary>
/// 复制文件流
/// </summary>
/// <param name="nothrow">无错误捕捉</param>
/// <returns>文件流</returns>
FileStream* Duplicate(bool nothrow = true) const;
/// <summary>
/// 中止文件流
/// </summary>
void Abort();
/// <summary>
/// 区域上锁
/// </summary>
/// <param name="lPos">上锁位置</param>
/// <param name="lCount">上锁长度</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
bool LockRange(const unsigned long long& lPos, const unsigned long long& lCount, bool nothrow = true);
/// <summary>
/// 区域解锁
/// </summary>
/// <param name="lPos">解锁位置</param>
/// <param name="lCount">解锁长度</param>
/// <param name="nothrow">无错误捕捉</param>
/// <returns></returns>
bool UnlockRange(const unsigned long long& lPos, const unsigned long long& lCount, bool nothrow = true);
/// <summary>
/// 获取文件类型
/// </summary>
/// <param name="nothrow">无错误捕捉</param>
/// <returns>文件类型</returns>
FileType GetFileType(bool nothrow = true);
};
}
}