Chinaunix首页 | 论坛 | 博客
  • 博客访问: 544716
  • 博文数量: 252
  • 博客积分: 6057
  • 博客等级: 准将
  • 技术积分: 1635
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-21 10:17
文章分类

全部博文(252)

文章存档

2013年(1)

2012年(1)

2011年(32)

2010年(212)

2009年(6)

分类:

2010-03-19 11:56:11

本代码由本人自己编写,虽然代码算法上有待改进,但也是本人原创。复制时请标明 作者:杭师大--杨立春

下篇:

/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
void CatchFileNameFromTheRoute(char *route,char *FileName)
{
char *p,*q;
p=q=route;

while(*p!='\0')
{
   if('\\'==*p||'/'==*p)
    q=p;
   ++p;
}
strcpy(FileName,q+1);
}
int existChar(char A[LEN/PARAMETER],char ch) //返回字符ch所在的位子,不存在则返回0
{
int n=1;
while (A[n-1])
{
   if(A[n-1]==ch)
   {
    return n;
   }
   ++n;
}
return 0;
}
Folder_Name *find_current_folder_from_UDisk(char str[LEN/PARAMETER],Folder_Name *Root_Folder)//查找当前目录的结构体指针    str为绝对地址如"u:/folder/"
{/*运行到此函数是路径一定是存在在U盘符中的*/
char FolderName[50]="";
char *p=str+3;
Folder_Name *RootCurrentFolder=Root_Folder->FolderChild;
Folder_Name *Answer=Root_Folder;

memccpy(FolderName, p, '\\', strlen(p));

while( '\0' != FolderName[0] && RootCurrentFolder && (existChar(FolderName,'\\')||!existChar(FolderName,'.')) ) //为了拥有通用性写“'\0' != FolderName[0]”是为了memccpy函数在遇到文件名时返回空
{
   FolderName[strlen(FolderName)-1]='\0';     //结果中末尾会出现符号: '\'

   p=p+strlen(FolderName)+1;
   memccpy(FolderName, p, '\\', strlen(p));    //memccpy函数作用是从p中拷贝最多strlen(p)长度的字符串,不过会在出现字符 \ 时就马上停止
   Answer=RootCurrentFolder;
   RootCurrentFolder = RootCurrentFolder->FolderChild;
}
return Answer;
}
int existFolder(char str[LEN/PARAMETER],Folder_Name *Root_Folder)   //判断文件夹是否存在
{
char FolderName[50]="";
char *p=str+3;
Folder_Name *RootCurrentFolder=Root_Folder->FolderChild;

memccpy(FolderName, p, '\\', strlen(p));

while('\0' != FolderName[0] && RootCurrentFolder && (existChar(FolderName,'\\')||!existChar(FolderName,'.')) )
{
   FolderName[strlen(FolderName)-1]='\0';    /*此语句对于判定文件是否存在有很大问题*/
//   printf("%s %s %d\n",RootCurrentFolder->FolderName,FolderName,strcmp(RootCurrentFolder->FolderName,FolderName));
   if( strcmp(RootCurrentFolder->FolderName,FolderName) )
    return 0;
   p=p+strlen(FolderName)+1;   //FolderName[strlen(FolderName)-1]='\0';语句使得长度缩减了1
   memccpy(FolderName, p, '\\', strlen(p));
   RootCurrentFolder = RootCurrentFolder->FolderChild;
}
return 1;
}
int cdFloder_virtual_version1(char str[LEN/PARAMETER],Folder_Name *Root_Folder)/*第一中情况,在u盘符中切换*//*此函数有冗余意味*/
{/*此函数的作用是判断文件夹或是文件是否存在,通过调用existFolder函数实现*/
if(!existFolder(str,Root_Folder))   //判断文件夹是否存在,需要在兄弟文件夹找,使用
   return 0;
else
   return 1;
}
unsigned long returnlenOfFile(fat *CurrentFat)
{
if(CurrentFat)
   return (long)strlen(CurrentFat->memoryaddress->content)+returnlenOfFile(CurrentFat->next);
else
   return 0;
}
void outputFolderName(char str[LEN/PARAMETER],Folder_Name *Root_Folder)   //dir
{
Folder_Name *RootCurrentFolder=find_current_folder_from_UDisk(str,Root_Folder);
File_Name *CurrentFile=RootCurrentFolder->FileChild;

if(RootCurrentFolder==Root_Folder&&!CurrentFile&&!RootCurrentFolder->FolderChild)
{
   printf("没有任何文件及文件夹\n");
   return;
}
RootCurrentFolder = RootCurrentFolder->FolderChild;
while(RootCurrentFolder)           //输出文件夹信息
{
   printf("%-25s%9s%9s   %s\n",RootCurrentFolder->Create_Date,"

","",RootCurrentFolder->FolderName);
   RootCurrentFolder = RootCurrentFolder->FolderBrother;
}
while(CurrentFile)             //输出文件信息
{
   printf("%-25s%9s%9u   %s\n",CurrentFile->Create_Date,"",returnlenOfFile(CurrentFile->File_address),CurrentFile->FileName);
   CurrentFile = CurrentFile->File_Brother;
}
}
void cdFloder_virtual(char A[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder)
{
char str[LEN/PARAMETER],smallStr[4];/*长度上存在漏洞*/
int result;

smallStr[3]='\0';
if(!strcmp(strncpy(smallStr,A,3),"../"))
{
   printf("U盘符下不支持返回父路径命令:../\n");
   return;
}
result=cdFloder_virtual_version1(strcpy(str,A),Root_Folder);
if(result)
   strcpy(CurrentFolder,str);   //文件夹存在,允许切换目录
else
   return;
}
void printFolder_virtual(char A[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder)
{
char str[LEN/PARAMETER];/*长度上存在漏洞*/
char folderName[50];
int result;
// Folder_Name *CurrentFolder=Root_Folder->FolderChild;

if( A[0]!='\0' ) //if( A[0]!='\0' && (A[1]!=':'||(A[1]==':'&&A[0]=='u')) )
{
   result=cdFloder_virtual_version1(strcpy(str,A),Root_Folder);
   if(result)
    outputFolderName(str,Root_Folder);
   else
    return;
}
else
   outputFolderName(CurrentFolder,Root_Folder); //dir显示当前目录
}
void REname_virtual(char oldname[LEN/PARAMETER],char newname[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder)
{
Folder_Name *RootCurrentFolder=NULL;//=find_current_folder_from_UDisk(CurrentFolder,Root_Folder);
File_Name *CurrentFile=NULL;//=RootCurrentFolder->FileChild;
char old_name[LEN/PARAMETER],new_name[LEN/PARAMETER];

if(oldname[0]=='u')
{
   RootCurrentFolder = find_current_folder_from_UDisk(oldname,Root_Folder);
   CatchFileNameFromTheRoute(oldname,old_name);   //取出文件名
   CatchFileNameFromTheRoute(newname,new_name);   //取出文件名
   CurrentFile = RootCurrentFolder->FileChild;
   while(CurrentFile)
   {
    if(strcmp(CurrentFile->FileName,old_name)==0)
    {
     strcpy(CurrentFile->FileName,new_name);      //更改文件名称
     printf("文件更改成功: %s %s\n",old_name,new_name);
     return;
    }
    CurrentFile = CurrentFile->File_Brother;
   }
}
if(CurrentFile)
   printf("你所指的文件:%s不存在!\n",old_name);
}
void outPut_virtual(char A[LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder)
{
char CurrentFileName[LEN/PARAMETER]="";
Folder_Name * RootCurrentFolder=Root_Folder;
File_Name * CurrentFile;
fat *fatname;

CatchFileNameFromTheRoute(A,CurrentFileName);   //提取文件名放入CurrentFileName
RootCurrentFolder=find_current_folder_from_UDisk(A,Root_Folder); //找到当前路径
CurrentFile = RootCurrentFolder->FileChild; //找到当前路径的第一个文件
while(CurrentFile)
{
   if(!strcmp(CurrentFile->FileName,CurrentFileName))
   {
    printf("%-25s%9s%9u   %s\n",CurrentFile->Create_Date,"",returnlenOfFile(CurrentFile->File_address),CurrentFile->FileName);
   // printf("%s",CurrentFile->File_address->memoryaddress->content);
    fatname=CurrentFile->File_address;   //找到fat表
    while(fatname)
    {
     printf("%s",fatname->memoryaddress->content); //从fat表中找到存储单元绝对地址并打印
     fatname = fatname->next;       //如果文件较大占用了多个存贮单元,则寻找下一个fat表...
    }
    printf("\n文件输出成功\n");
    return ;
   }
   CurrentFile = CurrentFile->File_Brother;
}
printf("找不到文件:%s\n",A);
}
int judge_(char *A,char str[LEN/PARAMETER*2],char str2[LEN/PARAMETER*2],Folder_Name *Root_Folder)
{
char fileName[50];
strcpy(str,A);
if(str[0]=='u'&&!cdFloder_virtual_version1(str,Root_Folder))
{
   printf("指定文件不存在:%s\n",str);
   return 0;
}
if(str[strlen(str)-1]=='\\'&&str[0]!='u')
{
   CatchFileNameFromTheRoute( str2 , fileName );
   strcat(str,fileName);
}
return 1;
}
int copyFile_virtual_str_deal(char A[PARAMETER][LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],char start[LEN/PARAMETER*2],char end[LEN/PARAMETER*2],Folder_Name *Root_Folder)
{/*函数作用是寻找路径中的文件夹是否存在*/
if(!judge_(A[1],start,end,Root_Folder))
   return 0 ;
if(!judge_(A[2],end,start,Root_Folder))
   return 0;
return 1;
}
fat *FindFat(fat *head_fat) //返回一个空白fat表地址
{
int n=0;
while(!head_fat[n].memoryaddress)     //此处可以用哈希表的方式查找空白的fat表项
   ++n;
return &head_fat[n];
}
File_Name *CreateFile(fat *head_fat)
{
File_Name *fileMalloc;
time_t   lt;   /*define   a   longint   time   varible*/  
    lt=time(NULL);/*system   time   and   date*/

fileMalloc = (File_Name *)malloc (sizeof(File_Name));
strcpy(fileMalloc->Create_Date,ctime(<));     //不知怎么搞的,这里的strcpy后最后一个字符不是'\0'
fileMalloc->Create_Date[24]='\0';       //只好自己加
fileMalloc->File_address=FindFat(head_fat);
fileMalloc->File_Brother=NULL;
fileMalloc->File_Length=0;
fileMalloc->FileName[0]='\0';
return fileMalloc;
}
void CopyData(File_Name *CurrentFile,FILE *fp,int method,fat *head_fat)//method为1表示从其他盘符复制到U盘,为2表示从U盘复制到其他盘符
{
char temp;
int n=0,len=LENGTH/NUM;
fat *currentFat=CurrentFile->File_address;

if(method==1) /*从其他盘符拷到U盘符*/
{
   temp=fgetc(fp);
   while(!feof(fp))
   {
    if(n     {
     currentFat->memoryaddress->content[n]=temp;
     ++n;
    }
    else
    {
     currentFat->next=FindFat(head_fat);
     currentFat = currentFat->next;
     currentFat->memoryaddress->content[n+1]='\0';
     n=0;
    }
    temp=fgetc(fp);
   }
   currentFat->memoryaddress->content[n]='\0';
}
else/*从U盘符拷到其他盘符*/
   while(currentFat)
   {
    fprintf(fp,"%s",currentFat->memoryaddress->content);
    currentFat = currentFat->next;
   }
}
void CopyDataUtoU(File_Name *CurrentFile1,File_Name *CurrentFile2,fat *head_fat)   //把文件从虚拟磁盘上复制到虚拟磁盘上
{
fat *CurrentFat1 = CurrentFile2->File_address,*CurrentFat2 = CurrentFile2->File_address;
strcpy(CurrentFat1->memoryaddress->content,CurrentFat2->memoryaddress->content);
while(CurrentFat1->next)
{
   CurrentFat1 = CurrentFat1->next;
   CurrentFat2->next = FindFat(head_fat);
   strcpy(CurrentFat1->memoryaddress->content,CurrentFat2->memoryaddress->content);
}
}
void CopyDataFromUtoU(char start[LEN/PARAMETER*2],char end[LEN/PARAMETER*2],fat *head_fat,Folder_Name *Root_Folder)//从U盘符复制到U盘
{
Folder_Name *RootCurrentFolder1,*RootCurrentFolder2;
File_Name *CurrentFile1=NULL,*CurrentFile2=NULL;
char startFileName[50],endFileName[50];

CatchFileNameFromTheRoute(start,startFileName);   //提取文件名
CatchFileNameFromTheRoute(end,endFileName);    //提取文件名
RootCurrentFolder1=find_current_folder_from_UDisk(start,Root_Folder); //找到当前目录
CurrentFile1 = RootCurrentFolder1->FileChild;
while(!strcmp(CurrentFile1->FileName,startFileName))     //找到当前目录下指定的文件
{
   CurrentFile1 = CurrentFile1->File_Brother;
}
RootCurrentFolder2=find_current_folder_from_UDisk(end,Root_Folder);   //找到当前目录
if(!RootCurrentFolder2->FileChild)           //判断该文件夹下是否已经有文件
{
   RootCurrentFolder2->FileChild=CreateFile(head_fat); //创建文件
   CatchFileNameFromTheRoute(startFileName,RootCurrentFolder2->FileChild->FileName); //赋予文件名
   CopyDataUtoU(CurrentFile1,RootCurrentFolder2->FileChild,head_fat);            //并复制
}
else
{
   CurrentFile2=RootCurrentFolder2->FileChild;
   while(CurrentFile2->File_Brother)
    CurrentFile2 = CurrentFile2->File_Brother;
   CurrentFile2->File_Brother=CreateFile(head_fat);   //创建文件
   CatchFileNameFromTheRoute(startFileName,CurrentFile2->File_Brother->FileName);
   CopyDataUtoU(CurrentFile1,CurrentFile2->File_Brother,head_fat);      //并复制
}
}
int initialcopyFile(FILE **fp,char str[LEN/PARAMETER*2],int ch,fat *head_fat,Folder_Name *Root_Folder)
{/*初始化文件复制,且其中两个路径中分别为两个盘符,其中一个为U盘符*/
Folder_Name * RootCurrentFolder=Root_Folder;
File_Name * CurrentFile;

if(ch==1)
   *fp=fopen(str,"r");
else
   *fp=fopen(str,"w");
if(*fp==NULL)
{
   printf("Can't open the file: %s.\n",str);
   return 0;
}
if(!RootCurrentFolder->FileChild)
{
   RootCurrentFolder->FileChild=CreateFile(head_fat); //创建文件
   CatchFileNameFromTheRoute(str,RootCurrentFolder->FileChild->FileName);
   CopyData(RootCurrentFolder->FileChild,*fp,ch,head_fat);            //并复制
}
else
{
   CurrentFile=RootCurrentFolder->FileChild;
   while(CurrentFile->File_Brother)
    CurrentFile = CurrentFile->File_Brother;
   CurrentFile->File_Brother=CreateFile(head_fat);   //创建文件
   CatchFileNameFromTheRoute(str,CurrentFile->File_Brother->FileName);
   CopyData(CurrentFile->File_Brother,*fp,ch,head_fat);      //并复制
}
return 1;
}
void copyFile_virtual(char A[PARAMETER][LEN/PARAMETER],char CurrentFolder[LEN/PARAMETER],fat *head_fat,Folder_Name *Root_Folder)
{
char start[LEN/PARAMETER*2],end[LEN/PARAMETER*2];
FILE *fp=NULL;
Folder_Name *RootCurrentFolder;

if(!copyFile_virtual_str_deal(A,CurrentFolder,start,end,Root_Folder))
   return ;
if(start[0]!='u')
{
   RootCurrentFolder=find_current_folder_from_UDisk(end,Root_Folder);
   if(!initialcopyFile(&fp,start,1,head_fat,RootCurrentFolder))
    return;
}
if(end[0]!='u')
{
   RootCurrentFolder=find_current_folder_from_UDisk(start,Root_Folder);
   if(!initialcopyFile(&fp,end,2,head_fat,RootCurrentFolder))
    return;
}
if(fp==NULL)
   CopyDataFromUtoU(start,end,head_fat,Root_Folder);
else
   fclose(fp);
printf("\n已复制         1 个文件。\n");
}

阅读(434) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-03-19 12:04:12

(一):5codes http://www.pudn.com/downloads80/sourcecode/windows/vxd/detail310642.html 所属分类: Driver Develop 开发工具: Visual C++ 文件大小: 16492 KB 上传时间: 2007-07-22 下载次数: 23 提 供 者: 王妃 详细说明:Windows XP WDM 驱动实例,使用DriverWorks编写,均通过调试-Windows XP WDM driver instance, the use of DriverWorks prepared through debugging (二):serial http://www.pudn.com/downloads/sourcecode/windows/vxd/detail705.html 所属分类: Driver Develop 开发工具: Visual C++ 文件大小: 59 KB 上传时间: 2001-01-06 下载次数: 1628

chinaunix网友2010-03-19 11:59:17

# 程序名称:虚拟打印机源码 v1.0 (完整源码) # 文件大小:351KB # 更新时间:2006-08-27 # 开发工具:C/C++ # url:[http://www.ccrun.com/view.asp?id=201]