-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile.c
87 lines (66 loc) · 1.6 KB
/
file.c
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
#include "file.h"
#include <string.h>
uint8_t read_bin_file(struct file_ops *fops,char *filename)
{
//TODO assert file name and size
if(filename == NULL)
{
printf("no image file.\n");
return -1;
}
fops->fp = fopen(filename,"r");
if(fops->fp<0)
{
printf("open the image file failed...\n");
return -1;
}
//get file size
fseek(fops->fp,0,SEEK_END);
fops->fileSize = ftell(fops->fp);
fseek(fops->fp,0,SEEK_SET);
//malloc memory
fops->buf = malloc(fops->fileSize);
if(fops->buf == NULL)
{
printf("malloc buffer failed...\n");
return -1;
}
if(fread(fops->buf,1,fops->fileSize,fops->fp) != fops->fileSize)
{
printf("read file failed...\n");
return -1;
}
printf("read file SUCCESS...\n");
return 0;
}
uint16_t file_create(struct file_ops *fops,frame_file_t *file_frame)
{
uint16_t len=MAX_FILE_LEN;
uint16_t i;
if( fops->fileIndex*MAX_FILE_LEN > fops->fileSize)
return 0;
if( (fops->fileIndex+1)*MAX_FILE_LEN > fops->fileSize)
len = fops->fileSize - fops->fileIndex*MAX_FILE_LEN;
printf("len is %d index is %d",len,fops->fileIndex);
file_frame->Sequence = fops->fileIndex;
file_frame->length = len;
memcpy(file_frame->buf,fops->buf+fops->fileIndex*MAX_FILE_LEN,len);
fops->fileIndex++;
return sizeof(frame_file_t)+len-MAX_FILE_LEN;
}
void file_close(struct file_ops *fops)
{
free(fops->buf);
fops->buf=NULL;
fclose(fops->fp);
}
file_ops __file_ops=
{
NULL,
NULL,
0,
0,
read_bin_file,
file_create,
file_close
};