Imported Upstream version 58.2
[platform/upstream/icu.git] / source / samples / cal / uprint.c
1 /*
2 ***********************************************************************
3 * Copyright (C) 2016 and later: Unicode, Inc. and others.
4 * License & terms of use: http://www.unicode.org/copyright.html#License
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 #include "unicode/ucnv.h"
22 #include "unicode/ustring.h"
23
24 #define BUF_SIZE 128
25
26 /* Print a ustring to the specified FILE* in the default codepage */
27 void
28 uprint(const UChar *s,
29        FILE *f,
30        UErrorCode *status)
31 {
32   /* converter */
33   UConverter *converter;
34   char buf [BUF_SIZE];
35   int32_t sourceLen;
36   const UChar *mySource;
37   const UChar *mySourceEnd;
38   char *myTarget;
39   int32_t arraySize;
40
41   if(s == 0) return;
42
43   /* set up the conversion parameters */
44   sourceLen    = u_strlen(s);
45   mySource     = s;
46   mySourceEnd  = mySource + sourceLen;
47   myTarget     = buf;
48   arraySize    = BUF_SIZE;
49
50   /* open a default converter */
51   converter = ucnv_open(0, status);
52   
53   /* if we failed, clean up and exit */
54   if(U_FAILURE(*status)) goto finish;
55   
56   /* perform the conversion */
57   do {
58     /* reset the error code */
59     *status = U_ZERO_ERROR;
60
61     /* perform the conversion */
62     ucnv_fromUnicode(converter, &myTarget, myTarget + arraySize,
63              &mySource, mySourceEnd, NULL,
64              TRUE, status);
65
66     /* Write the converted data to the FILE* */
67     fwrite(buf, sizeof(char), myTarget - buf, f);
68
69     /* update the conversion parameters*/
70     myTarget     = buf;
71     arraySize    = BUF_SIZE;
72   }
73   while(*status == U_BUFFER_OVERFLOW_ERROR); 
74
75  finish:
76   
77   /* close the converter */
78   ucnv_close(converter);
79 }