icu_ext.c
/*
 * Interface to uloc_getAvailable() for all locales.
 * Return a table of available locales with their main properties.
 */
Interface to uloc_getAvailable() for all locales.
Return a table of available locales with their main properties.
Interface to uloc_getAvailable () for all locales . Return a table of available locales with their main properties .
Datum
icu_locales_list(PG_FUNCTION_ARGS)
{
	ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
	MemoryContext per_query_ctx;
	MemoryContext oldcontext;
	TupleDesc	tupdesc;
	Tuplestorestate *tupstore;
	UErrorCode	status = U_ZERO_ERROR;
	int32_t loc_count = uloc_countAvailable();
	int32_t i;
	Datum values[7];
	bool nulls[7];
	if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
		ereport(ERROR,
				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),	
			 errmsg("set-valued function called in context that cannot accept a set")));
	per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
	oldcontext = MemoryContextSwitchTo(per_query_ctx);
	if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
		elog(ERROR, "return type must be a row type");
	tupstore = tuplestore_begin_heap(true, false, work_mem);
	rsinfo->returnMode = SFRM_Materialize;
	rsinfo->setResult = tupstore;
	rsinfo->setDesc = tupdesc;
	MemoryContextSwitchTo(oldcontext);
	for (i=0; i < loc_count; i++)
	{
		int col_num = 0;
		const char *p = uloc_getAvailable(i);
		col_num = add_string(p, col_num, values, nulls);
		{
			UChar country_buf[200];
			char* country;		
			uloc_getDisplayCountry(p, NULL , country_buf,
								   sizeof(country_buf), &status);
			if (U_FAILURE(status))
				elog(ERROR, "uloc_getDisplayCountry() failed on locale '%s': %s",
					 p, u_errorName(status));
			icu_from_uchar(&country, country_buf, u_strlen(country_buf));
			col_num = add_string(country, col_num, values, nulls);
		}
		col_num = add_string(uloc_getISO3Country(p), col_num, values, nulls);
		{
			UChar lang_buf[100];
			char* language;
			uloc_getDisplayLanguage(p, NULL, lang_buf, sizeof(lang_buf), &status);
			if (U_FAILURE(status))
				elog(ERROR, "uloc_getDisplayLanguage() failed on locale '%s': %s",
					 p, u_errorName(status));
			icu_from_uchar(&language, lang_buf, u_strlen(lang_buf));
			col_num = add_string(language, col_num, values, nulls);
		}
		col_num = add_string(uloc_getISO3Language(p), col_num, values, nulls);
		{
			UChar script_buf[100];
			char* script;
			uloc_getDisplayScript(p, NULL, script_buf, sizeof(script_buf), &status);
			if (U_FAILURE(status))
				elog(ERROR, "uloc_getDisplayScript() failed on locale '%s': %s",
					 p, u_errorName(status));
			icu_from_uchar(&script, script_buf, u_strlen(script_buf));
			col_num = add_string(script, col_num, values, nulls);
		}
		{
			const char* layout;
			ULayoutType t = uloc_getCharacterOrientation(p, &status);
			if (U_FAILURE(status))
				elog(ERROR, "uloc_getCharacterOrientation() failed on locale '%s': %s",
					 p, u_errorName(status));
			switch (t)
			{
			case ULOC_LAYOUT_LTR:
				layout = "LTR"; break;
			case ULOC_LAYOUT_RTL:
				layout = "RTL"; break;
			case ULOC_LAYOUT_TTB:
				layout = "TTB"; break;
			case ULOC_LAYOUT_BTT:
				layout = "BTT"; break;
			default:
				layout = ""; break;
			}
			col_num = add_string(layout, col_num, values, nulls);
		}
		tuplestore_putvalues(tupstore, tupdesc, values, nulls);
	}
	tuplestore_donestoring(tupstore);
	return (Datum) 0;
}
Datum icu_locales_list ( PG_FUNCTION_ARGS ) { ReturnSetInfo * rsinfo = ( ReturnSetInfo * ) fcinfo -> resultinfo ; MemoryContext per_query_ctx ; MemoryContext oldcontext ; TupleDesc tupdesc ; Tuplestorestate * tupstore ; UErrorCode status = U_ZERO_ERROR ; int32_t loc_count = uloc_countAvailable ( ) ; int32_t i ; Datum values [ 7 ] ; bool nulls [ 7 ] ; if ( rsinfo == NULL || ! IsA ( rsinfo , ReturnSetInfo ) ) ereport ( ERROR , ( errcode ( ERRCODE_FEATURE_NOT_SUPPORTED ) , errmsg ( " " ) ) ) ; per_query_ctx = rsinfo -> econtext -> ecxt_per_query_memory ; oldcontext = MemoryContextSwitchTo ( per_query_ctx ) ; if ( get_call_result_type ( fcinfo , NULL , & tupdesc ) != TYPEFUNC_COMPOSITE ) elog ( ERROR , " " ) ; tupstore = tuplestore_begin_heap ( true , false , work_mem ) ; rsinfo -> returnMode = SFRM_Materialize ; rsinfo -> setResult = tupstore ; rsinfo -> setDesc = tupdesc ; MemoryContextSwitchTo ( oldcontext ) ; for ( i = 0 ; i < loc_count ; i ++ ) { int col_num = 0 ; const char * p = uloc_getAvailable ( i ) ; col_num = add_string ( p , col_num , values , nulls ) ; { UChar country_buf [ 200 ] ; char * country ; uloc_getDisplayCountry ( p , NULL , country_buf , sizeof ( country_buf ) , & status ) ; if ( U_FAILURE ( status ) ) elog ( ERROR , " " , p , u_errorName ( status ) ) ; icu_from_uchar ( & country , country_buf , u_strlen ( country_buf ) ) ; col_num = add_string ( country , col_num , values , nulls ) ; } col_num = add_string ( uloc_getISO3Country ( p ) , col_num , values , nulls ) ; { UChar lang_buf [ 100 ] ; char * language ; uloc_getDisplayLanguage ( p , NULL , lang_buf , sizeof ( lang_buf ) , & status ) ; if ( U_FAILURE ( status ) ) elog ( ERROR , " " , p , u_errorName ( status ) ) ; icu_from_uchar ( & language , lang_buf , u_strlen ( lang_buf ) ) ; col_num = add_string ( language , col_num , values , nulls ) ; } col_num = add_string ( uloc_getISO3Language ( p ) , col_num , values , nulls ) ; { UChar script_buf [ 100 ] ; char * script ; uloc_getDisplayScript ( p , NULL , script_buf , sizeof ( script_buf ) , & status ) ; if ( U_FAILURE ( status ) ) elog ( ERROR , " " , p , u_errorName ( status ) ) ; icu_from_uchar ( & script , script_buf , u_strlen ( script_buf ) ) ; col_num = add_string ( script , col_num , values , nulls ) ; } { const char * layout ; ULayoutType t = uloc_getCharacterOrientation ( p , & status ) ; if ( U_FAILURE ( status ) ) elog ( ERROR , " " , p , u_errorName ( status ) ) ; switch ( t ) { case ULOC_LAYOUT_LTR : layout = " " ; break ; case ULOC_LAYOUT_RTL : layout = " " ; break ; case ULOC_LAYOUT_TTB : layout = " " ; break ; case ULOC_LAYOUT_BTT : layout = " " ; break ; default : layout = " " ; break ; } col_num = add_string ( layout , col_num , values , nulls ) ; } tuplestore_putvalues ( tupstore , tupdesc , values , nulls ) ; } tuplestore_donestoring ( tupstore ) ; return ( Datum ) 0 ; }
Interface to uloc_getAvailable() for all locales.
Interface to uloc_getAvailable () for all locales .
/* Switch into long-lived context to construct returned data structures */ /* Build a tuple descriptor for our result type */ /* Name */ /* Country */ /* with the database encoding */ /*ULOC_ENGLISH*/ /* Country code */ /* Language */ /* Language code */ /* Script */ /* Character orientation */