Blame src/local.c

Packit c32a2d
/*
Packit c32a2d
	local: some stuff for localisation
Packit c32a2d
Packit c32a2d
	Currently, this is just about determining if we got UTF-8 locale.
Packit c32a2d
Packit c32a2d
	copyright 2008 by the mpg123 project - free software under the terms of the LGPL 2.1
Packit c32a2d
	see COPYING and AUTHORS files in distribution or http://mpg123.org
Packit c32a2d
	initially written by Thomas Orgis, based on a patch by Thorsten Glaser.
Packit c32a2d
*/
Packit c32a2d
Packit c32a2d
#include "config.h"
Packit c32a2d
#ifdef HAVE_LOCALE_H
Packit c32a2d
#include <locale.h>
Packit c32a2d
#endif
Packit c32a2d
#ifdef HAVE_LANGINFO_H
Packit c32a2d
#include <langinfo.h>
Packit c32a2d
#endif
Packit c32a2d
#include "compat.h"
Packit c32a2d
#include "mpg123app.h"
Packit c32a2d
#include "debug.h"
Packit c32a2d
Packit c32a2d
int utf8env = 0;
Packit c32a2d
Packit c32a2d
/* Check some language variable for UTF-8-ness. */
Packit c32a2d
static int is_utf8(const char *lang);
Packit c32a2d
Packit c32a2d
void check_locale(void)
Packit c32a2d
{
Packit c32a2d
	if(param.force_utf8) utf8env = 1;
Packit c32a2d
	else
Packit c32a2d
	{
Packit c32a2d
		const char *cp;
Packit c32a2d
Packit c32a2d
		/* Check for env vars in proper oder. */
Packit c32a2d
		if((cp = getenv("LC_ALL")) == NULL && (cp = getenv("LC_CTYPE")) == NULL)
Packit c32a2d
		cp = getenv("LANG");
Packit c32a2d
Packit c32a2d
		if(is_utf8(cp)) utf8env = 1;
Packit c32a2d
	}
Packit c32a2d
Packit c32a2d
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
Packit c32a2d
	/* To query, we need to set from environment... */
Packit c32a2d
	if(!utf8env && is_utf8(setlocale(LC_CTYPE, ""))) utf8env = 1;
Packit c32a2d
#endif
Packit c32a2d
#if defined(HAVE_NL_LANGINFO) && defined(CODESET)
Packit c32a2d
	/* ...langinfo works after we set a locale, eh? So it makes sense after setlocale, if only. */
Packit c32a2d
	if(!utf8env && is_utf8(nl_langinfo(CODESET))) utf8env = 1;
Packit c32a2d
#endif
Packit c32a2d
Packit c32a2d
	debug1("UTF-8 locale: %i", utf8env);
Packit c32a2d
}
Packit c32a2d
Packit c32a2d
static int is_utf8(const char *lang)
Packit c32a2d
{
Packit c32a2d
	if(lang == NULL) return 0;
Packit c32a2d
Packit c32a2d
	/* Now, if the variable mentions UTF-8 anywhere, in some variation, the locale is UTF-8. */
Packit c32a2d
	if(   strstr(lang, "UTF-8") || strstr(lang, "utf-8")
Packit c32a2d
	   || strstr(lang, "UTF8")  || strstr(lang, "utf8")  )
Packit c32a2d
	return 1;
Packit c32a2d
	else
Packit c32a2d
	return 0;
Packit c32a2d
}