Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip> // output formatting in console
  3. #include <string>
  4. #include <fstream>
  5. #include <filesystem> // Requires C++17
  6. #include <vector> // for storing words from json file and writing them to output files.
  7.  
  8. namespace fs = std::filesystem; // for convenience
  9.  
  10. void printInstructionsAndGetInput();
  11. void openFiles();
  12. void readInputFile(const fs::directory_entry &filenameDirectEntry, const std::vector<std::string> &alphaNums); // child function of openFiles().
  13. void writeToOutputFile(fs::directory_entry filenameDirectEntry, const std::vector<std::string> &alphaNums); // child function of openFiles().
  14.  
  15. int main() {
  16.     printInstructionsAndGetInput(); // 1.
  17.  
  18.     openFiles();
  19.  
  20.     std::cout << "The program has completed successfully!\n";
  21.    
  22.     return 0;
  23. }
  24.  
  25.  
  26. void printInstructionsAndGetInput() {
  27.     const int width = 40;
  28.     const char sym = '-';
  29.     std::cout << std::setfill(sym) << std::setw(width) << "\n"
  30.         << "Hello, thank you for using JSON File Simplifier!\n\n"
  31.         << "Usage Steps:\n"
  32.         << "1. To get started, open the file path this program is located at on your computer.\n"
  33.         << "2. Now move the desired files you would like to simplify into the " << std::quoted("InputFiles") << " directory.\n"
  34.         << "3. Input 'y' when ready or input 'n' to quit: "
  35.         << std::setw(width) << std::setfill(' ');
  36.    
  37.     char ready = 'n';
  38.     std::cin >> ready;
  39.     if (ready != 'y' && ready != 'Y') {
  40.         std::cout << "Program has ended. You may now close the program.\n";
  41.         exit(0);
  42.     }
  43. }
  44.  
  45. /*
  46. Algorithm Steps of this Function:
  47.     1. Create the path variable.
  48.         - Make sure it includes the current path, InputFiles directory, and the current input filename.
  49.  
  50.     2. For each file in the InputFiles directory:
  51.         a. Declare the vector that will store the words from the input file.
  52.         b. Call the function readInputFile which will read the input file & store the data into the vector, which uses a reference.
  53.             - Once this finishes, the vec will have a all words (enclosed in "") from the inputFilename & the file will be closed.
  54.         c. Call the function writeToOutputFile which will write all the words from the vec (enclosed in "") to new lines in the OutputFiles directory.
  55. */
  56. void openFiles() {
  57.     // 1.
  58.     fs::path fullInPath = fs::current_path();
  59.     fullInPath /= "InputFiles";
  60.  
  61.     // 2.
  62.     for (const fs::directory_entry &inputFilename : fs::directory_iterator(fullInPath)) {
  63.         // 2a.
  64.         std::vector<std::string> alphaNums = {};
  65.  
  66.         // ----------------------------------------------
  67.         // <No algorithm step in comments for this yet.>
  68.         // Goal: Copy "InputFiles/<currentFileNameHere>" to a string so I can use it in argument functions as a string.
  69.         std::string inputFilesLocStr = "InputFiles/";
  70.         inputFilesLocStr += inputFilename; // **Trying to make it add the filename to the str**
  71.         // ----------------------------------------------
  72.  
  73.         // 2b.
  74.         readInputFile(inputFilename, alphaNums);
  75.         // 2c.
  76.         writeToOutputFile(inputFilename, alphaNums);
  77.     }
  78. }
  79.  
  80.  
  81. /*
  82. Algorithm Steps of this Function:
  83.     1. Open filename provided in argument.
  84.     2. If the file couldn't be opened, print error and stop program.
  85.     3. Define some variables that will be used for getting the proper data from the input file inside the while loop.
  86.     4. Read the file one char at a time.
  87.         a. If the char is a " then:
  88.             - add to the number of quotes (keeping track with a var)
  89.         b. If the count of quotes is equal to 1 then:
  90.             - add the current char to the word variable
  91.         c. If the char is a " again then:
  92.             - Add to the number of quotes (so it now equals 2)
  93.         d. If the count of quotes is greater than or equal to 2 then:
  94.             - add the current char (the " symbol) to the word variable.
  95.             - then add the word to a vector.
  96.             - then clear the word variable to prep for next word.
  97.     5. Close the input file.
  98.        
  99.     Important Note of This Method:
  100.         - This code assumes there are no quotes within quotes.
  101.             * Ex: "  "minecraft:word" " in the files.
  102. */
  103. void readInputFile(const fs::directory_entry &filenameDirectEntry, const std::vector<std::string> &alphaNums) {
  104.         // 1.
  105.         std::string filename = filenameDirectEntry.string(); // converts from fs::directory_entry to std::string
  106.         std::ifstream inFile.open(filename);
  107.  
  108.         // 2.
  109.         if (!inFile.is_open()) {
  110.             std::cout << filename << " couldn't be opened!\n"
  111.                 << "Program stopped.\n"
  112.                 << "Are you sure you input the files into the "
  113.                 << std::quoted("InputFiles") << " directory?\n";
  114.                 exit(1);
  115.         }
  116.  
  117.         // 3.
  118.         char c = ' ';
  119.         std::string word;
  120.         int countOfQuotes = 0;
  121.  
  122.         // 4.
  123.         while (inFile.get(c)) {
  124.             // 4a. & 4c.
  125.             if (c == '"') {
  126.                 ++countOfQuotes;
  127.             }
  128.             // 4b.
  129.             if (countOfQuotes == 1) {
  130.                 word += c;
  131.             }
  132.             // 4d.
  133.             else if (countOfQuotes >= 2) {
  134.                 word += c;
  135.                 // Add word to the vec (Ex: "minecraft:stone" WITH the "" symbols)
  136.                 alphaNums.push_back(word);
  137.                 countOfQuotes = 0; // Resets quote count.
  138.                 word = ""; // Resets the word variable.
  139.             }
  140.         }
  141.        
  142.         // Closes File:
  143.         inFile.close();
  144.  
  145.         // Note:
  146.         // - 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.
  147. }
  148.  
  149. /*
  150. Algorithm Steps of this Function:
  151.     1. Remove the ".json" ending from the filename string and add "txt" so it is a .txt extension.
  152.  
  153.     2. Open the file path: OutputFiles <insert OS separator symbol(s)> <current filename>
  154.  
  155.     3. Check if outfile properly opened. If not, give error and exit program.
  156.  
  157.     4. Write to the output file.
  158.         - For every word in the vec from the function argument, write it into the output file. 1 word per line (enclosed in "").
  159.  
  160.     5. Close output file.
  161. */
  162. void writeToOutputFile(fs::directory_entry filenameDirectEntry, const std::vector<std::string> &alphaNums) {
  163.     // 1.
  164.     // need to convert directory_entry to string copy then resize it to remove json and add txt.
  165.     std::string filename = filenameDirectEntry.string();
  166.     filename.resize(filename.size()-4);
  167.     filename += "txt";
  168.  
  169.     // 2.
  170.     std::string outFilename = "OutputFiles";
  171.     outputFilename /= filename;
  172.     std::ofstream outFile(outFilename); // Opens current output file
  173.  
  174.     // 3.
  175.     if (!outFile.is_open()) {
  176.         std::cerr << "Error! Could not open output file properly.\n"
  177.             << "Program is exiting...\n";
  178.         exit(1);
  179.     }
  180.  
  181.     // 4.
  182.     for (std::string wordStr : alphaNums) {
  183.         outFile << wordStr << std::endl;
  184.     }
  185.  
  186.     // 5.
  187.     outFile.close();
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement