heroys6

unDec

Oct 2nd, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. //Encode/decode symbols from cp1251 by their codes
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <Windows.h>
  7. #include <fstream>
  8. #include <cstring>
  9. #include <cctype>
  10.  
  11. #define inp_by_h 200
  12.  
  13. using namespace std;
  14.  
  15. short int encode_decode(char *ptr, int size);
  16. short int write(char *ptr);
  17. void pause_exit();
  18.  
  19. class intt {
  20.     short int i;
  21.     int error;
  22. public:
  23.     intt() { i = 0; error = 0; }
  24.     friend istream &operator>>(istream &stream, intt &i);
  25.     friend int main(int argc, char *argv[]);
  26. };
  27.  
  28. istream &operator>>(istream &stream, intt &i) {
  29.     stream >> i.i;
  30.     if (i.i != 1 && i.i != 2 && i.i != 3) i.error = 1;
  31.     //cout << '#' << (char)i.i << '# ' << (int)i.error << endl;
  32.     //system("pause");
  33.  
  34.     return stream;
  35. }
  36.  
  37. int main(int argc, char *argv[])
  38. {
  39.     // Add support of CP1251 in Win console
  40.     SetConsoleCP(1251);
  41.     SetConsoleOutputCP(1251);
  42.  
  43.     int short repeat = 1; // Loop
  44.  
  45.     // Step 1
  46.  
  47.     // If using terminal argument - skip choice once
  48.     int once = 1;
  49.     if (!argv[1] || !once) cout << "Hello there! This program can ENCODE/DECODE your text";
  50.     do
  51.     {
  52.         intt start_ans;
  53.         if (!argv[1] || !once)
  54.         {
  55.             do
  56.             {
  57.                 cout << "\n\nWhat do you want to do?\n\n"
  58.                     << " [1] Open file with the text\n [2] Write text here\n [3] Exit\n\n"
  59.                     << "Your answer will be: ";
  60.                 fflush(stdin);
  61.                 cin >> start_ans;
  62.             } while (start_ans.error);
  63.         }
  64.         else
  65.             start_ans.i = 1;
  66.  
  67.         if (start_ans.i == 1) // Open
  68.         {
  69.             char f_name[90];
  70.             if (!argv[1] || !once)
  71.             {
  72.                 cout << "\nEnter file full path or name: ";
  73.                 fflush(stdin);
  74.                 gets(f_name);
  75.             }
  76.             else
  77.             {
  78.                 strcpy(f_name, argv[1]);
  79.                 once = 0; // argv[1] must work only at first iteration of main loop
  80.             }
  81.  
  82.             // Open FILE stream for reading
  83.             ifstream f_stream(f_name, ios::in | ios::binary);
  84.             if (!f_stream)
  85.             {
  86.                 cout << "\nError in opening \"" << f_name << "\"...\n";
  87.                 pause_exit();
  88.                 return 1;
  89.             }
  90.             else
  91.                 cout << "\nFile successfully opened!\n";
  92.  
  93.             // Find the number of characters in the file
  94.             int chs_in_file = 0;
  95.             streamoff a = 0, b = 0;
  96.             a = f_stream.tellg();
  97.             f_stream.seekg(0, ios::end);
  98.             b = f_stream.tellg();
  99.             f_stream.seekg(0, ios::beg);
  100.             chs_in_file = (int)(b - a);
  101.  
  102.             // Allocate memory
  103.             char *ptr_file_buf = new char[chs_in_file + 1]; // +1 for '\0'
  104.             if (!*ptr_file_buf)
  105.             {
  106.                 cout << "\nError in memory allocation...\n\n";
  107.                 pause_exit();
  108.                 return 1;
  109.             }
  110.             char *ptr_file_start = ptr_file_buf;
  111.  
  112.             // Copy text from the file to buffer
  113.             cout << "\nRead \"" << chs_in_file << "\" chars in \"" << f_name << "\". Preview:\n\n";
  114.             register int i = 0;
  115.             while (f_stream.get(*ptr_file_buf))
  116.             {
  117.                 if (i < 301) cout << *ptr_file_buf;
  118.                 i++;
  119.                 ptr_file_buf++;
  120.             }
  121.             *ptr_file_buf = '\0'; // Edge of array (like EOF in file)
  122.             f_stream.close();
  123.             if (i > 300) cout << " ...etc.";
  124.  
  125.             // Call encode/decode function
  126.             if (!encode_decode(ptr_file_start, chs_in_file + 1)) cout << "\nError in coding/decoding process!";
  127.            
  128.             delete[] ptr_file_start;
  129.             ptr_file_buf = nullptr;
  130.             ptr_file_start = nullptr;
  131.         }
  132.         if (start_ans.i == 2)
  133.         {
  134.             char input_buff[inp_by_h];
  135.             cout << "\nEnter your text(not more than " << inp_by_h << " symbols): ";
  136.             fflush(stdin);
  137.             gets(input_buff);
  138.             if (!encode_decode(input_buff, inp_by_h)) cout << "\nError in coding/decoding process!";
  139.         }
  140.         if (start_ans.i == 3) repeat = 0; // Exit
  141.        
  142.         cout << "\n\n";
  143.     } while (repeat);
  144.  
  145.     pause_exit();
  146.     return 0;
  147. }
  148.  
  149. short int encode_decode(char *ptr, int size)
  150. {
  151.     // Step 2
  152.     short int c_dc_ans = 0;
  153.  
  154.     cout << "\n\nEnter keyword: ";
  155.     char key_word[90];
  156.     gets(key_word);
  157.     int koef = strlen(key_word);
  158.     do
  159.     {
  160.         cout << "\n\nSelect one of those options:\n\n" << " [1] Encode text\n" << " [2] Decode text\n\n" << "Your choice: ";
  161.         cin >> c_dc_ans;
  162.     } while (c_dc_ans != 1 && c_dc_ans != 2);
  163.  
  164.     char *outp_mas = new char[size]; // Array for encoded/decoded characters
  165.     if (!*outp_mas) return 0;
  166.     char *outp_mas_start = outp_mas;
  167.     int val; // var for int number of processed character
  168.  
  169.     while (*ptr != '\0')
  170.     {
  171.         val = (int)*ptr;
  172.        
  173.         if (c_dc_ans == 1)
  174.             val += koef;
  175.  
  176.         if (c_dc_ans == 2)
  177.             val -= koef;
  178.  
  179.         *outp_mas = (char)val;
  180.         ptr++;
  181.         outp_mas++;
  182.     }
  183.     *outp_mas = '\0'; // edge for next step while()
  184.    
  185.     if (c_dc_ans == 1) cout << "\nEncoded successfully";
  186.     if (c_dc_ans == 2) cout << "\nDecoded successfully";
  187.  
  188.     if (!write(outp_mas_start))
  189.     {
  190.         cout << "\nError in writing/displaying\n\n";
  191.         return 0;
  192.     }
  193.  
  194.     delete[] outp_mas_start;
  195.     outp_mas = nullptr;
  196.     outp_mas_start = nullptr;
  197.     return 1;
  198. }
  199.  
  200. short int write(char *ptr)
  201. {
  202.     // Step 3
  203.     short int wr_ans = 0;
  204.    
  205.     do
  206.     {
  207.         cout << "\n\nSelect one of those options:\n\n" << " [1] Write text to the file\n" << " [2] Display text on the screen\n\n" << "Your choice: ";
  208.         cin >> wr_ans;
  209.     } while (wr_ans != 1 && wr_ans != 2);
  210.     switch (wr_ans)
  211.     {
  212.     case 1:
  213.     {
  214.               cout << "\nEnter file name or full path: ";
  215.               char file_name[90];
  216.               fflush(stdin);
  217.               gets(file_name);
  218.  
  219.               ofstream write_group(file_name, ios::app | ios::binary);
  220.               if (!write_group) return 0;
  221.  
  222.               while (*ptr != '\0')
  223.               {
  224.                   write_group << *ptr;
  225.                   ptr++;
  226.               }
  227.               cout << "\nWrited successfuly";
  228.  
  229.               write_group.close();
  230.               return 1;
  231.               break;
  232.     }
  233.     case 2:
  234.     {
  235.               cout << "\nText:\n\n";
  236.               while (*ptr != '\0')
  237.               {
  238.                   cout << *ptr;
  239.                   ptr++;
  240.               }
  241.              
  242.               return 1;
  243.               break;
  244.     }
  245.     }
  246.    
  247.     return 0;
  248. }
  249.  
  250. void pause_exit()
  251. {
  252.     char pause[90];
  253.     cout << "\nPress Enter for exit...";
  254.     fflush(stdin);
  255.     gets(pause);
  256. }
Advertisement
Add Comment
Please, Sign In to add comment