<commit_before>/*
* Copyright (c) 2003-2011 <NAME> <<EMAIL>>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/

/**
 * \file Linux-specific implementation of utf8conv.h
 */

#include "../typedefs.h"
#include "../utf8conv.h"
#include <cstdlib>
#include <clocale>
#include <cassert>
#include <cstring>
#include <string>

using namespace std;

class Startup {
public:
  Startup() {
    char *sl = setlocale(LC_ALL, "");
    if (sl == NULL)
      throw "Couldn't initialize locale - bailing out";
  }
};

static Startup startup;

size_t pws_os::wcstombs(char *dst, size_t maxdstlen,
                        const wchar_t *src, size_t , bool )
{
  return ::wcstombs(dst, src, maxdstlen) + 1;
}

size_t pws_os::mbstowcs(wchar_t *dst, size_t maxdstlen,<commit_msg>Linux: Try harder and fail more gracefully if we can't load the environment-specified locale<commit_after>/*
* Copyright (c) 2003-2011 <NAME> <<EMAIL>>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/

/**
 * \file Linux-specific implementation of utf8conv.h
 */

#include "../typedefs.h"
#include "../utf8conv.h"
#include <cstdlib>
#include <clocale>
#include <cassert>
#include <cstring>
#include <string>
#include <unistd.h>

using namespace std;

class Startup {
public:
  Startup() {
    // Sice this can fail before main(), we can't rely on cerr etc.
    // being intialized. At least give the user a clue...
    char *gl = setlocale(LC_ALL, NULL);
    char *sl = setlocale(LC_ALL, "");
    if (sl == NULL) {
      // Couldn't get environment-specified locale, warn user
      // and punt to default "C"
      char wrnmess[] = "Couldn't load locale, falling back to default\n";
      write(STDERR_FILENO, wrnmess, sizeof(wrnmess)/sizeof(*wrnmess)-1);
      sl = setlocale(LC_ALL, gl);
    }
    if (sl == NULL) {
      // If we can't get the default,we're really FUBARed
      char errmess[] = "Couldn't initialize locale - bailing out\n";
      write(STDERR_FILENO, errmess, sizeof(errmess)/sizeof(*errmess)-1);
      _exit(2);
    }
  }
};

static Startup startup;

size_t pws_os::wcstombs(char *dst, size_t maxdstlen,
                        const wchar_t *src, size_t , bool )
{
  return ::wcstombs(dst, src, maxdstlen) + 1;
}

size_t pws_os::mbstowcs(wchar_t *dst, size_t maxdstlen,
{'fraction_non_alphanumeric': 0.13295238095238096, 'fraction_numerical': 0.012571428571428572, 'mean_word_length': 3.4283305227655987, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2258419', 'n_tokens_mistral': 939, 'n_tokens_neox': 898, 'n_words': 273}