tranerius

xlnt Работа с xlsx товарным отчетом

Feb 13th, 2018
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <xlnt\xlnt.hpp>
  5. #include <Windows.h>
  6.  
  7. #undef max
  8.  
  9. using namespace xlnt;
  10.  
  11. enum encode_mode {
  12.     UTFtoWIN,
  13.     WINtoUTF
  14. };
  15.  
  16. std::string file_open(std::string file_name);
  17. std::string backup_file(std::string file_name);
  18. double valueCheck(std::string &value);
  19. std::string encoder(std::string text_to_encode, int encode_mode);
  20.  
  21. class work_with_file {
  22.     workbook file;
  23.     worksheet open_sheet;
  24.     alignment alig;
  25.     font ft;
  26.     std::string file_name, operation, position_name;
  27.     std::string cell_string_filename = "file.temp";
  28.     std::fstream log_file;
  29.     int operation_num, calculation = 0;
  30.     char operation_symbol;
  31.     unsigned int position_num;
  32.  
  33.  
  34.     int main_menu() {
  35.         log_file << "===ОСНОВНОЕ МЕНЮ==="
  36.             << "\nВыбор операции";
  37.         std::cout << "Список операций:"
  38.             << "\n1 - Списание продаж"
  39.             << "\n2 - Изменение позиций"
  40.             << "\n3 - Отобразить общие суммы"
  41.             << "\n4 - Сохранить изменения"
  42.             << "\n5 - Закончить работу";
  43.         while (true) {
  44.             std::cout << "\nВведите номер операции, которую необходимо выполнить: ";
  45.             std::cin >> operation;
  46.             operation_num = static_cast<int>(valueCheck(operation));
  47.             if (operation_num > 5 || operation_num < 1) {
  48.                 system("cls");
  49.                 std::cout << "Введен неверный номер операции" << std::endl;
  50.                 continue;
  51.             }
  52.             else {
  53.                 break;
  54.             }
  55.         }
  56.         system("cls");
  57.         return operation_num;
  58.     }
  59.  
  60.  
  61.     int main_menu_switch(int operation_num) {
  62.         switch (operation_num) {
  63.         case 1:
  64.             log_file << "\n\nБыла выбрана операция 1 - Списание продаж:\n";
  65.             operation_1();
  66.             break;
  67.         case 2:
  68.             log_file << "\n\nБыла выбрана операция 2 - Изменение позиций:";
  69.             operation_2();
  70.             break;
  71.         case 3:
  72.             total_calculation();
  73.             log_file << "\n\nБыла выбрана операция 3 - Отобразить общие суммы\n";
  74.             std::cout << "Общая сумма: " << open_sheet.cell("F341").value<int>() << " руб" << std::endl;
  75.             log_file << "Общая сумма: " << open_sheet.cell("F341").value<int>() << " руб" << std::endl;
  76.             std::cout << "Сумма на Ленинском: " << open_sheet.cell("F342").value<int>() << " руб" << std::endl;
  77.             log_file << "Сумма на Ленинском: " << open_sheet.cell("F342").value<int>() << " руб" << std::endl;
  78.             std::cout << "Сумма на БХ: " << open_sheet.cell("F343").value<int>() << " руб" << std::endl;
  79.             log_file << "Сумма на БХ: " << open_sheet.cell("F343").value<int>() << " руб" << std::endl;
  80.             std::cout << "Сумма на складе: " << open_sheet.cell("F344").value<int>() << " руб" << std::endl;
  81.             log_file << "Сумма на складе: " << open_sheet.cell("F344").value<int>() << " руб\n\n" << std::endl;
  82.             system("pause");
  83.             system("cls");
  84.             break;
  85.         case 4:
  86.             log_file << "\n\nБыла выбрана операция 4 - Сохранить изменения\n\n";
  87.             file.save(file_name);
  88.             file_name = backup_file(file_name);
  89.             file.load(file_name);
  90.             open_sheet = file.active_sheet();
  91.             break;
  92.         case 5:
  93.             log_file << "\n\nБыла выбрана операция 5 - Закончить работу\n";
  94.             return 5;
  95.             break;
  96.         }
  97.     }
  98.  
  99.  
  100.     void operation_1() {
  101.         while (true) {
  102.             std::cout << "Введите номер позиции (для выхода введите 0): ";
  103.             std::cin >> position_num;
  104.             system("cls");
  105.             if (position_num == 0) {
  106.                 log_file << "Введен 0. Завершение работы с изменением позиций.\n\n";
  107.                 system("cls");
  108.                 break;
  109.             }
  110.             else if (position_num > 338) {
  111.                 std::cout << "Неверно введен номер позиции" << std::endl;
  112.                 log_file << "\nНеверно введен номер позиции\n\n";
  113.                 system("pause");
  114.                 system("cls");
  115.                 continue;
  116.             }
  117.             else {
  118.                 bool was_change = false;
  119.                 log_file << "\nСписание " << position_num << " позиции {";
  120.                 while (true) {
  121.                     unsigned int was_operation = operation_num;
  122.                     std::cout << "Номер позиции: " << open_sheet.cell("A" + std::to_string(position_num + 2)).value<int>() << std::endl;
  123.                     position_name = xlnt_stream_to_cout("B" + std::to_string(position_num + 2), cell_string_filename);
  124.                     std::cout << "Наименование: " << encoder(position_name, encode_mode::UTFtoWIN) << std::endl;
  125.                     std::cout << "Цена: " << open_sheet.cell("C" + std::to_string(position_num + 2)).value<int>() << " руб" << std::endl;
  126.                     std::cout << "Общее количество: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
  127.                     std::cout << "Количество на Ленинском: " << open_sheet.cell("E" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
  128.                     std::cout << "Количество на Богдана Хмельницкого: " << open_sheet.cell("F" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
  129.                     std::cout << "Количество на складе: " << open_sheet.cell("G" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
  130.                     std::cout << "\n1 - Отнять с Ленинского\n2 - Отнять с БХ\n3 - Отнять со склада"
  131.                         << "\n4 - Отмена последнего изменения\n0 - назад"
  132.                         << "\n\nОткуда необходимо отнять? ";
  133.                     std::cin >> operation;
  134.                     operation_num = static_cast<int>(valueCheck(operation));
  135.                     system("cls");
  136.                     if (operation_num == 0) {
  137.                         total_calculation();
  138.                         break;
  139.                     }
  140.                     if (operation_num > 4) {
  141.                         do {
  142.                             std::cout << "Введен неверный ответ " << std::endl;
  143.                             std::cout << "Откуда необходимо отнять? ";
  144.                             std::cin >> operation;
  145.                             operation_num = static_cast<int>(valueCheck(operation));
  146.                             system("cls");
  147.                         } while (operation_num > 4);
  148.                     }
  149.                     if (operation_num == 4) {
  150.                         if (was_change) {
  151.                             was_change = operation_1_switch(was_operation, true);
  152.                         }
  153.                         else if (!was_change && was_operation == 4) {
  154.                             std::cout << "Не было сделано изменений, которые можно отменить." << std::endl;
  155.                             system("pause");
  156.                         }
  157.                     }
  158.                     else {
  159.                         was_change = operation_1_switch(operation_num);
  160.                     }
  161.                     system("cls");
  162.                 }
  163.                 log_file << "}\n";
  164.             }
  165.         }
  166.     }
  167.  
  168.  
  169.     bool operation_1_switch(int operation_num, bool bring_back = false) {
  170.         int quantity;
  171.         std::string operation_name, col_name;
  172.         if (operation_num == 1) {
  173.             operation_name = " ленинском";
  174.             col_name = "E";
  175.         }
  176.         else if (operation_num == 2) {
  177.             operation_name = " БХ";
  178.             col_name = "F";
  179.         }
  180.         else if (operation_num == 3) {
  181.             operation_name = " складе";
  182.             col_name = "G";
  183.         }
  184.         if (bring_back) {
  185.             log_file << "\n\tПрибавление позиции на " << operation_name << ":";
  186.             log_file << "\n\tБыло: " << open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() << "\n";
  187.             quantity = open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() + 1;
  188.             open_sheet.cell(col_name + std::to_string(position_num + 2)).value(quantity);
  189.             log_file << "\tСтало: " << open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() << "\n";
  190.             log_file << "\n\tИзменение общего количества\n\tБыло: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
  191.             open_sheet.cell("D" + std::to_string(position_num + 2)).value(open_sheet.cell("E" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("F" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("G" + std::to_string(position_num + 2)).value<int>());
  192.             log_file << "\tСтало: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
  193.             return false;
  194.         }
  195.         else {
  196.             log_file << "\n\tСписание на " << operation_name << ":";
  197.             log_file << "\n\tБыло: " << open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() << "\n";
  198.             quantity = open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() - 1;
  199.             if (quantity < 0) {
  200.                 std::cout << "Ошибка: отрицательное количество." << std::endl;
  201.                 log_file << "\tОшибка: отрицательное количество.";
  202.                 system("pause");
  203.                 return false;
  204.             }
  205.             else {
  206.                 open_sheet.cell(col_name + std::to_string(position_num + 2)).value(quantity);
  207.                 log_file << "\tСтало: " << open_sheet.cell(col_name + std::to_string(position_num + 2)).value<int>() << "\n";
  208.                 log_file << "\n\tИзменение общего количества\n\tБыло: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
  209.                 open_sheet.cell("D" + std::to_string(position_num + 2)).value(open_sheet.cell("E" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("F" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("G" + std::to_string(position_num + 2)).value<int>());
  210.                 log_file << "\tСтало: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
  211.                 return true;
  212.             }
  213.         }
  214.     }
  215.  
  216.  
  217.     void operation_2() {
  218.         while (true) {
  219.             std::cout << "Введите номер позиции (для выхода введите 0): ";
  220.             std::cin >> position_num;
  221.             system("cls");
  222.             if (position_num == 0) {
  223.                 log_file << "Введен 0. Завершение работы с изменением позиций.\n\n";
  224.                 system("cls");
  225.                 break;
  226.             }
  227.             else if (position_num > 338) {
  228.                 std::cout << "Неверно введен номер позиции" << std::endl;
  229.                 log_file << "\nНеверно введен номер позиции\n\n";
  230.                 system("pause");
  231.                 system("cls");
  232.                 continue;
  233.             }
  234.             else {
  235.                 log_file << "\nИзменение " << position_num << " позиции {";
  236.                 while (true) {
  237.                     std::cout << "Номер позиции: " << open_sheet.cell("A" + std::to_string(position_num + 2)).value<int>() << std::endl;
  238.                     position_name = xlnt_stream_to_cout("B" + std::to_string(position_num + 2), cell_string_filename);
  239.                     std::cout << "Наименование: " << encoder(position_name, encode_mode::UTFtoWIN) << std::endl;
  240.                     std::cout << "Цена: " << open_sheet.cell("C" + std::to_string(position_num + 2)).value<int>() << " руб" << std::endl;
  241.                     std::cout << "Общее количество: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
  242.                     std::cout << "Количество на Ленинском: " << open_sheet.cell("E" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
  243.                     std::cout << "Количество на Богдана Хмельницкого: " << open_sheet.cell("F" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
  244.                     std::cout << "Количество на складе: " << open_sheet.cell("G" + std::to_string(position_num + 2)).value<int>() << " шт" << std::endl;
  245.                     std::cout << "\n1 - Наименование\n2 - Цена\n3 - Количество на Ленинском"
  246.                         << "\n4 - Количество на БХ\n5 - Количество на складе\n6 - выровнять\n7 - сделать текст жирным/убрать жирность\n0 - назад"
  247.                         << "\n\nЧто необходимо изменить? ";
  248.                     std::cin >> operation;
  249.                     operation_num = static_cast<int>(valueCheck(operation));
  250.                     system("cls");
  251.                     if (operation_num == 0) {
  252.                         total_calculation();
  253.                         break;
  254.                     }
  255.                     if (operation_num > 7) {
  256.                         do {
  257.                             std::cout << "Введен неверный ответ " << std::endl;
  258.                             std::cout << "Что необходимо изменить? ";
  259.                             std::cin >> operation;
  260.                             operation_num = static_cast<int>(valueCheck(operation));
  261.                             system("cls");
  262.                         } while (operation_num > 7);
  263.                     }
  264.                     operation_2_switch(operation_num);
  265.                     system("cls");
  266.                 }
  267.                 log_file << "}\n";
  268.             }
  269.         }
  270.     }
  271.  
  272.  
  273.     void operation_2_switch(int num_of_operation) {
  274.         std::string cell_symbol, text_operation;
  275.         if (num_of_operation == 1) {
  276.             cell_symbol = "B";
  277.             text_operation = " наименование";
  278.         }
  279.         else if (num_of_operation == 2) {
  280.             cell_symbol = "C";
  281.             text_operation = " цену";
  282.         }
  283.         else if (num_of_operation == 3) {
  284.             cell_symbol = "E";
  285.             text_operation = " количество на Ленинском";
  286.         }
  287.         else if (num_of_operation == 4) {
  288.             cell_symbol = "F";
  289.             text_operation = " количество на БХ";
  290.         }
  291.         else if (num_of_operation == 5) {
  292.             cell_symbol = "G";
  293.             text_operation = " количество на складе";
  294.         }
  295.         log_file << "\n\tМеняем" << text_operation;
  296.         if (num_of_operation == 1) {
  297.             log_file << "\n\tБыло: " << encoder(position_name, encode_mode::UTFtoWIN) << "\n";
  298.             std::cout << "Введите новое" << text_operation << ": ";
  299.             std::cin.clear();
  300.             std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  301.             SetConsoleCP(1251);
  302.             std::getline(std::cin, position_name);
  303.             SetConsoleCP(866);
  304.             log_file << "\tСтало: " << position_name << "\n";
  305.             open_sheet.cell("B" + std::to_string(position_num + 2)).value(encoder(position_name, encode_mode::WINtoUTF));
  306.             system("cls");
  307.         }
  308.         else if (num_of_operation > 1 && num_of_operation < 6) {
  309.             log_file << "\n\tБыло: " << open_sheet.cell(cell_symbol + std::to_string(position_num + 2)).value<int>() << "\n";
  310.             std::cout << "Введите новое" << text_operation << ": ";
  311.             std::cin >> operation_num;
  312.             open_sheet.cell(cell_symbol + std::to_string(position_num + 2)).value(operation_num);
  313.             log_file << "\tСтало: " << operation_num << "\n";
  314.             system("cls");
  315.             if (num_of_operation > 2) {
  316.                 log_file << "\n\tИзменение общего количества\n\tБыло: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
  317.                 open_sheet.cell("D" + std::to_string(position_num + 2)).value(open_sheet.cell("E" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("F" + std::to_string(position_num + 2)).value<int>() + open_sheet.cell("G" + std::to_string(position_num + 2)).value<int>());
  318.                 log_file << "\tСтало: " << open_sheet.cell("D" + std::to_string(position_num + 2)).value<int>() << "\n";
  319.             }
  320.         }
  321.         if (num_of_operation == 6) {
  322.             while (true) {
  323.                 std::cout << "1 - по левому краю\n2 - по правому краю\n3 - по центру\nКак выровнять? ";
  324.                 std::cin >> operation;
  325.                 operation_num = static_cast<int>(valueCheck(operation));
  326.                 if (operation_num > 3) {
  327.                     std::cout << "Неверный ответ." << std::endl;
  328.                     continue;
  329.                 }
  330.                 else {
  331.                     break;
  332.                 }
  333.             }
  334.             switch (operation_num) {
  335.             case 1:
  336.             {
  337.                 alig.horizontal(horizontal_alignment::left);
  338.                 open_sheet.cell("B" + std::to_string(position_num + 2)).alignment(alig);
  339.                 break;
  340.             }
  341.             case 2:
  342.             {
  343.                 alig.horizontal(horizontal_alignment::right);
  344.                 open_sheet.cell("B" + std::to_string(position_num + 2)).alignment(alig);
  345.                 break;
  346.             }
  347.             case 3:
  348.             {
  349.                 alig.horizontal(horizontal_alignment::center);
  350.                 open_sheet.cell("B" + std::to_string(position_num + 2)).alignment(alig);
  351.                 break;
  352.             }
  353.             }
  354.             system("cls");
  355.         }
  356.         if (num_of_operation == 7) {
  357.             while (true) {
  358.                 std::cout << "1 - сделать жирным\n2 - убрать жирность\nКакую операцию выполнить? ";
  359.                 std::cin >> operation;
  360.                 operation_num = static_cast<int>(valueCheck(operation));
  361.                 if (operation_num > 2) {
  362.                     std::cout << "Неверный ответ." << std::endl;
  363.                     continue;
  364.                 }
  365.                 else {
  366.                     break;
  367.                 }
  368.             }
  369.             switch (operation_num) {
  370.             case 1:
  371.                 open_sheet.cell("B" + std::to_string(position_num + 2)).font(ft.bold(true));
  372.                 break;
  373.             case 2:
  374.                 open_sheet.cell("B" + std::to_string(position_num + 2)).font(ft.bold(false));
  375.                 break;
  376.             }
  377.             system("cls");
  378.         }
  379.     }
  380.  
  381.  
  382.     void total_calculation() {
  383.         for (int i = 1; i <= 338; i++) {
  384.             calculation += open_sheet.cell("D" + std::to_string(i + 2)).value<int>()*open_sheet.cell("C" + std::to_string(i + 2)).value<int>();
  385.         }
  386.         open_sheet.cell("F341").value(calculation);
  387.         calculation = 0;
  388.         for (int i = 1; i <= 338; i++) {
  389.             calculation += open_sheet.cell("E" + std::to_string(i + 2)).value<int>()*open_sheet.cell("C" + std::to_string(i + 2)).value<int>();
  390.         }
  391.         open_sheet.cell("F342").value(calculation);
  392.         calculation = 0;
  393.         for (int i = 1; i <= 338; i++) {
  394.             calculation += open_sheet.cell("F" + std::to_string(i + 2)).value<int>()*open_sheet.cell("C" + std::to_string(i + 2)).value<int>();
  395.         }
  396.         open_sheet.cell("F343").value(calculation);
  397.         calculation = 0;
  398.         for (int i = 1; i <= 338; i++) {
  399.             calculation += open_sheet.cell("G" + std::to_string(i + 2)).value<int>()*open_sheet.cell("C" + std::to_string(i + 2)).value<int>();
  400.         }
  401.         open_sheet.cell("F344").value(calculation);
  402.         calculation = 0;
  403.     }
  404.  
  405.  
  406.     std::string xlnt_stream_to_cout(std::string cell_num, std::string temp_file) {
  407.         std::fstream object;
  408.         std::string text;
  409.         object.open(temp_file, std::fstream::app | std::fstream::in | std::fstream::out);
  410.         int current_position, current_position_2, text_size;
  411.         object.seekg(0, std::ios::end);
  412.         current_position = object.tellg();
  413.         object << open_sheet.cell(cell_num);
  414.         object.seekg(0, std::ios::cur);
  415.         current_position_2 = object.tellg();
  416.         text_size = current_position_2 - current_position;
  417.         object.seekg(-text_size, std::ios::end);
  418.         std::getline(object, text);
  419.         return text;
  420.     }
  421.  
  422.  
  423. public:
  424.  
  425.  
  426.     work_with_file(std::string file_name) {
  427.         this->file_name = file_name;
  428.         file.load(this->file_name);
  429.         open_sheet = file.active_sheet();
  430.         log_file.open(this->file_name + ".log", std::fstream::app | std::fstream::out);
  431.     }
  432.  
  433.  
  434.     void work() {
  435.         while (true) {
  436.             if (main_menu_switch(main_menu()) == 5) {
  437.                 break;
  438.             }
  439.         }
  440.     }
  441.  
  442.  
  443.     ~work_with_file() {
  444.         file.save(file_name);
  445.         log_file.close();
  446.         std::remove("file.temp");
  447.     }
  448.  
  449.  
  450. };
  451.  
  452. int main() {
  453.     setlocale(LC_ALL, "ru");
  454.     std::cout << "Введите имя файла: ";
  455.     std::string file_name;
  456.     SetConsoleCP(1251);
  457.     std::getline(std::cin, file_name);
  458.     SetConsoleCP(866);
  459.     file_name += ".xlsx";
  460.     file_name = file_open(file_name);
  461.     file_name = backup_file(file_name);
  462.     work_with_file xlsx_file(file_name);
  463.     system("cls");
  464.     xlsx_file.work();
  465.     return 0;
  466. }
  467.  
  468. std::string file_open(std::string file_name) {
  469.     std::fstream file;
  470.     file_name = "C:\\Users\\trane\\Desktop\\Товарный отчёт\\" + file_name;
  471.  
  472.     file.open(file_name);
  473.     if (file.is_open()) {
  474.         file.close();
  475.         return file_name;
  476.     }
  477.     else {
  478.         while (true) {
  479.             std::cout << "Невозможно открыть файл. Возможно неверно введено имя файла или изменилась директория";
  480.             std::cout << "\nВведите адрес до файла(включая имя файла и расширение): ";
  481.             SetConsoleCP(1251);
  482.             std::getline(std::cin, file_name);
  483.             SetConsoleCP(866);
  484.             system("cls");
  485.             file.open(file_name);
  486.             if (file.is_open()) {
  487.                 break;
  488.             }
  489.             else {
  490.                 continue;
  491.             }
  492.         }
  493.         file.close();
  494.         return file_name;
  495.     }
  496. }
  497.  
  498. std::string backup_file(std::string file_name) {
  499.     std::fstream file_backup;
  500.     std::fstream file_copy;
  501.     file_backup.open(file_name, std::fstream::in | std::fstream::app | std::fstream::binary);
  502.     std::string new_file_name;
  503.     for (int i = 0; i < file_name.size(); i++) {
  504.         new_file_name += file_name[i];
  505.         if (file_name[i] == '\\') {
  506.             new_file_name.clear();
  507.         }
  508.     }
  509.     bool is_copy;
  510.     std::fstream check;
  511.     char ch = 49;
  512.     do {
  513.         if (ch > 57) {
  514.             std::cout << "Слишком много копий" << std::endl;
  515.             return 0;
  516.         }
  517.         check.open(new_file_name);
  518.         if (check.is_open()) {
  519.             is_copy = true;
  520.             if (new_file_name[new_file_name.size() - 6] == ')') {
  521.                 if (new_file_name[new_file_name.size() - 7] == ch) {
  522.                     ch++;
  523.                 }
  524.                 else {
  525.                     new_file_name[new_file_name.size() - 7] = ch;
  526.                 }
  527.             }
  528.             else {
  529.                 new_file_name[new_file_name.size() - 5] = '(';
  530.                 new_file_name[new_file_name.size() - 4] = '1';
  531.                 new_file_name[new_file_name.size() - 3] = ')';
  532.                 new_file_name[new_file_name.size() - 2] = '.';
  533.                 new_file_name[new_file_name.size() - 1] = 'x';
  534.                 new_file_name += "lsx";
  535.                 ch++;
  536.             }
  537.         }
  538.         else {
  539.             is_copy = false;
  540.         }
  541.         check.close();
  542.     } while (is_copy);
  543.     file_copy.open(new_file_name, std::fstream::out | std::fstream::binary | std::fstream::app);
  544.     char symbol, symbol_1;
  545.     file_backup.seekg(0, std::ios::end);
  546.     int size = file_backup.tellg();
  547.     file_backup.seekg(0, std::ios::beg);
  548.     for (int i = 0; i < size; i++) {
  549.         file_backup.read((char*)&symbol, sizeof(char));
  550.         file_copy.write((char*)&symbol, sizeof(char));
  551.     }
  552.     file_backup.close();
  553.     file_copy.close();
  554.     return new_file_name;
  555. }
  556.  
  557. std::string encoder(std::string text_to_encode, int encode_mode) {
  558.     BSTR bstrWide;
  559.     int length;
  560.     char *text = new char[text_to_encode.size() + 1];
  561.     for (int i = 0; i < text_to_encode.size(); i++) {
  562.         text[i] = text_to_encode[i];
  563.     }
  564.     text[text_to_encode.size()] = '\0';
  565.     if (encode_mode == encode_mode::UTFtoWIN) {
  566.         length = MultiByteToWideChar(CP_UTF8, 0, text, strlen(text), NULL, NULL);
  567.         bstrWide = SysAllocStringLen(NULL, length);
  568.         MultiByteToWideChar(CP_UTF8, 0, text, strlen(text), bstrWide, length);
  569.         length = WideCharToMultiByte(1251, 0, bstrWide, -1, NULL, 0, NULL, NULL);
  570.         char *text_1251 = new char[length];
  571.         WideCharToMultiByte(1251, 0, bstrWide, -1, text_1251, length, NULL, NULL);
  572.         SysFreeString(bstrWide);
  573.         text_to_encode.clear();
  574.         for (int i = 0; i < strlen(text_1251); i++) {
  575.             text_to_encode += text_1251[i];
  576.         }
  577.         delete[]text_1251;
  578.     }
  579.     else if (encode_mode == encode_mode::WINtoUTF) {
  580.         length = MultiByteToWideChar(1251, 0, text, strlen(text), NULL, NULL);
  581.         bstrWide = SysAllocStringLen(NULL, length);
  582.         MultiByteToWideChar(1251, 0, text, strlen(text), bstrWide, length);
  583.         length = WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, NULL, 0, NULL, NULL);
  584.         char *text_u8 = new char[length];
  585.         WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, text_u8, length, NULL, NULL);
  586.         SysFreeString(bstrWide);
  587.         text_to_encode.clear();
  588.         for (int i = 0; i < strlen(text_u8); i++) {
  589.             text_to_encode += text_u8[i];
  590.         }
  591.         delete[]text_u8;
  592.     }
  593.     delete[]text;
  594.     return text_to_encode;
  595. }
  596.  
  597. double valueCheck(std::string &value) {
  598.     int temp;
  599.     for (int i = 0; i < value.size(); i++) {
  600.         if (value[i] == '.') {
  601.             value[i] = ',';
  602.         }
  603.         if (value[i] == ',') {
  604.             temp = i + 1;
  605.             for (; temp < value.size(); temp++) {
  606.                 if (value[temp] == ',' || value[temp] == '.') {
  607.                     for (; temp < value.size(); temp++) {
  608.                         value[temp] = value[temp + 1];
  609.                     }
  610.                     temp = i + 1;
  611.                 }
  612.             }
  613.         }
  614.         if ((value[i] < 48 && value[i] != 43 && value[i] != 44 && value[i] != 45 && value[i] != 0) || value[i] > 57) {
  615.             temp = i;
  616.             for (; temp < value.size(); temp++) {
  617.                 value[temp] = value[temp + 1];
  618.             }
  619.             i--;
  620.         }
  621.     }
  622.     if (value[0] == '\0') {
  623.         value[0] = '0';
  624.         value[1] = '\0';
  625.     }
  626.     return std::stod(value);
  627. }
Advertisement
Add Comment
Please, Sign In to add comment