Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
  28. *               DESCRIBE IT HERE!
  29. *
  30. ****************************************************************************/
  31.  
  32.  
  33. #include "variety.h"
  34. #include "widechar.h"
  35. #include <stdio.h>
  36. #include <stdarg.h>
  37. #include <limits.h>
  38. #include "printf.h"
  39.  
  40.  
  41. /*
  42.  * mem_putc -- append a character to a string in memory
  43.  */
  44. #ifdef __WIDECHAR__
  45.  
  46. struct vswprtf_buf {
  47.     CHAR_TYPE   *bufptr;
  48.     int         chars_output;
  49.     int         max_chars;
  50. };
  51.  
  52. static slib_callback_t mem_putc; // setup calling convention
  53. static void __SLIB_CALLBACK mem_putc( SPECS __SLIB *specs, int op_char )
  54. {
  55.     struct vswprtf_buf  *info;
  56.  
  57.     info = (struct vswprtf_buf*) specs->_dest;
  58.     if( info->chars_output + 1 <= info->max_chars ) {
  59.         *( info->bufptr++ ) = op_char;
  60.         specs->_output_count++;
  61.         info->chars_output++;
  62.     }
  63. }
  64.  
  65. #else
  66.  
  67. static slib_callback_t mem_putc; // setup calling convention
  68. static void __SLIB_CALLBACK mem_putc( SPECS __SLIB *specs, int op_char )
  69. {
  70.     *( specs->_dest++ ) = op_char;
  71.     specs->_output_count++;
  72. }
  73.  
  74. #endif
  75.  
  76.  
  77. #ifdef __WIDECHAR__
  78. _WCRTLINK int vswprintf( CHAR_TYPE *dest, size_t n, const CHAR_TYPE *format, va_list arg )
  79. {
  80.     slib_callback_t         *tmp;
  81.     auto struct vswprtf_buf info;
  82.  
  83.   #if defined( __386__ ) && defined( __QNX__ )
  84.     /* avoid some segment relocations for 32-bit QNX */
  85.     tmp = (void (*)())mem_putc;
  86.   #else
  87.     tmp = mem_putc;
  88.   #endif
  89.     if( n != 0 ) {
  90.         info.bufptr = dest;
  91.         info.chars_output = 0;
  92.         info.max_chars = n - 1;
  93.         __wprtf( &info, format, arg, tmp );
  94.         dest[info.chars_output] = NULLCHAR;
  95.     }
  96.     return( info.chars_output );
  97. }
  98. #endif
  99.  
  100. _WCRTLINK int __F_NAME(vsprintf,_vswprintf) ( CHAR_TYPE *dest, const CHAR_TYPE *format, va_list arg )
  101. {
  102.     slib_callback_t         *tmp;
  103. #ifndef __WIDECHAR__
  104.     register int            len;
  105. #else
  106.     auto struct vswprtf_buf info;
  107. #endif
  108.  
  109. #if defined( __386__ ) && defined( __QNX__ )
  110.     /* avoid some segment relocations for 32-bit QNX */
  111.     tmp = (void (*)())mem_putc;
  112. #else
  113.     tmp = mem_putc;
  114. #endif
  115. #ifdef __WIDECHAR__
  116.     info.bufptr = dest;
  117.     info.chars_output = 0;
  118.     info.max_chars = INT_MAX;
  119.     __wprtf( &info, format, arg, tmp );
  120.     dest[info.chars_output] = NULLCHAR;
  121.     return( info.chars_output );
  122. #else
  123.     len = __prtf( dest, format, arg, tmp );
  124.     dest[len] = NULLCHAR;
  125.     return( len );
  126. #endif
  127. }
  128.