diff --git a/config_generator/.idea/.name b/config_generator/.idea/.name new file mode 100644 index 0000000..da6df4d --- /dev/null +++ b/config_generator/.idea/.name @@ -0,0 +1 @@ +Assignment2-Config-Files \ No newline at end of file diff --git a/config_generator/.idea/Assignment2-Config-Files.iml b/config_generator/.idea/Assignment2-Config-Files.iml new file mode 100644 index 0000000..4af1238 --- /dev/null +++ b/config_generator/.idea/Assignment2-Config-Files.iml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/config_generator/.idea/codeStyleSettings.xml b/config_generator/.idea/codeStyleSettings.xml new file mode 100644 index 0000000..cea61a4 --- /dev/null +++ b/config_generator/.idea/codeStyleSettings.xml @@ -0,0 +1,41 @@ + + + + + + \ No newline at end of file diff --git a/config_generator/.idea/encodings.xml b/config_generator/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/config_generator/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/config_generator/.idea/misc.xml b/config_generator/.idea/misc.xml new file mode 100644 index 0000000..3eb495b --- /dev/null +++ b/config_generator/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/config_generator/.idea/modules.xml b/config_generator/.idea/modules.xml new file mode 100644 index 0000000..59bf792 --- /dev/null +++ b/config_generator/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/config_generator/.idea/workspace.xml b/config_generator/.idea/workspace.xml new file mode 100644 index 0000000..6383b99 --- /dev/null +++ b/config_generator/.idea/workspace.xml @@ -0,0 +1,555 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1455038167931 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/main.cpp + 109 + + + file://$USER_HOME$/Documents/Schoolwork/Spring2016/C++/Assignment3/lib/main.cpp + 52 + + + file://$USER_HOME$/Documents/Schoolwork/Spring2016/C++/Assignment3/lib/main.cpp + 53 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/config_generator/CMakeLists.txt b/config_generator/CMakeLists.txt new file mode 100644 index 0000000..e5458e6 --- /dev/null +++ b/config_generator/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.3) +project(Assignment2_Config_Files) + +include_directories("C:\\Users\\Samantha\\Documents\\Schoolwork\\Libraries") +link_directories("C:\\Users\\Samantha\\Documents\\Schoolwork\\Libraries") + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + +set(SOURCE_FILES main.cpp configheader.h) +add_executable(Assignment2_Config_Files ${SOURCE_FILES}) + +target_link_libraries(Assignment2_Config_Files lib) \ No newline at end of file diff --git a/config_generator/Config.txt b/config_generator/Config.txt new file mode 100644 index 0000000..f88c6d2 --- /dev/null +++ b/config_generator/Config.txt @@ -0,0 +1,10 @@ +[Name] + Sam Shurie +[E-mail] + shuries@my.wccc.edu +[Password] + pass +[Timezone Offset] + -5:00 +[File Path] + C:\Users\Samantha\ClionProjects\Assignment2-Config-Files\Config.txt diff --git a/config_generator/ConfigOutput.txt b/config_generator/ConfigOutput.txt new file mode 100644 index 0000000..133dda4 --- /dev/null +++ b/config_generator/ConfigOutput.txt @@ -0,0 +1,10 @@ +[name] + Samantha +[email] + shuries@my.wccc.edu +[cypher] + cypher +[timezone] + 5:00 +[filepath] + C:\Users\Samantha\Documents\Schoolwork\Spring2016\C++\Assignment2\ConfigOutput.txt diff --git a/config_generator/backup.txt b/config_generator/backup.txt new file mode 100644 index 0000000..3618bf4 --- /dev/null +++ b/config_generator/backup.txt @@ -0,0 +1,10 @@ +[Name] + Samantha Shurie +[E-mail] + shuries@my.wccc.edu +[Password] + pass +[Timezone Offset] + -5:00 +[File Path] + C:\Users\Samantha\ClionProjects\Assignment2-Config-Files\Config.txt diff --git a/config_generator/configheader.h b/config_generator/configheader.h new file mode 100644 index 0000000..8151238 --- /dev/null +++ b/config_generator/configheader.h @@ -0,0 +1,28 @@ +#include +#include +#include "Assignment3-Libraries.h" + +using std::string; +using std::cout; +using std::endl; +using std::cin; +using std::ofstream; +using std::ifstream; + +enum fields {f_name, email, cypher, timezone, f_path}; +const string FIELDS[5] = {"name", "email", "cypher", "timezone", "filepath"}; + +bool ifGood(string filepath); // checks whether file exists/was successfully created +string cast_str(char *arg); // casts character pointer to string +bool user_confirm(); // prompts the user before overwriting given files +string handlePath(); // handles overwriting old path and retreiving new ones + +string validateArg1(string arg); // makes sure arg 1 is equal to init or edit +string validateArg2(string arg); // makes sure arg 2 is equal to a file path + +string get_value(fields field); // retreives new value from the user for struct field + +void init(); // creates a new ConfigFile struct and writes its data to a file + +void edit(string field); /* creates a ConfigFile struct from an existing file and edits it before writing to + * new file or overwriting old one */ diff --git a/config_generator/main.cpp b/config_generator/main.cpp new file mode 100644 index 0000000..2249187 --- /dev/null +++ b/config_generator/main.cpp @@ -0,0 +1,232 @@ +#include "configheader.h" + +const string FILE_PATH = "C:\\Users\\Samantha\\Documents\\Schoolwork\\Spring2016\\C++\\Assignment5\\Config.txt"; + +int main(int argc, char *argv[]) { + + // initialize variables to hold given arguments. + // Initialized in case of user not entering arguments on start + string mode = ""; + string field = ""; + + // retrieve argument 1 and cast as string, then test the given argument. + // if anything other than edit or init is entered, + // prompt until correct input is entered, then return acceptable input. + mode = cast_str(argv[1]); + mode = validateArg1(mode); + + // only if the first agument is edit, cast the second argument and validate + if(mode == "edit"){ + field = cast_str(argv[2]); + field = validateArg2(field); + } + + // after all validation is complete, run either init or edit function + if(mode == "init"){ + init(); + }else if(mode == "edit"){ + edit(field); + } + + return 0; +} + +/** + * cast_str function casts a character pointer to a string + */ +string cast_str(char *arg){ + string str = static_cast(arg); + return str; +} + +/* + * validateArg1 function accepts a string and compares it to "init" and "edit." + * if true, the string is returned without doing anything. Else, keep + * prompting for input until an acceptable answer is given and then return + */ +string validateArg1(string arg) { + while ((arg != "init") && (arg != "edit")) { + cout << "To begin, please enter \"init\" or \"edit\":"; + cin >> arg; + } + return arg; +} + +/* + * Similar to validateArg1, accepts a string and compares it with the five field options of the + * ConfigFile Struct. If true, return with no action. Else, prompt for new input until user provides + * valid entry and then return. + */ +string validateArg2(string arg) { + bool found = false; + while(arg != "name" && arg != "email" && arg != "cypher" && arg != "timezone" && arg != "filepath"){ + cout << "To edit, please enter one of the following fields; \"name\", \"email\", \"cypher\", \"timezone\"," + " \"filepath\": "; + cin >> arg; + } + return arg; +} + +void init() { + + cout << "\nCONFIG FILE CREATION\n\n"; + + // declare struct + configio::ConfigFile config; + + // initialize struct fields by calling the get_value function and returning a string value + config.first_name = get_value(f_name); + config.email = get_value(email); + config.cypher = get_value(cypher); + config.timezone = get_value(timezone); + config.filepath = get_value(f_path); + + //write config to file and print success status + if(configio::write_config(config.filepath, config)){ + cout << "( File Created )" << endl; + }else { + cout << "( Error Creating File )" << endl; + } +} + +/** + * get_value will return dtring values for ConfigFile fields + */ +string get_value(fields field){ + bool isFPath = false; + + // Determine whether current field is f_path (handled differently than other fields) + if(field == f_path){ + isFPath = true; + cout << "\tEnter a value for the " << FIELDS[field] << " field " + "( Pressing \"Enter\" will set filepath to default location ):"; + } else { + cout << "\tEnter a value for the " << FIELDS[field] << " field:"; + } + string input = ""; + getline(cin, input); + + // if the user pressed Enter without entering input, determine whether current field + // is fpath. If isFPath, start path handling. + // Else, if not isFPath, prompt the user for a field value again + if (input == "") { + if (isFPath){ + handlePath(); + } else { + while(input == "") { + cout << "( Invalid Input )" << endl; + cout << "\tEnter a value for the " << FIELDS[field] << " field:"; + getline(cin, input); + } + } + } + return input; +} + +/* handlePath function prompts user a second time before overwriting default config file + * if user_confirm is true, filepath is set to default location + * if false, user enters new path and path is tested before being returned + */ +string handlePath() { + string path; + + // if user confirms that they want to overwrite the default template file, return the constant path + if (user_confirm()) { + path = FILE_PATH; + cout << "( File path set to default location )" << endl; + + // else, if user has changed their mind, give a option to enter new file path. + // After new file path is given by user, test path and return if good. + } else { + cout << "\tEnter a new value for the " << FIELDS[f_path] << " field:"; + getline(cin, path); + while(!ifGood(path)){ + cout << "( Invalid input )" << endl; + cout << "\tEnter a new value for the " << FIELDS[f_path] << " field:"; + getline(cin, path); + } + cout << "( New file path set )" << endl; + } + + return path; +} + +/* if user has chosen to overwrite the default file, + * prompt a second time before actually overwriting + */ +bool user_confirm(){ + cout << "\tAre you sure you want to overwrite the default config file?" << endl; + cout << "( " << FILE_PATH << " )" << endl << "\tyes or no: "; + string input; + getline(cin, input); + while (input != "yes" && input != "no") { + cout << "( Invalid input )" << endl << "\tPlease enter \"yes\" or \"no\": "; + getline(cin, input); + } + bool isOverwriting = false; + if (input == "yes") { + isOverwriting = true; + cout << "( You selected to overwrite the default file )" << endl; + } else { + cout << "( You selected not to overwrite the default file )" << endl; + } + return isOverwriting; +} + +/* + * ifGood accepts a file path string and determines whether the path is locatable + */ +bool ifGood(string filepath){ + ifstream file(filepath); + bool status = false; // initialize to false and only switch to true if file can be opened + if(file){ + status = true; // file can be opened + } + file.close(); + return status; +} + + +void edit(string field){ + + cout << "\nCONFIG FILE EDIT\n\n"; + cout << "\tEnter a filepath, or press 'Enter' to open the default path: "; + string path; + getline(cin, path); + if(path.length() == 0){ + path = FILE_PATH; + } + + //read_config function reads lines of a file, stores data in struct and returns struct pointer + configio::ConfigFile *config_ptr = configio::read_config(path); + configio::ConfigFile config = *config_ptr; + + // prompt user to change given field + cout << "\tEnter a new value for the " + field + " field: "; + if(field == "name"){ + getline(cin, config.first_name); + }else if(field == "email"){ + getline(cin, config.email); + }else if(field == "password"){ + getline(cin, config.cypher); + }else if(field == "timezone"){ + getline(cin, config.timezone); + } + + // User is always given the option to change the filepath + // If the user presses enter or gives an invalid path, the original filepath is kept + cout << "\tEnter a new value for the filepath field or press \"Enter\" to keep the current path"; + getline(cin, path); + if(path.length() != 0){ + ofstream file(path); + if(file.good()){ + config.filepath = path; + } + file.close(); + } + if(configio::write_config(config.filepath, config)){ + cout << "File Updated" << endl; + }else { + cout << "Error in creating file"; + } +} diff --git a/config_reader_lib/.idea/.name b/config_reader_lib/.idea/.name new file mode 100644 index 0000000..7951405 --- /dev/null +++ b/config_reader_lib/.idea/.name @@ -0,0 +1 @@ +lib \ No newline at end of file diff --git a/config_reader_lib/.idea/encodings.xml b/config_reader_lib/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/config_reader_lib/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/config_reader_lib/.idea/lib.iml b/config_reader_lib/.idea/lib.iml new file mode 100644 index 0000000..5476026 --- /dev/null +++ b/config_reader_lib/.idea/lib.iml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/config_reader_lib/.idea/misc.xml b/config_reader_lib/.idea/misc.xml new file mode 100644 index 0000000..5832cdc --- /dev/null +++ b/config_reader_lib/.idea/misc.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + C/C++ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/config_reader_lib/.idea/modules.xml b/config_reader_lib/.idea/modules.xml new file mode 100644 index 0000000..b0c4ff6 --- /dev/null +++ b/config_reader_lib/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/config_reader_lib/.idea/workspace.xml b/config_reader_lib/.idea/workspace.xml new file mode 100644 index 0000000..8c3e295 --- /dev/null +++ b/config_reader_lib/.idea/workspace.xml @@ -0,0 +1,703 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1456936489119 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/config_reader_lib/Assignment3-Libraries.h b/config_reader_lib/Assignment3-Libraries.h new file mode 100644 index 0000000..3fec9cc --- /dev/null +++ b/config_reader_lib/Assignment3-Libraries.h @@ -0,0 +1,30 @@ +#include +#include +#include + +namespace configio { + using std::string; + using std::cout; + using std::ifstream; + using std::ofstream; + using std::endl; + + struct ConfigFile { + string first_name; + string email; + string cypher; + string timezone; + string filepath; + }; + + ConfigFile* read_config(string config_file_path); + + bool write_config(string path, ConfigFile Config); + + int line_count(string file_path); +} + + + + + diff --git a/config_reader_lib/CMakeLists.txt b/config_reader_lib/CMakeLists.txt new file mode 100644 index 0000000..8e2ddf6 --- /dev/null +++ b/config_reader_lib/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.3) +project(lib) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + +set(SOURCE_FILES main.cpp Assignment3-Libraries.h) +add_library(lib ${SOURCE_FILES}) \ No newline at end of file diff --git a/config_reader_lib/main.cpp b/config_reader_lib/main.cpp new file mode 100644 index 0000000..a84a261 --- /dev/null +++ b/config_reader_lib/main.cpp @@ -0,0 +1,75 @@ + +#include "./Assignment3-Libraries.h" + +namespace configio{ + + // Function returns a pointer to a ConfigFile struct + ConfigFile* read_config(string file_path){ + + //get number of lines in file + // number of lines divided by two because file input will be distributed into two parallel arrays + //int num_lines = line_count(file_path); + const int SIZE = 5; + + // declare input file and open path + ifstream file(file_path); + + // declare parallel arrays for file input + string headers[SIZE]; + string values[SIZE]; + + //read lines of file and store in array + int i = 0; + do{ + getline(file, headers[i]); + getline(file, values[i]); + cout << headers[i] << endl; //test for lines being read correctly from file + cout << values[i] << endl; + i++; + } while(!file.eof()); + + // file closes after finished reading input + file.close(); + + // Create string pointer variable and assign address of array + ConfigFile config; + + // Still needs error checking to make sure fields are in correct order in file + config.first_name = values[0]; + config.email = values[1]; + config.cypher = values[2]; + config.timezone = values[3]; + config.filepath = values[4]; + + ConfigFile* configptr = &config; + return configptr; + } + + int line_count(string path){ + ifstream file(path); + string line = ""; + int count = 0; + while(getline(file, line)){ + count++; + } + file.close(); + return count; + } + + bool write_config(string path, ConfigFile config){ + const int SIZE = 5; + const string HEADERS[5] = {"[name]", "[email]", "[cypher]", "[timezone]", "[filepath]"}; + const string VALUES[5] = {config.first_name, config.email, config.cypher, config.timezone, config.filepath}; + ofstream file(path); + bool flag = false; + if(file.good()) { + for(int i = 0; i < SIZE; i++){ + file << HEADERS[i] << endl; + file << "\t" << VALUES[i] << endl; + } + flag = true; + } + file.close(); + return flag; + } +} \ No newline at end of file