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