-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicFunction.cpp
117 lines (106 loc) · 2.28 KB
/
BasicFunction.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
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
#include"BasicFunction.h"
void printf_error(const char* s)
{
printf("\033[0m\033[1;31m%s\033[0m", s);
}
void printf_success(const char* s)
{
printf("\033[0m\033[1;32m%s\033[0m", s);
}
void ReadSectorError(unsigned char* buffer)
{
for (int i = 0; i < SECTOR_SIZE; i++) {
buffer[i] = 0x0F;
}
return;
}
void ReadSector(FILE* file, unsigned int sector, unsigned char* buffer)
{
int tmp;
fseek(file, sector * SECTOR_SIZE, SEEK_SET);
for (int i = 0; i < SECTOR_SIZE; i++) {
tmp = fgetc(file);
if (tmp == EOF) {
ReadSectorError(buffer);
return;
}
buffer[i] = (unsigned char)tmp;
}
return;
}
void OutputFile(const char* filename, unsigned char* buffer, unsigned int begin, unsigned int offset)
{
FILE* fp = NULL;
printf_s("building file:%s\n", filename);
if (begin + offset > MAX_SIZE) {
printf_error("\noffset overflow\n");
return;
}
if (fopen_s(&fp, filename, "wb+")) {
printf_error("\nfilename unsuitable or this file is using by another process\n");
return;
}
for (unsigned int i = 0; i < offset; i++) {
if (fputc((int)buffer[begin + i], fp) == EOF) {
printf_error("\nwrite file error\n");
fclose(fp);
return;
}
}
fclose(fp);
printf_success("\nbuild file complete\n");
return;
}
int CalculateFileSize(FILE* fp)
{
FILE* tmpfp = fp;
fseek(fp, 0, SEEK_END);
int res = ftell(fp);
fp = tmpfp;
return res;
}
int ReadFileUntil(const unsigned char* keyword, int len, FILE* fp, unsigned char* buffer)
{
int cnt = 0;//读取字节数
int flag = 0;//是否找到关键词标识
while (true) {
int tmp = fgetc(fp);
if (tmp == EOF) {
return -1;
}
buffer[cnt] = tmp;
if (cnt + 1 >= len) for (int i = 0; i < len; i++) {
if (buffer[cnt - i] != keyword[len - i]) {
break;
}
if (i == len - 1) flag = 1;
}
cnt++;
if (flag) {
return cnt;
}
}
return 0;
}
unsigned int Bytes2Int(const unsigned char* buffer, int len)
{
if (len > 4) {
return 0;
}
unsigned int res = 0;
for (int i = 0; i < len; i++) {
res += ((unsigned int)buffer[i] << (8 * i));
}
return res;
}
int CreateFolder(const char* FolderPath)
{
int res;
if (_access(FolderPath, 0) != 0) {
res = _mkdir(FolderPath);
}
else {
res = 1;
}
return res;
}