Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Assignment5 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.3)
project(Assignment5)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_library(Assignment5 ${SOURCE_FILES})
29 changes: 29 additions & 0 deletions config_generator/.idea/config_generator.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions config_generator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.3)
project(Assignment5Generator)

include_directories("C:\\Users\\Seth\\Desktop\\WCCC\\CPT 180 - C++ Programming\\Assignment5\\config_reader_lib")
link_directories("C:\\Users\\Seth\\Desktop\\WCCC\\CPT 180 - C++ Programming\\Assignment5\\config_reader_lib")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp generator.h)
add_executable(Assignment5Generator ${SOURCE_FILES} generator.h)
target_link_libraries(Assignment5Generator Assignment5)
20 changes: 20 additions & 0 deletions config_generator/generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <fstream>

using std::cin;
using std::cout;
using std::endl;
using std::string;

struct Config {
string first_name;
string last_name;
string email;
string cypher;
string timezone;
};

namespace config_generator {
Config* read_config(string config_file_path);
bool write_config(string config_file_path, struct Config);
}
132 changes: 132 additions & 0 deletions config_generator/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include "./generator.h"

void init();
void edit(string edit_field);

int main(int argc, char *argv[]) {

cout << "Please enter 'init' to initialize config file or 'edit' plus the field you wish to change:" << endl;

if (static_cast<string>(argv[1]) == "init") {
cout << "Preparing config file initialization." << endl;
init();
}
else if (static_cast<string>(argv[1]) == "edit") {
cout << "Preparing to edit config file." << endl;
edit(static_cast<string>(argv[2]));
}

return 0;
}

void init(){
// Receiving config file data from user, repeating request while blank
string first_name = "";
do {
cout << "Enter your first name" << endl;
getline (cin, first_name);
} while (first_name == "");

string last_name = "";
do {
cout << "Enter your last name" << endl;
getline (cin, last_name);
} while (last_name == "");

string email = "";
do {
cout << "Enter your email address" << endl;
getline (cin, email);
} while (email == "");

string cypher = "";
do {
cout << "Enter a unique cypher" << endl;
getline (cin, cypher);
} while (cypher == "");

string timezone = "";
do {
cout << "Enter your timezone offset" << endl;
getline (cin, timezone);
} while (timezone == "");

string file_path = "";
cout << "Enter path to knownrecipients file" << endl;
getline (cin, file_path);
if (file_path == "") {
file_path = "knownrecipients.txt";
}

// Inserting data into a Config structure
Config data;
data.first_name = first_name;
data.last_name = last_name;
data.email = email;
data.cypher = cypher;
data.timezone = timezone;

// Sending structure to write_config function to create config file
bool file_found = config_generator::write_config(file_path, data);

if (file_found == true) {
cout << "Config file successfully initialized" << endl;
} else {
cout << "Could not initialize config file" << endl;
}
}

void edit(string edit_field) {
string file_path_edit;
cout << "Enter path to knownrecipients file:" << endl;
cin >> file_path_edit;

// Receiving structure Config pointer from read_config function
Config *config_ptr = nullptr;
config_ptr = config_generator::read_config(file_path_edit);

// Initializing fields with data from Config structure
string first_name_edit = config_ptr->first_name;
string last_name_edit = config_ptr->last_name;
string email_edit = config_ptr->email;
string cypher_edit = config_ptr->cypher;
string timezone_edit = config_ptr->timezone;

// Receiving edit field information from second argument
if (edit_field == "name") {
cout << "The current name is: " << first_name_edit << " " << last_name_edit << endl;
cout << "Enter new first name" << endl;
cin >> first_name_edit;
cout << "Enter new last name" << endl;
cin >> last_name_edit;
} else if (edit_field == "email") {
cout << "The current email is: " << email_edit << endl;
cout << "Enter new email" << endl;
cin >> email_edit;
} else if (edit_field == "cypher") {
cout << "The current cypher is: " << cypher_edit << endl;
cout << "Enter new cypher" << endl;
cin >> cypher_edit;
} else if (edit_field == "timezone") {
cout << "The current timezone offset is: " << timezone_edit << endl;
cout << "Enter new timezone offset" << endl;
cin >> timezone_edit;
}

// Putting previous data plus edited data back in to a Config struct
Config data;
data.first_name = first_name_edit;
data.last_name = last_name_edit;
data.email = email_edit;
data.cypher = cypher_edit;
data.timezone = timezone_edit;

// Sending structure to write_config function to update file
bool file_found = config_generator::write_config(file_path_edit, data);

if (file_found == true) {
cout << "Config file successfully edited" << endl;
} else {
cout << "Could not edit config file" << endl;
}
}
Binary file added config_reader_lib/libAssignment5.a
Binary file not shown.
19 changes: 19 additions & 0 deletions config_reader_lib/library.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <fstream>
using std::string;
using std::ifstream;
using std::ofstream;
using std::endl;

struct Config{
string first_name;
string last_name;
string email;
string timezone;
string cypher;
};

namespace config_generator {
Config* read_config(string config_file_path);
bool write_config(string config_file_path, Config data);
}
5 changes: 5 additions & 0 deletions knownrecipients.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Seth
Frosch
[email protected]
-5
cypher
55 changes: 55 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "./library.h"

namespace config_generator {

Config* read_config(string config_file_path) {
string lines[5];
Config config_data;
Config *config_ptr = nullptr;
config_ptr = new Config;
ifstream inputFile;

// Method will return file at the path passed to it
inputFile.open(config_file_path);

// For loop to store each line from the file to an array in lines
for (int i = 0; i < 5; i++) {
inputFile >> lines[i];
}

// Each line from lines array will be placed in config struct
config_data.first_name = lines[0];
config_data.last_name = lines[1];
config_data.email = lines[2];
config_data.cypher = lines[3];
config_data.timezone = lines[4];

// Pointer to struct address
config_ptr = &config_data;

inputFile.close();

return config_ptr;
}

bool write_config(string config_file_path, Config data) {
ofstream outputFile;
bool file_found = false;

// Method will return file at the path passed to it
outputFile.open(config_file_path);

// If the file path is correct, data from config struct will be written to it
if (outputFile) {
outputFile << data.first_name << endl;
outputFile << data.last_name << endl;
outputFile << data.email << endl;
outputFile << data.cypher << endl;
outputFile << data.timezone << endl;

outputFile.close();
file_found = true;
}
return file_found;
}
}