Original Text: WarIsKind.txt
Encrypted Example: obfuscateWar.txt
Text Copy Example: copyWar.txt
Uppercase Example: upperWar.txt
FileFilter.h
#ifndef FILE_FILTER_H_
#define FILE_FILTER_H_
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <string>
using std::string;
class FileFilter
{
protected:
ifstream in; // A ifstream object
ofstream out; // A ofstream object
public:
FileFilter()
{ }
// Constructor
FileFilter(const string &, const string &); // Defined in FileFilter.cpp
// Destructor
virtual ~FileFilter(); // Defined in FileFilter.cpp
// Mutator function
void doFilter(ifstream &, ofstream &);
// Pure virtual function
virtual char transform(char) const = 0;
};
#endif
FileFilter.cpp
#include "FileFilter.h"
#include <cstdlib>
#include <iostream>
using std::cout;
/* **********************************************************
FileFilter::FileFilter() const string &,
const string &
The constructor accepts two string objects, holding the
names of the input and output file to be opened. If the
operation is successful, the files are opened. In case of
an error, a message is output, and the program will exit.
********************************************************** */
FileFilter::FileFilter(const string &fNameIn, const string &fNameOut)
{
in.open(fNameIn);
if (!in)
{
cout << "\nFile read error: " << fNameIn << " could not be opened!\n\n"
<< "Make sure the filename is written correctly, that the file\n"
<< "is not damaged, and in the same folder as this program!\n";
cout << "This program will now exit ...\n\n";
exit(1);
}
out.open(fNameOut);
if (!out)
{
cout << "\nFile write error: " << fNameOut << " could not be created "
<< "or opened!\n\n";
cout << "Make sure that the file or folder is not write protected!\n";
cout << "This program will now exit ...\n\n";
exit(1);
}
}
/* **********************************************************
FileFilter::doFilter() : ifstream &, ofstream &
This function reads in a file, transforms its contents,
and writes the result to an output file.
********************************************************** */
void FileFilter::doFilter(ifstream &in, ofstream &out)
{
char ch = ' '; // Holds a single character
char transChar = ' '; // Holds a transformed character
in.get(ch);
while (!in.fail())
{
transChar = transform(ch);
out.put(transChar);
in.get(ch);
}
}
/* **********************************************************
v FileFilter::~FileFilter() - Destructor
The destructor closes the input and output files.
********************************************************** */
FileFilter::~FileFilter()
{
in.close();
out.close();
}
Encrypt.h
#ifndef ENCRYPT_H_
#define ENCRYPT_H_
#include "FileFilter.h"
#include <iostream>
using std::cin;
using std::cout;
// Encrypt class derived from the FileFilter class
class Encrypt : public FileFilter
{
private:
int encryptKey; // The encryption key
public:
// Constructor
Encrypt() : FileFilter()
{
encryptKey = 0;
}
// Parameterized Constructor
Encrypt(const string &fNameIn, const string &fNameOut, const int &key) :
FileFilter(fNameIn, fNameOut)
{
encryptKey = key;
doFilter(in, out);
}
// Destructor
~Encrypt()
{ }
// Overridden transform function
// Returns an encrypted character
virtual char transform(char ch) const
{
return ((ch * 13) / (encryptKey % 13));
}
};
#endif
Copy.h
#ifndef COPY_H_
#define COPY_H_
#include "FileFilter.h"
// Copy class derived from the FileFilter class
class Copy : public FileFilter
{
public:
// Parameterized Constructor
Copy(const string &fNameIn, const string &fNameOut) :
FileFilter(fNameIn, fNameOut)
{
doFilter(in, out);
}
~Copy()
{ }
// Overridden transform function
// Returns a character as is
virtual char transform(char ch) const
{
return ch;
}
};
#endif
UpperCase.h
#ifndef UPPERCASE_H_
#define UPPERCASE_H_
#include "FileFilter.h"
// UppeCase class derived from the FileFilter class
class UpperCase : public FileFilter
{
public:
// Constructor
UpperCase() : FileFilter()
{ }
// Parameterized Constructor
UpperCase(const string &fNameIn, const string &fNameOut) : FileFilter(fNameIn, fNameOut)
{
doFilter(in, out);
}
// Destructor
~UpperCase()
{ }
// Overridden transform function
// Converts a character to uppercase and returns it
virtual char transform(char ch) const
{
return toupper(ch);
}
};
#endif
FileFilterDm.cpp
#include "Encrypt.h"
#include "UpperCase.h"
#include "Copy.h"
#include <iostream>
using std::cin;
using std::cout;
int main()
{
string fNameIn = "";
string fNameOut = "";
int encKey = 0;
cout << "FILE FILTER DEMO\n\n"
<< "This program allows you to:\n\n"
<< " 1.) Encrypt a text file\n"
<< " 2.) Make 1:1 copies of a text file\n"
<< " 3.) Change all characters in a text file to uppercase\n\n";
cout << "Enter the name of the file to read in: ";
cin >> fNameIn;
// Demonstrates the derived Encrypt class
cout << "\nENCRYPT FILE\n\n";
cout << "Enter a file name to write the encrypted text to: ";
cin >> fNameOut;
cout << "Enter an encryption key: ";
cin >> encKey;
// Allocate an instance of the derived class Encrypt
Encrypt *obfuscate = new Encrypt(fNameIn, fNameOut, encKey);
cout << "Your file has been encrypted.\n\n";
delete obfuscate;
// Demonstrates the derived Copy class
cout << "COPY FILE\n\n";
cout << "Enter a file name to write a copy of "
<< fNameIn << " to: ";
cin >> fNameOut;
// Allocate an instance of the derived class Copy
Copy *fileCopy = new Copy(fNameIn, fNameOut);
cout << "A copy of your file has been created.\n\n";
delete fileCopy;
// Demonstrates the derived UpperCase class
cout << "CHANGE CASE\n\n";
cout << "Enter a file name to write the text in uppercase to: ";
cin >> fNameOut;
// Allocate an instance of the derived class UpperCase
UpperCase *changeCase = new UpperCase(fNameIn, fNameOut);
cout << "Your text has been transformed to uppercase.\n\n";
delete changeCase;
cout << "Thank you for trying the File Filter Demo. Have a nice day!";
cin.get();
cin.ignore();
return 0;
}
No comments:
Post a Comment