utf8-win32.cpp revision a595bebfc109f595e64e843f8faf9cfb97f35564
/* $Id$ */
/** @file
* innotek Portable Runtime - UTF8 helpers.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_UTF8
#include <Windows.h>
/**
* Allocates tmp buffer, translates pszString from UTF8 to current codepage.
*
* @returns iprt status code.
* @param ppszString Receives pointer of allocated native CP string.
* The returned pointer must be freed using RTStrFree().
* @param pszString UTF-8 string to convert.
*/
{
/*
* Check for zero length input string.
*/
if (!*pszString)
{
*ppszString = (char *)RTMemTmpAllocZ(sizeof(char));
if (*ppszString)
return VINF_SUCCESS;
return VERR_NO_TMP_MEMORY;
}
*ppszString = NULL;
/*
* Convert to wide char first.
*/
if (RT_FAILURE(rc))
return rc;
/*
* First calc result string length.
*/
if (cbResult > 0)
{
/*
* Alloc space for result buffer.
*/
if (lpString)
{
/*
* Do the translation.
*/
{
/* ok */
*ppszString = lpString;
return VINF_SUCCESS;
}
/* translation error */
int iLastErr = GetLastError();
}
else
}
else
{
/* translation error */
int iLastErr = GetLastError();
}
return rc;
}
/**
* Allocates tmp buffer, translates pszString from current codepage to UTF-8.
*
* @returns iprt status code.
* @param ppszString Receives pointer of allocated UTF-8 string.
* The returned pointer must be freed using RTStrFree().
* @param pszString Native string to convert.
*/
{
*ppszString = NULL;
/** @todo is there a quicker way? Currently: ACP -> UCS-2 -> UTF-8 */
if (cch <= 0)
{
/* zero length string passed. */
*ppszString = (char *)RTMemTmpAllocZ(sizeof(char));
if (*ppszString)
return VINF_SUCCESS;
return VERR_NO_TMP_MEMORY;
}
/*
* First calc result string length.
*/
int rc;
if (cuc > 0)
{
/*
* Alloc space for result buffer.
*/
if (pucszString)
{
/*
* Do the translation.
*/
{
/*
* Now we got UCS-2. Convert to UTF-8
*/
return rc;
}
/* translation error */
int iLastErr = GetLastError();
}
else
}
else
{
/* translation error */
int iLastErr = GetLastError();
}
return rc;
}