Eclipse SUMO - Simulation of Urban MObility
SysUtils.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2005-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
19 // A few system-specific functions
20 /****************************************************************************/
21 #include <config.h>
22 
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include "StringUtils.h"
27 #include "SysUtils.h"
28 
29 #ifndef WIN32
30 #include <sys/time.h>
31 #include <unistd.h>
32 #else
33 #define NOMINMAX
34 #include <windows.h>
35 #undef NOMINMAX
36 #define stat _stat
37 #endif
38 
39 // ===========================================================================
40 // member method definitions
41 // ===========================================================================
42 
43 long
45 #ifndef WIN32
46  timeval current;
47  gettimeofday(&current, 0);
48  long nanosecs =
49  (long) current.tv_sec * 1000L + (long) current.tv_usec / 1000L;
50  return nanosecs;
51 #else
52  LARGE_INTEGER val, val2;
53  BOOL check = QueryPerformanceCounter(&val);
54  check = QueryPerformanceFrequency(&val2);
55  return (long)(val.QuadPart * 1000 / val2.QuadPart);
56 #endif
57 }
58 
59 
60 #ifdef WIN32
61 long
62 SysUtils::getWindowsTicks() {
63  return (long) GetTickCount();
64 }
65 #endif
66 
67 
68 unsigned long
69 SysUtils::runHiddenCommand(const std::string& cmd) {
70 #ifdef WIN32
71  // code inspired by http://www.codeproject.com/Articles/2537/Running-console-applications-silently
72  STARTUPINFO StartupInfo;
73  PROCESS_INFORMATION ProcessInfo;
74  unsigned long rc;
75 
76  memset(&StartupInfo, 0, sizeof(StartupInfo));
77  StartupInfo.cb = sizeof(STARTUPINFO);
78  StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
79  StartupInfo.wShowWindow = SW_HIDE;
80 
81  // "/c" option - Do the command then terminate the command window
82  std::string winCmd = "CMD.exe /c " + StringUtils::transcodeToLocal(cmd);
83  char* args = new char[winCmd.size() + 1];
84  args[0] = 0;
85  strcpy(args, winCmd.c_str());
86  if (!CreateProcess(nullptr, args, nullptr, nullptr, FALSE,
87  CREATE_NEW_CONSOLE, nullptr, nullptr, &StartupInfo, &ProcessInfo)) {
88  delete[] args;
89  return (unsigned long)GetLastError();
90  }
91 
92  WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
93  if (!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) {
94  rc = 0;
95  }
96 
97  CloseHandle(ProcessInfo.hThread);
98  CloseHandle(ProcessInfo.hProcess);
99 
100  delete[] args;
101  return rc;
102 #else
103  return (unsigned long)system(cmd.c_str());
104 #endif
105 }
106 
107 
108 long long
109 SysUtils::getModifiedTime(const std::string& fname) {
110  struct stat result;
111  if (stat(fname.c_str(), &result) == 0) {
112  return result.st_mtime;
113  }
114  return -1;
115 }
116 
117 
118 /****************************************************************************/
static std::string transcodeToLocal(const std::string &utf8String)
convert a string from UTF-8 to the local codepage
static unsigned long runHiddenCommand(const std::string &cmd)
run a shell command without popping up any windows (particuarly on win32)
Definition: SysUtils.cpp:69
static long long getModifiedTime(const std::string &fname)
@brie get modified time
Definition: SysUtils.cpp:109
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:44
#define INFINITE
Definition: fxexdefs.h:80