-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarylib.h
148 lines (140 loc) · 5.45 KB
/
binarylib.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
147
148
#pragma once
#include <stdio.h>
#include <stdlib.h>
/**
* @brief Writes a buffer to a binary file.
*
* This function opens/creates a file in binary, writes the contents of the buffer,
* and then closes the file. If the file cannot be opened, the program will exit with an error.
*
* @param _Filename The name of the file to write to.
* @param _Type The data type of the buffer being written.
* @param _Buffer The buffer to write to the file.
* @param _ElementSize The size of each element in the buffer.
*/
#define binaryWrite(_Filename, _Type, _Buffer, _ElementSize) { \
FILE* fstream = fopen((_Filename), "wb"); \
if(fstream == NULL) { \
printf("Error: Can't connect to the file: %s\n", _Filename); \
exit(EXIT_FAILURE); \
} \
_Type databuff = (_Type)(_Buffer); \
fwrite(&(databuff), (_ElementSize), 1, (fstream)); \
fclose(fstream); \
}
/**
* @brief function pass a value by refference to write a buffer to a binary file.
*
* This function can passed a refference of the data and opens/creates a file,
* writes the buffer contents to it, and closes the file.
* If the file cannot be opened, the program will exit with an error.
*
* @param _Filename The name of the file to write to.
* @param _Buffer The buffer to write to the file.
* @param _ElementSize The size of each element in the buffer.
*/
void binaryWriteRef(const char* _Filename, const void* _Buffer, size_t _ElementSize) {
FILE* fstream = fopen(_Filename, "wb");
if(fstream == NULL) {
printf("Error: Can't connect to the file: %s\n", _Filename);
exit(EXIT_FAILURE);
}
fwrite(_Buffer, _ElementSize, 1, fstream);
fclose(fstream);
}
/**
* @brief Appends a buffer to a binary file.
*
* This function opens/creates a file in binary, append the contents of the buffer,
* and then closes the file. If the file cannot be opened, the program will exit with an error.
*
* @param _Filename The name of the file to append to.
* @param _Type The data type of the buffer being appended.
* @param _Buffer The buffer to append to the file.
* @param _ElementSize The size of each element in the buffer.
*/
#define binaryAppend(_Filename, _Type, _Buffer, _ElementSize) { \
FILE* fstream = fopen((_Filename), "ab"); \
if(fstream == NULL) { \
printf("Error: Can't connect to the file: %s\n", _Filename); \
exit(EXIT_FAILURE); \
} \
_Type databuff = (_Type)(_Buffer); \
fwrite(&(databuff), (_ElementSize), 1, (fstream)); \
fclose(fstream); \
}
/**
* @brief function to pass a refferenced value and append a buffer to a binary file.
*
* This function opens a file, appends the reffereced value buffer contents to it, and closes the file.
* If the file cannot be opened, the program will exit with an error.
*
* @param _Filename The name of the file to append to.
* @param _Buffer The buffer to append to the file.
* @param _ElementSize The size of each element in the buffer.
*/
void binaryAppendRef(const char* _Filename, const void* _Buffer, size_t _ElementSize) {
FILE* fstream = fopen(_Filename, "ab");
if(fstream == NULL) {
printf("Error: Can't connect to the file: %s\n", _Filename);
exit(EXIT_FAILURE);
}
fwrite(_Buffer, _ElementSize, 1, fstream);
fclose(fstream);
}
/**
* @brief Reads data from a binary file at a specific address into a buffer.
*
* This function opens a file, reads data from a specified address into the provided buffer,
* and closes the file. If the file cannot be opened or the read fails, the program will exit with an error.
*
* @param _FileName The name of the file to read from.
* @param _Address The address in the file to start reading from.
* @param _Location The buffer where the read data will be stored.
* @param _ElementSize The size of the data to read.
*/
void binaryRead(const char* _FileName, unsigned long _Address, void* _Location, size_t _ElementSize) {
FILE* fstream = fopen(_FileName, "rb");
if(fstream == NULL) {
printf("Error: Can't connect to the file: %s\n", _FileName);
exit(EXIT_FAILURE);
}
fseek(fstream, _Address, SEEK_SET);
fread(_Location, _ElementSize, 1, fstream);
fclose(fstream);
}
/**
* @brief Reads data from a binary file and returns it by value.
*
* This function opens a file, reads data from a specified address, allocates memory to store
* the data, and returns a pointer to the allocated memory. If the file cannot be opened or the
* memory allocation fails, the program will exit with an error.
*
* @param _FileName The name of the file to read from.
* @param _Address The address in the file to start reading from.
* @param _ElementSize The size of the data to read.
* @return A pointer to the allocated buffer containing the read data.
*/
void* binaryReadByValue(const char* _FileName, unsigned long _Address, size_t _ElementSize) {
FILE* fstream = fopen(_FileName, "rb");
if(fstream == NULL) {
printf("Error: Can't connect to the file: %s\n", _FileName);
exit(EXIT_FAILURE);
}
void* buff = malloc(_ElementSize);
if (buff == NULL) {
printf("Error: Memory allocation failed.\n");
fclose(fstream);
exit(EXIT_FAILURE);
}
fseek(fstream, _Address, SEEK_SET);
size_t readCount = fread(buff, _ElementSize, 1, fstream);
if (readCount != 1) {
printf("Error: Failed to read data from file.\n");
free(buff);
fclose(fstream);
exit(EXIT_FAILURE);
}
fclose(fstream);
return buff;
}