Skip to content

Commit

Permalink
rtlib: Add DISABLE_LANGINFO config option, to avoid the langinfo.h he…
Browse files Browse the repository at this point in the history
…ader.

When disabled, just use the default 'C' locale values for month names,
etc.  Setting this option config option actually makes zero difference
normally, because FB only calls setlocale(LC_CTYPE, ""), leaving
everything else set to the 'C' locale!

This option does not remove the setlocale(LC_CTYPE, "") call, because I
found that unnecessary on Android, but it could just as well.

I removed locks in unix fb_DrvIntlGetMonthName and
fb_DrvIntlGetWeekdayName because nl_langinfo is thread-safe (excluding
setlocale calls, which FB_LOCK does not protect against).
  • Loading branch information
rversteegen authored and jayrm committed Jan 2, 2024
1 parent ae0d5f3 commit 2c64639
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 12 deletions.
2 changes: 2 additions & 0 deletions makefile
Expand Up @@ -137,6 +137,8 @@
# -DDISABLE_FBDEV build without Linux framebuffer device headers (disables Linux fbdev gfx driver)
# -DDISABLE_D3D10 build without DirectX 10 driver(disable D2D driver in windows)
# -DDISABLE_NCURSES build without libtinfo or ncurses (disables console commands)
# -DDISABLE_LANGINFO build without locale info (affects Unix only; makes no difference unless you
# call setlocale() manually). Does not remove setlocale(LC_CTYPE, "") call.
#
# makefile variables may either be set on the make command line,
# or (in a more permanent way) inside a 'config.mk' file before
Expand Down
1 change: 1 addition & 0 deletions src/rtlib/init.c
Expand Up @@ -38,6 +38,7 @@ void fb_hRtInit( void )
* of the codepage <-> Unicode conversion functions, but not for
* example LC_NUMERIC, which would affect things like the decimal
* separator used by float <-> string conversion functions.
* Users should call setlocale() manually if they want localisation.
*
* Don't bother doing it under DJGPP - there we don't really support
* wstrings anyways, and the setlocale() reference increases .exe size.
Expand Down
9 changes: 9 additions & 0 deletions src/rtlib/unix/drv_intl_get.c
@@ -1,7 +1,9 @@
/* get i18n data */

#include "../fb.h"
#ifndef DISABLE_LANGINFO
#include <langinfo.h>
#endif

const char *fb_DrvIntlGet( eFbIntlIndex Index )
{
Expand All @@ -10,10 +12,17 @@ const char *fb_DrvIntlGet( eFbIntlIndex Index )
return "/";
case eFIL_TimeDivider:
return ":";
#ifdef DISABLE_LANGINFO
case eFIL_NumDecimalPoint:
return ".";
case eFIL_NumThousandsSeparator:
return ",";
#else
case eFIL_NumDecimalPoint:
return nl_langinfo( RADIXCHAR );
case eFIL_NumThousandsSeparator:
return nl_langinfo( THOUSEP );
#endif
}
return NULL;
}
6 changes: 6 additions & 0 deletions src/rtlib/unix/drv_intl_getdateformat.c
@@ -1,7 +1,9 @@
/* get localized short DATE format */

#include "../fb.h"
#ifndef DISABLE_LANGINFO
#include <langinfo.h>
#endif

int fb_DrvIntlGetDateFormat( char *buffer, size_t len )
{
Expand All @@ -10,7 +12,11 @@ int fb_DrvIntlGetDateFormat( char *buffer, size_t len )
char achAddBuffer[2] = { 0 };
const char *pszAdd = NULL;
size_t remaining = len - 1, add_len = 0;
#ifdef DISABLE_LANGINFO
const char *pszCurrent = "%m/%d/%y";
#else
const char *pszCurrent = nl_langinfo( D_FMT );
#endif

DBG_ASSERT(buffer!=NULL);

Expand Down
27 changes: 21 additions & 6 deletions src/rtlib/unix/drv_intl_getmonthname.c
@@ -1,31 +1,48 @@
/* get localized month name */

#include "../fb.h"
#ifndef DISABLE_LANGINFO
#include <langinfo.h>
#else
const char *month_names[] = {
"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"
};

const char *short_month_names[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
#endif

FBSTRING *fb_DrvIntlGetMonthName( int month, int short_names )
{
const char *pszName;
FBSTRING *result;
size_t name_len;
nl_item index;

if( month < 1 || month > 12 )
return NULL;

#ifdef DISABLE_LANGINFO
if( short_names ) {
pszName = short_month_names[month - 1];
} else {
pszName = month_names[month - 1];
}
#else
nl_item index;

if( short_names ) {
index = (nl_item) (ABMON_1 + month - 1);
} else {
index = (nl_item) (MON_1 + month - 1);
}

FB_LOCK();

pszName = nl_langinfo( index );
if( pszName==NULL ) {
FB_UNLOCK();
return NULL;
}
#endif

name_len = strlen( pszName );

Expand All @@ -34,7 +51,5 @@ FBSTRING *fb_DrvIntlGetMonthName( int month, int short_names )
FB_MEMCPY( result->data, pszName, name_len + 1 );
}

FB_UNLOCK();

return result;
}
6 changes: 6 additions & 0 deletions src/rtlib/unix/drv_intl_gettimeformat.c
@@ -1,7 +1,9 @@
/* get localized short TIME format */

#include "../fb.h"
#ifndef DISABLE_LANGINFO
#include <langinfo.h>
#endif

int fb_DrvIntlGetTimeFormat( char *buffer, size_t len )
{
Expand All @@ -10,7 +12,11 @@ int fb_DrvIntlGetTimeFormat( char *buffer, size_t len )
char achAddBuffer[2] = { 0 };
const char *pszAdd = NULL;
size_t remaining = len - 1, add_len = 0;
#ifdef DISABLE_LANGINFO
const char *pszCurrent = "%H:%M:%S";
#else
const char *pszCurrent = nl_langinfo( T_FMT );
#endif

DBG_ASSERT(buffer!=NULL);

Expand Down
26 changes: 20 additions & 6 deletions src/rtlib/unix/drv_intl_getweekdayname.c
@@ -1,31 +1,47 @@
/* get localized weekday name */

#include "../fb.h"
#ifndef DISABLE_LANGINFO
#include <langinfo.h>
#else
const char *weekday_names[] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};

const char *short_weekday_names[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
#endif

FBSTRING *fb_DrvIntlGetWeekdayName( int weekday, int short_names )
{
const char *pszName;
FBSTRING *result;
size_t name_len;
nl_item index;

if( weekday < 1 || weekday > 7 )
return NULL;

#ifdef DISABLE_LANGINFO
if( short_names ) {
pszName = short_weekday_names[weekday - 1];
} else {
pszName = weekday_names[weekday - 1];
}
#else
nl_item index;

if( short_names ) {
index = (nl_item) (ABDAY_1 + weekday - 1);
} else {
index = (nl_item) (DAY_1 + weekday - 1);
}

FB_LOCK();

pszName = nl_langinfo( index );
if( pszName==NULL ) {
FB_UNLOCK();
return NULL;
}
#endif

name_len = strlen( pszName );

Expand All @@ -34,7 +50,5 @@ FBSTRING *fb_DrvIntlGetWeekdayName( int weekday, int short_names )
FB_MEMCPY( result->data, pszName, name_len + 1 );
}

FB_UNLOCK();

return result;
}

0 comments on commit 2c64639

Please sign in to comment.