File: StrToWstr.cpp

package info (click to toggle)
librepfunc 1.11.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 468 kB
  • sloc: cpp: 1,768; makefile: 270
file content (31 lines) | stat: -rw-r--r-- 1,095 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*******************************************************************************
 * librepfunc - a collection of common functions, classes and tools.
 * See the README file for copyright information and how to reach the author.
 ******************************************************************************/
#include <repfunc.h>
#include <clocale>    // std::setlocale
#include <cstdlib>    // std::wcstombs, std::mbstowcs

/* 20251210: replace deprecated std::codecvt_utf8 with
 *           std::wcstombs, std::mbstowcs
 * https://en.cppreference.com/w/cpp/string/multibyte/mbstowcs.html
 * https://en.cppreference.com/w/cpp/string/multibyte/wcstombs.html
 */

std::string WStrToStr(std::wstring ws) {
  if (ws.empty())
     return "";
  auto* wstr = (const wchar_t*) ws.c_str();
  char mbstr[256];
  std::wcstombs(mbstr, wstr, sizeof(mbstr));
  return (const char*) mbstr;
}

std::wstring StrToWStr(std::string s) {
  if (s.empty())
     return L"";
  auto* mbstr = s.c_str();
  wchar_t wstr[256];
  std::mbstowcs(wstr, mbstr, sizeof(wstr)/sizeof(wstr[0]));
  return (const wchar_t*) wstr;
}