tags:

views:

41

answers:

0

I am implemmenting a simulation control program for a CP/for microcomputer.When I run the program, it run without returning any errors.however, I cannot read the file that I create. I am doubtful of my openFile function.If there is anything that is not well implemented, please give me some guidance..

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include "cpm.h"

unsigned char bitmap[23];
struct CPMdirent directory[32];
int i;
int writeBit=-1;
int filePos=-1;
char filename[9];
char filetype[9];

//Create  bitmap of  0 or 1.
void displayelem(int elem)
  {
    int i=0;
   for (i=0;i<8;i++)
    {
     if(block_status((elem*8)+i) ==0)
      {
       printf("%d  ",0);
      }
     else {printf("%d  ",1);}
    }
  }


 int toggle_bit(int bit)
  {
int elem=bit/8;
int pos=bit%8;
int mask=1<<pos;
bitmap[elem]^=mask;
return bitmap[elem]&mask;
  }


 void display()
    {
int i=0;
for (i=0; i<23;i++)
{   
  if((i%2) == 0)
    if((i/2)==10)
        {
            printf("A  ");
        }
        else if((i/2)==11)
        {
                printf("B  ");
        }
        else  
        {
                printf("%d  ",i/2);
        }

    displayelem(i);
    if((i%2) != 0)
    {
       printf("\n");
    }
}
printf("\n");
 }


 //return value 0 or 1 of the the bit  in bitmap
 int block_status(int bit)
    {
int elem=bit/8;
int pos=bit%8;
int mask=1<<pos;
return bitmap[elem]&mask;
    }


void initialise()// Initialising the bitmap
  {
   memset(&bitmap[0], 0, sizeof(bitmap)); 
   memset(&directory[0],0, sizeof(directory));
   display();
  }


void listFile() //for listing files
  {
int i;
int check=0;
 printf("File Name\t\tFile Type\t\tFile Size\n");

 for (i=0;i<32; i++)
  {  if(strcmp(directory[i].filename,"")!=0)
   {
 printf("%s\t\t\t%s\t\t\t%dKB\n",directory[i].filename,
      directory[i].filetype,       directory[i].blockcount);
check=1;
    }
  }
 if (check=0)
  {printf("Don't have any file this in directory\n");}
 }


 int openFile(char name[9],char type[9])
  {
int i=0;
for (i=0;i<32;i++)
{
   if (strcmp(directory[i].filename, name)==0 && strcmp(directory
        [i].filetype, type)==0)//true>
    {
        return i;
    }
 }
  for (i=0; i<32;i++)
 {
    if(strcmp(directory[i].filename,"")==0)
    {
        directory[i].usercode =1;
        strcat(directory[i].filename,name);
        strcat(directory[i].filetype, type);
        return -1;
    }
  }
  }


  //Menu of the System
 int main()
  {
   printf("================ CP/M File System simulation =================\n\n");
   int command=8;
  while(command != 0)
   {
   printf  ("\n\n|===========================================================|\n\n");
   printf(" |1| Innitialize\t" "|2| List Directory Files\t""|3| Display Bitmap\t \n" );
   printf(" |4| Create file\t"  "|5| Read File\t\t\t"     "|6| Write File\t  \n\n" );
   printf(" |7| Delete File\t"  "|0| Quit\n\n");
   printf ("|==============================================================|\n\n\n");
   //printf("Enter a choce: ");

if (filePos!=-1)
 {printf("Currently file %s.%s opened\n\n", directory[filePos].filename,directory
 [filePos].filetype);}
else {}
//{printf("No file open\n\n");}

printf("Enter a choce -> ");
scanf("%d",&command);

switch (command)
{
 case 1:
   initialise();
   filePos=-1;
   continue;
   break;   

 case 2:
listFile();
continue;

 case 3:
display();
continue;

 case 4:
{
    printf("Enter your file name: ");
    scanf("%s",filename);
    printf("Enter file extention: ");
    scanf("%s",filetype);
    filePos = openFile(filename,filetype);
    if(filePos==-1)
    {
        printf("File %s.%s created\n",filename, filetype);
        continue;
    }
 else
   {
    printf("Opened file %s.%s\n", directory[filePos].filename , directory 
    [filePos].filetype );
    continue;
   }

 case 5:
if(filePos>-1)
{
    printf("File name: %s.%s on bit: ",directory[filePos].filename, 
      directory  [filePos].filetype);
    for (i=0; i<16; i++)
    {
        printf("%d ,",directory[filePos].blocks [i]);
    }
    printf("\n");
    continue;
}
else
{
    printf("You need to open the file first\n");
    continue;
}

    case 6:
 if(filePos!=-1)
  {
    for (i=1; i<184; i++)
    {
        if(block_status(i)==0)
        {
            toggle_bit(i);
            writeBit=i;

            break;
        }
    }

  directory[filePos].blocks[directory[filePos].blockcount]=writeBit;
  directory[filePos].blockcount ++;

}
else {printf("You need to open a file first\n\n");}
continue;
 break;}

 case 7:
 if(filePos!=-1)
 {
    for(i=0;i<directory[filePos].blockcount+1;i++)
    {
            toggle_bit(directory[filePos].blocks[i]);
            continue;
    }
    memset(&directory[filePos].blocks,0, sizeof(char)*16);
    memset(&directory[filePos].filename,0, sizeof(char)*9);
    memset(&directory[filePos].filetype,0, sizeof(char)*9);
    directory[filePos].blockcount =0;
    filePos=-1;
}
else
{
    printf("No file open\n");
}
continue;

  case 0:
{
    filePos=-1;
}
    default: break;
    }
      //getch();

     }
   return 0;
}