migration
This commit is contained in:
7
semester-4/ОС/lb-1/src/.clangd
Normal file
7
semester-4/ОС/lb-1/src/.clangd
Normal file
@ -0,0 +1,7 @@
|
||||
CompileFlags:
|
||||
Add:
|
||||
- --target=x86_64-w64-mingw32
|
||||
- -I/usr/x86_64-w64-mingw32/include
|
||||
- -I/usr/x86_64-w64-mingw32/include/w32api
|
||||
- -DUNICODE
|
||||
- -D_UNICODE
|
5
semester-4/ОС/lb-1/src/Makefile
Normal file
5
semester-4/ОС/lb-1/src/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
unicode: main.cpp
|
||||
x86_64-w64-mingw32-g++ -Wall -Wextra -DUNICODE -D_UNICODE -o main.exe main.cpp
|
||||
|
||||
ascii: main.cpp
|
||||
x86_64-w64-mingw32-g++ -Wall -Wextra -DMBCS -o main.exe main.cpp
|
4
semester-4/ОС/lb-1/src/inputA.txt
Normal file
4
semester-4/ОС/lb-1/src/inputA.txt
Normal file
@ -0,0 +1,4 @@
|
||||
test
|
||||
1234
|
||||
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
4
semester-4/ОС/lb-1/src/inputU.txt
Normal file
4
semester-4/ОС/lb-1/src/inputU.txt
Normal file
@ -0,0 +1,4 @@
|
||||
test
|
||||
1234
|
||||
|
||||
привіт
|
173
semester-4/ОС/lb-1/src/main.cpp
Normal file
173
semester-4/ОС/lb-1/src/main.cpp
Normal file
@ -0,0 +1,173 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <locale.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
// #include <stringapiset.h>
|
||||
#include <tchar.h>
|
||||
// #include <wchar.h>
|
||||
#include <windows.h>
|
||||
|
||||
void reverse_file(const char *input_filename, const char *output_filename);
|
||||
void reverse_string(char *str, size_t len, int codepage);
|
||||
int get_codepage(const char *filename);
|
||||
|
||||
int main() {
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> UNICODE
|
||||
// setlocale(LC_ALL, "Ukrainian");
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> 1251 <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> ASCII
|
||||
SetConsoleOutputCP(1251);
|
||||
SetConsoleCP(1251);
|
||||
|
||||
int tchar_size = sizeof(TCHAR);
|
||||
printf("TCHAR size: %d\n", tchar_size);
|
||||
|
||||
char ua_strings[3][60] = {"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
|
||||
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
|
||||
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"};
|
||||
|
||||
printf("\nua_strings:\n");
|
||||
for (int i = 0; i < 3; i++) {
|
||||
printf("%d: %s\n", i + 1, ua_strings[i]);
|
||||
}
|
||||
|
||||
printf("\nua_wstrings:\n");
|
||||
wchar_t ua_wstrings[3][60] = {};
|
||||
for (int i = 0; i < 3; i++) {
|
||||
MultiByteToWideChar(CP_ACP, 0, ua_strings[i], -1, ua_wstrings[i], 60);
|
||||
|
||||
printf("- - - - - - - - - - - - - - - - - - - - - -\n");
|
||||
printf("%d:\n", i + 1);
|
||||
|
||||
_tprintf(_T("_tprintf:\t%ls\n"), ua_wstrings[i]);
|
||||
std::wcout << L"std::wcout:\t" << ua_wstrings[i] << std::endl;
|
||||
MessageBoxW(NULL, ua_wstrings[i], L"MessageBox", MB_OK);
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
wchar_t ua_wstrings_to_qsort[3][60] = {};
|
||||
wchar_t *ua_wstrings_to_sort[3] = {};
|
||||
for (int i = 0; i < 3; i++) {
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> qsort
|
||||
wcscpy(ua_wstrings_to_qsort[i], ua_wstrings[i]);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> std::sort
|
||||
ua_wstrings_to_sort[i] = (wchar_t *)malloc(sizeof(wchar_t) * 60);
|
||||
wcscpy(ua_wstrings_to_sort[i], ua_wstrings[i]);
|
||||
}
|
||||
|
||||
printf("\nqsort:\n");
|
||||
qsort(ua_wstrings_to_qsort, 3, sizeof(wchar_t) * 60,
|
||||
(int (*)(const void *, const void *))wcscoll);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
_tprintf(_T("%ls\n"), ua_wstrings_to_qsort[i]);
|
||||
}
|
||||
|
||||
printf("\nstd::sort:\n");
|
||||
std::sort(ua_wstrings_to_sort, ua_wstrings_to_sort + 3,
|
||||
[](const wchar_t *a, const wchar_t *b) -> bool {
|
||||
return wcscoll(a, b) < 0;
|
||||
});
|
||||
for (int i = 0; i < 3; i++) {
|
||||
_tprintf(_T("%ls\n"), ua_wstrings_to_sort[i]);
|
||||
}
|
||||
|
||||
printf("\nua_strings_from_wstrings:\n");
|
||||
char ua_strings_from_wstrings[3][60] = {};
|
||||
for (int i = 0; i < 3; i++) {
|
||||
WideCharToMultiByte(CP_ACP, 0, ua_wstrings[i], -1,
|
||||
ua_strings_from_wstrings[i], 60, NULL, NULL);
|
||||
|
||||
printf("%d: %s\n", i + 1, ua_strings_from_wstrings[i]);
|
||||
}
|
||||
|
||||
printf("\nfile convertions:\n");
|
||||
reverse_file("inputA.txt", "outputA.txt");
|
||||
reverse_file("inputU.txt", "outputU.txt");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void reverse_string(char *str, size_t len, int codepage) {
|
||||
if (str == NULL || len == 0)
|
||||
return;
|
||||
|
||||
wchar_t *wstr = (wchar_t *)malloc(sizeof(wchar_t) * (len + 1));
|
||||
if (wstr == NULL) {
|
||||
fprintf(stderr, "Error encountered while allocating memory\n");
|
||||
return;
|
||||
}
|
||||
|
||||
MultiByteToWideChar(codepage, 0, str, len + 1, wstr, len + 1);
|
||||
size_t wlen = wcslen(wstr);
|
||||
|
||||
for (size_t i = 0, j = wlen - 1; i < j; i++, j--) {
|
||||
wchar_t temp = wstr[i];
|
||||
wstr[i] = wstr[j];
|
||||
wstr[j] = temp;
|
||||
}
|
||||
|
||||
WideCharToMultiByte(codepage, 0, wstr, len, str, len, NULL, NULL);
|
||||
|
||||
free(wstr);
|
||||
}
|
||||
|
||||
void reverse_file(const char *input_filename, const char *output_filename) {
|
||||
FILE *input_file = fopen(input_filename, "r");
|
||||
if (input_file == NULL) {
|
||||
fprintf(stderr, "Cant open input file '%s'\n", input_filename);
|
||||
return;
|
||||
}
|
||||
|
||||
FILE *output_file = fopen(output_filename, "w");
|
||||
if (output_file == NULL) {
|
||||
fprintf(stderr, "Cant create output file '%s'\n", output_filename);
|
||||
fclose(input_file);
|
||||
return;
|
||||
}
|
||||
|
||||
int codepage = get_codepage(input_filename);
|
||||
|
||||
char buffer[4096];
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), input_file) != NULL) {
|
||||
size_t len = strlen(buffer);
|
||||
|
||||
if (len > 0 && buffer[len - 1] == '\n') {
|
||||
buffer[len - 1] = '\0';
|
||||
len--;
|
||||
}
|
||||
|
||||
reverse_string(buffer, len, codepage);
|
||||
|
||||
fprintf(output_file, "%s\n", buffer);
|
||||
}
|
||||
|
||||
fclose(input_file);
|
||||
fclose(output_file);
|
||||
|
||||
printf("Successfully reversed file '%s' to '%s'\n", input_filename,
|
||||
output_filename);
|
||||
};
|
||||
|
||||
int get_codepage(const char *filename) {
|
||||
FILE *file = fopen(filename, "r");
|
||||
if (file == NULL) {
|
||||
fprintf(stderr, "Cant open file '%s'\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int codepage;
|
||||
|
||||
if (fgetc(file) == 0xEF && fgetc(file) == 0xBB && fgetc(file) == 0xBF) {
|
||||
codepage = CP_UTF8;
|
||||
} else {
|
||||
codepage = CP_ACP;
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
return codepage;
|
||||
}
|
4
semester-4/ОС/lb-1/src/outputA.txt
Normal file
4
semester-4/ОС/lb-1/src/outputA.txt
Normal file
@ -0,0 +1,4 @@
|
||||
tset
|
||||
4321
|
||||
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
4
semester-4/ОС/lb-1/src/outputU.txt
Normal file
4
semester-4/ОС/lb-1/src/outputU.txt
Normal file
@ -0,0 +1,4 @@
|
||||
tset
|
||||
4321
|
||||
|
||||
тівирп
|
Reference in New Issue
Block a user