📄 api.lst
字号:
C51 COMPILER V8.08 API 04/25/2009 23:03:21 PAGE 1
C51 COMPILER V8.08, COMPILATION OF MODULE API
OBJECT MODULE PLACED IN API.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE API.C LARGE OMF2 BROWSE DEBUG
line level source
1 //************************************************************************
2 //author:dragon
3 //web:8dragon.com
4 //2004.2.5完成于桃龙源
5 //*************************************************************************
6 #include <c8051F020.h>
7 #include "api.h"
8 #include "file.h"
9 #include "host_811.h"
10 #include "ufi.h"
11 #include "string.h"
12 #include "math.h"
13 struct _FILE ApiFileControl;
14 extern struct _BPB SimpleBpb;
15 extern FAT_PARAMETER FatParameter;
16 BYTE xdata BuffFile[8*512] _at_ 0xa000;
17
18 extern xdata WORD FatCache[2][256] ;
19
20
21 BYTE CreateFile(char* filename, BYTE attribute)
22 {
23 1 struct _DIR dir,*pdir;
24 1 BYTE name[11];
25 1 struct _FILE file;
26 1 // WORD NewCluster;
27 1 xdata BYTE Cache[512];
28 1 static char NewFileName[12];
29 1 char * p=NewFileName;
30 1 ////////////////////////////////////////////////////////////////////////
31 1 //*Get valid filen name
32 1 if(!CheckFileName(filename,p))
33 1 return FALSE;
34 1 //*核对该文件是否已经存在
35 1 if(LocateFile(filename, NULL)!=0xffffffff)
36 1 return FALSE;
37 1 strncpy(name, p, 11);
38 1 memset(&dir, 0, sizeof(dir));
39 1 memcpy(dir.Name, name, 11);
40 1 dir.Attr = attribute;
41 1 dir.CrtDate =0;
42 1 dir.CrtTime =0;
43 1 dir.CrtTimeTenth =0;
44 1 dir.FileSize =0;
45 1 DelayMs(5);
46 1 if(!LocateDir(&dir, &file))
47 1 return FALSE;
48 1 // NewCluster=SeekEmptyCluseter();
49 1 // if(NewCluster==0xffff)
50 1 // return FALSE;
51 1 DelayMs(15);
52 1 if(!Read(file.DirSectorNum,1,(BYTE *)Cache))
53 1 return FALSE;
54 1 pdir = (struct _DIR *)Cache;
55 1 pdir += file.DirIndex;
C51 COMPILER V8.08 API 04/25/2009 23:03:21 PAGE 2
56 1 pdir->FstClusLO =0x00;//WordSwap(NewCluster);
57 1 pdir->FileSize=0;
58 1 if(!Write(file.DirSectorNum,1,Cache,TRUE))
59 1 if(!Write(file.DirSectorNum,1,Cache,TRUE))
60 1 return FALSE;
61 1 return OpenFile(filename);
62 1 }
63 BYTE DelFile(const char * filename)
64 {
65 1 DWORD FileFirstSector;
66 1 BYTE buf[512];
67 1 WORD Cluster,Cache[256];
68 1 struct _DIR *dir;
69 1 FileFirstSector=LocateFile(filename, &ApiFileControl);
70 1 if(FileFirstSector==0xffffffff)
71 1 return FALSE;
72 1 dir=(struct _DIR *)buf;
73 1 if(!Read(ApiFileControl.DirSectorNum,1,buf))
74 1 return FALSE;
75 1 ApiFileControl.dir.Name[0]=0xe5;
76 1 dir=dir+ApiFileControl.DirIndex;
77 1 memcpy(dir,&ApiFileControl.dir,sizeof(struct _DIR));
78 1 if(!Write(ApiFileControl.DirSectorNum ,1,buf,TRUE))
79 1 return FALSE;
80 1 Cluster=WordSwap(ApiFileControl.dir.FstClusLO);
81 1 if(!Read(((2*Cluster/SimpleBpb.BytsPerSec)+FatParameter.FirstFatSecNum),1,(BYTE *)Cache))
82 1 return FALSE;
83 1 for(;;)
84 1 {
85 2
86 2 // Cluster=GetListCluster(Cluster);
87 2 Cache[(Cluster%(SimpleBpb.BytsPerSec/2))]=0;
88 2 if((Cluster%(SimpleBpb.BytsPerSec/2))==0)
89 2 {
90 3 if(!Write((2*Cluster/SimpleBpb.BytsPerSec)+FatParameter.FirstFatSecNum,1,(BYTE *)Cache,TRUE))
91 3 return FALSE;
92 3 if(!Read(((2*Cluster/SimpleBpb.BytsPerSec)+FatParameter.FirstFatSecNum),1,(BYTE *)Cache))
93 3 return FALSE;
94 3 }
95 2 Cluster=GetListCluster(Cluster);
96 2 if((GetListCluster(Cluster)==0xffff)||(Cluster==0))
97 2 {
98 3 Cache[(Cluster%(SimpleBpb.BytsPerSec/2))]=0;
99 3 break;
100 3 }
101 2 }
102 1 if(!Write((2*Cluster/SimpleBpb.BytsPerSec)+FatParameter.FirstFatSecNum,1,(BYTE *)Cache,TRUE))
103 1 return FALSE;
104 1 return TRUE;
105 1 }
106 BYTE OpenFile(const char* filename)
107 {
108 1 DWORD FileFirstSector;
109 1 WORD Cluster;
110 1 ////////////////////////////////////////////////////////////////////////
111 1 FileFirstSector=LocateFile(filename, &ApiFileControl);
112 1 if(FileFirstSector==0xffffffff)
113 1 return FALSE;
114 1 ApiFileControl.StartSectorNum = FileFirstSector;
115 1 Cluster=WordSwap(ApiFileControl.dir.FstClusLO);
116 1 for(;;)
117 1 {
C51 COMPILER V8.08 API 04/25/2009 23:03:21 PAGE 3
118 2 if((GetListCluster(Cluster)==0xffff)||(Cluster==0))
119 2 break;
120 2 Cluster=GetListCluster(Cluster);
121 2 }
122 1 //ApiFileControl.CurrentSectorNum为该簇对应的第一个扇区
123 1 ApiFileControl.CurrentSectorNum = ClusterToSec(Cluster);
124 1 ApiFileControl.SectorOffset=(DWordSwap(ApiFileControl.dir.FileSize)%(SimpleBpb.BytsPerSec*SimpleBpb.SecPe
-rClus));
125 1 ApiFileControl.dir.FileSize=DWordSwap(ApiFileControl.dir.FileSize);
126 1 return TRUE;
127 1 }
128 BYTE WriteFile(const char* buffer, UINT bytes)
129 {
130 1 BYTE *pCache;
131 1 xdata WORD Cache[512];
132 1 UINT write_bytes =0,flag=0;
133 1 UINT max_write_bytes_in_sector;
134 1 WORD Cluster;
135 1 ////////////////////////////////////////////////////////////////////////
136 1 //如果打开的文件是一个空文件,则进入下面
137 1 //为其寻找一个开始的数据蔟,标记为0xffff
138 1 //数据直接填在该簇对应的扇区内
139 1 if(ApiFileControl.StartSectorNum<FatParameter.FirstDataSector)
140 1 {
141 2 Cluster=SeekEmptyCluster();
142 2 if(!Read(FatParameter.FirstFatSecNum+(2*Cluster/SimpleBpb.BytsPerSec),1,(BYTE *)Cache))
143 2 return FALSE;
144 2 Cache[Cluster%(SimpleBpb.BytsPerSec/2)]=0xffff;
145 2 if(!Write(FatParameter.FirstFatSecNum+(2*Cluster/SimpleBpb.BytsPerSec),1,(BYTE *)Cache,0))
146 2 return FALSE;
147 2 ApiFileControl.dir.FstClusLO=WordSwap(Cluster);
148 2 ApiFileControl.StartSectorNum=ApiFileControl.CurrentSectorNum=ClusterToSec(Cluster);
149 2 flag=1;
150 2 }
151 1 ///////////////////////////////////////////////////////////////////////////////
152 1 //
153 1 //如过前一个WriteFile函数的字节或者打开一个已经有为内容的文件的字节偏移刚好为
154 1 //SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus的整数倍,为了保持总的算法一致
155 1 //(都是先写好某簇为0xffff然后再写该簇的内容)则开始的时候必须先为其准备好
156 1 //一个未有内容的簇,标记好为0xffff,然后数据直接填在该簇对应的扇区内。
157 1 //
158 1 //////////////////////////////////////////////////////////////////////////////
159 1 if((ApiFileControl.SectorOffset==0)&&flag==0)
160 1 {
161 2 Cluster =LinkClusterList(SecToCluster(ApiFileControl.CurrentSectorNum));
162 2 if(Cluster == 0xffff)
163 2 return FALSE;
164 2 ApiFileControl.CurrentSectorNum = ClusterToSec(Cluster);
165 2 }
166 1 for(;;)
167 1 {
168 2 pCache=BuffFile;//Cache;
169 2 pCache += ApiFileControl.SectorOffset;
170 2 if((SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus - ApiFileControl.SectorOffset) > (bytes - write_bytes))
171 2 max_write_bytes_in_sector = (bytes - write_bytes);
172 2 else
173 2 max_write_bytes_in_sector=(SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus - ApiFileControl.SectorOffset);
174 2 memcpy(pCache, buffer, max_write_bytes_in_sector);
175 2 ApiFileControl.SectorOffset += max_write_bytes_in_sector;
176 2 if(ApiFileControl.SectorOffset>=SimpleBpb.BytsPerSec*SimpleBpb.SecPerClus)
177 2 {
178 3 if(!Write(ApiFileControl.CurrentSectorNum,SimpleBpb.SecPerClus,(BYTE*)BuffFile,FALSE))
C51 COMPILER V8.08 API 04/25/2009 23:03:21 PAGE 4
179 3 if(!Write(ApiFileControl.CurrentSectorNum,SimpleBpb.SecPerClus,(BYTE*)BuffFile,FALSE))
180 3 return FALSE;
181 3 if((write_bytes+max_write_bytes_in_sector)<bytes)
182 3 {
183 4 Cluster =LinkClusterList(SecToCluster(ApiFileControl.CurrentSectorNum));
184 4 if(Cluster == 0xffff)
185 4 return FALSE;
186 4 ApiFileControl.CurrentSectorNum = ClusterToSec(Cluster);
187 4 }
188 3
189 3 ApiFileControl.SectorOffset = 0;
190 3 }
191 2 write_bytes += max_write_bytes_in_sector;
192 2 buffer = (char*)buffer + max_write_bytes_in_sector;
193 2 ApiFileControl.dir.FileSize += max_write_bytes_in_sector;
194 2 if(write_bytes >= bytes)
195 2 return TRUE;
196 2 }//end for cycle
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -