From cc023c26b7fd4f9e70c7b95422438348a16e3aaa Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Mon, 30 Oct 2017 07:53:35 -0400 Subject: [PATCH 1/2] Address #639. --- console/tidy.c | 12 ++++-------- include/tidy.h | 6 ------ src/language.c | 32 -------------------------------- src/tidylib.c | 5 ----- 4 files changed, 4 insertions(+), 51 deletions(-) diff --git a/console/tidy.c b/console/tidy.c index 93a6e85..354e583 100644 --- a/console/tidy.c +++ b/console/tidy.c @@ -2003,7 +2003,6 @@ int main( int argc, char** argv ) ctmbstr cfgfil = NULL, errfil = NULL, htmlfil = NULL; TidyDoc tdoc = NULL; int status = 0; - tmbstr locale = NULL; uint contentErrors = 0; uint contentWarnings = 0; @@ -2023,13 +2022,10 @@ int main( int argc, char** argv ) /* Set an atexit handler. */ atexit( tidy_cleanup ); - /*************************************/ - /* Set the locale for tidy's output. */ - /*************************************/ - locale = tidySystemLocale(locale); - tidySetLanguage(locale); - if ( locale ) - free( locale ); + /* Set the locale for tidy's output. This both configures LibTidy to + use the environment's locale as well as the standard library. + */ + tidySetLanguage( setlocale( LC_ALL, "") ); #if defined(_WIN32) /* Force Windows console to use UTF, otherwise many characters will diff --git a/include/tidy.h b/include/tidy.h index 37c020d..be6e604 100644 --- a/include/tidy.h +++ b/include/tidy.h @@ -1969,12 +1969,6 @@ TIDY_EXPORT uint TIDY_CALL getNextErrorCode( TidyIterator* iter ); ** @{ */ -/** Determines the current locale without affecting the C locale. - ** @param result The buffer to use to return the string, or NULL on failure. - ** @return The same buffer for convenience. - */ -TIDY_EXPORT tmbstr TIDY_CALL tidySystemLocale(tmbstr result); - /** Tells Tidy to use a different language for output. ** @param languageCode A Windows or POSIX language code, and must match ** a TIDY_LANGUAGE for an installed language. diff --git a/src/language.c b/src/language.c index 7d7f5bd..4ae6f3a 100644 --- a/src/language.c +++ b/src/language.c @@ -295,38 +295,6 @@ ctmbstr TY_(tidyLocalizedString)( uint messageType ) } -/** - ** Determines the current locale without affecting the C locale. - ** Tidy has always used the default C locale, and at this point - ** in its development we're not going to tamper with that. - ** @note this routine uses default allocator, see tidySetMallocCall. - ** @param result The buffer to use to return the string. - ** Returns NULL on failure. - ** @return The same buffer for convenience. - */ -tmbstr TY_(tidySystemLocale)(tmbstr result) -{ - ctmbstr temp; - TidyAllocator* allocator = &TY_(g_default_allocator); - - /* This should set the OS locale. */ - setlocale( LC_ALL, "" ); - - /* This should read the current locale. */ - temp = setlocale( LC_ALL, NULL); - - /* Make a new copy of the string, because temp - always points to the current locale. */ - if (( result = TidyAlloc( allocator, strlen( temp ) + 1 ) )) - strcpy(result, temp); - - /* This should restore the C locale. */ - setlocale( LC_ALL, "C" ); - - return result; -} - - /** * Retrieves the POSIX name for a string. Result is a static char so please * don't try to free it. If the name looks like a cc_ll identifier, we will diff --git a/src/tidylib.c b/src/tidylib.c index 2ffe620..a5464d8 100644 --- a/src/tidylib.c +++ b/src/tidylib.c @@ -2472,11 +2472,6 @@ uint TIDY_CALL getNextErrorCode( TidyIterator* iter ) *******************************************************************/ -tmbstr TIDY_CALL tidySystemLocale(tmbstr result) -{ - return TY_(tidySystemLocale)( result ); -} - Bool TIDY_CALL tidySetLanguage( ctmbstr languageCode ) { return TY_(tidySetLanguage)( languageCode ); From 57f623eb7c2a8311ce0c11cdfb2641b9e20beead Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Tue, 31 Oct 2017 17:41:32 -0400 Subject: [PATCH 2/2] Updated PR to make LIBRARY default to the environment language, rather than just console tidy. --- console/tidy.c | 5 ----- include/tidy.h | 7 ++++--- src/language.c | 22 ++++++++++++++++++++++ src/language.h | 20 ++++++++++++++++++++ src/tidylib.c | 21 ++++++++++++++++++++- 5 files changed, 66 insertions(+), 9 deletions(-) diff --git a/console/tidy.c b/console/tidy.c index 354e583..891a199 100644 --- a/console/tidy.c +++ b/console/tidy.c @@ -2022,11 +2022,6 @@ int main( int argc, char** argv ) /* Set an atexit handler. */ atexit( tidy_cleanup ); - /* Set the locale for tidy's output. This both configures LibTidy to - use the environment's locale as well as the standard library. - */ - tidySetLanguage( setlocale( LC_ALL, "") ); - #if defined(_WIN32) /* Force Windows console to use UTF, otherwise many characters will * be garbage. Note that East Asian languages *are* supported, but diff --git a/include/tidy.h b/include/tidy.h index be6e604..7e98e9b 100644 --- a/include/tidy.h +++ b/include/tidy.h @@ -336,9 +336,10 @@ TIDY_EXPORT Bool TIDY_CALL tidySetPanicCall( TidyPanic fpanic ); */ /** The primary creation of a document instance. Instances of a TidyDoc are used - ** throughout the API as a token to represent a particular document. When done - ** using a TidyDoc instance, be sure to `tidyRelease(myTidyDoc);` in order - ** to free related memory. + ** throughout the API as a token to represent a particular document. You must + ** create at least one TidyDoc instance to initialize the library and begin + ** interaction with the API. When done using a TidyDoc instance, be sure to + ** `tidyRelease(myTidyDoc);` in order to free related memory. ** @result Returns a TidyDoc instance. */ TIDY_EXPORT TidyDoc TIDY_CALL tidyCreate(void); diff --git a/src/language.c b/src/language.c index 4ae6f3a..d55938c 100644 --- a/src/language.c +++ b/src/language.c @@ -24,6 +24,7 @@ * This structure type provides universal access to all of Tidy's strings. */ typedef struct { + Bool manually_set; languageDefinition *currentLanguage; languageDefinition *fallbackLanguage; languageDefinition *languages[]; @@ -35,6 +36,7 @@ typedef struct { * `.currentLanguage` to language_en, which is Tidy's default language. */ static tidyLanguagesType tidyLanguages = { + no, /* library language was NOT manually set */ &language_en, /* current language */ &language_en, /* first fallback language */ { @@ -451,6 +453,26 @@ ctmbstr TY_(tidyGetLanguage)() } +/** + * Indicates whether or not the current language was set by a + * LibTidy user (yes) or internally by the library (no). + */ +Bool TY_(tidyGetLanguageSetByUser)() +{ + return tidyLanguages.manually_set; +} + + +/** + * Specifies to LibTidy that the user (rather than the library) + * selected the current language. + */ +void TY_(tidySetLanguageSetByUser)( void ) +{ + tidyLanguages.manually_set = yes; +} + + /** * Provides a string given `messageType` in the default * localization (which is `en`), for single plural form. diff --git a/src/language.h b/src/language.h index 12991eb..c8542b5 100644 --- a/src/language.h +++ b/src/language.h @@ -115,6 +115,26 @@ Bool TY_(tidySetLanguage)( ctmbstr languageCode ); */ ctmbstr TY_(tidyGetLanguage)(void); + +/** + * Indicates whether or not the current language was set by a + * LibTidy user or internally by the library. This flag prevents + * subsequently created instances of TidyDocument from changing the + * user's language. + * @returns Returns yes to indicate that the current language was + * specified by an API user. + */ +Bool TY_(tidyGetLanguageSetByUser)(void); + + +/** + * Specifies to LibTidy that the user (rather than the library) + * selected the current language. This flag prevents subsequently + * created instances of TidyDocument from changing the user's language. + */ +void TY_(tidySetLanguageSetByUser)( void ); + + /** * Provides a string given `messageType` in the current * localization for `quantity`. diff --git a/src/tidylib.c b/src/tidylib.c index a5464d8..e558c20 100644 --- a/src/tidylib.c +++ b/src/tidylib.c @@ -34,6 +34,9 @@ #include "mappedio.h" #include "language.h" #include "sprtf.h" +#if SUPPORT_LOCALIZATIONS +# include "locale.h" +#endif /* Create/Destroy a Tidy "document" object */ static TidyDocImpl* tidyDocCreate( TidyAllocator *allocator ); @@ -108,6 +111,17 @@ TidyDocImpl* tidyDocCreate( TidyAllocator *allocator ) TY_(InitConfig)( doc ); TY_(InitPrintBuf)( doc ); + /* Set the locale for tidy's output. This both configures + ** LibTidy to use the environment's locale as well as the + ** standard library. + */ +#if SUPPORT_LOCALIZATIONS + if ( TY_(tidyGetLanguageSetByUser)() == no ) + { + TY_(tidySetLanguage)( setlocale( LC_ALL, "") ); + } +#endif + /* By default, wire tidy messages to standard error. ** Document input will be set by parsing routines. ** Document output will be set by pretty print routines. @@ -2474,7 +2488,12 @@ uint TIDY_CALL getNextErrorCode( TidyIterator* iter ) Bool TIDY_CALL tidySetLanguage( ctmbstr languageCode ) { - return TY_(tidySetLanguage)( languageCode ); + Bool result = TY_(tidySetLanguage)( languageCode ); + + if ( result ) + TY_(tidySetLanguageSetByUser)(); + + return result; } ctmbstr TIDY_CALL tidyGetLanguage()