Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- /*
- Random Password Generator located @ https://pastebin.com/fX0D26Je
- */
- // Program allows for 128 services to be stored
- const int MAX_SERVICES = 128;
- const std::string SERVICE_FILE_NAME = "service_list.txt";
- bool correctCommandSyntax(int argc);
- std::string getServicePassword(std::string serviceName);
- void addService(std::string serviceName, std::string servicePassword);
- void removeService(std::string serviceName);
- void runSetup();
- bool setupRequired();
- std::string getFileLine(int line, std::string fileName);
- std::string getFileLine(int line);
- void getServiceData();
- void listServices();
- int getIndexOfCharacterInString(std::string str, char c);
- int main(int argc, char** argv)
- {
- // Check to insure that issued command is in the right syntax
- if (!correctCommandSyntax(argc))
- {
- std::cout << "Bad syntax.\nUse pass <service_name> or pass add" << std::endl;
- return 1;
- }
- // Checking if we need to run program setup before move on
- if (setupRequired())
- {
- std::cout << "Creating required files." << std::endl;
- runSetup();
- }
- std::string issuedCommand = argv[1];
- // Start function for adding service to list
- if (issuedCommand == "add")
- {
- getServiceData();
- }
- else if (issuedCommand == "remove")
- {
- std::string serviceName;
- std::cout << "What service do you want to remove? ";
- std::cin >> serviceName;
- removeService(serviceName);
- }
- else if(issuedCommand == "generatep")
- {
- system("python3 rand.py");
- }
- else if(issuedCommand == "list")
- {
- listServices();
- }
- else
- {
- std::cout << getServicePassword(issuedCommand) << std::endl;
- }
- return 0;
- }
- bool correctCommandSyntax(int argc)
- {
- return argc == 2;
- }
- std::string getServicePassword(std::string serviceName)
- {
- // The delimiter marks where service name ends and password begins
- std::string password = "";
- char delimiter = ':';
- for (int i = 0; i < MAX_SERVICES; i++)
- {
- // rest password value after each time there is incorrect data in the variable
- password = "";
- std::string currentLine = getFileLine(i);
- // If the current line is nothing that means that we don't need to look through the array anymore, because nothing else will be filled
- if (currentLine == "")
- {
- break;
- }
- // splitting the password and service into two different variables
- std::string service = currentLine.substr(0, currentLine.find(delimiter));
- password = currentLine.substr(currentLine.find(delimiter) + 1);
- // The service has been found
- if(service == serviceName)
- {
- break;
- }
- }
- return password;
- }
- void addService(std::string serviceName, std::string servicePassword)
- {
- // Checking for banned service name
- std::string bannedServiceName = "add";
- if (serviceName == bannedServiceName)
- {
- std::cout << "Service name cannot be " << bannedServiceName << std::endl;
- return;
- }
- // Appending service name and password to file
- std::ofstream outputFile;
- outputFile.open(SERVICE_FILE_NAME, std::ios::app);
- outputFile << serviceName << ':' << servicePassword << '\n';
- outputFile.close();
- }
- void removeService(std::string serviceName)
- {
- int lineToDelete = -1;
- char delimiter = ':';
- // finding out what line needs to be deleted
- for (int i = 0; i < MAX_SERVICES; i++)
- {
- // if the current line is empty, that means there are no more services to go over
- if (getFileLine(i) == "")
- {
- break;
- }
- else
- {
- // finding service of current line and check if it's the name of looked for service
- std::string currentLine = getFileLine(i);
- std::string service = currentLine.substr(0, currentLine.find(delimiter));
- if (service == serviceName)
- {
- lineToDelete = i;
- break;
- }
- else
- {
- continue;
- }
- }
- }
- // returning if no service was found on the list
- if (lineToDelete == -1)
- {
- std::cout << "Service not found" << std::endl;
- return;
- }
- // writing file contents into a buffer file
- std::string bufferFileName = "service_buffer.txt";
- std::ofstream buffer;
- buffer.open(bufferFileName, std::ios::app);
- // copying all lines, but the one being deleted
- for (int i = 0; i < MAX_SERVICES; i++)
- {
- // skip if we are on the line to delete
- if (i == lineToDelete)
- {
- continue;
- }
- else
- {
- buffer << getFileLine(i);
- }
- }
- buffer.close();
- // clearing old file
- std::ofstream output;
- output.open(SERVICE_FILE_NAME);
- output << "";
- output.close();
- // writing new file
- output.open(SERVICE_FILE_NAME, std::ios::app);
- for (int i = 0; i < MAX_SERVICES; i++)
- {
- output << getFileLine(i, bufferFileName);
- }
- output.close();
- system("rm service_buffer.txt");
- return;
- }
- void runSetup()
- {
- std::ofstream outputFile;
- outputFile.open(SERVICE_FILE_NAME);
- outputFile.close();
- }
- bool setupRequired()
- {
- std::ifstream inputFile;
- inputFile.open(SERVICE_FILE_NAME);
- return !inputFile.is_open();
- }
- std::string getFileLine(int line, std::string fileName)
- {
- // Opening service file to search for password
- std::ifstream in;
- in.open(fileName);
- // Placeholder holds the final value while buffer helps continue the iteration through the loop
- std::string placeholder = "";
- std::string buffer = "";
- for (int i = 0; i < MAX_SERVICES; i++)
- {
- // When we reach the requested line place that value into place holder and leave loop
- if (i == line)
- {
- in >> placeholder;
- break;
- }
- // put current lines input into buffer so iteration through the file can continue
- in >> buffer;
- }
- // The value in placeholder is the value of the line
- if (placeholder != "")
- {
- return placeholder + '\n';
- }
- // return the empty line so we don't have a ton of whitespace filling the window
- return placeholder;
- }
- std::string getFileLine(int line)
- {
- return getFileLine(line, SERVICE_FILE_NAME);
- }
- void getServiceData()
- {
- std::cout << "Add service to list" << std::endl;
- std::string serviceName;
- std::string servicePass;
- std::cout << "Name: ";
- std::cin >> serviceName;
- std::cout << "Pass: ";
- std::cin >> servicePass;
- addService(serviceName, servicePass);
- }
- int getIndexOfCharacterInString(std::string str, char c)
- {
- int size = str.size();
- // searching through string and returning the index if found
- for (int i = 0; i < size; i++){
- if (str[i] == c)
- {
- return i;
- }
- }
- // string wasn't found
- return -1;
- }
- void listServices()
- {
- char delim = ':';
- for (int i = 0; i < MAX_SERVICES; i++)
- {
- std::string currentLine = getFileLine(i);
- // quit operation if we have exhausted the list
- if (currentLine == "")
- {
- return;
- }
- else
- {
- int indexOfDelimeter = getIndexOfCharacterInString(currentLine, delim);
- std::string serviceName = currentLine.substr(0, indexOfDelimeter);
- std::cout << serviceName << std::endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement