-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchip8_string.c
More file actions
88 lines (75 loc) · 2.14 KB
/
Copy pathchip8_string.c
File metadata and controls
88 lines (75 loc) · 2.14 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
#include "windows.h"
typedef struct
{
u64 Size;
void *Memory;
} file_contents;
void PlatformFreeFile(void *Memory)
{
if(Memory)
{
VirtualFree(Memory, 0, MEM_RELEASE);
}
}
file_contents PlatformReadFile(char *FileName)
{
HANDLE FileHandle = CreateFile(FileName,
GENERIC_READ,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
file_contents Result = {0};
if(FileHandle != INVALID_HANDLE_VALUE)
{
DWORD FileSize = GetFileSize(FileHandle, 0);
Result.Size = FileSize;
Result.Memory = VirtualAlloc(0, Result.Size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if(Result.Memory)
{
DWORD BytesRead;
if(!ReadFile(FileHandle, Result.Memory, FileSize, &BytesRead, 0) ||
BytesRead != FileSize)
{
VirtualFree(Result.Memory, 0, MEM_RELEASE);
Result.Size = 0;
Result.Memory = 0;
}
CloseHandle(FileHandle);
}
}
return Result;
}
b32 PlatformWriteFile(char *FileName, void *Memory, u32 BytesToWrite)
{
b32 Result = false;
HANDLE FileHandle = CreateFile(FileName,
GENERIC_WRITE,
0,
0,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
0);
if(FileHandle != INVALID_HANDLE_VALUE)
{
DWORD BytesWritten;
if(WriteFile(FileHandle, Memory, BytesToWrite, &BytesWritten, 0))
{
if(BytesWritten == BytesToWrite)
{
Result = true;
}
else
{
// NOTE: Failure
}
}
else
{
// NOTE: Failure
}
CloseHandle(FileHandle);
}
return Result;
}