iutest  1.17.99.14
iutest_port.hpp
[詳解]
1 //======================================================================
2 //-----------------------------------------------------------------------
13 //-----------------------------------------------------------------------
14 //======================================================================
15 #ifndef INCG_IRIS_IUTEST_PORT_HPP_7893F685_A1A9_477A_82E8_BF06237697FF_
16 #define INCG_IRIS_IUTEST_PORT_HPP_7893F685_A1A9_477A_82E8_BF06237697FF_
17 
18 //======================================================================
19 // include
20 #if defined(__MWERKS__)
21 # define _MSL_USING_NAMESPACE
22 #endif
23 
24 // IWYU pragma: begin_exports
25 #include "iutest_internal_defs.hpp"
26 
27 #if IUTEST_HAS_HDR_UNISTD
28 # include <unistd.h>
29 # include <locale.h>
30 #endif
31 
32 #if IUTEST_HAS_FILE_STAT
33 # include <sys/stat.h>
34 #endif
35 
36 // IWYU pragma: end_exports
37 
38 //======================================================================
39 // define
40 #if !defined(IUTEST_MAX_PATH)
41 # if defined(MAX_PATH) && MAX_PATH
42 # define IUTEST_MAX_PATH MAX_PATH
43 # elif defined(PATH_MAX) && PATH_MAX
44 # define IUTEST_MAX_PATH PATH_MAX
45 # elif defined(FILENAME_MAX) && FILENAME_MAX
46 # define IUTEST_MAX_PATH FILENAME_MAX
47 # else
48 # define IUTEST_MAX_PATH 1024
49 # endif
50 #endif
51 
55 #define IUTEST_LOG_(level) \
56  ::iutest::detail::IUTestLog( \
57  ::iutest::detail::IUTestLog::LOG_##level, __FILE__, __LINE__).GetStream()
58 
62 #define IUTEST_CHECK_(condition) \
63  IUTEST_AMBIGUOUS_ELSE_BLOCKER_ \
64  if( !::iutest::detail::IsTrue(condition) ) \
65  IUTEST_LOG_(FATAL) << "Condition " #condition " failed. "
66 
70 #if !defined(IUTEST_ABORT)
71 # define IUTEST_ABORT() ::iutest::internal::posix::Abort()
72 #endif
73 
74 namespace iutest {
75 
76 #ifdef IUTEST_OS_NACL
77 namespace nacl
78 {
79 
83 void vprint_message(const char *fmt, va_list va);
84 void print_message(const char *fmt, ...);
85 
86 }
87 #endif
88 
89 namespace internal {
90 namespace posix
91 {
92 
93 const char* GetEnv(const char* name);
94 int PutEnv(const char* expr);
95 int SetEnv(const char* name, const char* value, int overwrite);
96 
97 const char* GetCWD(char* buf, size_t length);
98 ::std::string GetCWD();
99 
100 void SleepMillisec(unsigned int millisec);
101 
102 IUTEST_ATTRIBUTE_NORETURN_ void Abort();
103 #if !defined(IUTEST_OS_WINDOWS_MOBILE)
104 inline void Abort() { abort(); }
105 #endif
106 
107 #if IUTEST_HAS_FOPEN
108 
109 inline FILE* FileOpen(const char* filename, const char* mode)
110 {
111 #if defined(_MSC_VER)
112 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_BEGIN()
113  return fopen(filename, mode);
114 IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_END()
115 #elif defined(_LARGEFILE64_SOURCE)
116  return fopen64(filename, mode);
117 #else
118  return fopen(filename, mode);
119 #endif
120 }
121 
122 inline int FileSeek(FILE* fp, iu_off_t pos, int origin)
123 {
124 #if defined(_MSC_VER)
125  return _fseeki64(fp, pos, origin);
126 #elif defined(_LARGEFILE64_SOURCE)
127  return fseeko64(fp, pos, origin);
128 #elif IUTEST_HAS_LARGEFILE_API
129  return fseeko(fp, pos, origin);
130 #else
131  return fseek(fp, pos, origin);
132 #endif
133 }
134 
135 inline iu_off_t FileTell(FILE* fp)
136 {
137 #if defined(_MSC_VER)
138  return static_cast<iu_off_t>(_ftelli64(fp));
139 #elif defined(_LARGEFILE64_SOURCE)
140  return static_cast<iu_off_t>(ftello64(fp));
141 #elif IUTEST_HAS_LARGEFILE_API
142  return static_cast<iu_off_t>(ftello(fp));
143 #else
144  return static_cast<iu_off_t>(ftell(fp));
145 #endif
146 }
147 
148 #endif
149 
150 #if IUTEST_HAS_HDR_UNISTD
151 
152 #if defined(_MSC_VER) || defined(IUTEST_OS_WINDOWS_MINGW)
153 inline int FdClose(int fd) { return _close(fd); }
154 inline int FdFlush(int fd) { return _commit(fd); }
155 #else
156 inline int FdClose(int fd) { return close(fd); }
157 inline int FdFlush(int fd) { return fsync(fd); }
158 #endif
159 #else
160 inline int FdClose(int) { return -1; }
161 inline int FdFlush(int) { return -1; }
162 #endif
163 
164 #if IUTEST_HAS_FD_OPEN
165 
166 #if defined(_MSC_VER) || defined(__MINGW64__)
167 inline FILE* FdOpen(int fd, const char* mode) { return _fdopen(fd, mode); }
168 #else
169 inline FILE* FdOpen(int fd, const char* mode) { return fdopen(fd, mode); }
170 #endif
171 
172 #else
173 
174 inline FILE* FdOpen(int, const char*) { return IUTEST_NULLPTR; }
175 
176 #endif
177 
178 
179 #if IUTEST_HAS_FD_DUP
180 
181 #if defined(_MSC_VER)
182 inline int Dup(int fd) { return _dup(fd); }
183 #else
184 inline int Dup(int fd) { return dup(fd); }
185 #endif
186 
187 #if defined(_MSC_VER)
188 inline int Dup2(int fd1, int fd2) { return _dup2(fd1, fd2); }
189 #else
190 inline int Dup2(int fd1, int fd2) { return dup2(fd1, fd2); }
191 #endif
192 
193 #else
194 inline int Dup(int) { return -1; }
195 inline int Dup2(int, int) { return -1; }
196 #endif
197 
198 #if IUTEST_HAS_FILENO
199 
200 #if defined(_MSC_VER)
201 inline int Fileno(FILE* fp) { return _fileno(fp); }
202 #else
203 inline int Fileno(FILE* fp) { return fileno(fp); }
204 #endif
205 
206 #else
207 
208 inline int Fileno(FILE*) { return -1; }
209 
210 #endif
211 
212 #if IUTEST_HAS_FILE_STAT
213 
214 #if defined(IUTEST_OS_WINDOWS) && !defined(IUTEST_OS_WINDOWS_WINE)
215 
216 typedef struct __stat64 StatStruct;
217 
218 inline int FileStat(int fd, StatStruct* buf) { return _fstat64(fd, buf); }
219 inline int Stat(const char* path, StatStruct* buf) { return _stat64(path, buf); }
220 inline bool IsDir(const StatStruct& st) { return (st.st_mode & _S_IFDIR) != 0; }
221 
222 #else
223 
224 #if defined(_LARGEFILE64_SOURCE)
225 
226 typedef struct stat64 StatStruct;
227 
228 inline int FileStat(int fd, StatStruct* buf) { return fstat64(fd, buf); }
229 inline int Stat(const char* path, StatStruct* buf) { return stat64(path, buf); }
230 
231 #else
232 
233 typedef struct stat StatStruct;
234 
235 inline int FileStat(int fd, StatStruct* buf) { return fstat(fd, buf); }
236 inline int Stat(const char* path, StatStruct* buf) { return stat(path, buf); }
237 
238 #endif
239 
240 inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
241 
242 #endif
243 
244 inline int Stat(FILE* fp, StatStruct* buf)
245 {
246  int fd = Fileno(fp);
247  return fd >= 0 ? FileStat(fd, buf) : fd;
248 }
249 
250 #endif
251 
252 #if IUTEST_HAS_FOPEN
253 
254 inline iu_uint_max_t FileSizeBySeekSet(FILE* fp)
255 {
256  if( fp == NULL )
257  {
258  return 0;
259  }
260  const iu_off_t pre = FileTell(fp);
261  if( (pre != -1) && (FileSeek(fp, 0, SEEK_END) == 0) )
262  {
263  const iu_off_t size = FileTell(fp);
264  IUTEST_UNUSED_RETURN(FileSeek(fp, pre, SEEK_SET));
265  return static_cast<iu_uint_max_t>(size);
266  }
267  return 0;
268 }
269 
270 inline iu_uint_max_t FileSize(FILE* fp)
271 {
272  if( fp == NULL )
273  {
274  return 0;
275  }
276 #if IUTEST_HAS_FILE_STAT
277  StatStruct st;
278  if (Stat(fp, &st) == 0)
279  {
280  return static_cast<iu_uint_max_t>(st.st_size);
281  }
282 #endif
283  return FileSizeBySeekSet(fp);
284 }
285 
286 #endif
287 
288 inline iu_uint_max_t FdSize(int fd)
289 {
290  if( fd == -1 )
291  {
292  return 0;
293  }
294 #if IUTEST_HAS_FILE_STAT
295  StatStruct st;
296  if (FileStat(fd, &st) == 0)
297  {
298  return static_cast<iu_uint_max_t>(st.st_size);
299  }
300 #endif
301  return 0;
302 }
303 
304 inline iu_uint_max_t FileSizeFromPath(const char* filename)
305 {
306 #if IUTEST_HAS_STD_FILESYSTEM
307  return ::std::filesystem::file_size(filename);
308 #else
309 
310 #if IUTEST_HAS_FILE_STAT
311  StatStruct st;
312  if (Stat(filename, &st) == 0)
313  {
314  return static_cast<iu_uint_max_t>(st.st_size);
315  }
316 #endif
317  return 0;
318 
319 #endif
320 }
321 
322 #if IUTEST_HAS_MKSTEMP
323 
324 #if defined(_MSC_VER)
325 inline int Mkstemp(char* template_path) { return _mkstemp(template_path); }
326 #else
327 inline int Mkstemp(char* template_path) { return mkstemp(template_path); }
328 #endif
329 
330 #else
331 inline int Mkstemp(char*) { return -1; }
332 #endif
333 
334 } // end of namespace posix
335 
336 inline void SleepMilliseconds(int n) { posix::SleepMillisec(static_cast<unsigned int>(n)); }
337 
338 } // end of namespace internal
339 
340 namespace detail
341 {
342 
343 namespace posix = internal::posix;
344 
348 char GetPathSeparator() IUTEST_CXX_NOEXCEPT_SPEC;
349 
353 bool IsPathSeparator(char c) IUTEST_CXX_NOEXCEPT_SPEC;
354 
358 bool IsAltPathSeparator(char c) IUTEST_CXX_NOEXCEPT_SPEC;
359 
363 const char* FindLastPathSeparator(const char* path, size_t length) IUTEST_CXX_NOEXCEPT_SPEC;
364 
368 size_t FindLastPathSeparatorPosition(const char* path, size_t length) IUTEST_CXX_NOEXCEPT_SPEC;
369 
373 bool SetEnvironmentVariable(const char* name, const char* value);
374 
375 
382 bool GetEnvironmentVariable(const char* name, char* buf, size_t size);
383 
384 #if !defined(IUTEST_NO_FUNCTION_TEMPLATE_ORDERING)
385 
386 template<size_t SIZE>
387 inline bool GetEnvironmentVariable(const char* name, char (&buf)[SIZE])
388 {
389 return GetEnvironmentVariable(name, buf, SIZE);
390 }
391 
392 #endif
393 
400 bool IUTEST_ATTRIBUTE_UNUSED_ GetEnvironmentVariable(const char* name, ::std::string& var);
401 
408 bool IUTEST_ATTRIBUTE_UNUSED_ GetEnvironmentInt(const char* name, int& var);
409 
410 #if defined(IUTEST_OS_WINDOWS)
411 namespace win
412 {
413 
417  ::std::string IUTEST_ATTRIBUTE_UNUSED_ WideStringToMultiByteString(const wchar_t* wide_c_str);
418 
424  ::std::string IUTEST_ATTRIBUTE_UNUSED_ GetHResultString(HRESULT hr);
425 
426 } // end of namespace win
427 #endif
428 
432 class IUTestLog
433 {
434 public:
435  enum Level
436  {
437  LOG_INFO
438  , LOG_WARNING
439  , LOG_ERROR
440  , LOG_FATAL
441  , LOG_LEVEL_NUM
442  };
443 public:
444  IUTestLog(Level level, const char* file, int line);
445 
446  ~IUTestLog();
447 
448 public:
449  iu_stringstream& GetStream() { return m_stream; }
450 
451 public:
452  static int GetCount(Level level) { return GetCountTable().count[level]; }
453  static bool HasWarning() { return GetCount(LOG_WARNING) > 0; }
454  static bool HasError() { return GetCount(LOG_ERROR) > 0 || GetCount(LOG_FATAL) > 0; }
455 
456 private:
457  struct Count
458  {
459  int count[LOG_LEVEL_NUM];
460  };
461 
462  static Count& GetCountTable() { static Count count = { {0} }; return count; }
463  static void CountUp(int level);
464 
465 private:
466  const Level kLevel;
467  iu_stringstream m_stream;
468 
470 };
471 
472 #if IUTEST_HAS_STREAM_BUFFER
473 
474 #if defined(BUFSIZ) && BUFSIZ > 1024
475 # define IUTEST_DEFAULT_STREAM_BUFFER_SIZE BUFSIZ
476 #else
477 # define IUTEST_DEFAULT_STREAM_BUFFER_SIZE 1024
478 #endif
479 
483 template<int SIZE=IUTEST_DEFAULT_STREAM_BUFFER_SIZE>
484 class IUStreamBuffer
485 {
486 public:
487  explicit IUStreamBuffer(FILE* fp)
488  : m_fp(fp)
489  {
490  m_buf[0] = '\0';
491  fflush(fp);
492  const int r = setvbuf(fp, m_buf, _IOFBF, SIZE);
493  if( r != 0 )
494  {
495  IUTEST_LOG_(WARNING) << "setvbuf failed: " << r;
496  m_fp = NULL;
497  }
498  }
499 
500  ~IUStreamBuffer()
501  {
502  if( m_fp != NULL )
503  {
504  fflush(m_fp);
505  setvbuf(m_fp, NULL, _IONBF, 0);
506  }
507  }
508 
509 public:
510  ::std::string GetStreamString() const
511  {
512  return m_buf;
513  }
514  bool IsValid() const { return m_fp != NULL; }
515 
516 private:
517  FILE* m_fp;
518  char m_buf[SIZE];
519 };
520 
521 #endif
522 
523 } // end of namespace detail
524 } // end of namespace iutest
525 
526 #if !IUTEST_HAS_LIB
527 # include "../impl/iutest_port.ipp" // IWYU pragma: export
528 #endif
529 
530 #endif // INCG_IRIS_IUTEST_PORT_HPP_7893F685_A1A9_477A_82E8_BF06237697FF_
#define IUTEST_CXX_NOEXCEPT_SPEC
noexcept specification definition
Definition: iutest_compiler.hpp:811
#define IUTEST_ATTRIBUTE_NORETURN_
noreturn
Definition: iutest_compiler.hpp:1433
internal definition
#define IUTEST_LOG_(level)
ログメッセージストリーム
Definition: iutest_port.hpp:56
#define IUTEST_PP_DISALLOW_COPY_AND_ASSIGN(TypeName)
コピー禁止定義
Definition: iutest_pp.hpp:31
iutest root namespace
Definition: iutest_charcode.hpp:33