public inbox for [email protected]
help / color / mirror / Atom feedFrom: Neel Patel <[email protected]>
To: Sergey Burladyan <[email protected]>
Cc: pgadmin-hackers <[email protected]>
Cc: Ashesh Vashi <[email protected]>
Cc: Dave Page <[email protected]>
Subject: Re: pgagent unicode support
Date: Wed, 24 Feb 2021 17:53:08 +0530
Message-ID: <CACCA4P0CnEe1+_yq4brdNKFHuL+HP0em+S2tfbLWcnEDPFNbfw@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
<CA+OCxoxsk-VJOJvw95V+pDN0G79T5-F1jAHB-THsPZd10f0KRg@mail.gmail.com>
<CACCA4P0PG8y_u9vDwTTL4AHbXOfP1k7joLY4tjLQ40DNpWgyoA@mail.gmail.com>
<CACCA4P3JvfMmG_LCm8Oeqnp4r=EuJBLuaS2mUieuw+--2ybgFQ@mail.gmail.com>
<CACCA4P1NgUGZO8PSC6_Js+__pqALrSkd-NGCGWr_OtYptGoqEA@mail.gmail.com>
<[email protected]>
<[email protected]>
Hi Sergey,
Thanks for the review. Please find the attached updated patch.
Do review it and let me know in case of any comments.
Thanks,
Neel Patel
On Wed, Feb 24, 2021 at 5:12 PM Sergey Burladyan <[email protected]>
wrote:
> Sergey Burladyan <[email protected]> writes:
>
> > Maybe it should look something like this:
> > + wcstombs_s(&charsConverted, mbs, mb_len + 1, wchar_str, mb_len);
>
> Ah, and we missed check for error.
>
> Something like this, maybe:
> +#ifdef __WIN32__
> + size_t charsConverted = 0;
> + if (wcstombs_s(&charsConverted, mbs, mb_len + 1, wchar_str,
> mb_len) != 0) {
> + delete [] mbs;
> + return NULL;
> + }
> +#else
>
> --
> Sergey Burladyan
>
Attachments:
[application/octet-stream] pgagent_unicode_v2.patch (2.1K, 3-pgagent_unicode_v2.patch)
download | inline diff:
diff --git a/misc.cpp b/misc.cpp
index 35ac83d..e518f3a 100644
--- a/misc.cpp
+++ b/misc.cpp
@@ -143,22 +143,66 @@ std::wstring NumToStr(const long l)
}
// This function is used to convert char* to std::wstring.
-std::wstring CharToWString(const char* cstr)
+std::wstring CharToWString(const char *cstr)
{
- std::string s = std::string(cstr);
- std::wstring wsTmp(s.begin(), s.end());
- return wsTmp;
+ if (cstr != NULL)
+ {
+ size_t wc_cnt = mbstowcs(NULL, cstr, 0);
+
+ if (wc_cnt == (size_t) -1) {
+ return std::wstring();
+ }
+
+ wchar_t *wcs = new wchar_t[wc_cnt + 1];
+ if (wcs == NULL) {
+ return std::wstring();
+ }
+
+ if (mbstowcs(wcs, cstr, wc_cnt + 1) == (size_t) -1) {
+ delete [] wcs;
+ return std::wstring();
+ }
+
+ std::wstring tmp(&wcs[0], &wcs[wc_cnt]);
+ delete [] wcs;
+
+ return tmp;
+ }
+ return std::wstring();
}
// This function is used to convert std::wstring to char *.
-char * WStringToChar(const std::wstring &wstr)
+char *WStringToChar(const std::wstring &wstr)
{
const wchar_t *wchar_str = wstr.c_str();
- int wstr_length = wcslen(wchar_str);
- char *dst = new char[wstr_length + 10];
- memset(dst, 0x00, (wstr_length + 10));
- wcstombs(dst, wchar_str, wstr_length);
- return dst;
+
+ int mb_len = wcstombs(NULL, wchar_str, 0);
+
+ if ((size_t)mb_len == (size_t) -1) {
+ return NULL;
+ }
+
+ char *mbs = new char[mb_len + 1];
+ if (mbs == NULL) {
+ return NULL;
+ }
+
+ memset(mbs, 0, mb_len + 1);
+
+#ifdef __WIN32__
+ size_t charsConverted = 0;
+ if (wcstombs_s(&charsConverted, mbs, mb_len + 1, wchar_str, mb_len) != 0) {
+ delete [] mbs;
+ return NULL;
+ }
+#else
+ if (wcstombs(mbs, wchar_str, mb_len + 1) == (size_t) -1) {
+ delete [] mbs;
+ return NULL;
+ }
+
+#endif
+ return mbs;
}
// Below function will generate random string of given character.
diff --git a/unix.cpp b/unix.cpp
index 9a41e38..d4b0d3d 100644
--- a/unix.cpp
+++ b/unix.cpp
@@ -155,6 +155,8 @@ static void daemonize(void)
int main(int argc, char **argv)
{
+ setlocale(LC_ALL, "");
+
std::wstring executable;
executable.assign(CharToWString(argv[0]));
view thread (13+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected]
Subject: Re: pgagent unicode support
In-Reply-To: <CACCA4P0CnEe1+_yq4brdNKFHuL+HP0em+S2tfbLWcnEDPFNbfw@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox