48889 (588618), страница 5
Текст из файла (страница 5)
*/
void __fastcall TForm1::LBFileListDblClick(TObject *Sender)
{
int i;
iSelected = LBFileList->ItemIndex;
char *ptr;
char bufferstr[65356];
char buffpath[2048];
PFILES pfirst, pfiles;
if(iSelected == -1)return;
mfile = FindFileByIndex(iSelected);
/*Реагируем только на вход в директорию и на выход из нее */
if((mfile->attrib & 0x10))
if((strlen(path)==1) || ((strlen(path)>1)&&(iSelected>0)))
{
if((strlen(mfile->ansiname)+strlen(path)+3)>sizeof(path))return;
strcat(path, mfile->ansiname);
wsprintf(bufferstr,mfile->ansiname);
strcat(path, "\\");
//ReadDir(currpld,path);
if(!ReadDir(currpld,path))
if (strcmp(bufferstr,"..")!=0)
{
ptr = strrchr(path,'\\');
while((ptr - path) < strlen(path))
strncpy(ptr,nulpat,strlen(path));
ptr = strrchr(path,'\\')+1;
while((ptr - path) < strlen(path))
strncpy(ptr,nulpat,strlen(path));
}
if(strlen(path) == 0) strcat(path, "\\");
else if(strlen(path) != 1)
{
if (strcmp(bufferstr,"..")==0)
{
ptr = strrchr(path,'\\');
while((ptr - path) < strlen(path))
strncpy(ptr,nulpat,strlen(path));
ptr = strrchr(path,'\\');
while((ptr - path) < strlen(path))
strncpy(ptr,nulpat,strlen(path));
ptr = strrchr(path,'\\')+1;
while((ptr - path) < strlen(path))
strncpy(ptr,nulpat,strlen(path));
LBFileList->Items->Clear();
ReadDir(currpld,path);
}
}
else
{
LBFileList->Items->Clear();
ReadDir(currpld,NULL);
wsprintf(path,"\\");
}
if (strcmp(bufferstr,".")==0)
{
ptr = strrchr(path,'\\')-1;
strncpy(ptr,nulpat,strlen(path));
}
Form1->Edit1->Text = Form1->CBDiskName->Text+path;
if (strlen(path) > 1) dir1 -= 2;
// (buffpath,IntToStr(dir1));
Form1->Label22->Caption=dir1;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LBFileList2DblClick(TObject *Sender)
{
int i;
iSelected2 = LBFileList2->ItemIndex;
char *ptr;
char bufferstr[65356];
char buffpath2[2048];
PFILES pfirst, pfiles;
if(iSelected2 == -1)return;
mfile = FindFileByIndex2(iSelected2);
/*Реагируем только на вход в директорию и на выход из нее */
if((mfile->attrib & 0x10))
if((strlen(path2)==1) || ((strlen(path2)>1)&&(iSelected2>0)))
{
if((strlen(mfile->ansiname)+strlen(path2)+3)>sizeof(path2))return;
strcat(path2, mfile->ansiname);
wsprintf(bufferstr,mfile->ansiname);
strcat(path2, "\\");
//ReadDir2(currpld2,path2);
if(!ReadDir2(currpld2,path2))
if (strcmp(bufferstr,"..")!=0)
{
ptr = strrchr(path2,'\\');
while((ptr - path2) < strlen(path2))
strncpy(ptr,nulpat,strlen(path2));
ptr = strrchr(path2,'\\')+1;
while((ptr - path2) < strlen(path2))
strncpy(ptr,nulpat,strlen(path2));
}
if(strlen(path2) == 0) strcat(path2, "\\");
else if(strlen(path2) != 1)
{
if (strcmp(bufferstr,"..")==0)
{
ptr = strrchr(path2,'\\');
while((ptr - path2) < strlen(path2))
strncpy(ptr,nulpat,strlen(path2));
ptr = strrchr(path2,'\\');
while((ptr - path2) < strlen(path2))
strncpy(ptr,nulpat,strlen(path2));
ptr = strrchr(path2,'\\')+1;
while((ptr - path2) < strlen(path2))
strncpy(ptr,nulpat,strlen(path2));
LBFileList2->Items->Clear();
ReadDir2(currpld2,path2);
}
}
else
{
LBFileList2->Items->Clear();
ReadDir2(currpld2,NULL);
wsprintf(path2,"\\");
}
if (strcmp(bufferstr,".")==0)
{
ptr = strrchr(path2,'\\')-1;
strncpy(ptr,nulpat,strlen(path2));
}
Form1->Edit2->Text = Form1->CBDiskName2->Text+path2;
if (strlen(path2) > 1) dir2 -= 2;
// (buffpath,IntToStr(dir1));
Form1->Label27->Caption=dir2;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Compare->Visible = false;
Button2->Visible = true;
Button2->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Compare->Visible = true;
Button2->Visible = false;
Button1->SetFocus();
}
//---------------------------------------------------------------------------
FAT32.CPP
#include
//#include "fat32.h"
#include "err.h"
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
//Чтение данных раздела
BOOL Fat32DataRead(PDISC_INFO info, char* buf, UINT bufsize)
{
int nRead;
BOOL bRetValue=ReadFile(info->hDrive, buf, bufsize,(unsigned long*) &nRead, NULL);
if(!bRetValue)AnalyzeError("# Error at ReadFile: ",GetLastError());
return bRetValue;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
//сдвинуть указатель внутри раздела
UINT Fat32DataMovePointer(PDISC_INFO info, UINT secpointer)
{
UINT iErr;
UINT HiPointer=secpointer>>(32-info->bitsPerSector);
UINT LoPointer=secpointer UINT bRetValue=SetFilePointer(info->hDrive,LoPointer,(long*)&HiPointer,FILE_BEGIN); if(bRetValue==-1) { iErr=GetLastError(); if(iErr!=NO_ERROR)AnalyzeError("# Error at SetFilePointer: ",iErr); } return bRetValue; } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ //найти следующий элемент цепочки кластеров UINT GetNextFileCluster(PDISC_INFO info, UINT nCurrCluster) { UINT nextcluster; if(info->bFAT16)nextcluster = ((USHORT*)(info->pFAT))[nCurrCluster]; else nextcluster = info->pFAT[nCurrCluster]; return nextcluster; } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ UINT Cluster2Sector(PDISC_INFO info, UINT cluster) { UINT retval; if(info->bFAT16) retval = info->sizeReserved+ (info->nFATCopy)*(info->sizeFAT)+ cluster*(info->SectPerCluster); else retval = info->sizeReserved+ (info->nFATCopy)*(info->sizeFAT)+ (cluster-2)*(info->SectPerCluster); return retval; } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ char* Fat32ReadFile(PDISC_INFO info, UINT FirstCluster, ULONG* dwFileSize) { char* retval = LoadDirectory(info, FirstCluster, dwFileSize); if(dwFileSize)*dwFileSize = (*dwFileSize)*(info->BytesPerCluster); return retval; } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ //пройтись по цепочке кластеров UINT WalkOnFATTable(PDISC_INFO info, UINT FirstCluster, UINT* LastCluster, UINT* nClusters) { UINT fragments=1; UINT predCluster, n=0; UINT currCluster=FirstCluster; while(1) { predCluster=currCluster; n++; currCluster=GetNextFileCluster(info, currCluster); if(currCluster==0)return 0; if(currCluster>=0x0FFFFFF8)break; if(info->bFAT16 && (currCluster>=0xfff8))break; if(currCluster!=(predCluster+1))fragments++; } if(LastCluster)*LastCluster=predCluster; if(nClusters)*nClusters=n; return fragments; } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ //Загружает директорию в память HDIR LoadDirectory(PDISC_INFO info, UINT cluster, ULONG* dirsize) { UINT sector,currCluster; UINT i; UINT nClusters,dwSize; HDIR hDir; char b[1024]; currCluster=cluster; if(info->bFAT16 && (0 == cluster)) { nClusters = 1 + (info->nRootElements * 32) / info->BytesPerCluster; dwSize = nClusters * info->BytesPerCluster; //MessageBox(0,"zzz","",MB_OK); }else{ WalkOnFATTable(info,cluster,NULL,&nClusters); dwSize=(info->BytesPerCluster)*nClusters; } hDir=(HDIR)malloc(dwSize); for(i=0;i { if(info->bFAT16 && (0 == cluster)) { sector = info->RootSector; }else sector = Cluster2Sector(info, currCluster); if(Fat32DataMovePointer(info,sector)==-1) { free(hDir); return NULL; } if(!Fat32DataRead(info,hDir+i*(info->BytesPerCluster),info->BytesPerCluster)) { free(hDir); return NULL; } if(info->bFAT16 && (0 == cluster)) {currCluster++;} else { currCluster = GetNextFileCluster(info,currCluster); if(currCluster==0) { free(hDir); return NULL; } } if(currCluster>=0x0FFFFFF8)break; } //MessageBox(0,"zzz2","",MB_OK); if(dirsize)*dirsize=nClusters; return hDir; } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ //Загружает таблицу FAT в память BOOL LoadFAT(PDISC_INFO info) { UINT dwSize=(info->sizeFAT)*(info->nBytePerSector); if(Fat32DataMovePointer(info,info->beginFAT)==-1)return 0; info->pFAT=(unsigned int*)malloc(dwSize); if(info->pFAT==NULL)return FALSE; if(!Fat32DataRead(info,(char*)(info->pFAT),dwSize)) { free(info->pFAT); return FALSE; } info->sizeFATbytes=dwSize; return TRUE; } /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ //если pObjectName==NULL то печатает содержимое директории, находящейся в памяти //если pObjectName!=NULL ищет в директории директорию с именем pObjectName UINT ListDirectory(PDISC_INFO info, HDIR hDir,UINT dwDirSize,char* cpObjectName, PFILES* ppfiles) { UCHAR attrib; UCHAR* p; UCHAR* t; USHORT firstclusterLo,firstclusterHi; UINT i,j,h,firstcluster,filesize; char ansiname[1024]; unsigned char uname[1024]; BOOL IsTheLong=FALSE; PFILES pfiles, pfirst=NULL, ppred=NULL; if(hDir==NULL)return 0; p=hDir; ansiname[11]=0; for(i=0;iBytesPerCluster))/32;i++) { if((p[0]==0xE5) || (p[0] == 0x8F) || (p[11]) == '\b') { p=p+32; continue; } if(p[0]==0)break; attrib=p[11]; if(attrib!=0x0F) { firstclusterLo=(*(USHORT*)&p[26]); firstclusterHi=(*(USHORT*)&p[20]); firstcluster=firstclusterHi; firstcluster=(firstcluster<<16)+firstclusterLo; if(!cpObjectName) { filesize=*(UINT*)&p[28]; pfiles =(_FILES*) malloc(sizeof(FILES)); pfiles->attrib = attrib; pfiles->firstcluster = firstcluster; pfiles->filesize = filesize; if(!pfirst)pfirst = pfiles; if(ppred)ppred->next = pfiles; } for(int g=10;g>1;g--) if(p[g]==' ') p[g]='\0'; memcpy(ansiname,p,11); for(j=10;j>1;j--) if(ansiname[j]!=0x20) { ansiname[j+1]=0; break; } if(IsTheLong) { WideCharToMultiByte(CP_ACP,0,(LPCWSTR)uname,-1,ansiname,sizeof(ansiname),NULL,NULL); IsTheLong=FALSE; } if(cpObjectName) if((!strcmpi(cpObjectName,ansiname)) && ((attrib&0x10)!=0)) return firstcluster; if(!cpObjectName) { pfiles->ansiname =(char*) malloc(strlen(ansiname)+1); strcpy(pfiles->ansiname, ansiname); pfiles->next = NULL; ppred = pfiles; } } else if((p[0]==1)||(p[0]&0x40)) { if(p!=(hDir+dwDirSize)) if((p[0]&0x40)&&((p+32)[11]==0x0F)) { p+=32; continue; } t=p; h=0; memset(uname,0,sizeof(uname)); while(1) { j=t[0]; memcpy(uname+h+00,t+1,10); memcpy(uname+h+10,t+14,12); memcpy(uname+h+22,t+28,4); if(j&0x40) { IsTheLong=TRUE; break; } t-=32; h+=26; if(t if(t[11]!=0x0F)break; } } p+=32; } if(ppfiles) *ppfiles = pfirst; return 0; }