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