view dep/fmt/test/util.cc @ 347:a0aa8c8c4307

dep/anitomy: port to use UCS-4 rather than wide strings rationale: wide strings are not the same on every platform, and might not even be Unicode. (while they usually are, its possible that they are not) I was *going* to change StringToInt to use a string stream, but outputting to an integer doesn't seem to work at all with UCS-4, even though it ought to, so I just rolled my own that uses the arabic digits only.
author Paper <paper@paper.us.eu.org>
date Sun, 23 Jun 2024 10:32:09 -0400
parents 1faa72660932
children
line wrap: on
line source

// Formatting library for C++ - test utilities
//
// Copyright (c) 2012 - present, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.

#include "util.h"

#include <cstring>

const char* const file_content = "Don't panic!";

fmt::buffered_file open_buffered_file(FILE** fp) {
#if FMT_USE_FCNTL
  fmt::file read_end, write_end;
  fmt::file::pipe(read_end, write_end);
  write_end.write(file_content, std::strlen(file_content));
  write_end.close();
  fmt::buffered_file f = read_end.fdopen("r");
  if (fp) *fp = f.get();
#else
  fmt::buffered_file f("test-file", "w");
  fputs(file_content, f.get());
  if (fp) *fp = f.get();
#endif
  return f;
}

std::locale do_get_locale(const char* name) {
  try {
    return std::locale(name);
  } catch (const std::runtime_error&) {
  }
  return std::locale::classic();
}

std::locale get_locale(const char* name, const char* alt_name) {
  auto loc = do_get_locale(name);
  if (loc == std::locale::classic() && alt_name)
    loc = do_get_locale(alt_name);
#ifdef __OpenBSD__
    // Locales are not working in OpenBSD:
    // https://github.com/fmtlib/fmt/issues/3670.
    loc = std::locale::classic();
#endif
  if (loc == std::locale::classic())
    fmt::print(stderr, "{} locale is missing.\n", name);
  return loc;
}