Browse Source

Merge branch 'master' of github.com:hrbrmstr/htmltidy

master
boB Rudis 6 years ago
parent
commit
4a02c49793
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 144
      src/language.c
  2. 6
      src/tidyplatform.h

144
src/language.c

@ -24,36 +24,54 @@
/** /**
* This structure type provides universal access to all of Tidy's strings. * This structure type provides universal access to all of Tidy's strings.
*/ */
// typedef struct {
// languageDefinition *currentLanguage;
// languageDefinition *fallbackLanguage;
// languageDefinition *languages[];
// } tidyLanguagesType;
typedef struct { typedef struct {
languageDefinition *currentLanguage; languageDefinition *currentLanguage;
languageDefinition *fallbackLanguage; languageDefinition *fallbackLanguage;
languageDefinition *languages[]; languageDefinition *languages[2];
} tidyLanguagesType; } tidyLanguagesType;
/** /**
* This single structure contains all localizations. Note that we preset * This single structure contains all localizations. Note that we preset
* `.currentLanguage` to language_en, which is Tidy's default language. * `.currentLanguage` to language_en, which is Tidy's default language.
*/ */
static tidyLanguagesType tidyLanguages = { static tidyLanguagesType tidyLanguages = {
&language_en, /* current language */ &language_en, /* current language */
&language_en, /* first fallback language */ &language_en, /* first fallback language */
{ {
/* Required localization! */ /* Required localization! */
&language_en, &language_en,
#if SUPPORT_LOCALIZATIONS NULL /* This array MUST be null terminated. */
/* These additional languages are installed. */ }
&language_en_gb,
&language_es,
&language_es_mx,
&language_zh_cn,
&language_fr,
#endif
NULL /* This array MUST be null terminated. */
}
}; };
// static tidyLanguagesType tidyLanguages = {
// &language_en, /* current language */
// &language_en, /* first fallback language */
// {
// /* Required localization! */
// &language_en,
// #if SUPPORT_LOCALIZATIONS
// /* These additional languages are installed. */
// &language_en_gb,
// &language_es,
// &language_es_mx,
// &language_zh_cn,
// &language_fr,
// #endif
// NULL /* This array MUST be null terminated. */
// }
// };
/** /**
* This structure maps old-fashioned Windows strings * This structure maps old-fashioned Windows strings
* to proper POSIX names (modern Windows already uses * to proper POSIX names (modern Windows already uses
@ -219,7 +237,7 @@ static const tidyLocaleMapItem localeMappings[] = {
{ "united-states", "en_us" }, { "united-states", "en_us" },
{ "us", "en_us" }, { "us", "en_us" },
{ "usa", "en_us" }, { "usa", "en_us" },
/* MUST be last. */ /* MUST be last. */
{ NULL, NULL } { NULL, NULL }
}; };
@ -499,7 +517,7 @@ ctmbstr TY_(tidyLocalizedString)( uint messageType, languageDefinition *definiti
int i; int i;
languageDictionary *dictionary = &definition->messages; languageDictionary *dictionary = &definition->messages;
uint pluralForm = definition->whichPluralForm(plural); uint pluralForm = definition->whichPluralForm(plural);
for (i = 0; (*dictionary)[i].value; ++i) for (i = 0; (*dictionary)[i].value; ++i)
{ {
if ( (*dictionary)[i].key == messageType && (*dictionary)[i].pluralForm == pluralForm ) if ( (*dictionary)[i].key == messageType && (*dictionary)[i].pluralForm == pluralForm )
@ -523,26 +541,26 @@ ctmbstr TY_(tidyLocalizedString)( uint messageType, languageDefinition *definiti
ctmbstr tidyLocalizedStringN( uint messageType, uint quantity ) ctmbstr tidyLocalizedStringN( uint messageType, uint quantity )
{ {
ctmbstr result; ctmbstr result;
result = TY_(tidyLocalizedString)( messageType, tidyLanguages.currentLanguage, quantity); result = TY_(tidyLocalizedString)( messageType, tidyLanguages.currentLanguage, quantity);
if (!result && tidyLanguages.fallbackLanguage ) if (!result && tidyLanguages.fallbackLanguage )
{ {
result = TY_(tidyLocalizedString)( messageType, tidyLanguages.fallbackLanguage, quantity); result = TY_(tidyLocalizedString)( messageType, tidyLanguages.fallbackLanguage, quantity);
} }
if (!result) if (!result)
{ {
/* Fallback to en which is built in. */ /* Fallback to en which is built in. */
result = TY_(tidyLocalizedString)( messageType, &language_en, quantity); result = TY_(tidyLocalizedString)( messageType, &language_en, quantity);
} }
if (!result) if (!result)
{ {
/* Last resort: Fallback to en singular which is built in. */ /* Last resort: Fallback to en singular which is built in. */
result = TY_(tidyLocalizedString)( messageType, &language_en, 1); result = TY_(tidyLocalizedString)( messageType, &language_en, 1);
} }
return result; return result;
} }
@ -572,21 +590,21 @@ ctmbstr tidyLocalizedString( uint messageType )
tmbstr tidySystemLocale(tmbstr result) tmbstr tidySystemLocale(tmbstr result)
{ {
ctmbstr temp; ctmbstr temp;
/* This should set the OS locale. */ /* This should set the OS locale. */
setlocale( LC_ALL, "" ); setlocale( LC_ALL, "" );
/* This should read the current locale. */ /* This should read the current locale. */
temp = setlocale( LC_ALL, NULL); temp = setlocale( LC_ALL, NULL);
/* Make a new copy of the string, because temp /* Make a new copy of the string, because temp
always points to the current locale. */ always points to the current locale. */
if (( result = malloc( strlen( temp ) + 1 ) )) if (( result = malloc( strlen( temp ) + 1 ) ))
strcpy(result, temp); strcpy(result, temp);
/* This should restore the C locale. */ /* This should restore the C locale. */
setlocale( LC_ALL, "C" ); setlocale( LC_ALL, "C" );
return result; return result;
} }
@ -603,7 +621,7 @@ tmbstr tidyNormalizedLocaleName( ctmbstr locale )
static char result[6] = "xx_yy"; static char result[6] = "xx_yy";
tmbstr search = strdup(locale); tmbstr search = strdup(locale);
search = TY_(tmbstrtolower)(search); search = TY_(tmbstrtolower)(search);
/* See if our string matches a Windows name. */ /* See if our string matches a Windows name. */
for (i = 0; localeMappings[i].winName; ++i) for (i = 0; localeMappings[i].winName; ++i)
{ {
@ -614,15 +632,15 @@ tmbstr tidyNormalizedLocaleName( ctmbstr locale )
break; break;
} }
} }
/* We're going to be stupid about this and trust the user, and /* We're going to be stupid about this and trust the user, and
return just the first two characters if they exist and the return just the first two characters if they exist and the
4th and 5th if they exist. The worst that can happen is a 4th and 5th if they exist. The worst that can happen is a
junk language that doesn't exist and won't be set. */ junk language that doesn't exist and won't be set. */
len = strlen( search ); len = strlen( search );
len = ( len <= 5 ? len : 5 ); len = ( len <= 5 ? len : 5 );
for ( i = 0; i < len; i++ ) for ( i = 0; i < len; i++ )
{ {
if ( i == 2 ) if ( i == 2 )
@ -641,7 +659,7 @@ tmbstr tidyNormalizedLocaleName( ctmbstr locale )
result[i] = tolower( search[i] ); result[i] = tolower( search[i] );
} }
} }
free( search ); free( search );
return result; return result;
} }
@ -657,17 +675,17 @@ languageDefinition *TY_(tidyTestLanguage)( ctmbstr languageCode )
languageDefinition *testLang; languageDefinition *testLang;
languageDictionary *testDict; languageDictionary *testDict;
ctmbstr testCode; ctmbstr testCode;
for (i = 0; tidyLanguages.languages[i]; ++i) for (i = 0; tidyLanguages.languages[i]; ++i)
{ {
testLang = tidyLanguages.languages[i]; testLang = tidyLanguages.languages[i];
testDict = &testLang->messages; testDict = &testLang->messages;
testCode = (*testDict)[0].value; testCode = (*testDict)[0].value;
if ( strcmp(testCode, languageCode) == 0 ) if ( strcmp(testCode, languageCode) == 0 )
return testLang; return testLang;
} }
return NULL; return NULL;
} }
@ -689,26 +707,26 @@ Bool tidySetLanguage( ctmbstr languageCode )
languageDefinition *dict2 = NULL; languageDefinition *dict2 = NULL;
tmbstr wantCode = NULL; tmbstr wantCode = NULL;
char lang[3] = ""; char lang[3] = "";
if ( !languageCode || !(wantCode = tidyNormalizedLocaleName( languageCode )) ) if ( !languageCode || !(wantCode = tidyNormalizedLocaleName( languageCode )) )
{ {
return no; return no;
} }
/* We want to use the specified language as the currentLanguage, and set /* We want to use the specified language as the currentLanguage, and set
fallback language as necessary. We have either a two or five digit code, fallback language as necessary. We have either a two or five digit code,
either or both of which might be installed. Let's test both of them: either or both of which might be installed. Let's test both of them:
*/ */
dict1 = TY_(tidyTestLanguage( wantCode )); /* WANTED language */ dict1 = TY_(tidyTestLanguage( wantCode )); /* WANTED language */
if ( strlen( wantCode ) > 2 ) if ( strlen( wantCode ) > 2 )
{ {
strncpy(lang, wantCode, 2); strncpy(lang, wantCode, 2);
lang[2] = '\0'; lang[2] = '\0';
dict2 = TY_(tidyTestLanguage( lang ) ); /* BACKUP language? */ dict2 = TY_(tidyTestLanguage( lang ) ); /* BACKUP language? */
} }
if ( dict1 && dict2 ) if ( dict1 && dict2 )
{ {
tidyLanguages.currentLanguage = dict1; tidyLanguages.currentLanguage = dict1;
@ -728,7 +746,7 @@ Bool tidySetLanguage( ctmbstr languageCode )
{ {
/* No change. */ /* No change. */
} }
return dict1 || dict2; return dict1 || dict2;
} }
@ -761,14 +779,14 @@ ctmbstr tidyDefaultString( uint messageType )
const uint TY_(tidyStringKeyListSize)() const uint TY_(tidyStringKeyListSize)()
{ {
static uint array_size = 0; static uint array_size = 0;
if ( array_size == 0 ) if ( array_size == 0 )
{ {
while ( language_en.messages[array_size].value != NULL ) { while ( language_en.messages[array_size].value != NULL ) {
array_size++; array_size++;
} }
} }
return array_size; return array_size;
} }
@ -795,15 +813,15 @@ uint getNextStringKey( TidyIterator* iter )
uint item = 0; uint item = 0;
size_t itemIndex; size_t itemIndex;
assert( iter != NULL ); assert( iter != NULL );
itemIndex = (size_t)*iter; itemIndex = (size_t)*iter;
if ( itemIndex > 0 && itemIndex <= TY_(tidyStringKeyListSize)() ) if ( itemIndex > 0 && itemIndex <= TY_(tidyStringKeyListSize)() )
{ {
item = language_en.messages[ itemIndex - 1 ].key; item = language_en.messages[ itemIndex - 1 ].key;
itemIndex++; itemIndex++;
} }
*iter = (TidyIterator)( itemIndex <= TY_(tidyStringKeyListSize)() ? itemIndex : (size_t)0 ); *iter = (TidyIterator)( itemIndex <= TY_(tidyStringKeyListSize)() ? itemIndex : (size_t)0 );
return item; return item;
} }
@ -816,14 +834,14 @@ uint getNextStringKey( TidyIterator* iter )
const uint TY_(tidyLanguageListSize)() const uint TY_(tidyLanguageListSize)()
{ {
static uint array_size = 0; static uint array_size = 0;
if ( array_size == 0 ) if ( array_size == 0 )
{ {
while ( localeMappings[array_size].winName ) { while ( localeMappings[array_size].winName ) {
array_size++; array_size++;
} }
} }
return array_size; return array_size;
} }
@ -846,15 +864,15 @@ const tidyLocaleMapItem *getNextWindowsLanguage( TidyIterator *iter )
const tidyLocaleMapItem *item = NULL; const tidyLocaleMapItem *item = NULL;
size_t itemIndex; size_t itemIndex;
assert( iter != NULL ); assert( iter != NULL );
itemIndex = (size_t)*iter; itemIndex = (size_t)*iter;
if ( itemIndex > 0 && itemIndex <= TY_(tidyLanguageListSize)() ) if ( itemIndex > 0 && itemIndex <= TY_(tidyLanguageListSize)() )
{ {
item = &localeMappings[ itemIndex -1 ]; item = &localeMappings[ itemIndex -1 ];
itemIndex++; itemIndex++;
} }
*iter = (TidyIterator)( itemIndex <= TY_(tidyLanguageListSize)() ? itemIndex : (size_t)0 ); *iter = (TidyIterator)( itemIndex <= TY_(tidyLanguageListSize)() ? itemIndex : (size_t)0 );
return item; return item;
} }
@ -866,14 +884,14 @@ const tidyLocaleMapItem *getNextWindowsLanguage( TidyIterator *iter )
const uint TY_(tidyInstalledLanguageListSize)() const uint TY_(tidyInstalledLanguageListSize)()
{ {
static uint array_size = 0; static uint array_size = 0;
if ( array_size == 0 ) if ( array_size == 0 )
{ {
while ( tidyLanguages.languages[array_size] ) { while ( tidyLanguages.languages[array_size] ) {
array_size++; array_size++;
} }
} }
return array_size; return array_size;
} }
@ -895,15 +913,15 @@ ctmbstr getNextInstalledLanguage( TidyIterator* iter )
ctmbstr item = NULL; ctmbstr item = NULL;
size_t itemIndex; size_t itemIndex;
assert( iter != NULL ); assert( iter != NULL );
itemIndex = (size_t)*iter; itemIndex = (size_t)*iter;
if ( itemIndex > 0 && itemIndex <= TY_(tidyInstalledLanguageListSize)() ) if ( itemIndex > 0 && itemIndex <= TY_(tidyInstalledLanguageListSize)() )
{ {
item = tidyLanguages.languages[itemIndex - 1]->messages[0].value; item = tidyLanguages.languages[itemIndex - 1]->messages[0].value;
itemIndex++; itemIndex++;
} }
*iter = (TidyIterator)( itemIndex <= TY_(tidyInstalledLanguageListSize)() ? itemIndex : (size_t)0 ); *iter = (TidyIterator)( itemIndex <= TY_(tidyInstalledLanguageListSize)() ? itemIndex : (size_t)0 );
return item; return item;
} }
@ -915,14 +933,14 @@ ctmbstr getNextInstalledLanguage( TidyIterator* iter )
const uint TY_(tidyErrorCodeListSize)() const uint TY_(tidyErrorCodeListSize)()
{ {
static uint array_size = 0; static uint array_size = 0;
if ( array_size == 0 ) if ( array_size == 0 )
{ {
while ( tidyErrorFilterKeysStruct[array_size].key ) { while ( tidyErrorFilterKeysStruct[array_size].key ) {
array_size++; array_size++;
} }
} }
return array_size; return array_size;
} }
@ -945,15 +963,15 @@ const tidyErrorFilterKeyItem *getNextErrorCode( TidyIterator* iter )
const tidyErrorFilterKeyItem *item = NULL; const tidyErrorFilterKeyItem *item = NULL;
size_t itemIndex; size_t itemIndex;
assert( iter != NULL ); assert( iter != NULL );
itemIndex = (size_t)*iter; itemIndex = (size_t)*iter;
if ( itemIndex > 0 && itemIndex <= TY_(tidyErrorCodeListSize)() ) if ( itemIndex > 0 && itemIndex <= TY_(tidyErrorCodeListSize)() )
{ {
item = &tidyErrorFilterKeysStruct[itemIndex - 1]; item = &tidyErrorFilterKeysStruct[itemIndex - 1];
itemIndex++; itemIndex++;
} }
*iter = (TidyIterator)( itemIndex <= TY_(tidyErrorCodeListSize)() ? itemIndex : (size_t)0 ); *iter = (TidyIterator)( itemIndex <= TY_(tidyErrorCodeListSize)() ? itemIndex : (size_t)0 );
return item; return item;
} }

6
src/tidyplatform.h

@ -61,9 +61,9 @@ extern "C" {
#endif #endif
/* Enable/disable support for additional languages */ /* Enable/disable support for additional languages */
#ifndef SUPPORT_LOCALIZATIONS // #ifndef SUPPORT_LOCALIZATIONS
#define SUPPORT_LOCALIZATIONS 1 // #define SUPPORT_LOCALIZATIONS 1
#endif // #endif
/* Convenience defines for Mac platforms */ /* Convenience defines for Mac platforms */

Loading…
Cancel
Save