Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:12

0001 // Copyright 2021 Hans Dembinski
0002 //
0003 // Distributed under the Boost Software License, Version 1.0.
0004 // (See accompanying file LICENSE_1_0.txt
0005 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef BOOST_HISTOGRAM_DETAIL_TERM_INFO_HPP
0008 #define BOOST_HISTOGRAM_DETAIL_TERM_INFO_HPP
0009 
0010 #include <algorithm>
0011 
0012 #if defined __has_include
0013 #if __has_include(<sys/ioctl.h>) && __has_include(<unistd.h>)
0014 #include <sys/ioctl.h>
0015 #include <unistd.h>
0016 #endif
0017 #endif
0018 #include <boost/config.hpp>
0019 #include <cstdlib>
0020 #include <cstring>
0021 
0022 namespace boost {
0023 namespace histogram {
0024 namespace detail {
0025 
0026 namespace term_info {
0027 class env_t {
0028 public:
0029   env_t(const char* key) {
0030 #if defined(BOOST_MSVC) // msvc complains about using std::getenv
0031     _dupenv_s(&data_, &size_, key);
0032 #else
0033     data_ = std::getenv(key);
0034     if (data_) size_ = std::strlen(data_);
0035 #endif
0036   }
0037 
0038   ~env_t() {
0039 #if defined(BOOST_MSVC)
0040     std::free(data_);
0041 #endif
0042   }
0043 
0044   bool contains(const char* s) {
0045     const std::size_t n = std::strlen(s);
0046     if (size_ < n) return false;
0047     return std::strstr(data_, s);
0048   }
0049 
0050   operator bool() { return size_ > 0; }
0051 
0052   explicit operator int() { return size_ ? std::atoi(data_) : 0; }
0053 
0054   const char* data() const { return data_; }
0055 
0056 private:
0057   char* data_;
0058   std::size_t size_ = 0;
0059 };
0060 
0061 inline bool utf8() {
0062   // return false only if LANG exists and does not contain the string UTF
0063   env_t env("LANG");
0064   bool b = true;
0065   if (env) b = env.contains("UTF") || env.contains("utf");
0066   return b;
0067 }
0068 
0069 inline int width() {
0070   int w = 0;
0071 #if defined TIOCGWINSZ
0072   struct winsize ws;
0073   ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
0074   w = (std::max)(static_cast<int>(ws.ws_col), 0); // not sure if ws_col can be less than 0
0075 #endif
0076   env_t env("COLUMNS");
0077   const int col = (std::max)(static_cast<int>(env), 0);
0078   // if both t and w are set, COLUMNS may be used to restrict width
0079   return w == 0 ? col : (std::min)(col, w);
0080 }
0081 } // namespace term_info
0082 
0083 } // namespace detail
0084 } // namespace histogram
0085 } // namespace boost
0086 
0087 #endif