Пример #1
0
/**
 * Output the buffer onto the stream as text. The buffer is a UTF8-encoded array of chars.
 * It is converted to the appropriate platform encoding.
 *
 * @param[in] portLibrary The port library
 * @param[in] fd the file descriptor.
 * @param[in] buf buffer of text to be output.
 * @param[in] nbytes size of buffer of text to be output.
 *
 * @return 0 on success, negative error code on failure.
 */
IDATA VMCALL
hyfile_write_text (struct HyPortLibrary *portLibrary, IDATA fd,
                   const char *buf, IDATA nbytes)
{
  IDATA result, i;
  int requiresTranslation = 0;

  const char *utf8Encoding = "UTF-8";

  /* we can short circuit if the string is all ASCII */
  for (i = 0; i < nbytes; i++)
    {
      if ((U_8) buf[i] >= 0x80)
        {
          requiresTranslation = 1;
          break;
        }
    }

  if (!requiresTranslation
      || strcmp (nl_langinfo (CODESET), utf8Encoding) == 0)
    {
      /* We're in luck! No transformations are necessary */
      result =
        portLibrary->file_write (portLibrary, fd, (void *) buf, nbytes);
      return (result == nbytes) ? 0 : result;
    }

#if defined(HYVM_USE_WCTOMB)
  return file_write_using_wctomb (portLibrary, fd, buf, nbytes);
#else
  return file_write_using_iconv (portLibrary, fd, buf, nbytes);
#endif

}
Пример #2
0
intptr_t
omrfile_write_text(struct OMRPortLibrary *portLibrary, intptr_t fd, const char *buf, intptr_t nbytes)
{
	intptr_t result = 0;
	intptr_t i = 0;
	int requiresTranslation = 0;

#ifdef J9ZOS390
#pragma convlit(suspend)
#endif
	const char *utf8Encoding = "UTF-8";
#ifdef J9ZOS390
#pragma convlit(resume)
#endif

#if defined(J9ZOS390) || defined(OMRZTPF)
	/* z/OS and z/TPF always needs to translate to EBCDIC */
	requiresTranslation = 1;
#else
	/* we can short circuit if the string is all ASCII */
	for (i = 0; i < nbytes; i++) {
		if ((uint8_t)buf[i] >= 0x80) {
			requiresTranslation = 1;
			break;
		}
	}
#endif

	if (!requiresTranslation || strcmp(nl_langinfo(CODESET), utf8Encoding) == 0) {
		/* We're in luck! No transformations are necessary */
		result = portLibrary->file_write(portLibrary, fd, (void *)buf, nbytes);
		return (result == nbytes) ? 0 : result;
	}

#if defined(J9VM_USE_WCTOMB)
	return file_write_using_wctomb(portLibrary, fd, buf, nbytes);
#else
	return file_write_using_iconv(portLibrary, fd, buf, nbytes);
#endif
}