uprintf.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 1998-2014, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File uprintf.cpp
  12. *
  13. * Modification History:
  14. *
  15. * Date Name Description
  16. * 11/19/98 stephen Creation.
  17. * 03/12/99 stephen Modified for new C API.
  18. * Added conversion from default codepage.
  19. * 08/07/2003 george Reunify printf implementations
  20. ******************************************************************************
  21. */
  22. #include "unicode/utypes.h"
  23. #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
  24. #include "unicode/ustdio.h"
  25. #include "unicode/ustring.h"
  26. #include "unicode/unum.h"
  27. #include "unicode/udat.h"
  28. #include "unicode/putil.h"
  29. #include "cmemory.h"
  30. #include "locbund.h"
  31. #include "mutex.h"
  32. #include "uassert.h"
  33. #include "uprintf.h"
  34. #include "ufile.h"
  35. #include "ucln_io.h"
  36. U_NAMESPACE_USE
  37. static UFILE *gStdOut = nullptr;
  38. static UInitOnce gStdOutInitOnce {};
  39. static UBool U_CALLCONV uprintf_cleanup()
  40. {
  41. if (gStdOut != nullptr) {
  42. u_fclose(gStdOut);
  43. gStdOut = nullptr;
  44. }
  45. gStdOutInitOnce.reset();
  46. return true;
  47. }
  48. static void U_CALLCONV u_stdout_init() {
  49. U_ASSERT(gStdOut == nullptr);
  50. gStdOut = u_finit(stdout, nullptr, nullptr);
  51. ucln_io_registerCleanup(UCLN_IO_PRINTF, &uprintf_cleanup);
  52. }
  53. U_CAPI UFILE * U_EXPORT2
  54. u_get_stdout()
  55. {
  56. umtx_initOnce(gStdOutInitOnce, &u_stdout_init);
  57. return gStdOut;
  58. }
  59. static int32_t U_EXPORT2
  60. u_printf_write(void *context,
  61. const char16_t *str,
  62. int32_t count)
  63. {
  64. return u_file_write(str, count, (UFILE *)context);
  65. }
  66. static int32_t
  67. u_printf_pad_and_justify(void *context,
  68. const u_printf_spec_info *info,
  69. const char16_t *result,
  70. int32_t resultLen)
  71. {
  72. UFILE *output = (UFILE *)context;
  73. int32_t written, i;
  74. /* pad and justify, if needed */
  75. if(info->fWidth != -1 && resultLen < info->fWidth) {
  76. /* left justify */
  77. if(info->fLeft) {
  78. written = u_file_write(result, resultLen, output);
  79. for(i = 0; i < info->fWidth - resultLen; ++i) {
  80. written += u_file_write(&info->fPadChar, 1, output);
  81. }
  82. }
  83. /* right justify */
  84. else {
  85. written = 0;
  86. for(i = 0; i < info->fWidth - resultLen; ++i) {
  87. written += u_file_write(&info->fPadChar, 1, output);
  88. }
  89. written += u_file_write(result, resultLen, output);
  90. }
  91. }
  92. /* just write the formatted output */
  93. else {
  94. written = u_file_write(result, resultLen, output);
  95. }
  96. return written;
  97. }
  98. U_CAPI int32_t U_EXPORT2
  99. u_fprintf( UFILE *f,
  100. const char *patternSpecification,
  101. ... )
  102. {
  103. va_list ap;
  104. int32_t count;
  105. va_start(ap, patternSpecification);
  106. count = u_vfprintf(f, patternSpecification, ap);
  107. va_end(ap);
  108. return count;
  109. }
  110. U_CAPI int32_t U_EXPORT2
  111. u_printf(const char *patternSpecification,
  112. ...)
  113. {
  114. va_list ap;
  115. int32_t count;
  116. va_start(ap, patternSpecification);
  117. count = u_vfprintf(u_get_stdout(), patternSpecification, ap);
  118. va_end(ap);
  119. return count;
  120. }
  121. U_CAPI int32_t U_EXPORT2
  122. u_fprintf_u( UFILE *f,
  123. const char16_t *patternSpecification,
  124. ... )
  125. {
  126. va_list ap;
  127. int32_t count;
  128. va_start(ap, patternSpecification);
  129. count = u_vfprintf_u(f, patternSpecification, ap);
  130. va_end(ap);
  131. return count;
  132. }
  133. U_CAPI int32_t U_EXPORT2
  134. u_printf_u(const char16_t *patternSpecification,
  135. ...)
  136. {
  137. va_list ap;
  138. int32_t count;
  139. va_start(ap, patternSpecification);
  140. count = u_vfprintf_u(u_get_stdout(), patternSpecification, ap);
  141. va_end(ap);
  142. return count;
  143. }
  144. U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
  145. u_vfprintf( UFILE *f,
  146. const char *patternSpecification,
  147. va_list ap)
  148. {
  149. int32_t count;
  150. char16_t *pattern;
  151. char16_t buffer[UFMT_DEFAULT_BUFFER_SIZE];
  152. size_t size = strlen(patternSpecification) + 1;
  153. /* convert from the default codepage to Unicode */
  154. if (size >= MAX_UCHAR_BUFFER_SIZE(buffer)) {
  155. pattern = (char16_t *)uprv_malloc(size * sizeof(char16_t));
  156. if (pattern == nullptr) {
  157. return 0;
  158. }
  159. }
  160. else {
  161. pattern = buffer;
  162. }
  163. u_charsToUChars(patternSpecification, pattern, static_cast<int32_t>(size));
  164. /* do the work */
  165. count = u_vfprintf_u(f, pattern, ap);
  166. /* clean up */
  167. if (pattern != buffer) {
  168. uprv_free(pattern);
  169. }
  170. return count;
  171. }
  172. static const u_printf_stream_handler g_stream_handler = {
  173. u_printf_write,
  174. u_printf_pad_and_justify
  175. };
  176. U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
  177. u_vfprintf_u( UFILE *f,
  178. const char16_t *patternSpecification,
  179. va_list ap)
  180. {
  181. int32_t written = 0; /* haven't written anything yet */
  182. /* parse and print the whole format string */
  183. u_printf_parse(&g_stream_handler, patternSpecification, f, nullptr, &f->str.fBundle, &written, ap);
  184. /* return # of UChars written */
  185. return written;
  186. }
  187. #endif /* #if !UCONFIG_NO_FORMATTING */