1 //
2 // Copyright (c) 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 {
18
19 namespace
20 {
21
GetExecutablePathImpl()22 std::string GetExecutablePathImpl()
23 {
24 std::array<char, MAX_PATH> executableFileBuf;
25 DWORD executablePathLen = GetModuleFileNameA(nullptr, executableFileBuf.data(),
26 static_cast<DWORD>(executableFileBuf.size()));
27 return (executablePathLen > 0 ? std::string(executableFileBuf.data()) : "");
28 }
29
GetExecutableDirectoryImpl()30 std::string GetExecutableDirectoryImpl()
31 {
32 std::string executablePath = GetExecutablePath();
33 size_t lastPathSepLoc = executablePath.find_last_of("\\/");
34 return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : "";
35 }
36
37 } // anonymous namespace
38
GetExecutablePath()39 const char *GetExecutablePath()
40 {
41 // TODO(jmadill): Make global static string thread-safe.
42 const static std::string &exePath = GetExecutablePathImpl();
43 return exePath.c_str();
44 }
45
GetExecutableDirectory()46 const char *GetExecutableDirectory()
47 {
48 // TODO(jmadill): Make global static string thread-safe.
49 const static std::string &exeDir = GetExecutableDirectoryImpl();
50 return exeDir.c_str();
51 }
52
GetSharedLibraryExtension()53 const char *GetSharedLibraryExtension()
54 {
55 return "dll";
56 }
57
GetCWD()58 Optional<std::string> GetCWD()
59 {
60 std::array<char, MAX_PATH> pathBuf;
61 DWORD result = GetCurrentDirectoryA(static_cast<DWORD>(pathBuf.size()), pathBuf.data());
62 if (result == 0)
63 {
64 return Optional<std::string>::Invalid();
65 }
66 return std::string(pathBuf.data());
67 }
68
SetCWD(const char * dirName)69 bool SetCWD(const char *dirName)
70 {
71 return (SetCurrentDirectoryA(dirName) == TRUE);
72 }
73
SetEnvironmentVar(const char * variableName,const char * value)74 bool SetEnvironmentVar(const char *variableName, const char *value)
75 {
76 return (SetEnvironmentVariableA(variableName, value) == TRUE);
77 }
78
GetEnvironmentVar(const char * variableName)79 std::string GetEnvironmentVar(const char *variableName)
80 {
81 std::array<char, MAX_PATH> oldValue;
82 DWORD result =
83 GetEnvironmentVariableA(variableName, oldValue.data(), static_cast<DWORD>(oldValue.size()));
84 if (result == 0)
85 {
86 return std::string();
87 }
88 else
89 {
90 return std::string(oldValue.data());
91 }
92 }
93
GetPathSeparator()94 const char *GetPathSeparator()
95 {
96 return ";";
97 }
98
99 } // namespace angle
100