-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfileutils.c
More file actions
55 lines (52 loc) · 1.46 KB
/
fileutils.c
File metadata and controls
55 lines (52 loc) · 1.46 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
#include "fileutils.h"
/**
* Get file size based on given fileContent.
*
* @param fileContent provide filePath, will also set fileSize in it.
* @retval 0 File does not exist.
* @retval size_t File size
*
*/
size_t get_file_size(FileContent *fileContent) {
FILE *pFile = fopen(fileContent->filePath, "r");
if (pFile == NULL) {
printf("Error: %s not found\n", fileContent->filePath);
return 0;
}
fseek(pFile, 0, SEEK_END);
size_t len = ftell(pFile);
fclose(pFile);
fileContent->fileSize = len;
return len;
}
/**
* Read File buffer based on given fileContent.
*
* @param fileContent provide filePath, will also set fileBuffer in it.
* @return Buffer read from file.
*/
uint8_t *read_file_content(FileContent *fileContent) {
if (fileContent->fileBuffer == NULL)
return NULL;
FILE *pFile = fopen(fileContent->filePath, "rb");
if (pFile == NULL)
return NULL;
fread(fileContent->fileBuffer, fileContent->fileSize, 1, pFile);
fclose(pFile);
return fileContent->fileBuffer;
}
/**
* Write buffer to filePath given by fileContent.
*
* @param fileContent Contains file information.
* @retval -EBADF Failed to write file
*
*/
int write_file_content(pFileContent fileContent) {
FILE *pFile = fopen(fileContent->filePath, "wb");
if (pFile == NULL)
return -EBADF;
fwrite(fileContent->fileBuffer, fileContent->fileSize, 1, pFile);
fclose(pFile);
return 0;
}