|
楼主 |
发表于 2004-11-23 22:52:04
|
显示全部楼层
#include <stdio.h>
#include <stdlib.h>
//#define DEBUG
typedef struct _FILEINDEX
{
int signEmpty; //0:empty; 1:be used;
char* name;
int start;
int length;
} FILEINDEX;
int fat[100] = {0}; //create & init fat;
//int* p_fat = fat; //fat point;
int fatFree = 100; //free spaces int fat, initialize value is 100;
FILEINDEX fileIndex[100];
char* command; //command which users input;
char* commandOperator;
int operatorStatus = -1; //-1:invalid; 0:write; 1:insert;
char* fileName; //for both write & insert;
int commandNumber; //for both write & insert;
void pause()
{
printf("Press enter to continue...\n");
getchar();
}
void commandAnalyse()
{
#ifdef DEBUG
printf("start command analyse\n");
#endif
int i;
char* p;
p = command;
//Analyse operator
commandOperator = (char*) malloc (7);
memset(commandOperator, '\0', 10);
i = 0;
while(*p != '(')
{
if(*p == ' ')
{
i--;
p++;
continue;
}
else
{
memcpy(commandOperator + i, p, 1);
i++;
p++;
continue;
}
}
if(strcmp(commandOperator, "write") == 0)
operatorStatus = 0;
if(strcmp(commandOperator, "insert") == 0)
operatorStatus = 1;
#ifdef DEBUG
printf("operate confirm:%s, Status = %d\n", commandOperator, operatorStatus);
#endif
p++; //jump the '(';
#ifdef DEBUG
printf("after jump the '(', now *p is [%c]\n", *p);
#endif
fileName = (char*) malloc (20);
memset(fileName, '\0', 20);
i = 0;
while(*p != ',')
{
if(*p == ' ')
{
p++;
continue;
}
else
{
memcpy(fileName + i, p, 1);
i++;
p++;
continue;
}
}
#ifdef DEBUG
printf("filename confirm:%s\n", fileName);
#endif
//jump the ','
p++;
#ifdef DEBUG
printf("after jump the ',', now *p is [%c]\n", *p);
#endif
//Analyse number
while(*p != ')')
{
#ifdef DEBUG
printf("In number analyse, now *p is [%c]\n", *p);
#endif
if(*p == ' ')
{
p++;
continue;
}
else if(*p >= '0' && *p <= '9')
{
commandNumber = commandNumber * 10 + (*p - '0');
p++;
continue;
}
else
{
printf("Input error!\n");
pause();
exit(0);
}
}
free(p);
#ifdef DEBUG
printf("Command analyse completed:\n");
printf("op:%s; filename:%s; number:%d\n", commandOperator, fileName, commandNumber);
#endif
}
void write()
{
#ifdef DEBUG
printf("---so we Wirte---\n");
#endif
//check filename;
int i, j;
int overwrite = -1; // "-1" is an invalid value, means no same filename;
int overwriteSno = -1; //"-1" is an invalid value.
for(i = 0; i < 100; i++)
{
if(fileIndex.signEmpty == 1)
{
if(strcmp(fileIndex.name, fileName) == 0) //file is already existed;
{
#ifdef DEBUG
printf("Same filename exists.\n");
#endif
printf("File %s is already existed.\n");
printf("0.overwrite\n");
printf("1.cancle\n");
printf("(0-1)");
scanf("%d", &overwrite);
getchar();
if(overwrite == 1)
{
printf("cancel!\n");
pause();
return;
}
else if(overwrite == 0) //overwrite, do nothing here;
{
overwriteSno = i;
break;
}
else
{
printf("unknown error!!!\n");
pause();
exit(0);
}
}
else
continue;
}
}
//check for free space;
int spaceEnough = -1; // "-1" is an invalid value, 0:not enough; 1:enough;
if(overwrite == 0)
{
#ifdef DEBUG
printf("Overwrite!\n");
#endif
if(fatFree + fileIndex[overwriteSno].length >= commandNumber)
spaceEnough = 1;
else
spaceEnough = 0;
}
else if(overwrite == -1)
{
#ifdef DEBUG
printf("New file!\n");
#endif
if(fatFree >= commandNumber)
spaceEnough = 1;
else
spaceEnough = 0;
}
#ifdef DEBUG
printf("Free space:%d\n", fatFree);
#endif
if(spaceEnough == 0) //no enough space
{
printf("There is no enough space, task terminated!\n");
pause();
return;
}
if(overwrite == 0) //overwrite!
{
int k, l;
k = fileIndex[overwriteSno].start;
while(fat[k] != -1)
{
l = k;
k = fat[k];
fat[l] = 0;
fatFree++;
}
fat[k] = 0;
fatFree++;
free(fileIndex[overwriteSno].name);
fileIndex[overwriteSno].length = 0;
fileIndex[overwriteSno].start = 0;
fileIndex[overwriteSno].signEmpty = 0;
}
//begin write;
for(i = 0; i < 100; i++)
{
if(fileIndex.signEmpty == 0)
break;
}
for(j = 0; j < 100; j++)
{
if(fat[j] == 0)
break;
}
fileIndex.name = (char*) malloc (strlen(fileName) + 1);
memset(fileIndex.name, '\0', strlen(fileName) + 1);
memcpy(fileIndex.name, fileName, strlen(fileName));
fileIndex.length = commandNumber;
fileIndex.start = j;
fileIndex.signEmpty = 1;
int current = fileIndex.start; //sign the current part of file
for(i = 0; i < commandNumber - 1; i++)
{
for(j = current + 1; j < 100; j++)
{
if(fat[j] == 0)
{
fat[current] = j;
current = j;
fatFree--;
break;
}
}
}
fat[current] = -1;
fatFree--;
printf("File writing is successful!\n");
return;
}
void insert()
{
#ifdef DEBUG
printf("---so we Insert---\n");
#endif
int i, j, k, l;
int insertSno = -1; //"-1" is an invalid value.
for(i = 0; i < 100; i++)
{
if(fileIndex.signEmpty == 1)
{
if(strcmp(fileIndex.name, fileName) == 0) //file is already existed;
{
#ifdef DEBUG
printf("file exists.\n");
#endif
insertSno = i;
break;
}
else
continue;
}
else
continue;
}
if(insertSno == -1)
{
printf("file does not exist!\n");
pause();
return;
}
if(fatFree <= 0)
{
printf("no free space!\n");
pause();
return;
}
for(i = 0; i < 100; i++)
{
if(fat == 0)
break;
}
k = i; //the new record;
if(commandNumber == 1)
{
l = fileIndex[insertSno].start;
fileIndex[insertSno].start = k;
fat[k] = l;
fatFree--;
fileIndex[insertSno].length++;
return;
}
j = fileIndex[insertSno].start;
for(i = 0; i < commandNumber - 2; i++)
{
j = fat[j];
}
l = fat[j];
fat[j] = k;
fat[k] = l;
fatFree--;
fileIndex[insertSno].length++;
printf("Insert is successful!\n");
}
void viewStatus()
{
printf("File Index:\n");
printf("Sno filename start length | Sno filename start length\n");
int i;
for(i = 0; i < 100; i += 2)
{
printf("[%-2d]. %-10s%-7d%-7d | ", i, fileIndex.name, fileIndex.start, fileIndex.length);
printf("[%-2d]. %-10s%-7d%-7d\n", i + 1, fileIndex[i + 1].name, fileIndex[i + 1].start, fileIndex[i + 1].length);
}
printf("Fat:\n");
printf(" 0 1 2 3 4 5 6 7 8 9");
for(i = 0; i < 100; i++)
{
if(i % 10 == 0)
printf("\n%-3d", i / 10);
printf("[%-2d] ", fat);
}
printf("\n");
return;
}
int main()
{
fat[0] = -2; //reserved unit
//get command and analyse it
while(1)
{
//init command
command = (char*) malloc (100);
memset(command, '\0', 100);
system("cls");
printf("MYFAT>");
gets(command);
#ifdef DEBUG
printf("------------------------------\n");
printf("command confirm:%s\n", command);
#endif
if(strcmp(command, "view") == 0)
{
viewStatus();
pause();
continue;
}
if(strcmp(command, "exit") == 0)
{
printf("Bye!\n");
pause();
exit(0);
}
if(strcmp(command, "free") == 0)
{
printf("Free: %d\n", fatFree);
pause();
continue;
}
commandAnalyse();
//select write or insert
if(operatorStatus == 0)
write();
else if(operatorStatus == 1)
insert();
else
{
printf("unknown error!!\n");
pause();
exit(0);
}
free(commandOperator);
free(command);
free(fileName);
commandNumber = 0;
pause();
}
pause();
return (0);
} |
|