haseeb_heaven

FileSummary.cpp

Aug 26th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. /*File summary - Prints file summary all printable, non-printable characters from any file.
  2. Can also works as strings extraction from any file just like GNU 'strings'. tool.
  3. Written by - Ha5eeB MiR (haseebmir.hm@gmail.com)
  4. Dated : 13/08/2018
  5. */
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <vector>
  10.  
  11. std::string ReadFile(std::string file_name);
  12.  
  13. int main()
  14. {
  15.     std::string file_name;
  16.  
  17.     std::cout << "Enter file name to print summary!" << std::endl;
  18.     std::cin>> file_name;
  19.  
  20.     std::string ascii_data = ReadFile(file_name);
  21.  
  22.     if(!ascii_data.empty()){
  23.     int cntrl_count = 0,graph_count = 0,space_count = 0,null_count = 0,strings_count = 0;
  24.     std::vector<uint8_t> cntrl_vec,print_vec,space_vec,string_s;
  25.  
  26. /* Minimum length of sequence of graphic chars to trigger output. (same as of (GNU strings tool)  */
  27.     static int string_min = 4;
  28.  
  29.     for(auto& c : ascii_data) {
  30.         if(::iscntrl(c)) {
  31.             cntrl_count++;
  32.             cntrl_vec.push_back(c);
  33.         }
  34.  
  35.         if(::isspace(c)) {
  36.             space_count++;
  37.             space_vec.push_back(c);
  38.         }
  39.  
  40.         if(::isprint(c) || c == '\0') {
  41.             graph_count++;
  42.  
  43.             if(::isprint(c)){
  44.             string_s.push_back(c);
  45.             strings_count++;
  46.             }
  47.  
  48.             if(string_min <= strings_count  && c == '\0') {
  49.  
  50.                     for(auto ss : string_s)
  51.                     print_vec.push_back(ss);
  52.                     print_vec.push_back('\0');
  53.  
  54.                     string_s.clear();
  55.                     strings_count = 0;
  56.                 }
  57.             }
  58.     }
  59.  
  60.  
  61.     std::cout << "File summary : " << std::endl;
  62.  
  63.     std::cout <<"\nNon-printable characters : " << cntrl_count << std::endl;
  64.     for(auto& v : cntrl_vec) std::cout << " '" << v << "' ";
  65.  
  66.     std::cout <<"\nPrintable characters : " << graph_count << std::endl;
  67.     for(auto& v : print_vec) {
  68.         if(v == '\0') {
  69.             std::cout << "\n";
  70.         }
  71.         std::cout << v;
  72.     }
  73.  
  74.     std::cout <<"\nSpaces : " << space_count << std::endl;
  75.     for(auto& v : space_vec) std::cout << " _ ";
  76.     }
  77.     else
  78.     {
  79.         return EXIT_FAILURE;
  80.     }
  81.  
  82.     return EXIT_SUCCESS;
  83. }
  84.  
  85.  
  86. std::string ReadFile(std::string file_name)
  87. {
  88.     /*Buffers to store output data from file.*/
  89.     std::string str_buf;
  90.  
  91.     try {
  92.         std::ifstream in_stream(file_name,std::ifstream::binary);
  93.         if (in_stream) {
  94.             /*Get the length of the file.*/
  95.             in_stream.seekg (0, in_stream.end);
  96.             int64_t length = in_stream.tellg();
  97.             in_stream.seekg (0, in_stream.beg);
  98.  
  99.             char* data_buf = new char [length];
  100.  
  101.             /*Read data in chunks into buffer.*/
  102.             in_stream.read(data_buf,length);
  103.  
  104.             if (!in_stream) {
  105.         std::cerr << "Error : " << in_stream.gcount() << " characters could be read!" << std::endl;
  106.             }
  107.             in_stream.close();
  108.  
  109.             std::string s_buf(data_buf,length);
  110.             str_buf = s_buf;
  111.  
  112.             delete[] data_buf;
  113.         } else {
  114.             throw std::exception();
  115.         }
  116.     }
  117.  
  118.     catch(std::exception const& ex) {
  119.         std::cerr << "Error : " << file_name <<  ": No such file or directory" << std::endl;
  120.     }
  121.     return str_buf;
  122. }
Add Comment
Please, Sign In to add comment