Don't crash keyword compilation on broken locales

Work around a weakness of older boosts by forcing C locale if
std::locale throws.

The real solution is to not have broken locales, but this should make it
slightly easier to get the JSON keywords compiled.
This commit is contained in:
Jørgen Kvalsvik
2016-12-05 12:57:59 +01:00
parent 237658825b
commit 5986393930

View File

@@ -16,13 +16,21 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined(WIN32)
#define _POSIX_C_SOURCE 200112L
#include <stdlib.h>
#endif
#include <iostream>
#include <locale>
#include <boost/filesystem.hpp>
#include <opm/parser/eclipse/Generator/KeywordGenerator.hpp>
#include <opm/parser/eclipse/Generator/KeywordLoader.hpp>
int main(int argc , char ** argv) {
if (argc == 7) {
const char * config_root = argv[1];
@@ -34,6 +42,33 @@ int main(int argc , char ** argv) {
bool verboseLoader = false;
bool verboseGenerator = true;
try {
/* sometimes the local env's locales are broken on POSIX. Boost <=
* 1.56 uses the std::locale("") constructor which respects user
* preferred locales, and might crash. If this is the case, and
* we're on a non-windows system (assuming POSIX), in case of an
* exception, set the environment to "C" and keep going.
*
* Can be removed once boost < 1.57 is no longer supported
*/
std::locale( "" );
} catch( const std::runtime_error& ) {
#if !defined(WIN32)
setenv( "LC_ALL", "C", 1 );
#endif
auto loc = boost::filesystem::path::imbue( std::locale::classic() );
boost::filesystem::path::imbue( loc );
std::cout << "User preferred locale setting is invalid "
<< "which breaks Boost <= 1.56 "
<< "- forcing to 'C' as workaround for Boost <= 1.56. "
<< "This workaround only applies to compile opm-parser, "
<< "but your locale settings seem BROKEN, "
<< "and opm-parser is likely NOT GOING TO WORK. "
<< "If you're on linux you can try setting the LANG "
<< "or LC_ALL environment variables to C or POSIX."
<< std::endl;
}
Opm::KeywordLoader loader(verboseLoader);
Opm::KeywordGenerator generator(verboseGenerator);
loader.loadMultipleKeywordDirectories( config_root );