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