No space calculation in wcstombs?

Jeff Johnston jjohnstn@redhat.com
Mon Oct 29 08:04:00 GMT 2007


A patch has been added, thanks.  The C99 spec doesn't mention the 
feature, however, it is SUSV2 and glibc supports the NULL argument.

Regarding other functions:  feel free to submit them here for review.  
Please include a suitable license for inclusion into newlib, otherwise 
they cannot be added.  Be aware if you find the function code elsewhere, 
even if you modify it, you cannot relicense it yourself.  Obviously, if 
you write the code from scratch, it is yours to license anyway you 
wish.  Do not choose GPL, LGPL (e.g. glibc), or proprietary licensing 
though.  BSD-style or freer is desired.  If you need some examples, just 
let me know.

-- Jeff J.

Christoph Ender wrote:
>
> Hi,
>
> thanks for your quick answer. I've taken a look at the source
> from the CVS and "fixed" the issue by replacing the contents of
> the file "wcstombs_r.c" with the quick-and-dirty solution appended
> below (which may still contain some serious flaw since I've never
> fiddled with a c-lib before but it works for me).
>
> Since I also need fwprintf and other functions which are currently
> not implemented I might extend newlib a bit more to get my program
> working. Would it make sense to mail the new code to some official
> maintainer so that someday these functions might be available in a
> regular release?
>
>
> Regards,
> Christoph.
>
>
> ----------8<----------8<----------8<----------8<----------
>
> #include <stdlib.h>
> #include <wchar.h>
>
> size_t
> _DEFUN (_wcstombs_r, (reent, s, pwcs, n, state),
>         struct _reent *r    _AND
>         char          *s    _AND
>         const wchar_t *pwcs _AND
>         size_t         n    _AND
>         mbstate_t     *state)
> {
>   char *ptr = s;
>   size_t max = n;
>   char buff[8];
>   int i, num_to_copy;
>   int sum_bytes;
>
>   if (s == NULL)
>   {
>     sum_bytes = 0;
>
>     while (*pwcs != 0)
>     {
>       sum_bytes += _wctomb_r (r, buff, *pwcs, state);
>       pwcs++;
>     }
>
>     return sum_bytes;
>   }
>   else
>   {
>     while (n > 0)
>       {
>         int bytes = _wctomb_r (r, buff, *pwcs, state);
>         if (bytes == -1)
>           return -1;
>         num_to_copy = (n > bytes ? bytes : (int)n);
>         for (i = 0; i < num_to_copy; ++i)
>           *ptr++ = buff[i];
>
>         if (*pwcs == 0x00)
>           return ptr - s - (n >= bytes);
>         ++pwcs;
>         n -= num_to_copy;
>       }
>
>     return max;
>   }
> }
>
> ----------8<----------8<----------8<----------8<----------
>
>
>> On Oct 23 11:52, Christoph Ender wrote:
>>> Hi all,
>>> when converting wchar_t*- to (mb)char*-Strings I've been
>>> so far using the wcstombs function in two steps: One call
>>> [...]
>>> I've just noticed that the first step does not work with
>>> cygwin and  I've attached a small example to demonstrate
>>> [...]
>
> On October 23, 2007 at 12:42 Corinna Vinschen wrote:
>> No, it's a newlib problem.  The wide character support in newlib
>> is somewhat rudimentary.  The reason is that newlib is mainly
>> targeting embedded systems which don't need full wchar support.
>> However, as this is an open source project, patches are always
>> welcome.
>> Corinna
>



More information about the Newlib mailing list