
//--------------------------------------------------------------------
// Class DirFile
//
// Purpose  : handle dir identity, and data content
//--------------------------------------------------------------------

public class DirFile
{

	private String absolutePath;
	private String dirName;
	private String fileContent;
//	private int currIndex;


//--------------------------------------------------------------------
// Contructor for DirFile (1)
//
// Input	: content of dirFile, parent absolutePath, dirName
// Output   : None
// Purpose  : contructing the directory data structure
//--------------------------------------------------------------------
	
	DirFile (String fileContent, String parentPath, String dirName)
	{
		this.dirName = dirName;
		this.absolutePath = parentPath + dirName + "/";
		int endIndex = fileContent.indexOf("(total files,");
		endIndex = fileContent.indexOf(")", endIndex);
		this.fileContent = fileContent.substring(0, endIndex + 2);
	}


//--------------------------------------------------------------------
// Contructor for DirFile (2)
//
// Input	: parentMFTIndex, dirMFTIndex, parentAbsolutePath, dirName
// Output   : None
// Purpose  : contructing newly-built directory data structure
//--------------------------------------------------------------------
	
	DirFile (int parentIndex, int index, String parentPath, String dirName)
	{
		// (<fileName>,<MFTIndex>)\n
		// :
		// :
		// (total files,<totalNoOfFiles>)\n

		this.fileContent = new String();
		this.fileContent = "(.," + Integer.toString(index) + ")\n"
			               + "(..," + Integer.toString(parentIndex) + ")\n"
						   + "(total files," + Integer.toString(2) + ")\n";
		this.dirName = dirName;
		this.absolutePath = parentPath + dirName + "/";
	}


//--------------------------------------------------------------------
// Contructor for DirFile (3)
//
// Input	: standard Master File Table Index
//            standard Free Space Manager Index
//            standard Root Index
//            root (volume) name
// Output   : None
// Purpose  : contructing the standard directory called during format
//--------------------------------------------------------------------

	DirFile (int stdMFTIndex, int stdFSMIndex, int stdRootIndex, String rootName)
	{
		// (<fileName>,<MFTIndex>)\n
		// :
		// :
		// (total files,<totalNoOfFiles>)\n

		this.fileContent = new String();
		this.fileContent = "(.," + Integer.toString(stdRootIndex) + ")\n"
			               + "(..," + Integer.toString(stdRootIndex) + ")\n"
			               + "(FSM," + Integer.toString(stdFSMIndex) + ")\n"
			               + "(MFT," + Integer.toString(stdMFTIndex) + ")\n"
						   + "(total files," + Integer.toString(4) + ")\n";
		this.dirName = rootName;
		this.absolutePath = dirName + "/";
	}



//--------------------------------------------------------------------
// Method getAbsolutePath
//
// Input	: None
// Output   : absolutePath
// Purpose  : to get AbsolutePath
//--------------------------------------------------------------------

	public String getAbsolutePath ()
	{
		return absolutePath;
	}


//--------------------------------------------------------------------
// Method getDirName
//
// Input	: None
// Output   : directory Name
// Purpose  : to get directory Name
//--------------------------------------------------------------------

	public String getDirName ()
	{
		return dirName;
	}


//--------------------------------------------------------------------
// Method getParentPath
//
// Input	: None
// Output   : parent path
// Purpose  : to get parent path
//--------------------------------------------------------------------

	public String getParentPath ()
	{
		return absolutePath.substring(0, 
			absolutePath.length() - dirName.length() -1);
	}
	

//--------------------------------------------------------------------
// Method getContent
//
// Input	: None
// Output   : directory file content
// Purpose  : to get file Content
//--------------------------------------------------------------------

	public String getContent ()
	{
		return fileContent;
	}


//--------------------------------------------------------------------
// Method getSize
//
// Input	: None
// Output   : size of the file
// Purpose  : to get file Size
//--------------------------------------------------------------------

	public int size ()
	{
		return fileContent.length();
	}


//--------------------------------------------------------------------
// Method getNoOfFiles
//
// Input	: None
// Output   : total number of the file undex the directory
// Purpose  : to total number of file
//--------------------------------------------------------------------

	public int getNoOfFiles ()
	{ 
		return getValue("total files"); 
	}


//--------------------------------------------------------------------
// Method setNoOfFiles
//
// Input	: noOfFiles
// Output   : none
// Purpose  : to set number of files
//--------------------------------------------------------------------

	private void setNoOfFiles (int noOfFiles)
	{
		setValue("total files", noOfFiles); 
		return;
	}


//--------------------------------------------------------------------
// Method isExist
//
// Input	: fileName
// Output   : status if directory contain that file
// Purpose  : check file under the directory
//--------------------------------------------------------------------

	public boolean isExist (String fileName)
	{
		return (fileContent.indexOf("(" + fileName + ",") != -1);
	}


//--------------------------------------------------------------------
// Method isEmpty
//
// Input	: none
// Output   : status if there is any file under that directory
// Purpose  : check if directory file contain anything
//--------------------------------------------------------------------

	public boolean isEmpty ()
	{
		return (getNoOfFiles() <= 2);
	}


//--------------------------------------------------------------------
// Method deleteEntry
//
// Input	: fileName
// Output   : status if deletion is successful
// Purpose  : to delete file from the directory
//--------------------------------------------------------------------

	public boolean deleteEntry (String fileName)
		throws FileNotExistException
	{
		if (!isExist(fileName))
			throw new FileNotExistException(fileName);

		int beginIndex, endIndex;
		beginIndex = fileContent.indexOf("(" + fileName + ",");
		endIndex = fileContent.indexOf(")\n", beginIndex);
		fileContent = new String(fileContent.substring(0, beginIndex)
							+ fileContent.substring(endIndex + 2));

		setNoOfFiles(getNoOfFiles() - 1);
		return true;
	}


//--------------------------------------------------------------------
// Method newEntry
//
// Input	: fileName
// Output   : status if insertion is successful
// Purpose  : to insert newfile into directory
//--------------------------------------------------------------------

	public boolean newEntry (String fileName, int value)
		throws FileAlreadyExistException
	{
		if (isExist(fileName)) 
			throw new FileAlreadyExistException(fileName);

		int beginIndex, endIndex;

		String tmpString;
		beginIndex = fileContent.indexOf("(..,");

		while (true)
		{
			beginIndex = fileContent.indexOf("(", beginIndex + 1);
			endIndex = fileContent.indexOf(",", beginIndex);
			tmpString = fileContent.substring(beginIndex + 1, endIndex);
			if (tmpString.equals("total files")) break;
			if (tmpString.toUpperCase().compareTo(fileName.toUpperCase()) > 0)
				break;
		}

		tmpString = fileContent.substring(0, beginIndex)
				    + "(" + fileName + "," + Integer.toString(value) + ")\n"
				    + fileContent.substring(beginIndex);

		fileContent = new String(tmpString);
		setNoOfFiles(getNoOfFiles() + 1);
		return true;
	}


//--------------------------------------------------------------------
// Method setValue
//
// Input	: fileName, value (MFTIndex)
// Output   : none
// Purpose  : to set the MFTIndex for the file
//--------------------------------------------------------------------

	private void setValue (String fileName, int value)
	{
		int beginIndex, endIndex;
		if ( (beginIndex = fileContent.indexOf("(" + fileName + ","))
			== -1 )	return;
		beginIndex = fileContent.indexOf(",", beginIndex);
		endIndex = fileContent.indexOf(")\n", beginIndex);
		String tmpString;
		tmpString = fileContent.substring(0, beginIndex + 1)
					+ Integer.toString(value)
					+ fileContent.substring(endIndex);
		fileContent = new String();
		fileContent = tmpString;
		return ;
	}


//--------------------------------------------------------------------
// Method getValue
//
// Input	: fileName
// Output   : none
// Purpose  : to get the MFTIndex of the file
//--------------------------------------------------------------------

	public int getValue (String fileName)
	{
		int beginIndex, endIndex;
		if ( (beginIndex = fileContent.indexOf("(" + fileName + ","))
			== -1 )	return -1;
		beginIndex = fileContent.indexOf(",", beginIndex);
		endIndex = fileContent.indexOf(")\n", beginIndex);
		return Integer.parseInt(fileContent.substring(beginIndex + 1, endIndex));
	}


//--------------------------------------------------------------------
// Method getFileName
//
// Input	: dirIndex
// Output   : none
// Purpose  : get the fileName at that dirIndex
//--------------------------------------------------------------------

	public String getFileName (int dirIndex)
	{
		int beginIndex = fileContent.indexOf(",", 0);
		for (int i = 0; i < dirIndex ; i++)
			beginIndex = fileContent.indexOf(",", beginIndex + 1);

		return fileContent.substring
			   (fileContent.lastIndexOf("(", beginIndex) + 1,
			    beginIndex);
	}


//--------------------------------------------------------------------
// Method getMFTIndexAt
//
// Input	: dirIndex
// Output   : none
// Purpose  : get the MFTIndex at that dirIndex
//--------------------------------------------------------------------

	public int getMFTIndexAt (int dirIndex)
	{
		int beginIndex = fileContent.indexOf(",", 0);
		for (int i = 0; i < dirIndex ; i++)
			beginIndex = fileContent.indexOf(",", beginIndex + 1);

		return Integer.parseInt(fileContent.substring
			    (beginIndex + 1,
			     fileContent.indexOf(")\n", beginIndex)));
	}

}

//--------------------------------------------------------------------
// End Class DirFile
//--------------------------------------------------------------------