/**
 * Wrapper around SG_utf8__from_utf32 that allows empty strings.
 * I'm not sure why that function doesn't allow them, but I didn't want to change
 * something so low-level right now, so I'm wrapping it instead.  Will log something
 * to have it looked into later.
 */
static void _utf32_to_utf8(
    SG_context* pCtx,   //< [in] [out] Error and context info.
    SG_int32*   pUtf32, //< [in] UTF32 string to convert, may be empty but not NULL.
    char**      ppUtf8, //< [in] Converted UTF8 string.
    SG_uint32*  pLength //< [out] Length of converted UTF32 string, in bytes.
)
{
    char*     szUtf8  = NULL;
    SG_uint32 uLength = 0u;

    SG_NULLARGCHECK(pUtf32);
    SG_NULLARGCHECK(ppUtf8);

    if (pUtf32[0] == 0)
    {
        SG_alloc1(pCtx, szUtf8);
        szUtf8[0] = 0;
        uLength = 1u;
    }
    else
    {
        SG_ERR_CHECK(  SG_utf8__from_utf32(pCtx, pUtf32, &szUtf8, &uLength)  );
    }

    *ppUtf8 = szUtf8;
    szUtf8 = NULL;
    if (pLength != NULL)
    {
        *pLength = uLength;
    }

fail:
    SG_NULLFREE(pCtx, szUtf8);
    return;
}
Пример #2
0
int u0020_utf8pathnames__create_file(SG_context * pCtx, const SG_pathname * pPathnameTmpDir, _tableitem * pti)
{
	// create a file in the given tmp dir using the given filename.

	SG_pathname * pPathnameNewFile;
	char * pBufUtf8;
	SG_uint32 lenUtf8;
	SG_file * pFile;
	int iResult;
	SG_bool bTest;

	// convert the utf32 string into utf8.

	VERIFY_ERR_CHECK_DISCARD(  SG_utf8__from_utf32(pCtx, pti->pa32,&pBufUtf8,&lenUtf8)  );	// we have to free pBufUtf8

	// verify that the computed utf8 string matches what we thought it should be.
	// (this hopefully guards against the conversion layer playing NFC/NFD tricks.)

	iResult = SG_utf8__compare(pBufUtf8,(char *)pti->pa8);
	VERIFYP_COND("u0020_utf8pathnames",(iResult==0),("Compare failed [%s][%s]",pBufUtf8,pti->pa8));

	// create full pathname to the file to create.

	VERIFY_ERR_CHECK_DISCARD(  SG_PATHNAME__ALLOC__PATHNAME_SZ(pCtx,
															   &pPathnameNewFile,
															   pPathnameTmpDir,pBufUtf8)  );

	// create the file and close it.
	// on Linux when our locale is set to something other than UTF-8, we may
	// get an ICU(10) == U_INVALID_CHAR_FOUND error because the test data is
	// not necessarily friendly to any one locale and there are some NFD
	// cases too.   we map ICU(10) to SG_ERR_UNMAPPABLE_UNICODE_CHAR

	SG_file__open__pathname(pCtx,pPathnameNewFile,SG_FILE_WRONLY|SG_FILE_CREATE_NEW,0755,&pFile);
#if defined(LINUX)
	bTest = (  (!SG_context__has_err(pCtx))  ||  (SG_context__err_equals(pCtx,SG_ERR_UNMAPPABLE_UNICODE_CHAR))  );
#else
	bTest = (  (!SG_context__has_err(pCtx))  );
#endif
	SG_context__err_reset(pCtx);

	VERIFYP_COND("u0020_utf8pathnames",bTest,
			 ("Error Creating file [%s]",SG_pathname__sz(pPathnameNewFile)));

	VERIFY_ERR_CHECK_DISCARD(  SG_file__close(pCtx, &pFile)  );

	SG_PATHNAME_NULLFREE(pCtx, pPathnameNewFile);
	SG_NULLFREE(pCtx, pBufUtf8);

	return 1;
}