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