Line data Source code
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 : /****************************************************************************/
14 : /// @file SysUtils.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @date Tue, 29.05.2005
18 : ///
19 : // A few system-specific functions
20 : /****************************************************************************/
21 : #include <config.h>
22 : #include <utils/xml/XMLSubSys.h>
23 :
24 : #include <stdlib.h>
25 : #include <sys/types.h>
26 : #include <sys/stat.h>
27 : #include "StringUtils.h"
28 : #include "SysUtils.h"
29 :
30 : #ifndef WIN32
31 : #include <sys/time.h>
32 : #include <unistd.h>
33 : #else
34 : #define NOMINMAX
35 : #include <windows.h>
36 : #undef NOMINMAX
37 : #define stat _stat
38 : #endif
39 :
40 : // ===========================================================================
41 : // member method definitions
42 : // ===========================================================================
43 :
44 : long
45 203219282 : SysUtils::getCurrentMillis() {
46 : #ifndef WIN32
47 : timeval current;
48 203219282 : gettimeofday(¤t, 0);
49 203219282 : long nanosecs =
50 203219282 : (long) current.tv_sec * 1000L + (long) current.tv_usec / 1000L;
51 203219282 : return nanosecs;
52 : #else
53 : LARGE_INTEGER val, val2;
54 : BOOL check = QueryPerformanceCounter(&val);
55 : check = QueryPerformanceFrequency(&val2);
56 : return (long)(val.QuadPart * 1000 / val2.QuadPart);
57 : #endif
58 : }
59 :
60 :
61 : #ifdef WIN32
62 : long
63 : SysUtils::getWindowsTicks() {
64 : return (long) GetTickCount();
65 : }
66 : #endif
67 :
68 :
69 : unsigned long
70 0 : SysUtils::runHiddenCommand(const std::string& cmd) {
71 : #ifdef WIN32
72 : // code inspired by http://www.codeproject.com/Articles/2537/Running-console-applications-silently
73 : STARTUPINFO StartupInfo;
74 : PROCESS_INFORMATION ProcessInfo;
75 : unsigned long rc;
76 :
77 : memset(&StartupInfo, 0, sizeof(StartupInfo));
78 : StartupInfo.cb = sizeof(STARTUPINFO);
79 : StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
80 : StartupInfo.wShowWindow = SW_HIDE;
81 :
82 : // "/c" option - Do the command then terminate the command window
83 : std::string winCmd = "CMD.exe /c " + XMLSubSys::transcodeToLocal(cmd);
84 : char* args = new char[winCmd.size() + 1];
85 : args[0] = 0;
86 : strcpy(args, winCmd.c_str());
87 : if (!CreateProcess(nullptr, args, nullptr, nullptr, FALSE,
88 : CREATE_NEW_CONSOLE, nullptr, nullptr, &StartupInfo, &ProcessInfo)) {
89 : delete[] args;
90 : return (unsigned long)GetLastError();
91 : }
92 :
93 : WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
94 : if (!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) {
95 : rc = 0;
96 : }
97 :
98 : CloseHandle(ProcessInfo.hThread);
99 : CloseHandle(ProcessInfo.hProcess);
100 :
101 : delete[] args;
102 : return rc;
103 : #else
104 0 : return (unsigned long)system(cmd.c_str());
105 : #endif
106 : }
107 :
108 :
109 : long long
110 38 : SysUtils::getModifiedTime(const std::string& fname) {
111 : struct stat result;
112 38 : if (stat(fname.c_str(), &result) == 0) {
113 38 : return result.st_mtime;
114 : }
115 : return -1;
116 : }
117 :
118 :
119 : /****************************************************************************/
|