📄 file.lst
字号:
ARM COMPILER V2.42, file 27/03/06 10:45:50 PAGE 1
ARM COMPILER V2.42, COMPILATION OF MODULE file
OBJECT MODULE PLACED IN .\obj\file.obj
COMPILER INVOKED BY: C:\Keil\ARM\BIN\CA.exe src\file.c THUMB DEBUG PRINT(.\LST\FILE.LST) TABS(4) OBJECT(.\obj\file.obj)
stmt level source
1 /*****************************************************************************\
2 * efs - General purpose Embedded Filesystem library *
3 * --------------------- ----------------------------------- *
4 * *
5 * Filename : file.c *
6 * Description : This file contains functions dealing with files such as: *
7 * fopen, fread and fwrite. *
8 * *
9 * This library is free software; you can redistribute it and/or *
10 * modify it under the terms of the GNU Lesser General Public *
11 * License as published by the Free Software Foundation; either *
12 * version 2.1 of the License, or (at your option) any later version. *
13 * *
14 * This library is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
17 * Lesser General Public License for more details. *
18 * *
19 * (c)2004 Lennart Yseboodt *
20 * (c)2005 Michael De Nil *
21 \*****************************************************************************/
22
23 /*****************************************************************************/
24 #include "file.h"
25 /*****************************************************************************/
26
27 /* ****************************************************************************
28 * euint32 file_fread(File *file,euint32 offset, euint32 size,euint8 *buf)
29 * Description: This function reads 'size' bytes from 'file' starting at
30 * 'offset' and puts the result in '*buf'.
31 * Return value: amount of bytes actually read (can differ from the given
32 * size when the file was smaller
33 */
34 euint32 file_fread(File *file,euint32 offset, euint32 size,euint8 *buf)
35 {
36 1 euint32 bytes_read=0,size_left=size,coffset=offset;
37 1 euint32 cclus,csec,cbyte;
38 1 euint32 rclus,rsec;
39 1 euint32 btr;
40 1 euint8 *tbuf;
41 1
42 1 if(!file_getAttr(file,FILE_STATUS_OPEN))return(0);
43 1
44 1 if(offset>=file->FileSize)
45 1 size_left=0; /* Offset check */
46 1
47 1 if( (offset+size > file->FileSize) && size_left!=0)
48 1 size_left=file->FileSize-offset;
49 1
50 1 while(size_left>0){
51 2
52 2 cclus = coffset/(512*file->fs->volumeId.SectorsPerCluster);
53 2 csec = (coffset/(512))%file->fs->volumeId.SectorsPerCluster;
54 2 cbyte = coffset%512;
55 2
56 2 if(cbyte!=0 || size_left<512){
57 3 btr = 512-(coffset%512)>=size_left?size_left:512-(coffset%512);
58 3 }else{
59 3 btr = 512;
ARM COMPILER V2.42, file 27/03/06 10:45:50 PAGE 2
60 3 }
61 2
62 2 if((fat_LogicToDiscCluster(file->fs,&(file->Cache),cclus))!=0){
63 3 return(0);
64 3 }
65 2 rclus=file->Cache.DiscCluster;
66 2 rsec=fs_clusterToSector(file->fs,rclus);
67 2
68 2
69 2 if(btr==512){
70 3 /*part_readBuf(file->fs->part,rsec+csec,buf+bytes_read);*/
71 3 part_directSectorRead(file->fs->part,rsec+csec,buf+bytes_read);
72 3 }else{
73 3 /*part_readBuf(file->fs->part,rsec+csec,tbuf);*/
74 3 tbuf = part_getSect(file->fs->part,rsec+csec,IOM_MODE_READONLY);
75 3 memCpy(tbuf+(coffset%512),buf+bytes_read,btr);
76 3 part_relSect(file->fs->part,tbuf);
77 3 }
78 2
79 2 coffset+=btr;
80 2 bytes_read+=btr;
81 2 size_left-=btr;
82 2 }
83 1
84 1 return(bytes_read);
85 1 }
86 /*****************************************************************************/
87
88 /* ****************************************************************************
89 * euint32 file_read (File *file,euint32 size,euint8 *buf)
90 * Description: This function reads from a file, taking the FilePtr into account
91 * and advancing it according to the freadcall.
92 * Return value: Value obtained from fread
93 */
94 euint32 file_read(File *file,euint32 size,euint8 *buf)
95 {
96 1 euint32 r;
97 1
98 1 r=file_fread(file,file->FilePtr,size,buf);
99 1 file->FilePtr+=r;
100 1 return(r);
101 1 }
102 /*****************************************************************************/
103
104 /* ****************************************************************************
105 * euint32 file_write(File *file, euint32 size,euint8 *buf)
106 * Description: This function writes to a file, taking FilePtr into account
107 * and advancing it according to the fwritecall.
108 * Return value: Value obtained from fread
109 */
110 euint32 file_write(File *file, euint32 size,euint8 *buf)
111 {
112 1 euint32 r;
113 1
114 1 r=file_fwrite(file,file->FilePtr,size,buf);
115 1 file->FilePtr+=r;
116 1 return(r);
117 1 }
118 /*****************************************************************************/
119
120 /* ****************************************************************************
121 * esint16 file_setpos(File *file,euint32 pos)
122 * Description: This function does a sanity check on the requested position
123 * and changes the fileptr accordingly.
124 * Return value: 0 on success and -1 on failure.
125 */
ARM COMPILER V2.42, file 27/03/06 10:45:50 PAGE 3
126 esint16 file_setpos(File *file,euint32 pos)
127 {
128 1 if(pos<=file->FileSize){
129 2 file->FilePtr=pos;
130 2 return(0);
131 2 }
132 1 return(-1);
133 1 }
134 /*****************************************************************************/
135
136 /* ****************************************************************************
137 * euint32 file_fwrite(File* file,euint32 offset,euint32 size,euint8* buf)
138 * Description: This function writes to a file, at offset 'offset' and size 'size'.
139 * It also updates the FileSize in the object, and discstructure.
140 * Return value: Bytes actually written.
141 */
142 euint32 file_fwrite(File* file,euint32 offset,euint32 size,euint8* buf)
143 {
144 1 euint32 need_cluster;
145 1 euint32 cclus,csec,cbyte;
146 1 euint32 size_left=size,bytes_written=0;
147 1 euint32 rclus,rsec;
148 1 euint32 coffset=offset;
149 1 euint16 btr;
150 1 euint8 *tbuf;
151 1
152 1 if(!file_getAttr(file,FILE_STATUS_OPEN) || !file_getAttr(file,FILE_STATUS_WRITE))return(0);
153 1
154 1 if(offset>file->FileSize){
155 2 offset=file->FileSize;
156 2 }
157 1
158 1 need_cluster = file_requiredCluster(file,offset,size);
159 1
160 1 if(need_cluster){
161 2 if(fat_allocClusterChain(file->fs,&(file->Cache),need_cluster+CLUSTER_PREALLOC_FILE)!=0){
162 3 return(0);
163 3 }
164 2 }
165 1
166 1 while(size_left>0){
167 2
168 2 cclus = coffset/(512*file->fs->volumeId.SectorsPerCluster);
169 2 csec = (coffset/(512))%file->fs->volumeId.SectorsPerCluster;
170 2 cbyte = coffset%512;
171 2
172 2 if(cbyte!=0 || size_left<512){
173 3 btr = 512-(coffset%512)>=size_left?size_left:512-(coffset%512);
174 3 }else{
175 3 btr = 512;
176 3 }
177 2
178 2 if((fat_LogicToDiscCluster(file->fs,&(file->Cache),cclus))!=0){
179 3 file->FileSize+=bytes_written;
180 3 dir_setFileSize(file->fs,&(file->Location),file->FileSize);
181 3 return(bytes_written);
182 3 }
183 2 rclus=file->Cache.DiscCluster;
184 2 rsec=fs_clusterToSector(file->fs,rclus);
185 2
186 2 if(btr==512){
187 3 /*part_writeBuf(file->fs->part,rsec+csec,buf+bytes_written);*/
188 3 part_directSectorWrite(file->fs->part,rsec+csec,buf+bytes_written);
189 3 }else{
190 3 /*part_readBuf(file->fs->part,rsec+csec,tbuf);*/
191 3 tbuf = part_getSect(file->fs->part,rsec+csec,IOM_MODE_READWRITE);
ARM COMPILER V2.42, file 27/03/06 10:45:50 PAGE 4
192 3 memCpy(buf+bytes_written,tbuf+(coffset%512),btr);
193 3 /*part_writeBuf(file->fs->part,rsec+csec,tbuf);*/
194 3 part_relSect(file->fs->part,tbuf);
195 3 }
196 2
197 2 coffset+=btr;
198 2 bytes_written+=btr;
199 2 size_left-=btr;
200 2 }
201 1
202 1 if(bytes_written>file->FileSize-offset){
203 2 file->FileSize+=bytes_written-(file->FileSize-offset);
204 2 }
205 1
206 1 return(bytes_written);
207 1 }
208
209 /* ***************************************************************************\
210 * signed eint8 file_fopen(FileSystem *fs,File* file,eint8* filename)
211 * Description: This functions opens a file.
212 * This function is about to be redesigned. No Docs.
213 * Return value:
214 */
215 esint8 file_fopen(File* file,FileSystem *fs,eint8* filename,eint8 mode)
216 {
217 1 FileLocation loc;
218 1 FileRecord wtmp;
219 1 eint8 fatfilename[11];
220 1 euint32 sec;
221 1
222 1 dir_getFatFileName(filename,fatfilename);
223 1
224 1 switch(mode)
225 1 {
226 2 case MODE_READ:
227 2 if(fs_findFile(fs,filename,&loc,0)==1)
228 2 {
229 3 dir_getFileStructure(fs,&(file->DirEntry), &loc);
230 3 file_initFile(file,fs,&loc);
231 3 file_setAttr(file,FILE_STATUS_OPEN,1);
232 3 file_setAttr(file,FILE_STATUS_WRITE,0);
233 3 return(0);
234 3 }
235 2 return(-1);
236 2 break;
237 2 case MODE_WRITE:
238 2 if(fs_findFile(fs,filename,&loc,0))
239 2 {
240 3 return(-2);
241 3 }
242 2 if(fs_findFreeFile(fs,filename,&loc,0))
243 2 {
244 3 dir_createDefaultEntry(fs,&wtmp,fatfilename);
245 3 dir_createDirectoryEntry(fs,&wtmp,&loc);
246 3 memCpy(&wtmp,&(file->DirEntry),sizeof(wtmp));
247 3 file_initFile(file,fs,&loc);
248 3 sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs));
249 3 dir_setFirstCluster(file->fs,&(file->Location),sec);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -