diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 1c2d52b..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.idea/* diff --git a/Assignment 3/.idea/.name b/Assignment 3/.idea/.name new file mode 100644 index 0000000..799e6dc --- /dev/null +++ b/Assignment 3/.idea/.name @@ -0,0 +1 @@ +Assignment 3 \ No newline at end of file diff --git a/Assignment 3/.idea/Assignment 3.iml b/Assignment 3/.idea/Assignment 3.iml new file mode 100644 index 0000000..bf99bc3 --- /dev/null +++ b/Assignment 3/.idea/Assignment 3.iml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment 3/.idea/encodings.xml b/Assignment 3/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/Assignment 3/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Assignment 3/.idea/misc.xml b/Assignment 3/.idea/misc.xml new file mode 100644 index 0000000..3eb495b --- /dev/null +++ b/Assignment 3/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment 3/.idea/modules.xml b/Assignment 3/.idea/modules.xml new file mode 100644 index 0000000..c9fd47b --- /dev/null +++ b/Assignment 3/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Assignment 3/.idea/workspace.xml b/Assignment 3/.idea/workspace.xml new file mode 100644 index 0000000..e1ccfd1 --- /dev/null +++ b/Assignment 3/.idea/workspace.xml @@ -0,0 +1,396 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1456937961490 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Assignment 3/CMakeLists.txt b/Assignment 3/CMakeLists.txt new file mode 100644 index 0000000..b88e73c --- /dev/null +++ b/Assignment 3/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.3) +project(Assignment3_1) + +include_directories("C:/Users/joshu/Desktop/Assignment 3/reader") +link_directories("C:/Users/joshu/Desktop/Assignment 3/reader") + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + +set(SOURCE_FILES main.cpp reader.h) +add_executable(Driver ${SOURCE_FILES}) +target_link_libraries(Driver ) diff --git a/Assignment 3/main.cpp b/Assignment 3/main.cpp new file mode 100644 index 0000000..078255f --- /dev/null +++ b/Assignment 3/main.cpp @@ -0,0 +1,245 @@ +#include +#include +#include +#include "./reader.h" + + +using std::cout; +using std::cin; +using std::string; +using std::endl; + + +void Initialization() { + string Username = ""; + do { + cout << "Enter your name(first and last)" << endl; + getline(cin, Username); + } while (Username == ""); + + string Email = ""; + do { + cout << "Enter your Email address" << endl; + getline(cin, Email); + } while (Email == ""); + + string Pass = ""; + do { + cout << "Enter your password" << endl; + getline(cin, Pass); + } while (Pass == ""); + + string Timezone = ""; + do { + cout << "Enter your timezone" << endl; + getline(cin, Timezone); + } while (Timezone == ""); + + //Asks for a file location ***WIP*** (right now just names file) + string Location = ""; + cout << "Enter file name (example 'document.txt') leave blank for default" << endl; + getline(cin, Location); + if (Location == "") { + Location = "Config.txt"; + } + + //Saves the data to a text file named by "location" + std::ofstream textfile; + textfile.open(Location); + + textfile << "[Username]" << endl << Username << endl; + textfile << "[Email]" << endl << Email << endl; + textfile << "[Password]" << endl << Pass << endl; + textfile << "[Timezone]" << endl << Timezone << endl; + textfile.close(); +} + +void EditFile() { +//asks for file name same as above + string Location = ""; + cout << "Enter file name, leave blank for default" << endl; + getline(cin, Location); + if (Location == "") { + Location = "Config.txt"; + } + + //retrieves data currently saved in file and prints for user + string Saved_Username = ""; + string Saved_Email = ""; + string Saved_Password = ""; + string Saved_Timezone = ""; + string line; + std::ifstream myfile(Location); + while (!myfile.eof()) { + for (int lineno = 0; getline(myfile, line) && lineno < 8; lineno++) { + if (lineno == 1) { + Saved_Username = line; + } + if (lineno == 3){ + Saved_Email = line; + } + if (lineno == 5){ + Saved_Password = line; + } + if (lineno == 7){ + Saved_Timezone = line; + } + } + } + cout << "Saved Username: " << Saved_Username << endl; + cout << "Saved Email: " << Saved_Email << endl; + cout << "Saved Password: " << Saved_Password << endl; + cout << "Saved Timezone: " << Saved_Timezone << endl; + myfile.close(); + + std::ofstream textfile; + textfile.open(Location); + + //asks user which variable to edit in the config file + string Edit_Variable = ""; + do { + cout << "Enter a variable to edit (Username, Email, Password, or Timezone" << endl; + getline(cin, Edit_Variable); + }while (Edit_Variable == ""); + + //prompts the user to enter a new variable and replaces the old one in the text file. + if (Edit_Variable == "Username") { + string New_Username = ""; + do { + cout << "Enter new Username" << endl; + getline(cin, New_Username); + } while (New_Username == ""); + textfile << "[Username]" << endl << New_Username << endl; + textfile << "[Email]" << endl << Saved_Email << endl; + textfile << "[Password]" << endl << Saved_Password << endl; + textfile << "[Timezone]" << endl << Saved_Timezone << endl; + textfile.close(); + cout << "Edits Complete" << endl; + + }else if (Edit_Variable == "Email"){ + string New_Email = ""; + do { + cout << "Enter new Email" << endl; + getline(cin, New_Email); + } while (New_Email == ""); + textfile << "[Username]" << endl << Saved_Username << endl; + textfile << "[Email]" << endl << New_Email << endl; + textfile << "[Password]" << endl << Saved_Password << endl; + textfile << "[Timezone]" << endl << Saved_Timezone << endl; + textfile.close(); + cout << "Edits Complete" << endl; + + }else if (Edit_Variable == "Password"){ + string New_Password = ""; + do { + cout << "Enter new Password" << endl; + getline(cin, New_Password); + } while (New_Password == ""); + textfile << "[Username]" << endl << Saved_Username << endl; + textfile << "[Email]" << endl << Saved_Email << endl; + textfile << "[Password]" << endl << New_Password << endl; + textfile << "[Timezone]" << endl << Saved_Timezone << endl; + textfile.close(); + cout << "Edits Complete" << endl; + }else if (Edit_Variable == "Timezone"){ + string New_Timezone = ""; + do { + cout << "Enter new Timezone" << endl; + getline(cin, New_Timezone); + } while (New_Timezone == ""); + textfile << "[Username]" << endl << Saved_Username << endl; + textfile << "[Email]" << endl << Saved_Email << endl; + textfile << "[Password]" << endl << Saved_Password << endl; + textfile << "[Timezone]" << endl << New_Timezone << endl; + textfile.close(); + cout << "Edits Complete" << endl; + }else{ + cout << "Unknown Input"; + } + //Reads and displays the new data + myfile.open (Location); + while (!myfile.eof()) { + for (int lineno = 0; getline(myfile, line) && lineno < 8; lineno++) { + if (lineno == 1) { + Saved_Username = line; + } + if (lineno == 3){ + Saved_Email = line; + } + if (lineno == 5){ + Saved_Password = line; + } + if (lineno == 7){ + Saved_Timezone = line; + } + } + } + cout << "Saved Username: " << Saved_Username << endl; + cout << "Saved Email: " << Saved_Email << endl; + cout << "Saved Password: " << Saved_Password << endl; + cout << "Saved Timezone: " << Saved_Timezone << endl; + myfile.close(); + +} + +void ConfigGenerator(){ + //Asks the user what they want to do. create a config file or edit one + //leaving Menu blank will ask the question again + string Menu = ""; + do { + cout << "Enter 'init' to enter data, or 'edit' to edit existing data" << endl; + getline(cin, Menu); + } while (Menu == ""); + + // Asks the user to fill in each category for the config file + if (Menu == "init") { + Initialization(); + //typing Edit in the menu allows you to change data + }else if (Menu == "edit") { + EditFile(); + } +} + + +namespace ConfigReader{ + string *ReadConfig(string config_file_path) { + string Location = ""; + cout << "Enter file name, leave blank for default" << endl; + getline(cin, Location); + if (Location == "") { + Location = "Config.txt"; + } + + //retrieves data currently saved in file and prints for user + string Saved_Username = ""; + string Saved_Email = ""; + string Saved_Password = ""; + string Saved_Timezone = ""; + string line; + + std::ifstream myfile(Location); + myfile.open(Location); + while (!myfile.eof()) { + for (int lineno = 0; getline(myfile, line) && lineno < 8; lineno++) { + if (lineno == 1) { + Saved_Username = line; + } + if (lineno == 3) { + Saved_Email = line; + } + if (lineno == 5) { + Saved_Password = line; + } + if (lineno == 7) { + Saved_Timezone = line; + } + + } + string SavedData[4] = {Saved_Username, Saved_Email, Saved_Password, Saved_Timezone}; + } + } +} + + + + diff --git a/Assignment 3/reader.h b/Assignment 3/reader.h new file mode 100644 index 0000000..aee64a6 --- /dev/null +++ b/Assignment 3/reader.h @@ -0,0 +1,8 @@ +#include + +void Initialization(); +void EditFile(); +void ConfigGenerator(); + +namespace ConfigReader{ + std::string* ReadConfig(std::string config_file_path); \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index f638eeb..0000000 --- a/README.md +++ /dev/null @@ -1,70 +0,0 @@ -### Assignment 3 -##### What aside from executable binaries can we make -Something to consider is that we have a lot of important logic baked into assignment 2 regarding how our config file is being read. -It would be best if all of our future applications could read our config file with out having to copy the code directly. One major concern -is that if we duplicate code and then need to make a change to that logic in a systemic way we might have to open a number of projects and -make a series of changes. That sounds painful to me! - -In the world of C++ it is trivial to combine code into a library as a binary and access it in any other application. To do this we need to -employ some new incantations in cmake. - -First lets create a library that has a single function which carries this signature. -``` -string* read_config(string config_file_path); -``` -A signature is a unique series of identifiers that a computer can use to identify a specific function. This signature will: -- return a string pointer -- accept a string argument for a file path - -The string pointer will hold the memory location of an array of strings which will hold the data within our config file. - -You will need to open a file and read its contents into an array. If the tricky part doesn't slap you in the face here it is. To declare an -array we need to know its size. You have to figure out a way to identify the number of lines in the file and then create an array to hold -those values of the correct size. - -I would start by building this project as a normal executable with a `main` function so you can test its inner workings. Once you are confident -that you libary works correctly you will need to rename the function to match the signature above and create a header file to expose our -function to the rest of the world. Its easy to create the header file in CLion. You should move your function prototype to the header and -include the header in your cpp file with a new include statement like `#include "./reader.h"` - -Make sure that your library lives in its own namespace by wrapping your function and using statements like this -``` -namespace config_reader{ - using ... - ... -} -``` -You will need to do the same thing to your header file. - -Finally you need to update your CMakeLists.txt to create a library instead of an executable. The line goes at the bottom of your CMakeLists.txt -and looks like this -``` -add_library ( ${SOURCE_FILES}) -``` -replacing `` with what is in is in the `project` line of the same file. - -Next you need to test your library. To do so open the projects build directory and copy the .a file and the .h file from your project directory -to a new folder on your computer. -- Put them in the same folder and in a location that has an easy to remember path. -- Create a new project -- Open the CMakeLists.txt and edit it by adding the following lines - ``` - project() - - include_directories( ) - link_directories( ) - - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - - set(SOURCE_FILES main.cpp) - add_executable( ${SOURCE_FILES}) - # The library_name is the .a file without the lib prefix and extension - target_link_libraries( ) - ``` -- Once CMakeLists.txt is reloaded you should be able to `#include` the header file you created in your project and access your function as usual -by prefixing it with your `::` -- You must give your second project a config file that you program can read and then display its contents -- Commit your lib project and driver project to seperate folders in the same repo. - - There are two folders already in this repo you can use to create your projects inside of to help. - -Good Luck :) diff --git a/driver/.gitkeep b/driver/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/driver/.idea/.name b/driver/.idea/.name new file mode 100644 index 0000000..ebf312e --- /dev/null +++ b/driver/.idea/.name @@ -0,0 +1 @@ +Driver \ No newline at end of file diff --git a/driver/.idea/Driver.iml b/driver/.idea/Driver.iml new file mode 100644 index 0000000..7b4be19 --- /dev/null +++ b/driver/.idea/Driver.iml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/driver/.idea/encodings.xml b/driver/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/driver/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/driver/.idea/misc.xml b/driver/.idea/misc.xml new file mode 100644 index 0000000..3eb495b --- /dev/null +++ b/driver/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/driver/.idea/modules.xml b/driver/.idea/modules.xml new file mode 100644 index 0000000..aa9846a --- /dev/null +++ b/driver/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/driver/.idea/workspace.xml b/driver/.idea/workspace.xml new file mode 100644 index 0000000..1815e6e --- /dev/null +++ b/driver/.idea/workspace.xml @@ -0,0 +1,266 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1456941980407 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/driver/CMakeLists.txt b/driver/CMakeLists.txt new file mode 100644 index 0000000..3d6936f --- /dev/null +++ b/driver/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.3) +project(Driver) + +include_directories("C:/Users/joshu/Desktop/Assignment 3/reader") +link_directories("C:/Users/joshu/Desktop/Assignment 3/reader") + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + +set(SOURCE_FILES main.cpp) +add_executable(Driver ${SOURCE_FILES}) +target_link_libraries(Driver ) diff --git a/driver/main.cpp b/driver/main.cpp new file mode 100644 index 0000000..bede0d2 --- /dev/null +++ b/driver/main.cpp @@ -0,0 +1,6 @@ +#include "reader.h" + +int main() { + ConfigReader::ReadConfig("C:/Users/joshu/Desktop/Assignment 3"); + return 0; +} \ No newline at end of file diff --git a/lib/.gitkeep b/lib/.gitkeep deleted file mode 100644 index e69de29..0000000