12e5b6d6dSopenharmony_ci// © 2017 and later: Unicode, Inc. and others.
22e5b6d6dSopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html
32e5b6d6dSopenharmony_ci/*
42e5b6d6dSopenharmony_ci**********************************************************************
52e5b6d6dSopenharmony_ci* Copyright (C) 1998-2009, International Business Machines Corporation
62e5b6d6dSopenharmony_ci* and others.  All Rights Reserved.
72e5b6d6dSopenharmony_ci**********************************************************************
82e5b6d6dSopenharmony_ci*
92e5b6d6dSopenharmony_ci* File uprint.cpp
102e5b6d6dSopenharmony_ci*
112e5b6d6dSopenharmony_ci* Modification History:
122e5b6d6dSopenharmony_ci*
132e5b6d6dSopenharmony_ci*	Date		Name		Description
142e5b6d6dSopenharmony_ci*	06/14/99	stephen 	Creation.
152e5b6d6dSopenharmony_ci*******************************************************************************
162e5b6d6dSopenharmony_ci*/
172e5b6d6dSopenharmony_ci
182e5b6d6dSopenharmony_ci#include "uprint.h"
192e5b6d6dSopenharmony_ci
202e5b6d6dSopenharmony_ci#include "unicode/ucnv.h"
212e5b6d6dSopenharmony_ci#include "unicode/ustring.h"
222e5b6d6dSopenharmony_ci
232e5b6d6dSopenharmony_ci#define BUF_SIZE 128
242e5b6d6dSopenharmony_ci
252e5b6d6dSopenharmony_ci/* Print a ustring to the specified FILE* in the default codepage */
262e5b6d6dSopenharmony_ciU_CAPI void
272e5b6d6dSopenharmony_ciuprint(const UChar *s,
282e5b6d6dSopenharmony_ci	   FILE *f,
292e5b6d6dSopenharmony_ci	   UErrorCode *status)
302e5b6d6dSopenharmony_ci{
312e5b6d6dSopenharmony_ci  /* converter */
322e5b6d6dSopenharmony_ci  UConverter *converter;
332e5b6d6dSopenharmony_ci  char buf [BUF_SIZE];
342e5b6d6dSopenharmony_ci  int32_t sourceLen;
352e5b6d6dSopenharmony_ci  const UChar *mySource;
362e5b6d6dSopenharmony_ci  const UChar *mySourceEnd;
372e5b6d6dSopenharmony_ci  char *myTarget;
382e5b6d6dSopenharmony_ci  int32_t arraySize;
392e5b6d6dSopenharmony_ci
402e5b6d6dSopenharmony_ci  if(s == 0) return;
412e5b6d6dSopenharmony_ci
422e5b6d6dSopenharmony_ci  /* set up the conversion parameters */
432e5b6d6dSopenharmony_ci  sourceLen    = u_strlen(s);
442e5b6d6dSopenharmony_ci  mySource	   = s;
452e5b6d6dSopenharmony_ci  mySourceEnd  = mySource + sourceLen;
462e5b6d6dSopenharmony_ci  myTarget	   = buf;
472e5b6d6dSopenharmony_ci  arraySize    = BUF_SIZE;
482e5b6d6dSopenharmony_ci
492e5b6d6dSopenharmony_ci  /* open a default converter */
502e5b6d6dSopenharmony_ci  converter = ucnv_open(0, status);
512e5b6d6dSopenharmony_ci
522e5b6d6dSopenharmony_ci  /* if we failed, clean up and exit */
532e5b6d6dSopenharmony_ci  if(U_FAILURE(*status)) goto finish;
542e5b6d6dSopenharmony_ci
552e5b6d6dSopenharmony_ci  /* perform the conversion */
562e5b6d6dSopenharmony_ci  do {
572e5b6d6dSopenharmony_ci	/* reset the error code */
582e5b6d6dSopenharmony_ci	*status = U_ZERO_ERROR;
592e5b6d6dSopenharmony_ci
602e5b6d6dSopenharmony_ci	/* perform the conversion */
612e5b6d6dSopenharmony_ci	ucnv_fromUnicode(converter, &myTarget,	myTarget + arraySize,
622e5b6d6dSopenharmony_ci			 &mySource, mySourceEnd, NULL,
632e5b6d6dSopenharmony_ci			 true, status);
642e5b6d6dSopenharmony_ci
652e5b6d6dSopenharmony_ci	/* Write the converted data to the FILE* */
662e5b6d6dSopenharmony_ci	fwrite(buf, sizeof(char), myTarget - buf, f);
672e5b6d6dSopenharmony_ci
682e5b6d6dSopenharmony_ci	/* update the conversion parameters*/
692e5b6d6dSopenharmony_ci	myTarget	 = buf;
702e5b6d6dSopenharmony_ci	arraySize	 = BUF_SIZE;
712e5b6d6dSopenharmony_ci  }
722e5b6d6dSopenharmony_ci  while(*status == U_BUFFER_OVERFLOW_ERROR);
732e5b6d6dSopenharmony_ci
742e5b6d6dSopenharmony_ci finish:
752e5b6d6dSopenharmony_ci
762e5b6d6dSopenharmony_ci  /* close the converter */
772e5b6d6dSopenharmony_ci  ucnv_close(converter);
782e5b6d6dSopenharmony_ci}
79