1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
//libraries
#include<algorithm>
#include <deque>
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
#include<cctype>
struct EmailList
{
string anEmail;
}; // EmailList
// required for conversion to lowercase
class toLower {public: char operator()(char c) const {return tolower(c);}};
//Programmer defined data types
//NONE
//Special compiler dependent definitions
//NONE
//global constants/variables
//NONE
//Programmer defined functions
void introduction()
{
// output my name and objective and program information
cout << "The purpose of the program is to extract email addresses embedded in the first (input) text file,and copy them to the second (output) text file.\n";
cout << "Programmer: Toraaglobal\n";
cout << "Editor(s) used: Notepad\n";
cout << "Compiler(s) used: MinGW\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
} // introduction
bool isValidEmailCharacter (char c)
{
bool result = false;
if (c == '_' || c== '.') result = true;
if (c == '-' || c == '+') result = true;
if (c >='a' && c <= 'z' )result = true;
if( c >= 'A' && c <= 'Z')result = true;
if (c >= '0' && c<= '9') result = true;
return result;
}//isValidEmailCharacter
bool hasDot(char c)
{
bool result = false;
if(c=='.') result = true;
return result;
}//hasDot
//main program
int main()
{
//program introduction
introduction();
//variables
string dFilename; // default input file name
string IFile; // the user input file name using the console
string OFile; // the user output file name using the console
string userrespond; // user respond to continue
ifstream fin ; // required for file input
ofstream fout; // required for file output
string line; // each line in the input file
int i=0; // loop counter
int s ; // to count backward until a non valid character is found
int e ; // to count forward until a non valid character is found
bool isdot; //use to confirm a dot
int numberOfEmail = 0;
bool dupe;
string temp; // to temporary store the valid email to check for duclicate
string temp2; // use to check for duclicate
EmailList aEmailList;
string validEmail;
// promt user for file input or use default input file
dFilename = "fileContainingEmails.txt";
cout<<"Enter input filename [default: fileContainingEmails.txt]: "<<endl;
getline(cin,IFile);
if(IFile.length() ==0)
{
IFile = dFilename;
}
else
{
IFile = IFile;
}
// promt the user for output file
dFilename = "copyPasteMyEmails.txt";
cout<<"Enter output filename [default: copyPasteMyEmails.txt]: "<<endl;
getline(cin,OFile);
if(OFile.length() == 0)
{
OFile = dFilename;
}
else
{
OFile =OFile;
}
// output user working files
cout<<"Your input file name is : "<<IFile<<endl;
cout<<"Your output file name is : "<<OFile<<endl;
// promt the user to continue the program
cout << "Press Enter Key to continue: ";
getline(cin,userrespond);
// open the input file
fin.open(IFile.c_str());
if(!fin.good())cout<<"Invalid file : "<<IFile<<endl;
deque<EmailList> emailList; // create an emty list
// read the input file
while(fin.good())
{
getline(fin, line);
for(i=0; i< line.length() ; i++)
{
if (line[i] =='@')
{
for( s= i-1;s > -1 ; s--)
{
if(isValidEmailCharacter(line[s]) == false)break;
}//for
s = s+1;
isdot = false;
for(e = i+1; e < line.length(); e++)
{
if(!isdot) isdot = hasDot(line[e]);
if(isValidEmailCharacter(line[e]) == false)break;
}//for
if(s<i && e>i && isdot )
{
temp = line.substr(s, e-s);
validEmail = line.substr(s, e-s);
dupe=false;
for(i = 0 ; i< emailList.size();i++)
{
temp2= emailList[i].anEmail;
transform(temp.begin(), temp.end(), temp.begin(), toLower());
transform(temp2.begin(), temp2.end(), temp2.begin(), toLower());
if(temp == temp2) dupe=true;
}//for
if(dupe==true)break;
aEmailList.anEmail = validEmail;
emailList.push_back(aEmailList);
}
}
}//for close
}// while close
fin.close();
if(emailList.size() == 0)
{
cout<<endl;
cout<<"Sorry, no email addresses were found in the "<<IFile<<endl;
}//if
else
{
cout<<endl;
fout.open(OFile.c_str());
if(!fout.good())throw "I/O error";
for(i=0; i < emailList.size();i++)
{
cout<<emailList[i].anEmail<<endl;
fout<<emailList[i].anEmail<<"; ";
}
cout<<endl;
cout<<emailList.size()<<" email addresses were found, and copied to the "<<OFile<<endl;
cout<<endl;
cout<<"OPen the "<<OFile<<" and copy and paste the email address to the cc or bcc,";
cout<<" but bcc will be better because others will not see the list of all the recipient";
cout<<" which also protect the privacy of individual "<<endl;
fout.close();
}//else
} //main
|