posix.h
Go to the documentation of this file.
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 #if FMT_GCC_VERSION >= 407
55 # define FMT_UNUSED __attribute__((unused))
56 #else
57 # define FMT_UNUSED
58 #endif
59 
60 #ifndef FMT_USE_STATIC_ASSERT
61 # define FMT_USE_STATIC_ASSERT 0
62 #endif
63 
64 #if FMT_USE_STATIC_ASSERT || FMT_HAS_FEATURE(cxx_static_assert) || \
65  (FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600
66 # define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message)
67 #else
68 # define FMT_CONCAT_(a, b) FMT_CONCAT(a, b)
69 # define FMT_STATIC_ASSERT(cond, message) \
70  typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED
71 #endif
72 
73 // Retries the expression while it evaluates to error_result and errno
74 // equals to EINTR.
75 #ifndef _WIN32
76 # define FMT_RETRY_VAL(result, expression, error_result) \
77  do { \
78  result = (expression); \
79  } while (result == error_result && errno == EINTR)
80 #else
81 # define FMT_RETRY_VAL(result, expression, error_result) result = (expression)
82 #endif
83 
84 #define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1)
85 
86 namespace fmt
87 {
88 
89 // An error code.
90 class ErrorCode
91 {
92 private:
93  int value_;
94 
95 public:
96 explicit ErrorCode(int value = 0) FMT_NOEXCEPT :
97  value_(value) {}
98 
99  int get() const FMT_NOEXCEPT
100  {
101  return value_;
102  }
103 };
104 
105 // A buffered file.
107 {
108 private:
109  FILE *file_;
110 
111  friend class File;
112 
113  explicit BufferedFile(FILE *f) : file_(f) {}
114 
115 public:
116  // Constructs a BufferedFile object which doesn't represent any file.
118  file_(0) {}
119 
120  // Destroys the object closing the file it represents if any.
122 
123 #if !FMT_USE_RVALUE_REFERENCES
124  // Emulate a move constructor and a move assignment operator if rvalue
125  // references are not supported.
126 
127 private:
128  // A proxy object to emulate a move constructor.
129  // It is private to make it impossible call operator Proxy directly.
130  struct Proxy
131  {
132  FILE *file;
133  };
134 
135 public:
136  // A "move constructor" for moving from a temporary.
138  file_(p.file) {}
139 
140  // A "move constructor" for moving from an lvalue.
142  file_(f.file_)
143  {
144  f.file_ = 0;
145  }
146 
147  // A "move assignment operator" for moving from a temporary.
149  {
150  close();
151  file_ = p.file;
152  return *this;
153  }
154 
155  // A "move assignment operator" for moving from an lvalue.
157  {
158  close();
159  file_ = other.file_;
160  other.file_ = 0;
161  return *this;
162  }
163 
164  // Returns a proxy object for moving from a temporary:
165  // BufferedFile file = BufferedFile(...);
166  operator Proxy() FMT_NOEXCEPT
167  {
168  Proxy p = {file_};
169  file_ = 0;
170  return p;
171  }
172 
173 #else
174 private:
176 
177 public:
179  file_(other.file_)
180  {
181  other.file_ = 0;
182  }
183 
184  BufferedFile& operator=(BufferedFile &&other)
185  {
186  close();
187  file_ = other.file_;
188  other.file_ = 0;
189  return *this;
190  }
191 #endif
192 
193  // Opens a file.
194  BufferedFile(CStringRef filename, CStringRef mode);
195 
196  // Closes the file.
197  void close();
198 
199  // Returns the pointer to a FILE object representing this file.
200  FILE *get() const FMT_NOEXCEPT
201  {
202  return file_;
203  }
204 
205  // We place parentheses around fileno to workaround a bug in some versions
206  // of MinGW that define fileno as a macro.
207  int (fileno)() const;
208 
209  void print(CStringRef format_str, const ArgList &args)
210  {
211  fmt::print(file_, format_str, args);
212  }
214 };
215 
216 // A file. Closed file is represented by a File object with descriptor -1.
217 // Methods that are not declared with FMT_NOEXCEPT may throw
218 // fmt::SystemError in case of failure. Note that some errors such as
219 // closing the file multiple times will cause a crash on Windows rather
220 // than an exception. You can get standard behavior by overriding the
221 // invalid parameter handler with _set_invalid_parameter_handler.
222 class File
223 {
224 private:
225  int fd_; // File descriptor.
226 
227  // Constructs a File object with a given descriptor.
228  explicit File(int fd) : fd_(fd) {}
229 
230 public:
231  // Possible values for the oflag argument to the constructor.
232  enum
233  {
234  RDONLY = FMT_POSIX(O_RDONLY), // Open for reading only.
235  WRONLY = FMT_POSIX(O_WRONLY), // Open for writing only.
236  RDWR = FMT_POSIX(O_RDWR) // Open for reading and writing.
237  };
238 
239  // Constructs a File object which doesn't represent any file.
241  fd_(-1) {}
242 
243  // Opens a file and constructs a File object representing this file.
244  File(CStringRef path, int oflag);
245 
246 #if !FMT_USE_RVALUE_REFERENCES
247  // Emulate a move constructor and a move assignment operator if rvalue
248  // references are not supported.
249 
250 private:
251  // A proxy object to emulate a move constructor.
252  // It is private to make it impossible call operator Proxy directly.
253  struct Proxy
254  {
255  int fd;
256  };
257 
258 public:
259  // A "move constructor" for moving from a temporary.
261  fd_(p.fd) {}
262 
263  // A "move constructor" for moving from an lvalue.
265  fd_(other.fd_)
266  {
267  other.fd_ = -1;
268  }
269 
270  // A "move assignment operator" for moving from a temporary.
272  {
273  close();
274  fd_ = p.fd;
275  return *this;
276  }
277 
278  // A "move assignment operator" for moving from an lvalue.
279  File &operator=(File &other)
280  {
281  close();
282  fd_ = other.fd_;
283  other.fd_ = -1;
284  return *this;
285  }
286 
287  // Returns a proxy object for moving from a temporary:
288  // File file = File(...);
289  operator Proxy() FMT_NOEXCEPT
290  {
291  Proxy p = {fd_};
292  fd_ = -1;
293  return p;
294  }
295 
296 #else
297 private:
299 
300 public:
301 File(File &&other) FMT_NOEXCEPT :
302  fd_(other.fd_)
303  {
304  other.fd_ = -1;
305  }
306 
307  File& operator=(File &&other)
308  {
309  close();
310  fd_ = other.fd_;
311  other.fd_ = -1;
312  return *this;
313  }
314 #endif
315 
316  // Destroys the object closing the file it represents if any.
317  ~File() FMT_NOEXCEPT;
318 
319  // Returns the file descriptor.
321  {
322  return fd_;
323  }
324 
325  // Closes the file.
326  void close();
327 
328  // Returns the file size. The size has signed type for consistency with
329  // stat::st_size.
330  LongLong size() const;
331 
332  // Attempts to read count bytes from the file into the specified buffer.
333  std::size_t read(void *buffer, std::size_t count);
334 
335  // Attempts to write count bytes from the specified buffer to the file.
336  std::size_t write(const void *buffer, std::size_t count);
337 
338  // Duplicates a file descriptor with the dup function and returns
339  // the duplicate as a file object.
340  static File dup(int fd);
341 
342  // Makes fd be the copy of this file descriptor, closing fd first if
343  // necessary.
344  void dup2(int fd);
345 
346  // Makes fd be the copy of this file descriptor, closing fd first if
347  // necessary.
348  void dup2(int fd, ErrorCode &ec) FMT_NOEXCEPT;
349 
350  // Creates a pipe setting up read_end and write_end file objects for reading
351  // and writing respectively.
352  static void pipe(File &read_end, File &write_end);
353 
354  // Creates a BufferedFile object associated with this file and detaches
355  // this File object from the file.
356  BufferedFile fdopen(const char *mode);
357 };
358 
359 // Returns the memory page size.
360 long getpagesize();
361 
362 #if (defined(LC_NUMERIC_MASK) || defined(_MSC_VER)) && \
363  !defined(__ANDROID__) && !defined(__CYGWIN__)
364 # define FMT_LOCALE
365 #endif
366 
367 #ifdef FMT_LOCALE
368 // A "C" numeric locale.
369 class Locale
370 {
371 private:
372 # ifdef _MSC_VER
373  typedef _locale_t locale_t;
374 
375  enum { LC_NUMERIC_MASK = LC_NUMERIC };
376 
377  static locale_t newlocale(int category_mask, const char *locale, locale_t)
378  {
379  return _create_locale(category_mask, locale);
380  }
381 
382  static void freelocale(locale_t locale)
383  {
384  _free_locale(locale);
385  }
386 
387  static double strtod_l(const char *nptr, char **endptr, _locale_t locale)
388  {
389  return _strtod_l(nptr, endptr, locale);
390  }
391 # endif
392 
393  locale_t locale_;
394 
396 
397 public:
398  typedef locale_t Type;
399 
400  Locale() : locale_(newlocale(LC_NUMERIC_MASK, "C", NULL))
401  {
402  if (!locale_)
403  FMT_THROW(fmt::SystemError(errno, "cannot create locale"));
404  }
405  ~Locale()
406  {
407  freelocale(locale_);
408  }
409 
410  Type get() const
411  {
412  return locale_;
413  }
414 
415  // Converts string to floating-point number and advances str past the end
416  // of the parsed input.
417  double strtod(const char *&str) const
418  {
419  char *end = 0;
420  double result = strtod_l(str, &end, locale_);
421  str = end;
422  return result;
423  }
424 };
425 #endif // FMT_LOCALE
426 } // namespace fmt
427 
428 #if !FMT_USE_RVALUE_REFERENCES
429 namespace std
430 {
431 // For compatibility with C++98.
433 {
434  return f;
435 }
437 {
438  return f;
439 }
440 }
441 #endif
442 
443 #endif // FMT_POSIX_H_
def write_end(s, spec)
BufferedFile(Proxy p) FMT_NOEXCEPT
Definition: posix.h:137
#define FMT_NOEXCEPT
Definition: format.h:190
int * count
#define FMT_VARIADIC(ReturnType, func,...)
Definition: format.h:4036
f
BufferedFile & operator=(Proxy p)
Definition: posix.h:148
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:873
BufferedFile(FILE *f)
Definition: posix.h:113
BufferedFile(BufferedFile &f) FMT_NOEXCEPT
Definition: posix.h:141
int value_
Definition: posix.h:93
File & operator=(Proxy p)
Definition: posix.h:271
#define FMT_THROW(x)
Definition: format.h:172
int descriptor() const FMT_NOEXCEPT
Definition: posix.h:320
bool write(ros_opcua_srvs::Write::Request &req, ros_opcua_srvs::Write::Response &res)
ErrorCode(int value=0) FMT_NOEXCEPT
Definition: posix.h:96
#define FMT_POSIX(call)
Definition: posix.h:37
File() FMT_NOEXCEPT
Definition: posix.h:240
File(Proxy p) FMT_NOEXCEPT
Definition: posix.h:260
bool read(ros_opcua_srvs::Read::Request &req, ros_opcua_srvs::Read::Response &res)
#define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: format.h:222
BufferedFile() FMT_NOEXCEPT
Definition: posix.h:117
FILE * file_
Definition: posix.h:109
File & operator=(File &other)
Definition: posix.h:279
long getpagesize()
Definition: posix.cc:227
File(int fd)
Definition: posix.h:228
Definition: format.cc:81
BufferedFile & operator=(BufferedFile &other)
Definition: posix.h:156
void print(CStringRef format_str, const ArgList &args)
Definition: posix.h:209
File(File &other) FMT_NOEXCEPT
Definition: posix.h:264
fmt::File & move(fmt::File &f)
Definition: posix.h:436
FMT_GCC_EXTENSION typedef long long LongLong
Definition: format.h:418
int fd_
Definition: posix.h:225


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:07