The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
io.c
Go to the documentation of this file.
1 
12 #include <arch/io.h>
13 
34 #include <core/core.h>
35 
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <unistd.h>
40 
41 #ifdef __POSIX
42 
43 bool io_terminal_can_colorize(void)
44 {
45  return isatty(STDERR_FILENO);
46 }
47 
48 void io_local_time_get(char res[IO_TIME_BUFFER_LEN])
49 {
50  time_t t = time(NULL);
51  struct tm *loc_t = localtime(&t);
52  strftime(res, IO_TIME_BUFFER_LEN, "%H:%M:%S", loc_t);
53 }
54 
55 FILE *io_file_tmp_get(void)
56 {
57  return tmpfile();
58 }
59 
60 #endif
61 
62 #ifdef __WINDOWS
63 
64 #include <io.h>
65 
66 bool io_terminal_can_colorize(void)
67 {
68  HANDLE term = GetStdHandle(STD_ERROR_HANDLE);
69  if (term == NULL || term == INVALID_HANDLE_VALUE)
70  return false;
71 
72  DWORD cmode;
73  if (!GetConsoleMode(term, &cmode))
74  return false;
75 
76  return SetConsoleMode(term, cmode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
77 }
78 
79 void io_local_time_get(char res[IO_TIME_BUFFER_LEN])
80 {
81  struct tm loc_t;
82  __time64_t long_time;
83  _time64(&long_time);
84 
85  _localtime64_s(&loc_t, &long_time);
86  strftime(res, IO_TIME_BUFFER_LEN, "%H:%M:%S", &loc_t);
87 }
88 
89 FILE *io_file_tmp_get(void)
90 {
91  TCHAR tmp_folder_path[MAX_PATH + 1];
92  if (!GetTempPath(MAX_PATH + 1, tmp_folder_path))
93  return NULL;
94 
95  TCHAR tmp_path[MAX_PATH];
96  if (!GetTempFileName(tmp_folder_path, TEXT("ROOTSIM"), 0, tmp_path))
97  return NULL;
98 
99  SECURITY_ATTRIBUTES tmp_sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
100  HANDLE h = CreateFile(
101  tmp_path, GENERIC_READ | GENERIC_WRITE,
102  FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
103  &tmp_sa, CREATE_ALWAYS,
104  FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL);
105  if (h == INVALID_HANDLE)
106  return NULL;
107 
108  int fd = _open_osfhandle((intptr_t)h, _O_RDWR);
109  if (fd == -1)
110  return NULL;
111 
112  return _fdopen(fd, "rb+");
113 }
114 
115 #endif
io_file_tmp_get
FILE * io_file_tmp_get(void)
Creates a temporary file.
io_terminal_can_colorize
bool io_terminal_can_colorize(void)
Determines if stdout supports colored text.
Definition: init_test.c:16
io.h
Generic input-output facilities.
core.h
Core ROOT-Sim functionalities.