BobR said:
(e-mail address removed) wrote in message ...
OK, I can't be sure yet, but I think your one of your problems is with
'FilesQuickSort()'.
Comment that out of your program temporarily (read below first).
Actually my QuickSort works. I had to overload operators for it to do
so, but it seems to be fine.
I don't know what is in your "File.h".
< just a thought >
Now, here is a bigger problem. You use a pointer to a dynamic allocated array
('*Files'). If you do anything that needs to copy the Directory class object
(std containers use copy constructor), the default copy constructor just
copies the pointer, no new allocation takes place. That's is a big problem,
all the copies are trying (could be) to work on the same realestate. That may
be the problem in 'FilesQuickSort()'.
Rule Of Three (or two, if you use smart pointers)!
I know. And I think the destructor is going to be a pain too. I think I
will have to loop through the array and delete each pointer to each
file.
The problem that this solves? is that I need to be able to 'loop' and
create a new object multiple times. I don't know how to do that except
with a pointer; as in
x = new file.
Also if I 'hard-coded' file x, then it would go out of scope and be
deleted at the end of the function even if I put it into a public array
would it not?
Let's do a test:
class Directory{
private:
// Prevent assignment and copy-construction:
Directory( Directory const & ); // just this, don't define it.
void operator=( Directory const & );
// rest of your class
}; // class Directory
Now, try to compile it. Work? ( it should not ).
Umm... it did work actually. Here is the 'new' header:
***********************************************************
#ifndef Directory_h
#define Directory_h
#include <cstdlib>
#include <iostream>
#include <string>
#include "File.h"
#include <vector>
//using namespace std;
class Directory{
private:
// Prevent assignment and copy-construction:
Directory( Directory const & ); // just this, don't define it.
void operator=( Directory const & );
/*
class Directory
{
private: */
string Name; // Name of the Directory
unsigned int NumSubDirs, NumFiles, MaxSubDirs, MaxFiles;
int Level;
File *Files; //Pointer to an array of Files
vector<Directory*> Directories; // vector to pointers to
directories
//vector<Directory*>::iterator iter;
Directory *Parent; // pointer to this directories parent dir
/*
DoubleDirectories
Input: Directories (array)
Output: None
Note: Double the size of the Directories array. Initially all
arrays will start at 5 (just because).
*/
void DoubleDirectories();
/*
DoubleFiles
Input: Files (array)
Output: None
Note: Double the size of the Files Array. Initially all
arrays will start at 5 (just because).
*/
void DoubleFiles();
/*
ClearFiles
Input: None
Output: None
Note: Used to delete all Files from the directory.
*/
void ClearFiles();
/*
ClearDirectories
Input: None
Output: None
Note: Used to delete all sub-directories from this directory.
*/
void ClearDirectories();
/*
FindFile
Input: Array of Files, first position, last position key.
Output: Int (position in the array of the item if found,
otherwise
-1)
*/
int FindFile(File sortedArray[], int first, int last,
string key);
public:
/*
Constructor
Input: None
Output: None
Note: Create a new Directory object which is empty.
*/
Directory();
/*
Constructor
Input: N (directory name)
Output: None
Note: Creates a new empty Directory named Name
*/
Directory(Directory *Parent, string N);
/*
Empty
Input: None
Output: Bool
Note: Used to check to see if the directory is empty. A directory
is empty if it has no files and no subdirectories.
}
*/
bool Empty();
/*
Operator ==
Inputs: string D (directory name)
Output: bool
Note: Used to detect if a directory name matches a string.
For example, two Directories of the same parent cannot
have the same name.
*/
bool operator==(const string &D);
/*
operator <
Inputs: Directory D
Outputs: bool
Notes: Used to determine if the NAME of the directory is
less than (before) the one compared.
*/
bool operator<(const Directory & D); //Note to self pointer to
D???
/*
operator <
Inputs: DN (Directory name)
Outputs: bool
Notes: If the Directory name is < the 'other' Directory name True.
*/
bool operator<(string DN);
/*
operator >
Inputs: Directory D
Outputs: bool
Note: Used to determine if the NAME of the directory is greater
than the other.
*/
bool operator>(const Directory & D);
/*
operator >
Inputs: DN (string file name)
Outputs: bool
Note: Uses a string value to compare a string to the NAME
of the directory.
*/
bool operator>(string DN);
/*
Insert
Input: CF (Current file)
Output: bool
Note: Will return false if the file cannot be inserted into
the directory. This will occur if the filename is already
in
use.
*/
bool Insert(File CF);
/*
MKFile (make file)
Input: Filename
Output: bool
Note: Used to prompt for input to create a file, then uses
Insert
to insert the file into the files array.
*/
bool MKFile();
/*
Remove
Input: FN (File name)
Output: bool
Note: Will remove a file from the directory if it can be
found.
Will return false if the file was not found.
*/
bool Remove(string FN);
/*
Create
Input: Dir (Subdirectory)
Output: bool
Note: Will attempt to add a subdirectory to this directory.
Returns
false if this cannot be done (duplicate).
*/
bool Create(string Dir);
bool MKDir();
/*
Delete
Input: DN (Directory Name)
Output: None
Note: Used to delete a subdirectory and all of it's
contents.
*/
bool Delete(string DN);
bool Delete();
bool DeleteFile(int i);
/*
Move
Input: DN (Directory Name)
Output: bool
Note: Will move to the parent directory (..) or a
subdirectory
as required. Returns true or false for success. Asking
for
parent of the root will return false. Asking for a subdir
where it cannot be found will return false.
*/
bool Move(string DN);
/*
Print
Input: None
Output: bool
Note: Will print the contents of this directory(files) and ALL
SUBDIRECTORIES. Uses the property Level to determin how
many characters to indent before each line of output.
will return false if there is nothing to print.
*/
void Print();
/*
SMPrint (Small Print)
Input: None
Output: None
Note: Prints only the content of the current directory and
not
of any subdirectories.
*/
void SMPrint();
/*
GetNumFiles
Input: None
Output: None
*/
int GetNumFiles();
void insertionSort ( File A [ ], int low, int high );
//void insertionSort (vector<Directory*> A, int low, int
high);
inline void swap ( File & x, File & y );
//inline void swap (Directory & x, Directory & y);
/*
QuickSort
*/
void QuickSort ( File A [ ], int low, int high );
//void QuickSort (vector<Directory> A, int low, int high);
/*
FindFile
*/
int FindFile(File sortedArray[], int first, int last, int
key);
/*
GetFileCount
*/
int FileCount();
/*
FindFile
Input: F (File)
Output: bool
Note: will find a file matching the file passed in will return
true if found.
*/
bool FileExists(File &F);
string GetName();
};
#endif
**********************************************
A little side note: a class defaults to 'private', so you don't need to put
'private:' at the very top of your class.
class Directory{
// this part is private
public:
// this part is public
private:
// this part is private again
}; // class Directory
The prof is big on seeing those words though. It does make it clearer
too.
Your class also needs a destructor that will clean up 'Files'. As is, you
have a memory leak.
Yup. A big nasty one. I will have to loop through and delete each File
from the array, then the array. Big pain, but I think that's the point.
Directory::~Directory(){
delete[] Files;
// and since your vector holds pointers to 'new' objects.
for( size_t i(0); i < Directories.size(); ++i ){
delete Directories.at( i );
}
} // Dtor
If the vector held actual objects, it would do all the cleanup for you.
How can I dynamically create actual objects though? They do go out of
scope at the end of the function don't they?
If you had put the FilesQuickSort() call inside the try{}, you may have seen
a message. Try it and see (may give another clue).
I think the old bug is quite fixed. I have been using it for a while
with no problem, even adding subdirectories. The QuickSort works for my
Files (putting them in alphabetical order).
The sort for the vector however does not seem to. In fact I cannot see
it changing anything.
I added code to read a comma delimited file with a bunch of random
words in it. I use that to create directories (who wants to type all
that!) the vector sort is given at the end of each addition, I then
list the directory and the files come back out in the order they went
in.
My suspicion is that they are sorted by the pointer, not the thing
pointed to. I am looking at that but....
here is the new cpp file, it has changed a little to accomodate things
like automated directory addition.
*********************************
#include "Directory.h"
#include <stdexcept> // for exception
//#include <iostream>
//#include <cstdlib>
//#include <string>
/*
Constructor
*/
Directory:
irectory()
{
Name = "";
NumSubDirs = 0;
NumFiles = 0;
MaxSubDirs = 5;
MaxFiles = 5;
Level = -1;
//Directories.resize(MaxSubDirs){NULL};
Directories.clear();
//Directories.push_back(new Directory);
Files = new File[MaxFiles];
}
Directory:
irectory(Directory *Parent, string N)
{
Name = N;
NumSubDirs = 0;
NumFiles = 0;
MaxSubDirs = 5;
MaxFiles = 5;
Level = -1;
Directories.clear();
Files = new File[MaxFiles];
}
/*
operator==
*/
bool Directory:
perator==(const string & D)
{
if(Name != D)
return false;
else
return true;
}
bool Directory:
perator<(const Directory & D)
{
// both names are strings so this should work.
//if the Directory Name is >, then false
if(Name > D.Name)
return false;
//if the Directory Name is <, then true
if(Name < D.Name)
return true;
}
/*
operator>
*/
bool Directory:
perator>(const Directory & D)
{
if(Name < D.Name)
return false;
if(Name > D.Name)
return true;
}
/*
DoubleFiles (Double array file size)
*/
void Directory:
oubleFiles()
{
File * OldArray = Files;
Files = new File[MaxFiles * 2];
for(int i = 0; i <= MaxFiles -1; i++)
{
Files
= new File(OldArray.GetFileName());
}
delete [] OldArray;
MaxFiles = MaxFiles * 2;
}
//Empty()
bool Directory::Empty()
{
if(NumSubDirs == 0 && NumFiles == 0)
return true;
else
return false;
}
//MKFile()
bool Directory::MKFile()
{
string FN; //File name
//system("cls"); // clear screen
int i = 0;
bool ValidName = false;
File *tmpFile = new File;
//Get the name of the new file.
do
{
if(i > 0)
{
cout << "Please enter a valid file name. No spaces and must ";
cout << "include a dot." << endl;
}
cout << "Enter file name: ";
getline(cin, FN);
//Check to see if that file name is allowed
ValidName = tmpFile->ValidName(FN);
i++;
}while(FN.empty() || ValidName == false);
if(NumFiles > 0 && FindFile(Files, 0, NumFiles, FN)< 0)
{
// this would be a unique file
Files[NumFiles] = tmpFile;
if(NumFiles == MaxFiles -1)
{
DoubleFiles(); // double the size of the files array
}
NumFiles++;
QuickSort (Files, 0, NumFiles -1 );
}
else if(NumFiles == 0)
{
// this would be a unique file
Files[NumFiles] = tmpFile;
if(NumFiles == MaxFiles -1)
{
DoubleFiles(); // double the size of the files array
}
NumFiles++;
}
else
{
// Error checking
//cout << "NumFiles: " << NumFiles << endl;
//cout << "FindFiles = " << FindFile(Files, 0, NumFiles, FN);
delete tmpFile;
cout << "** Error: File already exists. Aborted." << endl;
system("PAUSE");
}
}
bool Directory::MKDir()
{
string DN; // directory name
int i = 0; // a counter
bool result;
//Get the name of the new file.
do
{
if(i > 0)
{
cout << "Please enter a valid Directory name. No spaces and
must ";
cout << "not include a dot." << endl;
}
cout << "Enter directory name: ";
getline(cin, DN);
//Check to see if that Directory name is allowed
//ValidName = tmpFile->ValidName(FN);
i++;
}while(DN.empty()); //|| ValidName == false
//create the directory
result = Create(DN);
}
bool Directory::Create(string DN)
{
Directory *ND = new Directory(this, DN);
Directories.push_back(ND);
NumSubDirs++;
std::sort(Directories.begin(), Directories.end());
return true;
}
bool Directory:elete()
{
int index, i;
string Item;
bool result;
system("cls");
//SMPrint();
do
{
if(i > 0)
{
cout << "Enter the name of the item you wish to delete: " <<
endl;
cout << "->";
}
getline(cin, Item);
i++;
}while(Item == "");
//search for a file with that name
index = FindFile(Files, 0, NumFiles, Item);
//cout << "FindFile index = " << index << endl;
if(index > -1)
{
result = DeleteFile(index);
cout << "Deleted." << endl;
system("PAUSE");
}
}
bool Directory:eleteFile(int i)
{
int j;
//create a new array and copy all elements to it except the one to be
// deleted
File * OldArray = Files;
*Files = new File[MaxFiles];
for(j = 0; j <= NumFiles -1; j++)
{
//cout << "j = " << j << endl;
if(j < i)
{
Files[j] = new File(OldArray[j].GetFileName());
//cout << "Moved " << OldArray[j].GetFileName() << " to Files"
<< endl;
}
else
{
Files[j] = new File(OldArray[j+1].GetFileName());
//cout << "Moved " << OldArray[j+1].GetFileName() << " to
Files" << endl;
}
}
NumFiles--; //decriment NumFiles
delete [] OldArray;
return true;
}
//GetNumFiles
int Directory::GetNumFiles()
{
return NumFiles;
}
//SMPrint (Small Print)
void Directory::SMPrint()
{
//cout << NumFiles;
if( NumFiles > 0)
{
for(int i = 0; i <= NumFiles -1; i++)
{
Files.Print();
cout << endl;
}
}
if(Directories.size() > 0)
{
try
{
for(size_t i(0); i < Directories.size(); ++i)
{
cout << Directories.at( i )->GetName()<< endl;
} // for(i)
} // try
catch( std:ut_of_range &Oor )
{
std::cerr<<" caught: "<<Oor.what()<<std::endl;
}
catch(...)
{
std::cerr<<" caught: some other error."<<std::endl;
}
}
cout << NumFiles << " Files and " << Directories.size() << "
Directories" << endl;
}
void Directory::insertionSort ( File A [ ], int low, int high )
{
for ( int p = low+1; p <= high; p++ )
{
File tmp = A [ p ];
int j;
for ( j = p; j > low && tmp < A [ j-1 ]; j-- )
A [ j ] = A [ j-1 ];
A [ j ] = tmp;
}
}
/*
void Directory::insertionSort ( vector<Directory*> A , int low, int
high )
{
for ( int p = low+1; p <= high; p++ )
{
Directory *tmp = A [ p ];
int j;
for ( j = p; j > low && tmp < A [ j-1 ]; j-- )
A [ j ] = A [ j-1 ];
A [ j ] = tmp;
}
}
*/
inline void Directory::swap ( File & x, File & y )
{
File temp = x;
x = y;
y = temp;
}
/*
inline void Directory::swap (Directory &x, Directory &y)
{
Directory temp = x;
x = y;
y = temp;
}
*/
//QuickSort
// Best and average case time complexity of quickSort : O(n log n )
// Worst case time complexity of quickSort : O(n^2)
//QuickSort
// Best and average case time complexity of quickSort : O(n log n )
// Worst case time complexity of quickSort : O(n^2)
void Directory::QuickSort ( File A [ ], int low, int high )
{
if ( low + 10 > high )
insertionSort ( A, low, high );
else
{
// Sort low, middle and high
int middle = ( low + high ) / 2;
if ( A [ middle ] < A [ low ] )
swap ( A [ low ], A [ middle ] );
if ( A [ high ] < A [ low ] )
swap ( A [ low ], A [ high ] );
if ( A [ high ] < A [ middle ] )
swap ( A [ middle ], A [ high ] );
// Place pivot at position high - 1
File pivot = A [ middle ];
swap ( A [ middle ], A [ high-1 ] );
// Partition around the pivot
int i, j;
for ( i = low, j = high-1; ; )
{
while ( A [ ++i ] < pivot ) { }
while ( pivot < A [ --j ] ) { }
if ( i < j )
swap ( A [ i ], A [ j ] );
else
break;
}
swap ( A [ i ], A [ high-1 ] );
QuickSort ( A, low, i-1 );
QuickSort ( A, i+1, high );
}
}
/*
FindFile
*/
int Directory::FindFile(File sortedArray[], int first, int last, string
key)
{
while (first <= last) {
int mid = (first + last) / 2; // compute mid point.
if (sortedArray[mid] < key)
first = mid + 1; // repeat search in top half.
else if (sortedArray[mid] > key)
last = mid - 1; // repeat search in bottom half.
else
return mid; // found it. return position /////
}
return -(first + 1); // failed to find key
}
int Directory::FileCount()
{
return NumFiles;
}
bool Directory::FileExists(File &F)
{
if(FindFile(Files, 0, NumFiles, F.GetFileName()) > -1)
return true;
else
return false;
}
string Directory::GetName()
{
return Name;
}
void Directory:rint()
{
cout << Name;
}
************************************************
Not that I want to over do it, but here's the 'controlling' app called
FileSystem:
header:
************************************************
#ifndef FileSystem_h
#define FileSystem_h
#include <cstdlib>
#include <iostream>
#include <string>
#include "Directory.h"
using namespace std;
class FileSystem
{
private:
Directory *CurrentDir, *RootDir;
public:
/*
constructor
*/
FileSystem();
/*
Destructor
*/
~FileSystem();
/*
main
inputs: none
outputs: none
Note: used start the application process.
*/
void main();
/*
mkdir
inputs: none
output: none
Note: used to make a new sub-directory of the current directory.
*/
void mkdir();
/*
mkfile
input: none
output: none
Note: used to make a new file
*/
void mkfile();
/*
del
input: none
output: none
Note: used to delete BOTH files and directories. Will search for a
matching file, if none is found will search for a directory
to delete.
*/
void del();
/*
dir
input: none
output: none
Note: used to list the contents of the current directory.
*/
void dir();
/*
move
input: none
output: none
Note: used to either move to the parent directory or to a subdir.
*/
void move();
/*
print
input: none
output: none
Note: used to list the contents of the current directory and
subdirectories.
*/
void print();
/*
count
intput: none
output: none
Note: used to return a count of all of the files in the system
from the current directory to it's lowest child.
*/
void count();
/*
help
input: none
output: none
Note: Help
*/
void help();
/*
ShowCursor
input: none
output: none
Note: creates the cursor (->) on the screen.
*/
void ShowCursor();
/*
toupper
input: string
output: string
Note: I found that toupper only works for characters, so I wrote
one for strings. (WHY ISN"T THERE ONE???)
*/
string toupper(string S);
/******************** used for testing ********/
// loads an array from a text file
void openInFile();
// list test array contents
void ListTestArray();
void FileSystem::TestMakeDirs()
};
#endif
******************************************
Code:
*******************************************
#include "FileSystem.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <fstream>
// An array used for testing only
int AS = 50; //Size of Array, Array Size
int CS = 0; // current size (in use)
string Words[50];
using namespace std;
FileSystem::FileSystem()
{
RootDir = new Directory(NULL, "/");
CurrentDir = RootDir;
}
FileSystem::~FileSystem()
{
}
void FileSystem::ShowCursor()
{
cout << "-> ";
}
void FileSystem::help()
{
cout << "The following commands are available." << endl;
cout << "COUNT";
cout.width(10);
cout << "DEL";
cout.width(10);
cout << "DIR";
cout.width(10);
cout << "HELP";
cout << endl;
cout << "MKDIR";
cout.width(10);
cout << "MKFILE";
cout.width(10);
cout << "MOVE";
cout.width(10);
cout << "PRINT";
cout << endl;
cout << "QUIT" << endl << "Type 'HELP' to see this list again.";
cout << endl;
}
void FileSystem::main()
{
string command;
help();
//ShowCursor();
//getline(cin, command);
do
{
ShowCursor();
getline(cin, command);
command = toupper(command);
//Can't use switch with a string... if then else I guess...
// help menu
if(command == "HELP")
{
system("cls");
help();
} // make a file
else if(command == "MKFILE")
{
system("cls");
CurrentDir->MKFile();
} // make a directory
else if(command == "MKDIR")
{
system("cls");
CurrentDir->MKDir();
} // show files and subdirs for current directory
else if(command == "DIR")
{
system("cls");
CurrentDir->SMPrint();
} // delete (a file or directory)
else if(command == "DEL")
{
system("cls");
CurrentDir->Delete();
} // Quit
else if(command == "QUIT")
{
system("cls");
cout << "Quiting" << endl;
}
else if (command == "LOADARRAY")
{
openInFile();
}
else if(command == "SHOWARRAY")
{
ListTestArray();
}
else if(command == "TESTMKDIR")
{
TestMakeDirs();
}
else
{
cout << "Un-recognized command" << endl;
}
}while(command != "QUIT");
}
string FileSystem::toupper(string S)
{
for (int j=0; j<S.length(); ++j)
{
S[j]=std::toupper(S[j]);
}
return S;
}
/************************ Code for Testing only ********/
void FileSystem:penInFile()
{
int charLoc;
string currentWords;
CS = 0;
//bool newLine = false;
// open the input file Input.txt
ifstream inFile("Input.txt");
// if the file failed to open, notify the user.
if(inFile.is_open())
{
getline(inFile, currentWords);
if(currentWords.empty() == false)
{
do
{
charLoc = currentWords.find(",");
Words[CS] = currentWords.substr(0, charLoc);
currentWords = currentWords.substr(charLoc + 1,
currentWords.length());
CS++;
}while(CS < AS && currentWords.length() > 3);
}
/* // as long as there is a 'word' to read from the file, loop.
while(inFile >> currentWord && CS < AS -1)
{
cout << currentWord;
CS++;
} */
// Close the input file
inFile.close();
}
else //if the file did open, process the file.
{
cout << "error opening file";
}
}
void FileSystem::ListTestArray()
{
system("cls");
for(int i = 0; i < CS; i++)
{
cout << i << " " << Words << endl;
}
system("PAUSE");
system("cls");
}
void FileSystem::TestMakeDirs()
{
//load the array with random words
openInFile();
if(CS > 0)
{
// make a directory for each random word
for(int i = 0; i <=CS; i++)
{
cout << "Creating dir: " << Words << endl;
CurrentDir->Create(Words);
}
}
// display the directories and files
CurrentDir->SMPrint();
}
************************************
File header:
**************************************
#ifndef File_h
#define File_h
#include <cstdlib>
#include <iostream>
#include <string>
//using namespace std;
class File
{
private:
std::string Name, Extension;
/*
SplitName
Inputs: FN (String)
Output: None
Notes: Used to split a string based on the '.' character. This
allows the Name and Extension to be found.
*/
void SplitName(string FN);
public:
/*
Constructor
Inputs: FullName
Outputs: None
Notes: Takes in the full name (name + extension) splits it.
*/
File(string FullName);
/*
Constructor
Inputs: FullName
Outputs: None
Notes: Takes in the full name (name + extension) splits it.
*/
File();
/*
Copy Constructor
Due to the requirement that no files in the same directory can
have
the same name, and the fact there is no copy required for this
assignment the copy constructor is not required.
*/
/*
Copy Assignment
Inputs: F (file)
Outputs: bool
*/
bool operator=(const File *F);
File & File:perator=(const File & F);
/*
Destructor
Because there are no pointers used the default destructor is
fine.
No destructor was created for 'file'
*/
/*
GetName
Inputs: None
Outputs Name (minus extension)
*/
string GetName();
/*
GetExtension
Inputs: None
Outputs: File Extension
*/
string GetExtension();
/*
GetFileName
Inputs: None
Output: full file name (name and extension)
*/
string GetFileName();
/*
Print
Inputs: None
Outputs: prints to screen full file name (name and extension)
*/
void Print();
/*
operator ==
Inputs: File F
Outputs: bool
Notes: If the file.Name and file.Extension are equal the files
are equal if either is not equal, the files are not equal.
*/
bool operator==(const File & F);
/*
operator==
Input: FN (string file name)
Output: bool
Notes: returns true if the file uses the same name as FN
*/
bool operator==(string FN);
/*
operator <
Inputs: File F
Outputs: bool
Notes: If the full file name is < the 'other' file name True.
*/
bool operator<(const File & F);
/*
operator <
Inputs: FN (string file name)
Outputs: bool
Notes: If the full file name is < the 'other' file name True.
*/
bool operator<(string FN);
/*
operator >
Inputs: File F
Outputs: bool
*/
bool operator>(const File & F);
/*
operator >
Inputs: FN (string file name)
Outputs: bool
*/
bool operator>(string FN);
/*
ValidName
Input: N (name)
Output: bool
Note: used to determine if a name is valid for a file.
No spaces and a dot.
*/
bool ValidName(string &N);
};
#endif
************************
File Code:
****************************
#include "File.h"
#include <iostream>
#include <cstdlib>
#include <string>
//using namespace std;
/*
Constructor
*/
File::File(string FullName)
{
SplitName(FullName);
}
File::File()
{
Name = "";
Extension = "";
}
//GetFileName
string File::GetFileName()
{
return (Name + "." + Extension);
}
// Copy Assignment
bool File:perator=(const File *F)
{
if(F->Name != "" && F->Extension != "")
{
Name = F->Name;
Extension = F->Extension;
return true;
}
else
return false;
}
File & File:perator=(const File & F)
{
if(F.Name != "" && F.Extension != "")
{
Name = F.Name;
Extension = F.Extension;
}
}
/*
SplitName
*/
void File::SplitName(string FN)
{
int charLoc; // character location
charLoc = FN.find('.');
// if there is no 'dot' then the Name is the Fullname (no extension)
if(charLoc == -1)
{
Name = FN;
Extension = "";
}
else //There is a dot so set the Name and Extension values
{
Name = FN.substr(0, charLoc);
Extension = FN.substr(charLoc + 1, FN.length());
}
}
bool File::ValidName(string &N)
{
int charLoc; // character location
SplitName(N);
// there must be a 'name' portion
if(Name == "")
return false;
if(Extension == "")
return false;
charLoc = N.find(' '); // search for a space in the name anywhere
if(charLoc == -1) // no spaces
return true;
else
return false;
}
/*
GetName
*/
string File::GetName()
{
return Name;
}
/*
GetExtension
*/
string File::GetExtension()
{
return Extension;
}
/*
Print
*/
void File:rint()
{
cout << Name << '.' << Extension;
}
/*
operator==
*/
bool File:perator==(const File & F)
{
if(Name != F.Name)
return false;
if(Extension != F.Extension)
{
return false;
}
else
{
return true;
}
}
/*
operator<
*/
bool File:perator<(const File & F)
{
//if the file Name is >, then false
if(Name > F.Name)
return false;
//if the file Name is <, then true
if(Name < F.Name)
return true;
//if we got to here then the file Name properties are equal
if(Extension < F.Extension) // this extension is < other, then
true
{
return true;
}
else // this extension is NOT < other, then false
{
return false;
}
}
bool File:perator<(string FN)
{
if(GetFileName() > FN)
return false;
if(GetFileName() == FN)
return false;
if(GetFileName() < FN)
return true;
}
/*
operator>
*/
bool File:perator>(const File & F)
{
if(Name < F.Name)
return false;
if(Name > F.Name)
return true;
// if we got to here the names are equal
if(Extension > F.Extension)
return true;
else
return false;
}
bool File:perator>(string FN)
{
if(GetFileName() < FN)
return false;
if(GetFileName() == FN)
return false;
if(GetFileName() > FN)
return true;
}
**************************
Main:
******************************
#include <cstdlib>
#include <iostream>
//#include "File.h"
//#include "Directory.h"
#include "FileSystem.h"
using namespace std;
int main(int argc, char *argv[])
{
/*string FN;
Directory dir("/");
bool result;
for(int i = 0; i <= 6; i++)
{
result = dir.MKFile();
}
dir.SMPrint();
*/
FileSystem FS;
FS.main();
// cout << "Find a file position; enter file name: ";
//getline(cin, FN);
//dir.FindFile(dir.Files, 0, dir.FileCount -1);
system("PAUSE");
return EXIT_SUCCESS;
}
**************************
Oh; a comment on the std:: - the teaching assistant seems to favour NOT
using std:: so that's the way I will go for now. He's got to mark it.
Best to keep it to what he likes.
By the way... are you independantly wealthy or just very generous with
your time??
Also; is there a way to get the system to email me if there is a reply?