-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.c
executable file
·266 lines (235 loc) · 8.02 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/******************************************************************************\
Copyright(C): Huazhong University of Science & Technology.
FileName: File.c
Author: Zhulinfeng
Version: 1.0
Date: 2014-3-9
Description: Operation on files.
Others:
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
\******************************************************************************/
/**include public head files**/
#include "Type.h"
#include "Sys.h"
#include "Err.h"
/**include module head files**/
#include "File.h"
/*******************************************************************************
Func Name: File_Init
Date Created: 2014-3-11
Author: Zhulinfeng
Description: Init struct File_S *psFile.
Input: File_S *psFile Storation of some temp data during read
or write file.
Output: File_S *psFile
Return: NONE
Caution: psFile should not be null, this function should be used first.
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PUBLIC void File_Init(INOUT struct File_S *psFile)
{
assert(NULL != psFile);
psFile -> uibase = 0;
psFile -> ulfileOffset = 0;
psFile -> uibyteCount = 0;
psFile -> uicompletion = 0;
psFile -> pfinFd = NULL;
psFile -> pfoutFd = NULL;
psFile -> sinFilePath = NULL;
psFile -> soutFilePath = NULL;
}
/*******************************************************************************
Func Name: File_Finit
Date Created: 2014-3-11
Author: Zhulinfeng
Description: Init struct File_S *psFile.
Input: File_S *psFile Storation of some temp data during read
or write file.
Output: File_S *psFile
Return: int
DS_SUCCESS
DS_SYSERR Error occured when close file fd.
Caution: psFile should not be null, this function should be used at last
to set off all the resources when psFile will never be used.
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PUBLIC int File_Finit(INOUT struct File_S *psFile)
{
assert(NULL != psFile);
int iresultCode1 = 0;
int iresultCode2 = 0;
if(NULL != psFile -> pfinFd)
{
iresultCode1 = fclose(psFile -> pfinFd);
}
if(NULL != psFile -> pfoutFd)
{
iresultCode2 = fclose(psFile -> pfoutFd);
}
psFile -> uibase = 0;
psFile -> ulfileOffset = 0;
psFile -> uibyteCount = 0;
psFile -> uicompletion = 0;
psFile -> pfinFd = NULL;
psFile -> pfoutFd = NULL;
psFile -> sinFilePath = NULL;
psFile -> soutFilePath = NULL;
if(0 == iresultCode1 && 0 == iresultCode2)
{
return DS_SUCCESS;
}
else
{
return DS_SYSERR;
}
}
/*******************************************************************************
Func Name: File_OpenInFile
Date Created: 2014-3-11
Author: Zhulinfeng
Description: Open a file to read from, store file fd in psFile.
Input: string sfilePath File path, should not be null.
Output: File_S *psFile Storation of some temp data during read
or write file.
Return: int
DS_SUCCESS Open file successfully.
DS_FAILED Error occured when open file.
Caution: sfilePath and psFile should not be null, this function should be
used after function File_Init.
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PUBLIC int File_OpenInFile(IN string sfilePath, OUT struct File_S *psFile)
{
assert(NULL != sfilePath);
assert(NULL != psFile);
psFile -> sinFilePath = sfilePath;
const char *pcfileName = psFile -> sinFilePath;
const char *pcmode = "rb";
psFile -> pfinFd = fopen(pcfileName, pcmode);
if(NULL == psFile -> pfinFd)
{
return DS_FAILED;
}
else
{
return DS_SUCCESS;
}
}
/*******************************************************************************
Func Name: File_OpenOutFile
Date Created: 2014-3-11
Author: Zhulinfeng
Description: Open a file to write on, store file fd in psFile.
Input: string sfilePath File path, should not be null.
Output: File_S *psFile Storation of some temp data during read
or write file.
Return: int
DS_SUCCESS Open file successfully.
DS_FAILED Error occured when open file.
Caution: sfilePath and psFile should not be null, this function should be
used after function File_Init.
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PUBLIC int File_OpenOutFile(IN string sfilePath, OUT struct File_S *psFile)
{
assert(NULL != sfilePath);
assert(NULL != psFile);
psFile -> soutFilePath = sfilePath;
const char *pcfileName = psFile -> soutFilePath;
const char *pcmode = "wb+";
psFile -> pfoutFd = fopen(pcfileName, pcmode);
if(NULL == psFile -> pfoutFd)
{
return DS_FAILED;
}
else
{
return DS_SUCCESS;
}
}
/*******************************************************************************
Func Name: File_Read
Date Created: 2014-3-11
Author: Zhulinfeng
Description: Read some data from input file.
Input: File_S *psFile Storation of some temp data during read
or write file.
Output: NONE
Return: int
DS_SUCCESS Read file successfully.
DS_FILE_END End reading file.
Caution: sfilePath and psFile should not be null, this function should be
used after function File_OpenInFile.
The data read in must align at 8, so this function will complete
some bytes if neccessary, the number of completed bytes will be
stored in psFile.
----------------------------------------------------------------------------
Modification History
Date Author Modification
----------------------------------------------------------------------------
YYYY-MM-DD
*******************************************************************************/
PUBLIC int File_Read(INOUT struct File_S *psFile)
{
assert(NULL != psFile);
assert(NULL != psFile -> pfinFd);
psFile -> uibyteCount = 0;
if(DATABUF_LENGTH == fread(psFile -> DATABUF, 1, DATABUF_LENGTH, psFile -> pfinFd))
{
psFile -> uibyteCount = DATABUF_LENGTH;
psFile -> ulfileOffset = ftell(psFile -> pfinFd);
return DS_SUCCESS;
}
else
{
/**step back**/
fseek(psFile -> pfinFd, psFile -> ulfileOffset, psFile -> uibase);
while(0 == feof(psFile -> pfinFd))
{
psFile -> DATABUF[psFile -> uibyteCount] = fgetc(psFile -> pfinFd);
psFile -> uibyteCount ++;
}
psFile -> uibyteCount--;
/**complet package**/
psFile -> uicompletion = (PACK_LENGTH - (psFile -> uibyteCount%PACK_LENGTH)) % PACK_LENGTH + PACK_LENGTH;
int icptCount = 0;
for(icptCount; icptCount < psFile -> uicompletion ; icptCount++)
{
psFile -> DATABUF[psFile -> uibyteCount] = 0x00;
psFile -> uibyteCount++;
}
psFile -> DATABUF[psFile -> uibyteCount - 1] = (unsigned char)(psFile -> uicompletion);
return DS_FILE_END;
}
return DS_SUCCESS;
}
PUBLIC int File_Write(INOUT struct File_S *psFile)
{
assert(NULL != psFile);
assert(NULL != psFile -> pfoutFd);
if(psFile -> uibyteCount != fwrite(psFile -> DATABUF, 1, psFile -> uibyteCount, psFile -> pfoutFd))
{
return DS_FAILED;
}
return DS_SUCCESS;
}