Expand|Select|Wrap|Line Numbers
- // main.cpp - email checker (HW1) main entry point
- // By Emilio Gonzalez
- #include <iostream>
- #include <fstream>
- using namespace std;
- #define MAX_SIZE 260
- #define NOT_FOUND -1
- //Returns the length of text by scanning for null-term
- int GetLength(char* text)
- {
- int i;
- for(i = 0; text[i] != '/0'; i++)
- {
- }
- return i;
- }
- bool CheckRuleLocal(char* text, int Achar)
- {
- int invalid = 0;
- if(text[0] == '.'|| text[Achar - 1] == '.')
- invalid++;
- if(text[0] == '-'|| text[Achar - 1] == '-')
- invalid++;
- for(int i = 0; i < Achar && invalid == 0; i++)
- {
- if(text[i] == '.' && text[i + 1] == '.')
- invalid++;
- if(text[i] == '-' && text[i + 1] == '-')
- invalid++;
- }
- if(invalid > 0)
- return false;
- else
- return true;
- }
- bool CheckRuleDomain(char* text, int Achar, int length)
- {
- int invalid = 0;
- if(text[Achar + 1] == '.'|| text[length - 1] == '.')
- invalid++;
- if(text[Achar + 1] == '-'|| text[length - 1] == '-')
- invalid++;
- for(int i = Achar + 1; i < MAX_SIZE && invalid == 0; i++)
- {
- if(text[i] == '.' && text[i + 1] == '.')
- invalid++;
- if(text[i] == '-' && text[i + 1] == '-')
- invalid++;
- }
- if(invalid > 0)
- return false;
- else
- return true;
- }
- bool ValidCharsL(char* text, char* invalidCharacters, int Achar)
- {
- int invalid = 0;
- for(int i = 0; i < Achar && invalid == 0; i++)
- {
- for(int t = 0; t < GetLength(invalidCharacters); t++)
- {
- if(text[i] == invalidCharacters[t])
- invalid++;
- }
- }
- if(invalid > 0)
- return false;
- else
- return true;
- }
- bool ValidCharsD(char* text, char* invalidCharacters, int Achar)
- {
- int invalid = 0;
- for(int i = Achar + 1; text[i] != '/0'; i++)
- {
- for(int t = 0; t < GetLength(invalidCharacters); t++)
- {
- if(text[i] == invalidCharacters[t])
- invalid++;
- }
- }
- if(invalid > 0)
- return false;
- else
- return true;
- }
- //Finds the position of @ and returns an int that will store value if not found returns -1
- int Find(char* text, int arraySize)
- {
- int AIndex = 0;
- for(int i = 0; i <= arraySize; i++)
- {
- if(text[i] == '@')
- AIndex = i;
- }
- if(AIndex > 0)
- return AIndex;
- else
- return NOT_FOUND;
- }
- int CheckLengthLocal(char* text, int Achar)
- {
- int count = 0;
- for(int i = 0; i != Achar; i++)
- {
- count ++;
- }
- return count;
- }
- int CheckLengthDomain(char* text, int Achar)
- {
- int count = 0;
- for(int i = Achar + 1; text[i] != '/0'; i++)
- {
- count ++;
- }
- return count;
- }
- bool IsValid(char* email)
- {
- int Length, atIndex, LocalLength, DomainLength, aCheck = 0;
- char* Invalid = "()[]\;:,<>";
- // Searches for the NULL terminator at the end of the char array and returns Length of the arry
- Length = GetLength(email);
- if(Length <= 256)
- {
- // Finds the @ char and returns an int variable labeling its position
- atIndex = Find(email, Length);
- aCheck++;
- if(atIndex != NOT_FOUND)
- {
- LocalLength = CheckLengthLocal(email, atIndex);
- DomainLength = CheckLengthDomain(email, atIndex);
- aCheck++;
- if(LocalLength <= 64 && DomainLength <= 253)
- {
- aCheck++;
- if(ValidCharsL(email, Invalid, atIndex) == true && ValidCharsD(email, Invalid, atIndex) == true)
- {
- aCheck++;
- if(CheckRuleLocal(email, atIndex) == true && CheckRuleDomain(email, atIndex, Length) == true)
- {
- aCheck++;
- return true;
- }
- }
- }
- }
- }
- if(aCheck != 5)
- return false;
- //Email = local@domain
- //domain = subdomain1.subdomain2....tld
- //NOTE: tlds once tld is identified run match to identify valid tlds: only letters, atleast 2 chars
- // Algorithm
- // 1) Find @ and place its position into atIndex
- // - 0..atIndex is going to be local part
- // - atIndex + 1..length(email)-1 = domain
- // 2) Check the length of entire email, should be <= 256
- // 3) Check length of Local Part, <=64
- // 4) Check length of domain, <= 253
- // 5) Check localPart for allowed chracters
- // - 0..9 A..Z a..z _ - .
- // 6) Check Domain for allowed chracters
- // - 0..9 A..Z a..z _ - .
- // 7) Dot rule on Local Part
- // - cannot begin with a dot
- // - cannot end with a .
- // - cannot have two .
- // 8) Dash rule on Local Part
- // - cannot begin with a -
- // - cannot end with a -
- // - cannot have two -
- // 9-10) Repeat 7-8 for domain
- }
- // Dash/dot rule checker
- // specialChar can be a dash or a dot
- // can add another argument
- // CheckRule(email, '.', atIndex +1)
- // CheckRule(&email[atIndex + 1], '.')
- // CheckRule(&email + atIndex + 1, '.')
- //
- bool CheckRule(char* emailPart, char speacialChar);
- //Returns the length of text by scanning for null-term
- // Returns the first position of aChar in text when aChar is found
- // or -1 (NOT_FOUND) otherwise
- // returns false when text tat are not present in validCharacters
- bool CheckForValidChars(char* text, char* validCharacters);
- void main()
- {
- //High level alg
- // O) Open input file and output file
- ifstream input("Email.txt");
- ofstream output("Result.txt");
- // Oa) While not at the end of input
- while(!input.eof())
- {
- char email[MAX_SIZE];
- // Ob)Read email input
- input.getline(email, MAX_SIZE);
- // Validat email
- if (IsValid(email))
- // Write results to output file
- output<< "1" << endl;
- else
- output << "0" << endl;
- }
- // out << IsValid(email) << endl;
- //Email = local@domain
- //domain = subdomain1.subdomain2....tld
- // Algorithm
- // 1) Find @ and place its position into atIndex
- // - 0..atIndex is going to be local part
- // - atIndex + 1..length(email)-1 = domain
- // 2) Check the length of entire email, should be <= 256
- // 3) Check length of Local Part, <=64
- // 4) Check length of domain, <= 253
- // 5) Check localPart for allowed chracters
- // - 0..9 A..Z a..z _ - .
- // 6) Check Domain for allowed chracters
- // - 0..9 A..Z a..z _ - .
- // 7) Dot rule on Local Part
- // - cannot begin with a dot
- // - cannot end with a .
- // - cannot have two .
- // 8) Dash rule on Local Part
- // - cannot begin with a -
- // - cannot end with a -
- // - cannot have two -
- // 9-10) Repeat 7-8 for domain
- system("pause");
- }