• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (C) 1998-2009, International Business Machines Corporation
6 * and others.  All Rights Reserved.
7 **********************************************************************
8 *
9 * File uprint.cpp
10 *
11 * Modification History:
12 *
13 *	Date		Name		Description
14 *	06/14/99	stephen 	Creation.
15 *******************************************************************************
16 */
17 
18 #include "uprint.h"
19 
20 #include "unicode/ucnv.h"
21 #include "unicode/ustring.h"
22 
23 #define BUF_SIZE 128
24 
25 /* Print a ustring to the specified FILE* in the default codepage */
26 U_CAPI void
uprint(const UChar * s,FILE * f,UErrorCode * status)27 uprint(const UChar *s,
28 	   FILE *f,
29 	   UErrorCode *status)
30 {
31   /* converter */
32   UConverter *converter;
33   char buf [BUF_SIZE];
34   int32_t sourceLen;
35   const UChar *mySource;
36   const UChar *mySourceEnd;
37   char *myTarget;
38   int32_t arraySize;
39 
40   if(s == 0) return;
41 
42   /* set up the conversion parameters */
43   sourceLen    = u_strlen(s);
44   mySource	   = s;
45   mySourceEnd  = mySource + sourceLen;
46   myTarget	   = buf;
47   arraySize    = BUF_SIZE;
48 
49   /* open a default converter */
50   converter = ucnv_open(0, status);
51 
52   /* if we failed, clean up and exit */
53   if(U_FAILURE(*status)) goto finish;
54 
55   /* perform the conversion */
56   do {
57 	/* reset the error code */
58 	*status = U_ZERO_ERROR;
59 
60 	/* perform the conversion */
61 	ucnv_fromUnicode(converter, &myTarget,	myTarget + arraySize,
62 			 &mySource, mySourceEnd, NULL,
63 			 TRUE, status);
64 
65 	/* Write the converted data to the FILE* */
66 	fwrite(buf, sizeof(char), myTarget - buf, f);
67 
68 	/* update the conversion parameters*/
69 	myTarget	 = buf;
70 	arraySize	 = BUF_SIZE;
71   }
72   while(*status == U_BUFFER_OVERFLOW_ERROR);
73 
74  finish:
75 
76   /* close the converter */
77   ucnv_close(converter);
78 }
79