// Wrongfile. h
# Ifndef wrongfile_h
# Define wrongfile_h
/* Some exceptions that may occur in the project
* Samefile: processing when an existing file is added to a user
* Wronguser: the user to be searched does not exist. This exception is thrown.
* Wrongfile: the file to be searched does not exist.
* Userexist: the added user already exists.
*/
# Include <exception>
Using namespace STD;
Class samefile: Public exception
{
Public:
Virtual const char * What () const
{
Return "the file already exists! ";
}
};
Class wronguser: Public exception
{
Public:
Virtual const char * What () const
{
Return "the user does not exist! ";
}
};
Class wrongfile: Public exception
{
Public:
Virtual const char * What () const
{
Return "the file does not exist! ";
}
};
Class userexist: Public exception
{
Public:
Virtual const char * What () const
{
Return "the user already exists! ";
}
};
# Endif
// File. h
# Ifndef file_h
# Define file_h
/* Second-level storage of files, user-level, user directory (MFD)
* Saves user information in MFD. to query a file, you must first query the user.
* Whether the search exists in the user information area specified by MFD
*/
# Include <iostream>
# Include <vector>
# Include <string>
# Include "wrongfile. H"
Using namespace STD;
Class MFD;
Class user
{
PRIVATE:
User (const string & name): m_name (name)
{}
// Add a new file to the user's file directory
Void AddFile (const string & fname) Throw (samefile );
// Check whether the fname exists in the user's file directory
Bool findfile (const string & fname );
// Output user information
Friend ostream & operator <(ostream & OS, const user & uname)
{
OS <"username" <"file" <Endl;
OS <uname. m_name <"";
String: size_type SL;
SL = uname. m_name.size ();
Vector <string >:: size_type lengh (0 );
Vector <string >:: const_iterator ite;
ITE = uname. m_filename.begin ();
For (; lengh! = Uname. m_filename.size (); ++ lengh)
{
OS <* ite <Endl;
For (string: size_type I = 0; I! = SL + 4; ++ I)
Cout <"";
++ Ite;
}
Return OS;
}
PRIVATE:
// User Name
String m_name;
// User's file directory
Vector <string> m_filename;
Friend class MFD;
};
Class MFD
{
Public:
// Read users and files from files
MFD ();
// Add a new user
Void adduser (const string & uname) Throw (userexist );
// Add files belonging to a new user
Void addufile (const string & uname, const string & fname) Throw (samefile );
// Find the user to which the object belongs. An error is returned because the object does not exist and an exception is thrown.
String & findufile (const string & fname) Throw (wrongfile );
// Print the user information if the specified user exists
// If no exception exists, an exception is thrown.
Void printuser (const string & uname) Throw (wronguser );
// Save it to the file during Structure Analysis
~ MFD ();
PRIVATE:
// User directory
Vector <user> m_user;
// Determine whether a user exists in the user directory and returns its location
// If m_user.end () does not exist ()
Vector <user >:: iterator existuser (const string & uname );
};
# Endif
// Filefun. cpp
# Include <iostream>
# Include <fstream>
# Include <algorithm>
# Include "file. H"
Using namespace STD;
MFD: MFD ()
{
Ifstream user, file;
String username, filename;
String endoffile (". txt ");
Vector <user >:: iterator ite;
User. Open ("user.txt ");
While (User> username)
{
User U (username );
Filename = username + endoffile;
File. Open (filename. c_str ());
While (File> filename)
U. m_filename.push_back (filename );
M_user.push_back (U );
File. Close ();
}
}
Void User: AddFile (const string & fname) Throw (samefile)
{
Vector <string >:: iterator ite;
ITE = find (m_filename.begin (), m_filename.end (), fname );
If (ITE = m_filename.end ())
M_filename.push_back (fname );
Else
Throw samefile ();
}
// This function cannot be declared as a regular function and is related to find.
Bool User: findfile (const string & fname)
{
Vector <string >:: iterator ite;
ITE = find (m_filename.begin (), m_filename.end (), fname );
If (ITE! = M_filename.end ())
Return true;
Else
Return false;
}
Vector <user >:: iterator MFD: existuser (const string & uname)
{
Vector <user >:: iterator ite;
ITE = m_user.begin ();
For (; ite! = M_user.end (); ++ ITE)
{
If (ITE-> m_name = uname)
Break;
}
Return ite;
}
Void MFD: adduser (const string & uname) Throw (userexist)
{
Vector <user >:: iterator ite;
ITE = existuser (uname );
If (ITE = m_user.end ())
{
User U (uname );
M_user.push_back (U );
}
Else
Throw userexist ();
}
Void MFD: addufile (const string & uname, const string & fname) Throw (samefile)
{
Vector <user >:: iterator ite;
ITE = existuser (uname );
If (ITE = m_user.end ())
{
User U (uname );
U. m_filename.push_back (fname );
M_user.push_back (U );
}
Else
ITE-> AddFile (fname );
}
String & MFD: findufile (const string & fname) Throw (wrongfile)
{
Vector <user >:: iterator ite;
ITE = m_user.begin ();
For (; ite! = M_user.end (); ++ ITE)
{
If (ITE-> findfile (fname ))
Break;
}
If (ITE! = M_user.end ())
Return ite-> m_name;
Else
Throw wrongfile ();
}
Void MFD: printuser (const string & uname) Throw (wronguser)
{
Vector <user >:: iterator ite;
ITE = existuser (uname );
If (ITE! = M_user.end ())
Cout <* ite <Endl;
Else
Throw wronguser ();
}
MFD ::~ MFD ()
{
Ofstream user, file;
User. Open ("user.txt ");
String endoffile (". txt ");
String filename;
Vector <string >:: iterator site;
Vector <user >:: iterator ite;
ITE = m_user.begin ();
For (; ite! = M_user.end (); ++ ITE)
{
User <ite-> m_name <"";
Filename = ite-> m_name + endoffile;
File. Open (filename. c_str ());
Site = ite-> m_filename.begin ();
For (; site! = Ite-> m_filename.end (); ++ site)
File <* site <"";
File. Close ();
}
}
// Manager. h
# Ifndef manager_h
# Define manager_h
/* Two identities of the Operation file: common user and Manager)
* Different identities have different file access permissions, because common users have different permissions.
* The administrator can also set the commonuser class as the base class and derive
* Administrator (manager ).
*/
# Include <iostream>
# Include <string>
# Include "file. H"
Using namespace STD;
Class commonuser
{
Public:
// Set the password when constructing the object
Commonuser (const string & Password = "19851010 "):
M_password (password)
{}
// File Operations
Virtual void operate ();
Virtual ~ Commonuser (){}
Protected:
MFD m_mfd;
// Password
String m_password;
// Determine whether the password is correct
Bool rightpassword (const string & password) const;
// Search for files
Void searchfile (const string & fname );
// Search for users
Void searchuser (const string & uname );
PRIVATE:
// User Function menu
Virtual void showmenu () const;
};
Class MANAGER: Public commonuser
{
Public:
// Construct the base class
Manager (): commonuser ("19851121 ")
{}
// File Operations
Virtual void operate ();
Virtual ~ Manager (){}
Protected:
// Add a user
Void appenduser (const string & uname );
// Add files belonging to a user when adding a user
Void appendufile (const string & uname, const string & fname );
PRIVATE:
// User Function menu
Virtual void showmenu () const;
};
Inline void commonuser: showmenu () const
{
Cout <"operations you can perform:" <Endl;
Cout <"1 find a file" <Endl;
Cout <"2 find a user" <Endl;
}
Inline bool commonuser: rightpassword (const string & password) const
{
Return m_password = password;
}
Inline void MANAGER: showmenu () const
{
Cout <"operations you can perform:" <Endl;
Cout <"1 find a file" <Endl;
Cout <"2 find a user" <Endl;
Cout <"3 Add User" <Endl;
Cout <"4 adding users and files" <Endl;
}
# Endif
// Manfun. cpp
# Include <iostream>
# Include "manager. H"
Using namespace STD;
Void commonuser: Operate ()
{
String password;
Cout <"enter the password :";
Cin> password;
Cout <Endl;
If (! Rightpassword (password ))
{
Cout <"Incorrect password! "<Endl;
Return;
}
Else
{
Showmenu ();
Cout <"select :";
Int choice;
Cin> choice;
Cout <Endl;
String name;
Switch (choice)
{
Case 1: cout <"enter the name of the file to be searched :";
Cin> name;
Cout <Endl;
Searchfile (name );
Break;
Case 2: cout <"Enter the user to search :";
Cin> name;
Cout <Endl;
Searchuser (name );
Break;
Default: cout <"incorrect selection! "<Endl;
Break;
}
}
}
Void commonuser: searchfile (const string & fname)
{
String user;
Try
{
User = m_mfd.findufile (fname );
}
Catch (wrongfile WF)
{
Cerr <WF. What () <Endl;
Return;
}
Cout <"file:" <fname <"belongs to the user:" <user <Endl;
}
Void commonuser: searchuser (const string & uname)
{
Try
{
M_mfd.printuser (uname );
}
Catch (wronguser Wu)
{
Cerr <Wu. What () <Endl;
}
}
Void MANAGER: Operate ()
{
String password;
Cout <"enter the password :";
Cin> password;
Cout <Endl;
If (! Rightpassword (password ))
{
Cout <"Incorrect password! "<Endl;
Return;
}
Else
{
Showmenu ();
Cout <"select :";
Int choice;
Cin> choice;
Cout <Endl;
String uname, fname;
Switch (choice)
{
Case 1: cout <"enter the name of the file to be searched :";
Cin> fname;
Cout <Endl;
Searchfile (fname );
Break;
Case 2: cout <"Enter the user to search :";
Cin> uname;
Cout <Endl;
Searchuser (uname );
Break;
Case 3: cout <"Enter the user name to be added :";
Cin> uname;
Cout <Endl;
Appenduser (uname );
Break;
Case 4: cout <"Enter the user name to be added :";
Cin> uname;
Cout <Endl;
Cout <"input file name :";
Cin> fname;
Cout <Endl;
Appendufile (uname, fname );
Break;
Default: cout <"incorrect selection! "<Endl;
Break;
}
}
}
Void MANAGER: appenduser (const string & uname)
{
Try
{
M_mfd.adduser (uname );
}
Catch (userexist U)
{
Cerr <u. What () <Endl;
}
}
Void MANAGER: appendufile (const string & uname, const string & fname)
{
Try
{
M_mfd.addufile (uname, fname );
}
Catch (samefile SF)
{
Cout <"the user entered already exists! "<Endl;
Cout <Endl;
Cerr <SF. What () <Endl;
}
}
// Main. cpp
# Include <iostream>
# Include "manager. H"
Using namespace STD;
Int main ()
{
Commonuser * Pu;
Int choice;
Cout <"Logon identity:" <Endl;
Cout <"1 user" <Endl;
Cout <"2 administrator" <Endl;
Cout <"select :";
Cin> choice;
Cout <Endl;
If (1 = choice)
Pu = new commonuser ();
Else
{
If (2 = choice)
Pu = new manager ();
Else
{
Cout <"incorrect selection! "<Endl;
Return-1;
}
}
Pu-> operate ();
Delete Pu;
Return 0;
}