All pastes #412377 Raw Edit

Anonymous

public text v1 · immutable
#412377 ·published 2007-03-27 17:40 UTC
rendered paste body
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include "Filesystem.h"



File::File(string inName, int inSize)
	:name(inName),
	 size(inSize)
	{
	nextPtr = NULL;
	}


const string File::get_name()
	{
	return name;
	}

int File::get_size()
	{
	return size;
	}

List::List()
	{
	headPtr = NULL;
	}

List::~List()
	{
	cout << "Destructing List" << endl;
	while (headPtr) pop_front();
	}


void List::push_front(File *newPtr)
	{
	if (headPtr == NULL){
		headPtr = newPtr;
	} else {
		newPtr->nextPtr = headPtr;
		headPtr = newPtr;
	}
	}

void List::pop_front()
	{
	if (headPtr != NULL) {
		File *tempPtr = headPtr;
		headPtr = headPtr->nextPtr;
		delete tempPtr;
	}
	}

void List::clear()
	{
	while (headPtr != NULL) {
		pop_front();
	}
	}

void List::traverse_list(void (*funcPtr)(File&))
	{
	File *nodePtr;
	nodePtr = headPtr;
	while (nodePtr != NULL) {
		(*funcPtr)(*nodePtr);
		nodePtr = nodePtr->nextPtr;
	}
	}
	
string Filesystem::fileName;
int Filesystem::totalSize = 0;
bool Filesystem::fileExist = false;

Filesystem::Filesystem() 
	{
	}

Filesystem::~Filesystem()
	{
	}

void Filesystem::accumulate_size(File &filePtr)
	{
	totalSize += filePtr.get_size();
	}

void Filesystem::print_file_list(File &filePtr)
	{
	cout << filePtr.get_name() << "\t";
	cout << filePtr.get_size() << endl;
	}

void Filesystem::find_same_file(File &filePtr)
	{
	if (filePtr.get_name() == fileName) fileExist = true;
	}

void Filesystem::add_file(string inName, int inSize) 
	{
		fileExist = false;
		fileName = inName;
		fileList.traverse_list(find_same_file);

		if (fileExist == false){
			File *filePtr;
			filePtr = new File(inName, inSize);
			fileList.push_front(filePtr);
			cout << "File was sucessfully created." << endl;
		} else {
			cout << "A file by this name already exists, this file could not be added." << endl;
		}		
	}

void Filesystem::du()
	{
	totalSize = 0;		//resets size before each count
	fileList.traverse_list(accumulate_size);
	cout << "\nTotal Filesystem size is " << totalSize << "\n" << endl;
	}

void Filesystem::ls()
	{
	cout << "\n" << endl;	
	cout << "Name \t\t" << "Size" << endl;
	fileList.traverse_list(print_file_list);
	cout << "\n" << endl;
	}

void Filesystem::clean()
	{
	fileList.clear();
	}


int main(){

	Filesystem myFilesystem;

	int choice=1;
	string fileName;
	int fileSize;

	while (choice <= 4 && choice >= 1){	
	
		cout << "Enter 1 to Add Files" << endl;
		cout << "Enter 2 to View File List" << endl;
		cout << "Enter 3 to View Total Size of the Filesystem" << endl;
		cout << "Enter 4 to Format the File System" << endl;
		cout << "Enter 5 to Exit" << endl;

		cin >> choice;

		while (choice > 5 || choice < 1){
			cout << "Invalid Input Selection." << endl;
			cout << "Enter 1 to Add Files" << endl;
			cout << "Enter 2 to View File List" << endl;
			cout << "Enter 3 to View Total Size of the Filesystem" << endl;
			cout << "Enter 4 to Format the File System" << endl;
			cout << "Enter 5 to Exit" << endl;
	
			cin >> choice;
		}

		switch (choice)
		{
			case 1:
				cout <<	"Please enter the name of the file you wish to add: ";	
				cin >> fileName;
	
				cout << "Please enter the size of " << fileName << ": ";
				cin >> fileSize;

				myFilesystem.add_file(fileName, fileSize);
				break;
			case 2:
				myFilesystem.ls();
				break;
			case 3:
				myFilesystem.du();
				break;
			case 4:
				myFilesystem.clean();
				break;
			case 5:
				return 0;
			
			default:
				break;
		}
	}

}