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