Abdulg

Convert Bash scripts into one-line equivalents [WIP]

Mar 28th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<stdlib.h>
  4. #include<string.h>
  5.  
  6. using namespace std;
  7.  
  8. int main(int argc, char * argv[])
  9. {
  10.     cout << "CAUTION: This will probably not work with advanced bash scripts. dos, dones and fis only please!\n";
  11.    
  12.     if (argc != 3)
  13.     {
  14.         cout << "Usage: " << argv[0] << " /path/to/bash.script /path/to/output.file" << endl;
  15.         return 1;
  16.     }
  17.    
  18.     ifstream FileInput;
  19.     FileInput.open(argv[1]);
  20.    
  21.     if (FileInput.is_open() == false)
  22.     {
  23.         cout << "Could not find input file \"" << argv[1] << "\", exiting..." << endl;
  24.         return 1;
  25.     }
  26.    
  27.     ofstream FileOutput;
  28.     FileOutput.open(argv[2], ofstream::trunc);
  29.     FileOutput.close();
  30.     FileOutput.open(argv[2], ofstream::app);
  31.    
  32.     if (FileOutput.is_open() == false)
  33.     {
  34.         cout << "Could not find output file \"" << argv[2] << "\", creating..." << endl;
  35.     }
  36.    
  37.     string Buffer;
  38.    
  39.     while (FileInput.eof() == false)
  40.     {
  41.         getline(FileInput,Buffer);
  42.         if(Buffer[0] != '#')FileOutput << Buffer;
  43.        
  44.         if(Buffer.size() > 1)
  45.         {
  46.             if (Buffer != "do" && Buffer != "then" && Buffer != "done" && Buffer != "fi" ) FileOutput << ";";
  47.            
  48.             else if (Buffer.size() >= 4)
  49.             {   //Screw you this kind of works
  50.                 if (
  51.                 Buffer.substr(Buffer.size() - 2,2) != "do" &&
  52.                 Buffer.substr(Buffer.size() - 2,2) != "fi" &&
  53.                 Buffer.substr(Buffer.size() - 4,4) != "done" &&
  54.                 Buffer.substr(Buffer.size() - 4,4) != "then")
  55.                 FileOutput << ";";
  56.             }
  57.         }
  58.     }
  59.    
  60.     if(FileOutput.fail() == true)
  61.     {
  62.         cout << "Writing probably failed! Have fun guessing why! ( ͡° ͜ʖ ͡°)" << endl;
  63.         return 1;
  64.     }
  65.    
  66.     FileInput.close();
  67.     FileOutput.close();
  68.    
  69.     cout << "File (hopefully) converted successfully! New one line equivalent is located at " << argv[2] << endl;
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment