Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Libraries.
- #include <deque>
- #include <fstream>
- #include <iostream>
- #include <string>
- using namespace std;
- #include <cctype>
- // Programmer-defined data types.
- struct Email
- {
- string line;
- string anEmail;
- string nonDup;
- };
- // Special compiler-dependent definitions.
- // Global constants/variables.
- // Programmer-defined functions.
- void introduction(string objective); // Prototype introduction subprogram.
- string getFile(string fileType, string dFileName); // Prototype file name input prompt subprogram.
- bool isValidEmailChar(char c); // Prototype character validation subprogram.
- string changeCase(string line); // Prototype string case conversion subprogram, assists in checking for duplicate addresses.
- //string sortList(string line); // Prototype address sorting subprogram.
- void readFile(string iFileName, string oFileName); // Prototype file reader subprogram.
- int main()
- {// Begin main.
- // Data. This is where all variables for each program/subprogram are declared.
- string objective = "Select an input and output file containing emails, and output the valid email addresses found to console."; // Program objective.
- string fileName; // User's input for both file name prompts.
- string dFileName = "fileContainingEmails.txt"; // Default file name for input, determined by programmer.
- string iFileName; // Input file name, determined by user.
- string oFileName; // Output file name, determined by user.
- string confirm; // This allows the user to continue program execution.
- // Introduction. Provides information about the program and its author. This is a prototype void function called to main.
- introduction(objective);
- // Prompt user for input file name and output file name.
- fileName = getFile("input", dFileName);
- if (fileName.length() == 0) // If Enter key is pressed, input file name will be program default, default file name will be program default.
- {
- iFileName = dFileName;
- dFileName = "copyPasteMyEmails.txt";
- }
- else
- {
- iFileName = fileName;
- dFileName = iFileName;
- }
- fileName = getFile("output", dFileName);
- if (fileName.length() == 0) // If Enter key is pressed, output file name will be program default or input file name depending on user input.
- oFileName = dFileName;
- else
- oFileName = fileName;
- cout << endl;
- // Output input and output file names to console. Pause program execution until Enter key is pressed.
- cout << "Input file name: " << iFileName << endl;
- cout << "Output file name: " << oFileName << endl << endl;
- while (true)
- {
- cout << "Press ENTER to continue: ";
- getline(cin, confirm);
- if (confirm.length() == 0) break;
- cout << endl;
- }
- cout << endl;
- // Output valid email addresses from selected input file to console and copy to output file.
- readFile(iFileName, oFileName);
- }// End main.
- void introduction(string objective)
- {// Begin introduction.
- cout << "Objective: " << objective << endl;
- cout << "Programmer: Griffin (Last Name)\n";
- cout << "Editor(s) used: Notepad\n";
- cout << "Compiler(s) used: TDM MinGW\n";
- cout << "File: " << __FILE__ << endl;
- cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
- }// End introduction.
- string getFile(string fileType, string dFileName)
- {
- string fileName;
- cout << "Enter a file name for " << fileType << " [Press ENTER to use default " << fileType << " file name (" << dFileName << ")]: ";
- getline(cin, fileName);
- return fileName;
- }
- bool isValidEmailChar(char c)
- {// Begin isValidEmailChar.
- if ((c == '@') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '_') || (c == '-') || (c == '+') || (c == '.'))
- return true;
- else
- return false;
- }// End isValidEmailChar.
- string changeCase(string line)
- {
- int i;
- for (i = 0; i < line.length(); i++)
- {
- line[i] = tolower(line[i]);
- }
- return line;
- }
- void readFile(string iFileName, string oFileName)
- {
- ifstream fin; // This enables us to open the input file.
- ofstream fout; // This enables us to open the output file.
- int i = 0; // For-loop index.
- int s = 0; // Second loop index.
- int e = 0; // Third loop index.
- int j = 0;
- int k = 0;
- int invS = 0; // This will be the index of the first invalid character in a potential address.
- int invE = 0; // This will be the index of the second invalid character in a potential address.
- int dotPos; // The position of the period in the address.
- int count; // File output loop index.
- bool hasDot = false; // Checking to see if the address contains a period.
- bool duplicate = false; // Checking to see if the addresses has already been added to the list.
- deque<Email> addresses; // This will be our empty list.
- Email file; // This is our Email variable.
- cout << "Email addresses on file: " << endl;
- cout << "------------------------" << endl;
- fin.open(iFileName.c_str());
- if (!fin.good()) throw "I/O error";
- while (fin.good())
- {
- getline(fin, file.line);
- for (i = 0; i < file.line.length(); i++)
- {
- if (file.line[i] == '@') // If the email address on file has an @ character in it, is is considered valid and will be output to console.
- {
- for (s = i; s >= 0; s--)
- {
- if (!isValidEmailChar(file.line[s])) break;
- }
- invS = s + 1;
- s++;
- for (e = i; e < file.line.length() + 1; e++)
- {
- if (!isValidEmailChar(file.line[e]))
- {
- invE = e;
- break;
- }
- if (file.line[e] == '.') // If the email address on file has an @ and a period after it, it is considered valid.
- {
- dotPos = e;
- hasDot = true;
- }
- }
- file.anEmail = file.line.substr(invS, invE - invS);
- file.nonDup = changeCase(file.anEmail);
- for (j = 0; j < addresses.size(); j++)
- {
- for (k = j + 1; k < addresses.size(); k++)
- {
- if (addresses[j].nonDup == addresses[k].nonDup)
- duplicate = true;
- else
- duplicate = false;
- }
- }
- if ((s < i) && (e > i) && (hasDot == true) && (dotPos > i + 1) && (duplicate == false))
- {
- cout << file.anEmail << endl;
- addresses.push_back(file);
- }
- }
- }
- }
- fin.close();
- if (addresses.size() == 0)
- cout << "No email addresses were found on this file." << endl;
- else
- {
- cout << addresses.size() << " valid email addresses were found on file and were copied to " << oFileName << "." << endl;
- cout << "To view these email addresses, open " << oFileName << "." << endl;
- }
- cout << "------------------------";
- // Output all valid email addresses found to the user's selected output file.
- if (addresses.size() > 0)
- {
- fout.open(oFileName.c_str());
- for (count = 0; count < addresses.size(); count++)
- {
- if (count == addresses.size() - 1)
- fout << addresses[count].anEmail;
- else
- fout << addresses[count].anEmail << "; ";
- }
- fout.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement