Advertisement
Guest User

Untitled

a guest
May 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <fstream>
  8. #include <codecvt>
  9.  
  10. void ReadFile(std::vector<std::wstring> & text);
  11. bool isBom(std::wstring path);
  12.  
  13. int main()
  14. {
  15. std::wcin.imbue(std::locale("rus_rus.866"));
  16. std::wcout.imbue(std::locale("rus_rus.866"));
  17. std::vector<std::wstring> text;
  18. std::wstring buffer;
  19. const auto& facet = std::use_facet<std::ctype<wchar_t>>(std::locale());
  20. for (;;)
  21. {
  22. static int action = -1;
  23. std::wcout << L"1) Ввести новую строку и добавить ее в конец текста\n"
  24. << L"2) Заменить все первые строчные буквы слов прописными\n"
  25. << L"3) Вставить строки из файла\n"
  26. << L"4) Выйти\n\n"
  27. << L"Вы выбираете действие: ";
  28. if (!(std::wcin >> action))
  29. {
  30. std::wcin.clear();
  31. std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
  32. std::wcout << L"Ошибка" << std::endl;
  33. continue;
  34. }
  35. switch (action)
  36. {
  37. case 1:
  38. std::wcout << L"\nВведите строку:" << std::endl;
  39. std::wcin.clear();
  40. std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
  41. std::getline(std::wcin, buffer);
  42. text.push_back(buffer);
  43. break;
  44. case 2:
  45. std::for_each(text.begin(), text.end(), [&facet](std::wstring& line)
  46. {
  47. wchar_t last = ' ';
  48. std::for_each(line.begin(), line.end(), [&last, &facet](wchar_t& c) {
  49. if (iswalpha(c) && (last == ' ') && c != ' ')
  50. c = facet.toupper(c);
  51. last = c;
  52. });
  53. });
  54. break;
  55. case 3:
  56. ReadFile(text);
  57. break;
  58. case 4:
  59. system("pause");
  60. return 0;
  61. default:
  62. std::wcout << L"Нет такого действия" << std::endl;
  63. }
  64. std::wcout << L"\nТекст:" << std::endl;
  65. std::for_each(text.begin(), text.end(), [](const std::wstring& line)
  66. {
  67. std::wcout << line << std::endl;
  68. });
  69. std::wcout << std::endl;
  70. }
  71. }
  72.  
  73. void ReadFile(std::vector<std::wstring> & text)
  74. {
  75. std::wstring buffer;
  76. for (;;)
  77. {
  78. std::wcout << L"Введите путь к файлу:" << std::endl;
  79. std::wcin.clear();
  80. std::wcin.ignore(std::numeric_limits<std::streamsize>::max(), L'\n');
  81. std::getline(std::wcin, buffer);
  82. if (_waccess_s(buffer.c_str(), 4))
  83. {
  84. std::wcout << L"Не удалось прочитать файл" << std::endl;
  85. continue;
  86. }
  87. break;
  88. }
  89. std::wifstream file(buffer);
  90. if (isBom(buffer))
  91. file.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t, 0x10ffff, std::consume_header>));
  92. else
  93. file.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
  94. while (std::getline(file, buffer))
  95. text.push_back(buffer);
  96. }
  97.  
  98. bool isBom(std::wstring path)
  99. {
  100. bool isBom = false;
  101. std::wifstream checkBom(path);
  102. wchar_t bom[3];
  103. checkBom.read(bom, 3);
  104. if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
  105. isBom = true;
  106. return isBom;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement