Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Encode/decode symbols from cp1251 by their codes
- #include "stdafx.h"
- #include <iostream>
- #include <cstdio>
- #include <Windows.h>
- #include <fstream>
- #include <cstring>
- #include <cctype>
- #define inp_by_h 200
- using namespace std;
- short int encode_decode(char *ptr, int size);
- short int write(char *ptr);
- void pause_exit();
- class intt {
- short int i;
- int error;
- public:
- intt() { i = 0; error = 0; }
- friend istream &operator>>(istream &stream, intt &i);
- friend int main(int argc, char *argv[]);
- };
- istream &operator>>(istream &stream, intt &i) {
- stream >> i.i;
- if (i.i != 1 && i.i != 2 && i.i != 3) i.error = 1;
- //cout << '#' << (char)i.i << '# ' << (int)i.error << endl;
- //system("pause");
- return stream;
- }
- int main(int argc, char *argv[])
- {
- // Add support of CP1251 in Win console
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- int short repeat = 1; // Loop
- // Step 1
- // If using terminal argument - skip choice once
- int once = 1;
- if (!argv[1] || !once) cout << "Hello there! This program can ENCODE/DECODE your text";
- do
- {
- intt start_ans;
- if (!argv[1] || !once)
- {
- do
- {
- cout << "\n\nWhat do you want to do?\n\n"
- << " [1] Open file with the text\n [2] Write text here\n [3] Exit\n\n"
- << "Your answer will be: ";
- fflush(stdin);
- cin >> start_ans;
- } while (start_ans.error);
- }
- else
- start_ans.i = 1;
- if (start_ans.i == 1) // Open
- {
- char f_name[90];
- if (!argv[1] || !once)
- {
- cout << "\nEnter file full path or name: ";
- fflush(stdin);
- gets(f_name);
- }
- else
- {
- strcpy(f_name, argv[1]);
- once = 0; // argv[1] must work only at first iteration of main loop
- }
- // Open FILE stream for reading
- ifstream f_stream(f_name, ios::in | ios::binary);
- if (!f_stream)
- {
- cout << "\nError in opening \"" << f_name << "\"...\n";
- pause_exit();
- return 1;
- }
- else
- cout << "\nFile successfully opened!\n";
- // Find the number of characters in the file
- int chs_in_file = 0;
- streamoff a = 0, b = 0;
- a = f_stream.tellg();
- f_stream.seekg(0, ios::end);
- b = f_stream.tellg();
- f_stream.seekg(0, ios::beg);
- chs_in_file = (int)(b - a);
- // Allocate memory
- char *ptr_file_buf = new char[chs_in_file + 1]; // +1 for '\0'
- if (!*ptr_file_buf)
- {
- cout << "\nError in memory allocation...\n\n";
- pause_exit();
- return 1;
- }
- char *ptr_file_start = ptr_file_buf;
- // Copy text from the file to buffer
- cout << "\nRead \"" << chs_in_file << "\" chars in \"" << f_name << "\". Preview:\n\n";
- register int i = 0;
- while (f_stream.get(*ptr_file_buf))
- {
- if (i < 301) cout << *ptr_file_buf;
- i++;
- ptr_file_buf++;
- }
- *ptr_file_buf = '\0'; // Edge of array (like EOF in file)
- f_stream.close();
- if (i > 300) cout << " ...etc.";
- // Call encode/decode function
- if (!encode_decode(ptr_file_start, chs_in_file + 1)) cout << "\nError in coding/decoding process!";
- delete[] ptr_file_start;
- ptr_file_buf = nullptr;
- ptr_file_start = nullptr;
- }
- if (start_ans.i == 2)
- {
- char input_buff[inp_by_h];
- cout << "\nEnter your text(not more than " << inp_by_h << " symbols): ";
- fflush(stdin);
- gets(input_buff);
- if (!encode_decode(input_buff, inp_by_h)) cout << "\nError in coding/decoding process!";
- }
- if (start_ans.i == 3) repeat = 0; // Exit
- cout << "\n\n";
- } while (repeat);
- pause_exit();
- return 0;
- }
- short int encode_decode(char *ptr, int size)
- {
- // Step 2
- short int c_dc_ans = 0;
- cout << "\n\nEnter keyword: ";
- char key_word[90];
- gets(key_word);
- int koef = strlen(key_word);
- do
- {
- cout << "\n\nSelect one of those options:\n\n" << " [1] Encode text\n" << " [2] Decode text\n\n" << "Your choice: ";
- cin >> c_dc_ans;
- } while (c_dc_ans != 1 && c_dc_ans != 2);
- char *outp_mas = new char[size]; // Array for encoded/decoded characters
- if (!*outp_mas) return 0;
- char *outp_mas_start = outp_mas;
- int val; // var for int number of processed character
- while (*ptr != '\0')
- {
- val = (int)*ptr;
- if (c_dc_ans == 1)
- val += koef;
- if (c_dc_ans == 2)
- val -= koef;
- *outp_mas = (char)val;
- ptr++;
- outp_mas++;
- }
- *outp_mas = '\0'; // edge for next step while()
- if (c_dc_ans == 1) cout << "\nEncoded successfully";
- if (c_dc_ans == 2) cout << "\nDecoded successfully";
- if (!write(outp_mas_start))
- {
- cout << "\nError in writing/displaying\n\n";
- return 0;
- }
- delete[] outp_mas_start;
- outp_mas = nullptr;
- outp_mas_start = nullptr;
- return 1;
- }
- short int write(char *ptr)
- {
- // Step 3
- short int wr_ans = 0;
- do
- {
- 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: ";
- cin >> wr_ans;
- } while (wr_ans != 1 && wr_ans != 2);
- switch (wr_ans)
- {
- case 1:
- {
- cout << "\nEnter file name or full path: ";
- char file_name[90];
- fflush(stdin);
- gets(file_name);
- ofstream write_group(file_name, ios::app | ios::binary);
- if (!write_group) return 0;
- while (*ptr != '\0')
- {
- write_group << *ptr;
- ptr++;
- }
- cout << "\nWrited successfuly";
- write_group.close();
- return 1;
- break;
- }
- case 2:
- {
- cout << "\nText:\n\n";
- while (*ptr != '\0')
- {
- cout << *ptr;
- ptr++;
- }
- return 1;
- break;
- }
- }
- return 0;
- }
- void pause_exit()
- {
- char pause[90];
- cout << "\nPress Enter for exit...";
- fflush(stdin);
- gets(pause);
- }
Advertisement
Add Comment
Please, Sign In to add comment