All pastes #160269 Raw Edit

Unnamed

public text v1 · immutable
·published 2006-09-04 00:49 UTC
rendered paste body
// leet converter v.2
// © Copyright Brandon Weeks 2006
// 9/3/2006

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cstdlib>

using namespace std;

// Function Declarations
string converter(string);
string lowercase(string);

int main (int argc, char * const argv[]) {
	string input; // Variable for the text to be converted 
	string output; // Variable for converted text to be outputted to console
	string arg[argc]; // Variable where console args in string form
	bool fileOutput; // Set to true if output to file instead of console
	string outputFileName; // If file output, this is the output file name
	
	for (int i=1;i < argc;i++) // Place the argv's into the string array 'arg'
		arg[i]=argv[i];
	
	for (int i=1;i < argc;i++) { 
			if (arg[i] == "-o") { // Test if one of the args is "-o"
				fileOutput=true; // If true, set fileOutput=true
				outputFileName=arg[i+1]; // Also set the next arg as outputFileName
				break; // Leave the loop
			}
	}
	
	if (argc == 1) {
		bool firstRun=true;
		cout << "Welcome to BJ's l33t converter, type quit to exit at any time"
				 << endl
				 <<  "Enter something to be converted to l33t: ";
		
		while (input != "quit") {
			if (firstRun == false)
				cout << endl << "Enter something else to be converted to leet: ";
			firstRun=false;
			getline(cin, input);
			if (input != "quit") {
				output=converter(input);
				cout << output << endl; 
			}
		}

		return 0;
	}

	else if (arg[1] == "file") { // If we are input from a file
		vector<string> container; // Vector to hold file contents
		string buffer; // String buffer to hold the file contents before we put them in the vector
		
		if (arg[2].empty()) {					// If no input file, tell the user and exit code 1
			cout << "No input file!" << endl;
			return 1;
		}
		
		ifstream inputFile(arg[2].c_str()); // Open the input file
		
		if (!inputFile) {												// If the file didn't open, warn the user and exit code 1
			cout << "An error has occurred opening the file!" << endl;
			return 1;
		}
		
		while (!inputFile.eof()) {		 // Loop intil we hit the end of file
			getline(inputFile,buffer);	 // Put the curent line into the buffer
			container.push_back(buffer); // Place the buffer into the vactor
		}
		
		for (unsigned i=0; i < container.size();i++) // Loop till we convert the whole vector
			container[i]=converter(container[i]);	 // Run every line thru the converter fuction
		
		inputFile.close(); // Close the input file

		if (fileOutput==true) {										// If we are going output to file
			ofstream outputFile(outputFileName.c_str());			// Open the output file
			if (!outputFile) {										// If we can't open the output file, warn the user and exit code 1
				cout << "Error creating the output file!" << endl;
				return 1;
			}
			for (unsigned i=0;i < container.size();i++) { // Place the contents of the vector into the file
					outputFile << container[i];
					outputFile << endl;					  // Go to next line
			} 			
			outputFile.close();							  // Close the output file
		}
		 else {											  // If we are not going to output to file, just output to console
			for (unsigned i=0;i < container.size();i++) 
				cout << container[i] << endl; 
		}
	}
		else {										// If the user inputs an unknown arg, warn user and exit code 1
		cout << "leet: unknown option" << endl; 
		return 1;
	}
}

string converter(string foo) {
	for (unsigned i=0;i < foo.size();i++)
		foo[i]=tolower(foo[i]);
	
	for (unsigned i=0;i < foo.size(); i++)
		if (foo[i] == 'a' )
			foo[i] = '4';
		else if (foo[i] == 'b')
			foo[i] = '8';
		else if (foo[i] == 'e')
			foo[i] = '3';
		else if (foo[i] == 'i')
			foo[i] = '1';
		else if (foo[i] == 'l')
			foo[i] = '1';
		else if (foo[i] == 'o')
			foo[i] = '0';
		else if (foo[i] == 's')
			foo[i] = '5';
		else if (foo[i] == 't')
			foo[i] = '7';
		return foo;
}