runMACS
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
utils.cpp
Go to the documentation of this file.
1 #include <utils.h>
2 
3 #ifdef _WIN32
4 #include <winsock2.h>
5 #include <windows.h>
6 #include <io.h>
7 #define O_RDWR _O_RDWR
8 #define O_BINARY _O_BINARY
9 #define O_CREAT _O_CREAT
10 #define O_EXCL _O_EXCL
11 #define S_IREAD _S_IREAD
12 #define S_IWRITE _S_IWRITE
13 #endif
14 #ifdef HAVE_LINUX
15 #include <arpa/inet.h>
16 #define O_BINARY (0)
17 #endif
18 
19 #include <inttypes.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <limits.h>
23 
24 #include <cstdlib>
25 #include <cstring>
26 #include <functional>
27 #include <sstream>
28 
29 /*#include <portable_endian.h>*/
30 #include <endianness_config.h>
31 
32 
33 void cbWrapper(void * data, void * hint) {
34  (void)data;
35  std::function<void()>* f = reinterpret_cast<std::function<void()>*>(hint);
36  (*f)();
37  delete f;
38 }
39 
40 void insertNetworkUInt32(void * buffer, uint32_t value) {
41  uint32_t temp = htobe32(value);
42  memcpy(buffer, &temp, sizeof(uint32_t));
43 }
44 
45 void insertNetworkUInt64(void * buffer, uint64_t value) {
46  uint64_t temp = htobe64(value);
47  memcpy(buffer, &temp, sizeof(uint64_t));
48 }
49 
50 uint32_t readNetworkUInt32(void * buffer) {
51  uint32_t temp;
52  memcpy(&temp, buffer, sizeof(uint32_t));
53  return betoh32(temp);
54 }
55 
56 uint64_t readNetworkUInt64(void * buffer) {
57  uint64_t temp;
58  memcpy(&temp, buffer, sizeof(uint64_t));
59  return betoh64(temp);
60 }
61 
62 void setThreadPriority(std::thread & th, thread_priority_t prio) {
63 #ifdef WIN32
64  HANDLE h_th = (HANDLE)th.native_handle();
65 
66  switch (prio)
67  {
68  case REALTIME : (void) SetThreadPriority(h_th, THREAD_PRIORITY_TIME_CRITICAL); break;
69  case HIGH : (void) SetThreadPriority(h_th, THREAD_PRIORITY_HIGHEST); break;
70  case ABOVE_NORMAL : (void) SetThreadPriority(h_th, THREAD_PRIORITY_ABOVE_NORMAL); break;
71  case NORMAL : (void) SetThreadPriority(h_th, THREAD_PRIORITY_NORMAL); break;
72  case BELOW_NORMAL : (void) SetThreadPriority(h_th, THREAD_PRIORITY_BELOW_NORMAL); break;
73  case IDLE : (void) SetThreadPriority(h_th, THREAD_PRIORITY_LOWEST); break;
74  }
75 #else
76 #ifndef __APPLE__
77  pthread_t thId = (pthread_t)th.native_handle();
78  pthread_attr_t thAttr;
79  int policy = 0;
80  int minPrio = 0;
81  int maxPrio = 0;
82  int iprio;
83 
84  pthread_attr_init(&thAttr);
85  pthread_attr_getschedpolicy(&thAttr, &policy);
86  minPrio = sched_get_priority_min(policy);
87  maxPrio = sched_get_priority_max(policy);
88 
89  switch (prio) {
90  case REALTIME:
91  iprio = maxPrio;
92  break;
93  case HIGH:
94  iprio = (3 * maxPrio + minPrio) / 4;
95  break;
96  case ABOVE_NORMAL:
97  iprio = (2 * maxPrio + minPrio) / 3;
98  break;
99  case BELOW_NORMAL:
100  iprio = (maxPrio + 2 * minPrio) / 3;
101  break;
102  case IDLE:
103  iprio = minPrio;
104  break;
105  case NORMAL:
106  default:
107  iprio = (maxPrio + minPrio) / 2;
108  break;
109  }
110  pthread_setschedprio(thId, iprio);
111  pthread_attr_destroy(&thAttr);
112 #endif
113 #endif
114 }
115 
116 size_t fwriteString(FILE * stream, const std::string & str) {
117  return fwrite(str.c_str(), 1, str.size(), stream);
118 }
119 
120 #ifdef HAVE_WINDOWS
121 /* mkstemp extracted from libc/sysdeps/posix/tempname.c. Copyright
122  (C) 1991-1999, 2000, 2001, 2006 Free Software Foundation, Inc.
123 
124  The GNU C Library is free software; you can redistribute it and/or
125  modify it under the terms of the GNU Lesser General Public
126  License as published by the Free Software Foundation; either
127  version 2.1 of the License, or (at your option) any later version. */
128 
129 static const char letters[] =
130 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
131 
132 /* Generate a temporary file name based on TMPL. TMPL must match the
133  rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
134  does not exist at the time of the call to mkstemp. TMPL is
135  overwritten with the result. */
136 int mkstemp (char *tmpl) {
137  size_t len;
138  char *XXXXXX;
139  static unsigned long long value;
140  unsigned long long random_time_bits;
141  unsigned int count;
142  int fd = -1;
143  int save_errno = errno;
144 
145  /* A lower bound on the number of temporary files to attempt to
146  generate. The maximum total number of temporary file names that
147  can exist for a given template is 62**6. It should never be
148  necessary to try all these combinations. Instead if a reasonable
149  number of names is tried (we define reasonable as 62**3) fail to
150  give the system administrator the chance to remove the problems. */
151 #define ATTEMPTS_MIN (62 * 62 * 62)
152 
153  /* The number of times to attempt to generate a temporary file. To
154  conform to POSIX, this must be no smaller than TMP_MAX. */
155 #if ATTEMPTS_MIN < TMP_MAX
156  unsigned int attempts = TMP_MAX;
157 #else
158  unsigned int attempts = ATTEMPTS_MIN;
159 #endif
160 
161  len = strlen (tmpl);
162  if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
163  {
164  errno = EINVAL;
165  return -1;
166  }
167 
168 /* This is where the Xs start. */
169  XXXXXX = &tmpl[len - 6];
170 
171  /* Get some more or less random data. */
172  {
173  SYSTEMTIME stNow;
174  FILETIME ftNow;
175 
176  // get system time
177  GetSystemTime(&stNow);
178  stNow.wMilliseconds = 500;
179  if (!SystemTimeToFileTime(&stNow, &ftNow))
180  {
181  errno = -1;
182  return -1;
183  }
184 
185  random_time_bits = (((unsigned long long)ftNow.dwHighDateTime << 32)
186  | (unsigned long long)ftNow.dwLowDateTime);
187  }
188  value += random_time_bits ^ (unsigned long long)GetCurrentThreadId ();
189 
190  for (count = 0; count < attempts; value += 7777, ++count)
191  {
192  unsigned long long v = value;
193 
194  /* Fill in the random bits. */
195  XXXXXX[0] = letters[v % 62];
196  v /= 62;
197  XXXXXX[1] = letters[v % 62];
198  v /= 62;
199  XXXXXX[2] = letters[v % 62];
200  v /= 62;
201  XXXXXX[3] = letters[v % 62];
202  v /= 62;
203  XXXXXX[4] = letters[v % 62];
204  v /= 62;
205  XXXXXX[5] = letters[v % 62];
206 
207 #ifdef HAVE_WINDOWS
208  _sopen_s(&fd, tmpl, _O_RDWR | _O_BINARY | _O_CREAT | _O_EXCL, _SH_DENYRW, _S_IREAD | _S_IWRITE);
209 #else
210  fd = open (tmpl, O_RDWR | O_BINARY | O_CREAT | O_EXCL, S_IREAD | S_IWRITE);
211 #endif
212  if (fd >= 0)
213  {
214  errno = save_errno;
215  return fd;
216  }
217  else if (errno != EEXIST)
218  return -1;
219  }
220 
221  /* We got out of the loop because we ran out of combinations to try. */
222  errno = EEXIST;
223  return -1;
224 }
225 #endif
226 
227 std::string getHome() {
228  std::stringstream home;
229 #ifdef _WIN32
230  char * temp;
231  size_t size;
232  _dupenv_s(&temp, &size, "HOMEDRIVE");
233  home << temp;
234  free(temp);
235  _dupenv_s(&temp, &size, "HOMEPATH");
236  home << temp;
237  free(temp);
238 #else
239  home << getenv("HOME");
240 #endif
241  return home.str();
242 }
243 
void insertNetworkUInt32(void *buffer, uint32_t value)
Definition: utils.cpp:40
std::string getHome()
Definition: utils.cpp:227
uint64_t readNetworkUInt64(void *buffer)
Definition: utils.cpp:56
Definition: utils.h:33
Definition: utils.h:31
size_t fwriteString(FILE *stream, const std::string &str)
Definition: utils.cpp:116
Definition: utils.h:29
void cbWrapper(void *data, void *hint)
Definition: utils.cpp:33
void insertNetworkUInt64(void *buffer, uint64_t value)
Definition: utils.cpp:45
void setThreadPriority(std::thread &th, thread_priority_t prio)
Definition: utils.cpp:62
Definition: utils.h:28
thread_priority_t
Definition: utils.h:27
uint32_t readNetworkUInt32(void *buffer)
Definition: utils.cpp:50