LINUX.ORG.RU

История изменений

Исправление MOPKOBKA, (текущая версия) :

Ну работает же нормально в Windows 11 и Linux. Только в Wine и старых Windows будут проблемы, но их можно избежать сконвертировав весь вывод в wchar_t. В Linux конвертацию можно оставить из std.

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#include <windows.h>
#endif

#ifdef _WIN32
wchar_t* cstr2wstr(const char* s)
{
  size_t s_len, ws_len;
  wchar_t *ws;

  s_len = strlen(s);
  ws_len = MultiByteToWideChar(CP_UTF8, 0, s, s_len, NULL, 0);
  if ((ws = malloc(ws_len + 1)) == NULL) {
    wprintf(L"cstr2wstr: low memory\n");
    exit(1);
  }
  MultiByteToWideChar(CP_UTF8, 0, s, s_len, ws, ws_len);
  ws[ws_len] = 0;
  return ws;
}
#endif

void right_pad(const char *text, int pad_size)
{
	wchar_t *wtext = cstr2wstr(text);
	wprintf(L"%*ls |\n", pad_size, wtext);
	free(wtext);
}

int main()
{
	setlocale(LC_ALL, "");
	right_pad("Привет мир!", 30);
	right_pad("Hello World!", 30);
	right_pad("Довольно длинная строка ...", 30);
	return 0;
}

Исправление MOPKOBKA, :

Ну работает же нормально в Windows 11 и Linux. Только в Wine и старых Windows будут проблемы, но их можно избежать сконвертировав весь вывод в wchar_t. В Linux конвертацию можно оставить из std.

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#endif

#ifdef _WIN32
wchar_t* cstr2wstr(const char* s)
{
  size_t s_len, ws_len;
  wchar_t *ws;

  s_len = strlen(s);
  ws_len = MultiByteToWideChar(CP_UTF8, 0, s, s_len, NULL, 0);
  if ((ws = malloc(ws_len + 1)) == NULL) {
    wprintf(L"cstr2wstr: low memory\n");
    exit(1);
  }
  MultiByteToWideChar(CP_UTF8, 0, s, s_len, ws, ws_len);
  ws[ws_len] = 0;
  return ws;
}
#endif

void right_pad(const char *text, int pad_size)
{
	wchar_t *wtext = cstr2wstr(text);
	wprintf(L"%*ls |\n", pad_size, wtext);
	free(wtext);
}

int main()
{
	setlocale(LC_ALL, "");
	right_pad("Привет мир!", 30);
	right_pad("Hello World!", 30);
	right_pad("Довольно длинная строка ...", 30);
	return 0;
}

Исходная версия MOPKOBKA, :

Ну работает же нормально в Windows 11 и Linux. Только в Wine и старых Windows будут проблемы, но их можно избежать сконвертировав весь вывод в wchar_t. В Linux конвертацию можно оставить из std.

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

wchar_t* cstr2wstr(const char* s)
{
  size_t s_len, ws_len;
  wchar_t *ws;

  s_len = strlen(s);
  ws_len = MultiByteToWideChar(CP_UTF8, 0, s, s_len, NULL, 0);
  if ((ws = malloc(ws_len + 1)) == NULL) {
    wprintf(L"cstr2wstr: low memory\n");
    exit(1);
  }
  MultiByteToWideChar(CP_UTF8, 0, s, s_len, ws, ws_len);
  ws[ws_len] = 0;
  return ws;
}

void right_pad(const char *text, int pad_size)
{
	wchar_t *wtext = cstr2wstr(text);
	wprintf(L"%*ls |\n", pad_size, wtext);
	free(wtext);
}

int main()
{
	setlocale(LC_ALL, "");
	right_pad("Привет мир!", 30);
	right_pad("Hello World!", 30);
	right_pad("Довольно длинная строка ...", 30);
	return 0;
}