Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip> // output formatting in console
- #include <string>
- #include <fstream>
- #include <filesystem> // Requires C++17
- #include <vector> // for storing words from json file and writing them to output files.
- namespace fs = std::filesystem; // for convenience
- void printInstructionsAndGetInput();
- void openFiles();
- void readInputFile(const fs::directory_entry &filenameDirectEntry, const std::vector<std::string> &alphaNums); // child function of openFiles().
- void writeToOutputFile(fs::directory_entry filenameDirectEntry, const std::vector<std::string> &alphaNums); // child function of openFiles().
- int main() {
- printInstructionsAndGetInput(); // 1.
- openFiles();
- std::cout << "The program has completed successfully!\n";
- return 0;
- }
- void printInstructionsAndGetInput() {
- const int width = 40;
- const char sym = '-';
- std::cout << std::setfill(sym) << std::setw(width) << "\n"
- << "Hello, thank you for using JSON File Simplifier!\n\n"
- << "Usage Steps:\n"
- << "1. To get started, open the file path this program is located at on your computer.\n"
- << "2. Now move the desired files you would like to simplify into the " << std::quoted("InputFiles") << " directory.\n"
- << "3. Input 'y' when ready or input 'n' to quit: "
- << std::setw(width) << std::setfill(' ');
- char ready = 'n';
- std::cin >> ready;
- if (ready != 'y' && ready != 'Y') {
- std::cout << "Program has ended. You may now close the program.\n";
- exit(0);
- }
- }
- /*
- Algorithm Steps of this Function:
- 1. Create the path variable.
- - Make sure it includes the current path, InputFiles directory, and the current input filename.
- 2. For each file in the InputFiles directory:
- a. Declare the vector that will store the words from the input file.
- b. Call the function readInputFile which will read the input file & store the data into the vector, which uses a reference.
- - Once this finishes, the vec will have a all words (enclosed in "") from the inputFilename & the file will be closed.
- c. Call the function writeToOutputFile which will write all the words from the vec (enclosed in "") to new lines in the OutputFiles directory.
- */
- void openFiles() {
- // 1.
- fs::path fullInPath = fs::current_path();
- fullInPath /= "InputFiles";
- // 2.
- for (const fs::directory_entry &inputFilename : fs::directory_iterator(fullInPath)) {
- // 2a.
- std::vector<std::string> alphaNums = {};
- // ----------------------------------------------
- // <No algorithm step in comments for this yet.>
- // Goal: Copy "InputFiles/<currentFileNameHere>" to a string so I can use it in argument functions as a string.
- std::string inputFilesLocStr = "InputFiles/";
- inputFilesLocStr += inputFilename; // **Trying to make it add the filename to the str**
- // ----------------------------------------------
- // 2b.
- readInputFile(inputFilename, alphaNums);
- // 2c.
- writeToOutputFile(inputFilename, alphaNums);
- }
- }
- /*
- Algorithm Steps of this Function:
- 1. Open filename provided in argument.
- 2. If the file couldn't be opened, print error and stop program.
- 3. Define some variables that will be used for getting the proper data from the input file inside the while loop.
- 4. Read the file one char at a time.
- a. If the char is a " then:
- - add to the number of quotes (keeping track with a var)
- b. If the count of quotes is equal to 1 then:
- - add the current char to the word variable
- c. If the char is a " again then:
- - Add to the number of quotes (so it now equals 2)
- d. If the count of quotes is greater than or equal to 2 then:
- - add the current char (the " symbol) to the word variable.
- - then add the word to a vector.
- - then clear the word variable to prep for next word.
- 5. Close the input file.
- Important Note of This Method:
- - This code assumes there are no quotes within quotes.
- * Ex: " "minecraft:word" " in the files.
- */
- void readInputFile(const fs::directory_entry &filenameDirectEntry, const std::vector<std::string> &alphaNums) {
- // 1.
- std::string filename = filenameDirectEntry.string(); // converts from fs::directory_entry to std::string
- std::ifstream inFile.open(filename);
- // 2.
- if (!inFile.is_open()) {
- std::cout << filename << " couldn't be opened!\n"
- << "Program stopped.\n"
- << "Are you sure you input the files into the "
- << std::quoted("InputFiles") << " directory?\n";
- exit(1);
- }
- // 3.
- char c = ' ';
- std::string word;
- int countOfQuotes = 0;
- // 4.
- while (inFile.get(c)) {
- // 4a. & 4c.
- if (c == '"') {
- ++countOfQuotes;
- }
- // 4b.
- if (countOfQuotes == 1) {
- word += c;
- }
- // 4d.
- else if (countOfQuotes >= 2) {
- word += c;
- // Add word to the vec (Ex: "minecraft:stone" WITH the "" symbols)
- alphaNums.push_back(word);
- countOfQuotes = 0; // Resets quote count.
- word = ""; // Resets the word variable.
- }
- }
- // Closes File:
- inFile.close();
- // Note:
- // - As soon as loop ends there will be a vec full of the words with no special symbols except the symbols inside a word like: minecraft:diamond or double quotes.
- }
- /*
- Algorithm Steps of this Function:
- 1. Remove the ".json" ending from the filename string and add "txt" so it is a .txt extension.
- 2. Open the file path: OutputFiles <insert OS separator symbol(s)> <current filename>
- 3. Check if outfile properly opened. If not, give error and exit program.
- 4. Write to the output file.
- - For every word in the vec from the function argument, write it into the output file. 1 word per line (enclosed in "").
- 5. Close output file.
- */
- void writeToOutputFile(fs::directory_entry filenameDirectEntry, const std::vector<std::string> &alphaNums) {
- // 1.
- // need to convert directory_entry to string copy then resize it to remove json and add txt.
- std::string filename = filenameDirectEntry.string();
- filename.resize(filename.size()-4);
- filename += "txt";
- // 2.
- std::string outFilename = "OutputFiles";
- outputFilename /= filename;
- std::ofstream outFile(outFilename); // Opens current output file
- // 3.
- if (!outFile.is_open()) {
- std::cerr << "Error! Could not open output file properly.\n"
- << "Program is exiting...\n";
- exit(1);
- }
- // 4.
- for (std::string wordStr : alphaNums) {
- outFile << wordStr << std::endl;
- }
- // 5.
- outFile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement