ampsci
c++ program for high-precision atomic structure calculations of single-valence systems
os.h
1 // Formatting library for C++ - optional OS-specific functionality
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7 
8 #ifndef FMT_OS_H_
9 #define FMT_OS_H_
10 
11 #ifdef __GNUC__
12 // Avoid tons of warnings with root code
13 #pragma GCC system_header
14 #endif
15 #ifdef __clang__
16 // Avoid tons of warnings with root code
17 #pragma clang system_header
18 #endif
19 
20 #include <cerrno>
21 #include <cstddef>
22 #include <cstdio>
23 #include <system_error> // std::system_error
24 
25 #if defined __APPLE__ || defined(__FreeBSD__)
26 #include <xlocale.h> // for LC_NUMERIC_MASK on OS X
27 #endif
28 
29 #include "format.h"
30 
31 #ifndef FMT_USE_FCNTL
32 // UWP doesn't provide _pipe.
33 #if FMT_HAS_INCLUDE("winapifamily.h")
34 #include <winapifamily.h>
35 #endif
36 #if (FMT_HAS_INCLUDE(<fcntl.h>) || defined(__APPLE__) || \
37  defined(__linux__)) && \
38  (!defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP))
39 #include <fcntl.h> // for O_RDONLY
40 #define FMT_USE_FCNTL 1
41 #else
42 #define FMT_USE_FCNTL 0
43 #endif
44 #endif
45 
46 #ifndef FMT_POSIX
47 #if defined(_WIN32) && !defined(__MINGW32__)
48 // Fix warnings about deprecated symbols.
49 #define FMT_POSIX(call) _##call
50 #else
51 #define FMT_POSIX(call) call
52 #endif
53 #endif
54 
55 // Calls to system functions are wrapped in FMT_SYSTEM for testability.
56 #ifdef FMT_SYSTEM
57 #define FMT_POSIX_CALL(call) FMT_SYSTEM(call)
58 #else
59 #define FMT_SYSTEM(call) ::call
60 #ifdef _WIN32
61 // Fix warnings about deprecated symbols.
62 #define FMT_POSIX_CALL(call) ::_##call
63 #else
64 #define FMT_POSIX_CALL(call) ::call
65 #endif
66 #endif
67 
68 // Retries the expression while it evaluates to error_result and errno
69 // equals to EINTR.
70 #ifndef _WIN32
71 #define FMT_RETRY_VAL(result, expression, error_result) \
72  do { \
73  (result) = (expression); \
74  } while ((result) == (error_result) && errno == EINTR)
75 #else
76 #define FMT_RETRY_VAL(result, expression, error_result) result = (expression)
77 #endif
78 
79 #define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1)
80 
81 FMT_BEGIN_NAMESPACE
82 FMT_MODULE_EXPORT_BEGIN
83 
109 template <typename Char> class basic_cstring_view {
110 private:
111  const Char *data_;
112 
113 public:
115  basic_cstring_view(const Char *s) : data_(s) {}
116 
122  basic_cstring_view(const std::basic_string<Char> &s) : data_(s.c_str()) {}
123 
125  const Char *c_str() const { return data_; }
126 };
127 
130 
131 #ifdef _WIN32
132 FMT_API const std::error_category &system_category() noexcept;
133 
134 FMT_BEGIN_DETAIL_NAMESPACE
135 // A converter from UTF-16 to UTF-8.
136 // It is only provided for Windows since other systems support UTF-8 natively.
137 class utf16_to_utf8 {
138 private:
139  memory_buffer buffer_;
140 
141 public:
142  utf16_to_utf8() {}
143  FMT_API explicit utf16_to_utf8(basic_string_view<wchar_t> s);
144  operator string_view() const { return string_view(&buffer_[0], size()); }
145  size_t size() const { return buffer_.size() - 1; }
146  const char *c_str() const { return &buffer_[0]; }
147  std::string str() const { return std::string(&buffer_[0], size()); }
148 
149  // Performs conversion returning a system error code instead of
150  // throwing exception on conversion error. This method may still throw
151  // in case of memory allocation error.
152  FMT_API int convert(basic_string_view<wchar_t> s);
153 };
154 
155 FMT_API void format_windows_error(buffer<char> &out, int error_code,
156  const char *message) noexcept;
157 FMT_END_DETAIL_NAMESPACE
158 
159 FMT_API std::system_error vwindows_error(int error_code, string_view format_str,
160  format_args args);
161 
190 template <typename... Args>
191 std::system_error windows_error(int error_code, string_view message,
192  const Args &...args) {
193  return vwindows_error(error_code, message, fmt::make_format_args(args...));
194 }
195 
196 // Reports a Windows error without throwing an exception.
197 // Can be used to report errors from destructors.
198 FMT_API void report_windows_error(int error_code, const char *message) noexcept;
199 #else
200 inline const std::error_category &system_category() noexcept {
201  return std::system_category();
202 }
203 #endif // _WIN32
204 
205 // std::system is not available on some platforms such as iOS (#2248).
206 #ifdef __OSX__
207 template <typename S, typename... Args, typename Char = char_t<S>>
208 void say(const S &format_str, Args &&...args) {
209  std::system(format("say \"{}\"", format(format_str, args...)).c_str());
210 }
211 #endif
212 
213 // A buffered file.
214 class buffered_file {
215 private:
216  FILE *file_;
217 
218  friend class file;
219 
220  explicit buffered_file(FILE *f) : file_(f) {}
221 
222 public:
223  buffered_file(const buffered_file &) = delete;
224  void operator=(const buffered_file &) = delete;
225 
226  // Constructs a buffered_file object which doesn't represent any file.
227  buffered_file() noexcept : file_(nullptr) {}
228 
229  // Destroys the object closing the file it represents if any.
230  FMT_API ~buffered_file() noexcept;
231 
232 public:
233  buffered_file(buffered_file &&other) noexcept : file_(other.file_) {
234  other.file_ = nullptr;
235  }
236 
237  buffered_file &operator=(buffered_file &&other) {
238  close();
239  file_ = other.file_;
240  other.file_ = nullptr;
241  return *this;
242  }
243 
244  // Opens a file.
245  FMT_API buffered_file(cstring_view filename, cstring_view mode);
246 
247  // Closes the file.
248  FMT_API void close();
249 
250  // Returns the pointer to a FILE object representing this file.
251  FILE *get() const noexcept { return file_; }
252 
253  FMT_API int descriptor() const;
254 
255  void vprint(string_view format_str, format_args args) {
256  fmt::vprint(file_, format_str, args);
257  }
258 
259  template <typename... Args>
260  inline void print(string_view format_str, const Args &...args) {
261  vprint(format_str, fmt::make_format_args(args...));
262  }
263 };
264 
265 #if FMT_USE_FCNTL
266 // A file. Closed file is represented by a file object with descriptor -1.
267 // Methods that are not declared with noexcept may throw
268 // fmt::system_error in case of failure. Note that some errors such as
269 // closing the file multiple times will cause a crash on Windows rather
270 // than an exception. You can get standard behavior by overriding the
271 // invalid parameter handler with _set_invalid_parameter_handler.
272 class FMT_API file {
273 private:
274  int fd_; // File descriptor.
275 
276  // Constructs a file object with a given descriptor.
277  explicit file(int fd) : fd_(fd) {}
278 
279 public:
280  // Possible values for the oflag argument to the constructor.
281  enum {
282  RDONLY = FMT_POSIX(O_RDONLY), // Open for reading only.
283  WRONLY = FMT_POSIX(O_WRONLY), // Open for writing only.
284  RDWR = FMT_POSIX(O_RDWR), // Open for reading and writing.
285  CREATE = FMT_POSIX(O_CREAT), // Create if the file doesn't exist.
286  APPEND = FMT_POSIX(O_APPEND), // Open in append mode.
287  TRUNC = FMT_POSIX(O_TRUNC) // Truncate the content of the file.
288  };
289 
290  // Constructs a file object which doesn't represent any file.
291  file() noexcept : fd_(-1) {}
292 
293  // Opens a file and constructs a file object representing this file.
294  file(cstring_view path, int oflag);
295 
296 public:
297  file(const file &) = delete;
298  void operator=(const file &) = delete;
299 
300  file(file &&other) noexcept : fd_(other.fd_) { other.fd_ = -1; }
301 
302  // Move assignment is not noexcept because close may throw.
303  file &operator=(file &&other) {
304  close();
305  fd_ = other.fd_;
306  other.fd_ = -1;
307  return *this;
308  }
309 
310  // Destroys the object closing the file it represents if any.
311  ~file() noexcept;
312 
313  // Returns the file descriptor.
314  int descriptor() const noexcept { return fd_; }
315 
316  // Closes the file.
317  void close();
318 
319  // Returns the file size. The size has signed type for consistency with
320  // stat::st_size.
321  long long size() const;
322 
323  // Attempts to read count bytes from the file into the specified buffer.
324  size_t read(void *buffer, size_t count);
325 
326  // Attempts to write count bytes from the specified buffer to the file.
327  size_t write(const void *buffer, size_t count);
328 
329  // Duplicates a file descriptor with the dup function and returns
330  // the duplicate as a file object.
331  static file dup(int fd);
332 
333  // Makes fd be the copy of this file descriptor, closing fd first if
334  // necessary.
335  void dup2(int fd);
336 
337  // Makes fd be the copy of this file descriptor, closing fd first if
338  // necessary.
339  void dup2(int fd, std::error_code &ec) noexcept;
340 
341  // Creates a pipe setting up read_end and write_end file objects for reading
342  // and writing respectively.
343  static void pipe(file &read_end, file &write_end);
344 
345  // Creates a buffered_file object associated with this file and detaches
346  // this file object from the file.
347  buffered_file fdopen(const char *mode);
348 };
349 
350 // Returns the memory page size.
351 long getpagesize();
352 
353 FMT_BEGIN_DETAIL_NAMESPACE
354 
355 struct buffer_size {
356  buffer_size() = default;
357  size_t value = 0;
358  buffer_size operator=(size_t val) const {
359  auto bs = buffer_size();
360  bs.value = val;
361  return bs;
362  }
363 };
364 
365 struct ostream_params {
366  int oflag = file::WRONLY | file::CREATE | file::TRUNC;
367  size_t buffer_size = BUFSIZ > 32768 ? BUFSIZ : 32768;
368 
369  ostream_params() {}
370 
371  template <typename... T>
372  ostream_params(T... params, int new_oflag) : ostream_params(params...) {
373  oflag = new_oflag;
374  }
375 
376  template <typename... T>
377  ostream_params(T... params, detail::buffer_size bs)
378  : ostream_params(params...) {
379  this->buffer_size = bs.value;
380  }
381 
382 // Intel has a bug that results in failure to deduce a constructor
383 // for empty parameter packs.
384 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 2000
385  ostream_params(int new_oflag) : oflag(new_oflag) {}
386  ostream_params(detail::buffer_size bs) : buffer_size(bs.value) {}
387 #endif
388 };
389 
390 class file_buffer final : public buffer<char> {
391  file file_;
392 
393  FMT_API void grow(size_t) override;
394 
395 public:
396  FMT_API file_buffer(cstring_view path, const ostream_params &params);
397  FMT_API file_buffer(file_buffer &&other);
398  FMT_API ~file_buffer();
399 
400  void flush() {
401  if (size() == 0)
402  return;
403  file_.write(data(), size() * sizeof(data()[0]));
404  clear();
405  }
406 
407  void close() {
408  flush();
409  file_.close();
410  }
411 };
412 
413 FMT_END_DETAIL_NAMESPACE
414 
415 // Added {} below to work around default constructor error known to
416 // occur in Xcode versions 7.2.1 and 8.2.1.
417 constexpr detail::buffer_size buffer_size{};
418 
420 class FMT_API ostream {
421 private:
422  FMT_MSC_WARNING(suppress : 4251)
423  detail::file_buffer buffer_;
424 
425  ostream(cstring_view path, const detail::ostream_params &params)
426  : buffer_(path, params) {}
427 
428 public:
429  ostream(ostream &&other) : buffer_(std::move(other.buffer_)) {}
430 
431  ~ostream();
432 
433  void flush() { buffer_.flush(); }
434 
435  template <typename... T>
436  friend ostream output_file(cstring_view path, T... params);
437 
438  void close() { buffer_.close(); }
439 
444  template <typename... T> void print(format_string<T...> fmt, T &&...args) {
445  vformat_to(detail::buffer_appender<char>(buffer_), fmt,
446  fmt::make_format_args(args...));
447  }
448 };
449 
465 template <typename... T>
466 inline ostream output_file(cstring_view path, T... params) {
467  return {path, detail::ostream_params(params...)};
468 }
469 #endif // FMT_USE_FCNTL
470 
471 FMT_MODULE_EXPORT_END
472 FMT_END_NAMESPACE
473 
474 #endif // FMT_OS_H_
Definition: os.h:109
basic_cstring_view(const std::basic_string< Char > &s)
Definition: os.h:122
const Char * c_str() const
Definition: os.h:125
basic_cstring_view(const Char *s)
Definition: os.h:115
Definition: core.h:1975
Definition: core.h:2888
Definition: format.h:907
Definition: core.h:435
void clear()
Definition: core.h:951
FMT_CONSTEXPR auto data() noexcept -> T *
Definition: core.h:945
virtual FMT_CONSTEXPR20 void grow(size_t capacity)=0
constexpr auto size() const noexcept -> size_t
Definition: core.h:939
double f(RaB r, PrincipalQN n, DiracQN k, Zeff z, AlphaFS a)
Upper radial component.
Definition: DiracHydrogen.cpp:71