small_wcsrtombs.c

00001 #include <reent.h>
00002 #include <wchar.h>
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <errno.h>
00006 
00007 #if defined( _SMALL_PRINTF ) || defined(SMALL_SCANF) 
00008  #define _ASCII_CAR
00009  #endif
00010 
00011 size_t
00012 _DEFUN (_wcsrtombs_r, (r, dst, src, len, ps),
00013         struct _reent *r _AND
00014         char *dst _AND
00015         const wchar_t **src _AND
00016         size_t len _AND
00017         mbstate_t *ps)
00018 {
00019   
00020   
00021   char *ptr = dst;
00022   char buff[10];
00023   wchar_t *pwcs;
00024   size_t n;
00025   int i;
00026 
00027 #ifdef MB_CAPABLE
00028   if (ps == NULL)
00029     {
00030       _REENT_CHECK_MISC(r);
00031       ps = &(_REENT_WCSRTOMBS_STATE(r));
00032     }
00033 #endif
00034 
00035   /* If no dst pointer, treat len as maximum possible value. */
00036   if (dst == NULL)
00037     len = (size_t)-1;
00038 
00039   n = 0;
00040   pwcs = (wchar_t *)(*src);
00041 
00042   while (n < len)
00043     {
00044       int count = ps->__count;
00045       wint_t wch = ps->__value.__wch;
00046       #ifndef _ASCII_CAR
00047       int bytes = _wcrtomb_r (r, buff, *pwcs, ps);      
00048       if (bytes == -1)
00049         {
00050           r->_errno = EILSEQ;
00051           ps->__count = 0;
00052           return (size_t)-1;
00053         }
00054             #else
00055         int bytes = 1 ;
00056        #endif
00057         
00058       if (n <= len - bytes && bytes < len)
00059         {
00060           n += bytes;
00061           if (dst)
00062             {
00063               for (i = 0; i < bytes; ++i)
00064                 *ptr++ = buff[i];
00065               ++(*src);
00066             }
00067           if (*pwcs++ == 0x00)
00068             {
00069               if (dst)
00070                 *src = NULL;
00071               ps->__count = 0;
00072               return n - 1;
00073             }
00074         }
00075       else
00076         {
00077           /* not enough room, we must back up state to before _wctomb_r call */
00078           ps->__count = count;
00079           ps->__value.__wch = wch;
00080           len = 0;
00081         }
00082     }
00083 
00084   return n;
00085 } 
00086 
00087 #ifndef _REENT_ONLY
00088 size_t
00089 _DEFUN (wcsrtombs, (dst, src, len, ps),
00090         char *dst _AND
00091         const wchar_t **src _AND
00092         size_t len _AND
00093         mbstate_t *ps)
00094 {
00095   return _wcsrtombs_r (_REENT, dst, src, len, ps);
00096 }
00097 #endif /* !_REENT_ONLY */

Generated on Mon Apr 11 14:23:40 2011 for Contiki 2.5 by  doxygen 1.6.1