Re: PQexecPrepared() question - Mailing list pgsql-general

From Peter J. Holzer
Subject Re: PQexecPrepared() question
Date
Msg-id 47v26pfhc2hnz3ax2u6ogke7npeyghbkxghov5hysv2f7ttchs@c5vrfavj7oqe
Whole thread Raw
In response to Re: PQexecPrepared() question  (Igor Korot <ikorot01@gmail.com>)
Responses Re: PQexecPrepared() question
List pgsql-general
On 2025-12-21 17:30:38 -0800, Igor Korot wrote:
> On Sun, Dec 21, 2025 at 1:19 PM Peter J. Holzer <hjp-pgsql@hjp.at> wrote:
> > As I see it, Igor has two options:
> >
> > 1) Send UTF8. wcstombs is the wrong tool for this, since it uses the
> >    current locale which is obviously not using UTF-8. I don't program in
> >    C++, but I'm pretty sure there is a portable way to encode a string
> >    in UTF-8.
[...]
> > I think 1) is the better strategy, but them I've been a UTF-8 fan-boy
> > for over 30 years ;-).
>
> Maybe all I need is something like this:
>
> convert(char *dest, const std::wstring src)
> {
>     const wchar_t *temp = src.c_str();
>     while( *dest )
>     {
>         dest++;
>     }
>     while( *temp )
>     {
>         *dest = *temp;
>         dest++;
>         temp++;
>     }
>     *dest++ = '\0';
>     *dest = '\0';
> }
>
> ?

I hope your compiler complains loudly about that code. You can't just
assign a wchar_t to a char and expect it to do anything sensible.

A short search points to std::wstring_convert, but that has been
deprecated in C++17, and it's not obvious to me what the replacement is.

There is always ICU, of course, but that feels like cracking a nut with
a sledgehammer.

Personally, I might roll my own if an hour of googling doesn't turn up
anything promising (UTF-8 encoding is quite simple), but that shouldn't
be necessary.


        hjp

--
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachment

pgsql-general by date:

Previous
From: Laurenz Albe
Date:
Subject: Re: PQexecPrepared() question
Next
From: Igor Korot
Date:
Subject: Re: PQexecPrepared() question