/*This file contains all the functions for working with virtual
 * Disks and the RWFS filesystem*/
 


#include "fs_struct.h"
#include <fstream>
#include <iostream>
using namespace std;
#include "disk.h"
////////////////////////////////////////////////////////////////////////////
bool vdisk::open(char* file)
{
	diskfile.open(file,ios::binary | ios::in | ios::out);
	/*TODO Add number of sectors calculating code*/
	return(true);//we succeeded if we got here
}



bool vdisk::make(char* file,int sectors)
{
	unsigned char zero = 0;
	char* datapointer = (char*) &zero;//points to the current data we are writing to the disk
	this->nsectors = sectors;
	this->diskfile.open(file,ios::binary | ios::in | ios::out | ios::trunc);
	
	for(int i = 0; i<sectors;i++)//write sectors to disk
	{
		for(int t = 0; t<512; t++)//write a sector
		{
			this->diskfile.write(datapointer,sizeof(zero));
		}
	}
	
	return(true);//we succeeded if we got here
}

bool vdisk::write(char* data,int size,int sector)//write data to sector
{

	this->offset = (sector*512)-512;
	this->diskfile.seekp(offset);
	
	for(int i = 0; i<size; i++)
	{
		this->diskfile.write(data,1);
		data++;
	}
	return(true);

}

bool vdisk::read(int nsector,sector_t &sector)
{
	this->offset = (nsector*512)-512;
	this->diskfile.seekg(offset);
	diskfile.read(((char*)sector),512);
	return(true)'
}//read data from sector
/////////////////////////////////////////////////////////////////////////
 

bool rwfs::init(vdisk* disk)
{
	fs_disk = disk;
	return(true);
}
 
 