1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // system_utils_win.cpp: Implementation of OS-specific functions for Windows
8 
9 #include "system_utils.h"
10 
11 #include <stdarg.h>
12 #include <windows.h>
13 #include <array>
14 #include <vector>
15 
16 namespace angle
17 {
GetExecutablePath()18 std::string GetExecutablePath()
19 {
20     std::array<char, MAX_PATH> executableFileBuf;
21     DWORD executablePathLen = GetModuleFileNameA(nullptr, executableFileBuf.data(),
22                                                  static_cast<DWORD>(executableFileBuf.size()));
23     return (executablePathLen > 0 ? std::string(executableFileBuf.data()) : "");
24 }
25 
GetExecutableDirectory()26 std::string GetExecutableDirectory()
27 {
28     std::string executablePath = GetExecutablePath();
29     size_t lastPathSepLoc      = executablePath.find_last_of("\\/");
30     return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : "";
31 }
32 
GetSharedLibraryExtension()33 const char *GetSharedLibraryExtension()
34 {
35     return "dll";
36 }
37 
GetCWD()38 Optional<std::string> GetCWD()
39 {
40     std::array<char, MAX_PATH> pathBuf;
41     DWORD result = GetCurrentDirectoryA(static_cast<DWORD>(pathBuf.size()), pathBuf.data());
42     if (result == 0)
43     {
44         return Optional<std::string>::Invalid();
45     }
46     return std::string(pathBuf.data());
47 }
48 
SetCWD(const char * dirName)49 bool SetCWD(const char *dirName)
50 {
51     return (SetCurrentDirectoryA(dirName) == TRUE);
52 }
53 
GetPathSeparatorForEnvironmentVar()54 const char *GetPathSeparatorForEnvironmentVar()
55 {
56     return ";";
57 }
58 
GetCurrentTime()59 double GetCurrentTime()
60 {
61     LARGE_INTEGER frequency = {};
62     QueryPerformanceFrequency(&frequency);
63 
64     LARGE_INTEGER curTime;
65     QueryPerformanceCounter(&curTime);
66 
67     return static_cast<double>(curTime.QuadPart) / frequency.QuadPart;
68 }
69 
IsDirectory(const char * filename)70 bool IsDirectory(const char *filename)
71 {
72     WIN32_FILE_ATTRIBUTE_DATA fileInformation;
73 
74     BOOL result = GetFileAttributesExA(filename, GetFileExInfoStandard, &fileInformation);
75     if (result)
76     {
77         DWORD attribs = fileInformation.dwFileAttributes;
78         return (attribs != INVALID_FILE_ATTRIBUTES) && ((attribs & FILE_ATTRIBUTE_DIRECTORY) > 0);
79     }
80 
81     return false;
82 }
83 
IsDebuggerAttached()84 bool IsDebuggerAttached()
85 {
86     return !!::IsDebuggerPresent();
87 }
88 
BreakDebugger()89 void BreakDebugger()
90 {
91     __debugbreak();
92 }
93 
GetExecutableExtension()94 const char *GetExecutableExtension()
95 {
96     return ".exe";
97 }
98 
GetPathSeparator()99 char GetPathSeparator()
100 {
101     return '\\';
102 }
103 
GetHelperExecutableDir()104 std::string GetHelperExecutableDir()
105 {
106     return "";
107 }
108 }  // namespace angle
109