cechow
cechow

Reputation: 170

Can we check if the locale output from boost::locale::generator::generate is invalid?

This is a 2-part question that began with me wondering: what happens if the ID input to boost::locale::generator::generate() was some invalid value?

I checked the documentation and I couldn't find anything on what happens if we passed in something like test instead of en_CA.UTF-8. I know that no exceptions are thrown but I would expect it to have some indication that the locale input was invalid.

I played around with this and realized that a problem appears later on when I attempt to use the generated locale with the collator compare function. An access violation exception is thrown.

Code Snippet Below:

#include <string>
#include "boost/locale.hpp"
#include "boost/locale/collator.hpp"

using namespace boost::locale;

void InitializeLocale( const std::string zLanguage, const std::string zCountry, std::locale & out_Locale )
{
    generator gen;
    gen.categories( collation_facet | formatting_facet | convert_facet );

    //out_Locale = gen( zLanguage + "_" + zCountry + ".UTF-8" );
    out_Locale = gen( "test" );

    // TODO: Check if out_Locale is valid??
    std::locale::global( out_Locale );
}

int main( int iNumArgs, char ** azArgs )
{
    std::locale currentLocale;
    InitializeLocale( "en", "CA", currentLocale );

    // Works
    std::cout << boost::locale::to_upper( "test", currentLocale ) << std::endl;
    // Throws exception: Exception thrown at 0x00007FF6FFFCB8C2 in TestProgram.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
    std::use_facet<boost::locale::collator<char>>( currentLocale ).compare( collator_base::secondary, "test", "TEST");
}

The second part of the question addresses my more immediate problem: is it possible to check if the locale generated is invalid or not?

I control exactly what locale ID is passed in right now but that will definitely change in the future.

Alternatively, perhaps I'm not understanding how boost::locale::collator should be used?

Upvotes: 3

Views: 408

Answers (0)

Related Questions