Advertisement
Guest User

email.cpp

a guest
Dec 10th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. // Libraries.
  2. #include <deque>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7. #include <cctype>
  8.  
  9. // Programmer-defined data types.
  10. struct Email
  11. {
  12. string line;
  13. string anEmail;
  14. string nonDup;
  15. };
  16.  
  17. // Special compiler-dependent definitions.
  18.  
  19. // Global constants/variables.
  20.  
  21. // Programmer-defined functions.
  22. void introduction(string objective); // Prototype introduction subprogram.
  23. string getFile(string fileType, string dFileName); // Prototype file name input prompt subprogram.
  24. bool isValidEmailChar(char c); // Prototype character validation subprogram.
  25. string changeCase(string line); // Prototype string case conversion subprogram, assists in checking for duplicate addresses.
  26. //string sortList(string line); // Prototype address sorting subprogram.
  27. void readFile(string iFileName, string oFileName); // Prototype file reader subprogram.
  28.  
  29. int main()
  30. {// Begin main.
  31. // Data. This is where all variables for each program/subprogram are declared.
  32. string objective = "Select an input and output file containing emails, and output the valid email addresses found to console."; // Program objective.
  33. string fileName; // User's input for both file name prompts.
  34. string dFileName = "fileContainingEmails.txt"; // Default file name for input, determined by programmer.
  35. string iFileName; // Input file name, determined by user.
  36. string oFileName; // Output file name, determined by user.
  37. string confirm; // This allows the user to continue program execution.
  38.  
  39. // Introduction. Provides information about the program and its author. This is a prototype void function called to main.
  40. introduction(objective);
  41.  
  42. // Prompt user for input file name and output file name.
  43. fileName = getFile("input", dFileName);
  44. if (fileName.length() == 0) // If Enter key is pressed, input file name will be program default, default file name will be program default.
  45. {
  46. iFileName = dFileName;
  47. dFileName = "copyPasteMyEmails.txt";
  48. }
  49. else
  50. {
  51. iFileName = fileName;
  52. dFileName = iFileName;
  53. }
  54.  
  55. fileName = getFile("output", dFileName);
  56. if (fileName.length() == 0) // If Enter key is pressed, output file name will be program default or input file name depending on user input.
  57. oFileName = dFileName;
  58. else
  59. oFileName = fileName;
  60. cout << endl;
  61.  
  62. // Output input and output file names to console. Pause program execution until Enter key is pressed.
  63. cout << "Input file name: " << iFileName << endl;
  64. cout << "Output file name: " << oFileName << endl << endl;
  65.  
  66. while (true)
  67. {
  68. cout << "Press ENTER to continue: ";
  69. getline(cin, confirm);
  70. if (confirm.length() == 0) break;
  71. cout << endl;
  72. }
  73. cout << endl;
  74.  
  75. // Output valid email addresses from selected input file to console and copy to output file.
  76. readFile(iFileName, oFileName);
  77. }// End main.
  78.  
  79. void introduction(string objective)
  80. {// Begin introduction.
  81. cout << "Objective: " << objective << endl;
  82. cout << "Programmer: Griffin (Last Name)\n";
  83. cout << "Editor(s) used: Notepad\n";
  84. cout << "Compiler(s) used: TDM MinGW\n";
  85. cout << "File: " << __FILE__ << endl;
  86. cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
  87. }// End introduction.
  88.  
  89. string getFile(string fileType, string dFileName)
  90. {
  91. string fileName;
  92. cout << "Enter a file name for " << fileType << " [Press ENTER to use default " << fileType << " file name (" << dFileName << ")]: ";
  93. getline(cin, fileName);
  94. return fileName;
  95. }
  96.  
  97. bool isValidEmailChar(char c)
  98. {// Begin isValidEmailChar.
  99. if ((c == '@') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '_') || (c == '-') || (c == '+') || (c == '.'))
  100. return true;
  101. else
  102. return false;
  103. }// End isValidEmailChar.
  104.  
  105. string changeCase(string line)
  106. {
  107. int i;
  108.  
  109. for (i = 0; i < line.length(); i++)
  110. {
  111. line[i] = tolower(line[i]);
  112. }
  113.  
  114. return line;
  115. }
  116.  
  117. void readFile(string iFileName, string oFileName)
  118. {
  119. ifstream fin; // This enables us to open the input file.
  120. ofstream fout; // This enables us to open the output file.
  121. int i = 0; // For-loop index.
  122. int s = 0; // Second loop index.
  123. int e = 0; // Third loop index.
  124. int j = 0;
  125. int k = 0;
  126. int invS = 0; // This will be the index of the first invalid character in a potential address.
  127. int invE = 0; // This will be the index of the second invalid character in a potential address.
  128. int dotPos; // The position of the period in the address.
  129. int count; // File output loop index.
  130. bool hasDot = false; // Checking to see if the address contains a period.
  131. bool duplicate = false; // Checking to see if the addresses has already been added to the list.
  132. deque<Email> addresses; // This will be our empty list.
  133. Email file; // This is our Email variable.
  134.  
  135. cout << "Email addresses on file: " << endl;
  136. cout << "------------------------" << endl;
  137. fin.open(iFileName.c_str());
  138. if (!fin.good()) throw "I/O error";
  139. while (fin.good())
  140. {
  141. getline(fin, file.line);
  142. for (i = 0; i < file.line.length(); i++)
  143. {
  144. 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.
  145. {
  146. for (s = i; s >= 0; s--)
  147. {
  148. if (!isValidEmailChar(file.line[s])) break;
  149. }
  150. invS = s + 1;
  151. s++;
  152. for (e = i; e < file.line.length() + 1; e++)
  153. {
  154. if (!isValidEmailChar(file.line[e]))
  155. {
  156. invE = e;
  157. break;
  158. }
  159. if (file.line[e] == '.') // If the email address on file has an @ and a period after it, it is considered valid.
  160. {
  161. dotPos = e;
  162. hasDot = true;
  163. }
  164. }
  165. file.anEmail = file.line.substr(invS, invE - invS);
  166. file.nonDup = changeCase(file.anEmail);
  167. for (j = 0; j < addresses.size(); j++)
  168. {
  169. for (k = j + 1; k < addresses.size(); k++)
  170. {
  171. if (addresses[j].nonDup == addresses[k].nonDup)
  172. duplicate = true;
  173. else
  174. duplicate = false;
  175. }
  176. }
  177. if ((s < i) && (e > i) && (hasDot == true) && (dotPos > i + 1) && (duplicate == false))
  178. {
  179. cout << file.anEmail << endl;
  180. addresses.push_back(file);
  181. }
  182. }
  183. }
  184. }
  185. fin.close();
  186. if (addresses.size() == 0)
  187. cout << "No email addresses were found on this file." << endl;
  188. else
  189. {
  190. cout << addresses.size() << " valid email addresses were found on file and were copied to " << oFileName << "." << endl;
  191. cout << "To view these email addresses, open " << oFileName << "." << endl;
  192. }
  193. cout << "------------------------";
  194.  
  195. // Output all valid email addresses found to the user's selected output file.
  196. if (addresses.size() > 0)
  197. {
  198. fout.open(oFileName.c_str());
  199. for (count = 0; count < addresses.size(); count++)
  200. {
  201. if (count == addresses.size() - 1)
  202. fout << addresses[count].anEmail;
  203. else
  204. fout << addresses[count].anEmail << "; ";
  205. }
  206. fout.close();
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement