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.org/sumo
3// Copyright (C) 2005-2022 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 "SysUtils.h"
27
28#ifndef WIN32
29#include <sys/time.h>
30#include <unistd.h>
31
32#else
33
34#define NOMINMAX
35#include <windows.h>
36#undef NOMINMAX
37
38#define stat _stat
39
40#endif
41
42
43// ===========================================================================
44// member method definitions
45// ===========================================================================
46long
48#ifndef WIN32
49 timeval current;
50 gettimeofday(&current, 0);
51 long nanosecs =
52 (long) current.tv_sec * 1000L + (long) current.tv_usec / 1000L;
53 return nanosecs;
54#else
55 LARGE_INTEGER val, val2;
56 BOOL check = QueryPerformanceCounter(&val);
57 check = QueryPerformanceFrequency(&val2);
58 return (long)(val.QuadPart * 1000 / val2.QuadPart);
59#endif
60}
61
62
63#ifdef WIN32
64long
65SysUtils::getWindowsTicks() {
66 return (long) GetTickCount();
67}
68#endif
69
70
71unsigned long
72SysUtils::runHiddenCommand(const std::string& cmd) {
73#ifdef WIN32
74 // code inspired by http://www.codeproject.com/Articles/2537/Running-console-applications-silently
75 STARTUPINFO StartupInfo;
76 PROCESS_INFORMATION ProcessInfo;
77 unsigned long rc;
78
79 memset(&StartupInfo, 0, sizeof(StartupInfo));
80 StartupInfo.cb = sizeof(STARTUPINFO);
81 StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
82 StartupInfo.wShowWindow = SW_HIDE;
83
84 // "/c" option - Do the command then terminate the command window
85 std::string winCmd = "CMD.exe /c " + cmd;
86 char* args = new char[winCmd.size() + 1];
87 args[0] = 0;
88 strcpy(args, winCmd.c_str());
89 if (!CreateProcess(nullptr, args, nullptr, nullptr, FALSE,
90 CREATE_NEW_CONSOLE, nullptr, nullptr, &StartupInfo, &ProcessInfo)) {
91 delete[] args;
92 return (unsigned long)GetLastError();
93 }
94
95 WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
96 if (!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) {
97 rc = 0;
98 }
99
100 CloseHandle(ProcessInfo.hThread);
101 CloseHandle(ProcessInfo.hProcess);
102
103 delete[] args;
104 return rc;
105#else
106 return (unsigned long)system(cmd.c_str());
107#endif
108}
109
110
111long long
112SysUtils::getModifiedTime(const std::string& fname) {
113 struct stat result;
114 if (stat(fname.c_str(), &result) == 0) {
115 return result.st_mtime;
116 }
117 return -1;
118}
119
120
121/****************************************************************************/
static unsigned long runHiddenCommand(const std::string &cmd)
run a shell command without popping up any windows (particuarly on win32)
Definition: SysUtils.cpp:72
static long long getModifiedTime(const std::string &fname)
Definition: SysUtils.cpp:112
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:47
#define INFINITE
Definition: fxexdefs.h:80