bitz-server  2.0.0
posix.h
1 /*
2  A C++ interface to POSIX functions.
3 
4  Copyright (c) 2012 - 2016, Victor Zverovich
5  All rights reserved.
6 
7  For the license information refer to format.h.
8  */
9 
10 #ifndef FMT_POSIX_H_
11 #define FMT_POSIX_H_
12 
13 #if defined(__MINGW32__) || defined(__CYGWIN__)
14 // Workaround MinGW bug https://sourceforge.net/p/mingw/bugs/2024/.
15 #undef __STRICT_ANSI__
16 #endif
17 
18 #include <errno.h>
19 #include <fcntl.h> // for O_RDONLY
20 #include <locale.h> // for locale_t
21 #include <stdio.h>
22 #include <stdlib.h> // for strtod_l
23 
24 #include <cstddef>
25 
26 #if defined __APPLE__ || defined(__FreeBSD__)
27 #include <xlocale.h> // for LC_NUMERIC_MASK on OS X
28 #endif
29 
30 #include "format.h"
31 
32 #ifndef FMT_POSIX
33 #if defined(_WIN32) && !defined(__MINGW32__)
34 // Fix warnings about deprecated symbols.
35 #define FMT_POSIX(call) _##call
36 #else
37 #define FMT_POSIX(call) call
38 #endif
39 #endif
40 
41 // Calls to system functions are wrapped in FMT_SYSTEM for testability.
42 #ifdef FMT_SYSTEM
43 #define FMT_POSIX_CALL(call) FMT_SYSTEM(call)
44 #else
45 #define FMT_SYSTEM(call) call
46 #ifdef _WIN32
47 // Fix warnings about deprecated symbols.
48 #define FMT_POSIX_CALL(call) ::_##call
49 #else
50 #define FMT_POSIX_CALL(call) ::call
51 #endif
52 #endif
53 
54 // Retries the expression while it evaluates to error_result and errno
55 // equals to EINTR.
56 #ifndef _WIN32
57 #define FMT_RETRY_VAL(result, expression, error_result) \
58  do \
59  { \
60  result = (expression); \
61  } while (result == error_result && errno == EINTR)
62 #else
63 #define FMT_RETRY_VAL(result, expression, error_result) result = (expression)
64 #endif
65 
66 #define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1)
67 
68 namespace fmt {
69 
70 // An error code.
71 class ErrorCode
72 {
73 private:
74  int value_;
75 
76 public:
77  explicit ErrorCode(int value = 0) FMT_NOEXCEPT : value_(value) {}
78 
79  int get() const FMT_NOEXCEPT
80  {
81  return value_;
82  }
83 };
84 
85 // A buffered file.
87 {
88 private:
89  FILE *file_;
90 
91  friend class File;
92 
93  explicit BufferedFile(FILE *f)
94  : file_(f)
95  {
96  }
97 
98 public:
99  // Constructs a BufferedFile object which doesn't represent any file.
100  BufferedFile() FMT_NOEXCEPT : file_(FMT_NULL) {}
101 
102  // Destroys the object closing the file it represents if any.
103  FMT_API ~BufferedFile() FMT_NOEXCEPT;
104 
105 #if !FMT_USE_RVALUE_REFERENCES
106  // Emulate a move constructor and a move assignment operator if rvalue
107  // references are not supported.
108 
109 private:
110  // A proxy object to emulate a move constructor.
111  // It is private to make it impossible call operator Proxy directly.
112  struct Proxy
113  {
114  FILE *file;
115  };
116 
117 public:
118  // A "move constructor" for moving from a temporary.
119  BufferedFile(Proxy p) FMT_NOEXCEPT : file_(p.file) {}
120 
121  // A "move constructor" for moving from an lvalue.
122  BufferedFile(BufferedFile &f) FMT_NOEXCEPT : file_(f.file_)
123  {
124  f.file_ = FMT_NULL;
125  }
126 
127  // A "move assignment operator" for moving from a temporary.
128  BufferedFile &operator=(Proxy p)
129  {
130  close();
131  file_ = p.file;
132  return *this;
133  }
134 
135  // A "move assignment operator" for moving from an lvalue.
136  BufferedFile &operator=(BufferedFile &other)
137  {
138  close();
139  file_ = other.file_;
140  other.file_ = FMT_NULL;
141  return *this;
142  }
143 
144  // Returns a proxy object for moving from a temporary:
145  // BufferedFile file = BufferedFile(...);
146  operator Proxy() FMT_NOEXCEPT
147  {
148  Proxy p = {file_};
149  file_ = FMT_NULL;
150  return p;
151  }
152 
153 #else
154 private:
155  FMT_DISALLOW_COPY_AND_ASSIGN(BufferedFile);
156 
157 public:
158  BufferedFile(BufferedFile &&other) FMT_NOEXCEPT : file_(other.file_)
159  {
160  other.file_ = FMT_NULL;
161  }
162 
163  BufferedFile &operator=(BufferedFile &&other)
164  {
165  close();
166  file_ = other.file_;
167  other.file_ = FMT_NULL;
168  return *this;
169  }
170 #endif
171 
172  // Opens a file.
173  FMT_API BufferedFile(CStringRef filename, CStringRef mode);
174 
175  // Closes the file.
176  FMT_API void close();
177 
178  // Returns the pointer to a FILE object representing this file.
179  FILE *get() const FMT_NOEXCEPT
180  {
181  return file_;
182  }
183 
184  // We place parentheses around fileno to workaround a bug in some versions
185  // of MinGW that define fileno as a macro.
186  FMT_API int(fileno)() const;
187 
188  void print(CStringRef format_str, const ArgList &args)
189  {
190  fmt::print(file_, format_str, args);
191  }
192  FMT_VARIADIC(void, print, CStringRef)
193 };
194 
195 // A file. Closed file is represented by a File object with descriptor -1.
196 // Methods that are not declared with FMT_NOEXCEPT may throw
197 // fmt::SystemError in case of failure. Note that some errors such as
198 // closing the file multiple times will cause a crash on Windows rather
199 // than an exception. You can get standard behavior by overriding the
200 // invalid parameter handler with _set_invalid_parameter_handler.
201 class File
202 {
203 private:
204  int fd_; // File descriptor.
205 
206  // Constructs a File object with a given descriptor.
207  explicit File(int fd)
208  : fd_(fd)
209  {
210  }
211 
212 public:
213  // Possible values for the oflag argument to the constructor.
214  enum
215  {
216  RDONLY = FMT_POSIX(O_RDONLY), // Open for reading only.
217  WRONLY = FMT_POSIX(O_WRONLY), // Open for writing only.
218  RDWR = FMT_POSIX(O_RDWR) // Open for reading and writing.
219  };
220 
221  // Constructs a File object which doesn't represent any file.
222  File() FMT_NOEXCEPT : fd_(-1) {}
223 
224  // Opens a file and constructs a File object representing this file.
225  FMT_API File(CStringRef path, int oflag);
226 
227 #if !FMT_USE_RVALUE_REFERENCES
228  // Emulate a move constructor and a move assignment operator if rvalue
229  // references are not supported.
230 
231 private:
232  // A proxy object to emulate a move constructor.
233  // It is private to make it impossible call operator Proxy directly.
234  struct Proxy
235  {
236  int fd;
237  };
238 
239 public:
240  // A "move constructor" for moving from a temporary.
241  File(Proxy p) FMT_NOEXCEPT : fd_(p.fd) {}
242 
243  // A "move constructor" for moving from an lvalue.
244  File(File &other) FMT_NOEXCEPT : fd_(other.fd_)
245  {
246  other.fd_ = -1;
247  }
248 
249  // A "move assignment operator" for moving from a temporary.
250  File &operator=(Proxy p)
251  {
252  close();
253  fd_ = p.fd;
254  return *this;
255  }
256 
257  // A "move assignment operator" for moving from an lvalue.
258  File &operator=(File &other)
259  {
260  close();
261  fd_ = other.fd_;
262  other.fd_ = -1;
263  return *this;
264  }
265 
266  // Returns a proxy object for moving from a temporary:
267  // File file = File(...);
268  operator Proxy() FMT_NOEXCEPT
269  {
270  Proxy p = {fd_};
271  fd_ = -1;
272  return p;
273  }
274 
275 #else
276 private:
277  FMT_DISALLOW_COPY_AND_ASSIGN(File);
278 
279 public:
280  File(File &&other) FMT_NOEXCEPT : fd_(other.fd_)
281  {
282  other.fd_ = -1;
283  }
284 
285  File &operator=(File &&other)
286  {
287  close();
288  fd_ = other.fd_;
289  other.fd_ = -1;
290  return *this;
291  }
292 #endif
293 
294  // Destroys the object closing the file it represents if any.
295  FMT_API ~File() FMT_NOEXCEPT;
296 
297  // Returns the file descriptor.
298  int descriptor() const FMT_NOEXCEPT
299  {
300  return fd_;
301  }
302 
303  // Closes the file.
304  FMT_API void close();
305 
306  // Returns the file size. The size has signed type for consistency with
307  // stat::st_size.
308  FMT_API LongLong size() const;
309 
310  // Attempts to read count bytes from the file into the specified buffer.
311  FMT_API std::size_t read(void *buffer, std::size_t count);
312 
313  // Attempts to write count bytes from the specified buffer to the file.
314  FMT_API std::size_t write(const void *buffer, std::size_t count);
315 
316  // Duplicates a file descriptor with the dup function and returns
317  // the duplicate as a file object.
318  FMT_API static File dup(int fd);
319 
320  // Makes fd be the copy of this file descriptor, closing fd first if
321  // necessary.
322  FMT_API void dup2(int fd);
323 
324  // Makes fd be the copy of this file descriptor, closing fd first if
325  // necessary.
326  FMT_API void dup2(int fd, ErrorCode &ec) FMT_NOEXCEPT;
327 
328  // Creates a pipe setting up read_end and write_end file objects for reading
329  // and writing respectively.
330  FMT_API static void pipe(File &read_end, File &write_end);
331 
332  // Creates a BufferedFile object associated with this file and detaches
333  // this File object from the file.
334  FMT_API BufferedFile fdopen(const char *mode);
335 };
336 
337 // Returns the memory page size.
338 long getpagesize();
339 
340 #if (defined(LC_NUMERIC_MASK) || defined(_MSC_VER)) && !defined(__ANDROID__) && !defined(__CYGWIN__)
341 #define FMT_LOCALE
342 #endif
343 
344 #ifdef FMT_LOCALE
345 // A "C" numeric locale.
346 class Locale
347 {
348 private:
349 #ifdef _MSC_VER
350  typedef _locale_t locale_t;
351 
352  enum
353  {
354  LC_NUMERIC_MASK = LC_NUMERIC
355  };
356 
357  static locale_t newlocale(int category_mask, const char *locale, locale_t)
358  {
359  return _create_locale(category_mask, locale);
360  }
361 
362  static void freelocale(locale_t locale)
363  {
364  _free_locale(locale);
365  }
366 
367  static double strtod_l(const char *nptr, char **endptr, _locale_t locale)
368  {
369  return _strtod_l(nptr, endptr, locale);
370  }
371 #endif
372 
373  locale_t locale_;
374 
375  FMT_DISALLOW_COPY_AND_ASSIGN(Locale);
376 
377 public:
378  typedef locale_t Type;
379 
380  Locale()
381  : locale_(newlocale(LC_NUMERIC_MASK, "C", FMT_NULL))
382  {
383  if (!locale_)
384  FMT_THROW(fmt::SystemError(errno, "cannot create locale"));
385  }
386  ~Locale()
387  {
388  freelocale(locale_);
389  }
390 
391  Type get() const
392  {
393  return locale_;
394  }
395 
396  // Converts string to floating-point number and advances str past the end
397  // of the parsed input.
398  double strtod(const char *&str) const
399  {
400  char *end = FMT_NULL;
401  double result = strtod_l(str, &end, locale_);
402  str = end;
403  return result;
404  }
405 };
406 #endif // FMT_LOCALE
407 } // namespace fmt
408 
409 #if !FMT_USE_RVALUE_REFERENCES
410 namespace std {
411 // For compatibility with C++98.
412 inline fmt::BufferedFile &move(fmt::BufferedFile &f)
413 {
414  return f;
415 }
416 inline fmt::File &move(fmt::File &f)
417 {
418  return f;
419 }
420 } // namespace std
421 #endif
422 
423 #endif // FMT_POSIX_H_
Definition: format.h:1876
Definition: format.h:705
Definition: format.h:448
Definition: posix.h:71
Definition: posix.h:86
Definition: posix.h:201
Definition: format.cc:84
Definition: format.h:3053