#include <iostream>
#include <iomanip>
#include <locale>
#include <stdexcept>
#include <cstdlib>

/* Just a dinky little program to print out every character that's *
 * considered a valid printable letter in a locale given on the
 * command line.
 *
 * To compile:
 * g++ -O -o chartest chartest.cc
 *
 * To run:
 * % ./chartest
 * % ./chartest en_US
 * and so on.
*/

void show_printable(const std::locale& loc) {
  std::cout << "Using locale '" << loc.name() << "'\n";
  for (int c = 0; c < UCHAR_MAX + 1; c++) { // Hee hee hee
    if (std::use_facet<std::ctype<char> >(loc).is(std::ctype<char>::print, c)) {
      std::cout << std::right << std::setw(3) << c;
      std::cout << " is a printable character: " << static_cast<char>(c) << '\n';
    }
  }
}

int main(int argc, char **argv) {
  std::ios_base::sync_with_stdio(false);
  int n = 1;
  try {
    if (argc >= 2) {
      while (n < argc) {
	std::locale loc(argv[n]);
	show_printable(loc);
	n++;
      }
    } else
      show_printable(std::locale::classic());
  } catch (std::runtime_error&) {
    std::cerr << "Could not set the " << argv[n] << " locale.\n";
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}
