-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathConfigFileReader.cpp
More file actions
138 lines (125 loc) · 5.24 KB
/
ConfigFileReader.cpp
File metadata and controls
138 lines (125 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include <iostream>
#include <fstream>
#include <windef.h>
#include "NativeMsh.h"
#include "ConfigFileReader.h"
namespace NativeMsh
{
// The name of the PowerShell config file that identifies the PowerShell install location.
// We use a config file to avoid writing to the registry during install
// or hard coding paths into the binary.
static PCWSTR powerShellConfigFileName = L"RemotePowerShellConfig.txt";
ConfigFileReader::ConfigFileReader()
{
}
unsigned int ConfigFileReader::Read(
std::wstring pathToConfig)
{
std::wstring absolutePathToConfigFile(pathToConfig);
absolutePathToConfigFile += powerShellConfigFileName;
std::wifstream psConfigFile(absolutePathToConfigFile.c_str());
if (!psConfigFile.is_open())
{
return EXIT_CODE_INIT_FAILURE;
}
std::wstring line;
std::wstring psHomeDirTag(L"PSHOMEDIR=");
std::wstring coreClrDirTag(L"CORECLRDIR=");
while (std::getline(psConfigFile, line))
{
// Search for the first actionable character in the line to
// limit the number of passes that are made iterating through the line.
for(std::wstring::const_iterator iter = line.begin(); iter != line.end(); iter++)
{
if (*iter == L'#')
{
// Stop parsing the line because the rest of it is a comment
break;
}
else if (*iter == L'p' || *iter == L'P')
{
std::wstring psHomeDir = this->getValueFromLine(line, psHomeDirTag);
HANDLE dirHandle = CreateFileW(psHomeDir.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (INVALID_HANDLE_VALUE != dirHandle)
{
CloseHandle(dirHandle);
this->pathToPowerShellAssemblies = psHomeDir;
std::wstring::const_iterator slashIter = this->pathToPowerShellAssemblies.end();
slashIter--;
if (*slashIter != L'\\')
{
// Guarantee that there is a '\' at the end of the path
this->pathToPowerShellAssemblies.append(L"\\");
}
}
break;
}
else if (*iter == L'c' || *iter == L'C')
{
std::wstring coreClrDir = this->getValueFromLine(line, coreClrDirTag);
HANDLE dirHandle = CreateFileW(coreClrDir.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (INVALID_HANDLE_VALUE != dirHandle)
{
CloseHandle(dirHandle);
this->coreClrDirectory = coreClrDir;
std::wstring::const_iterator slashIter = this->coreClrDirectory.end();
slashIter--;
if (*slashIter != L'\\')
{
// Guarantee that there is a '\' at the end of the path
this->coreClrDirectory.append(L"\\");
}
}
break;
}
// Else: Do nothing to ignore unmatched characters (whitespace, etc.)
}
}
if (0 == this->pathToPowerShellAssemblies.size() ||
0 == this->coreClrDirectory.size())
{
return EXIT_CODE_INIT_FAILURE;
}
else
{
return EXIT_CODE_SUCCESS;
}
}
// This trim function removes beginning and ending whitespace. It does not
// remove internal whitespace because that is valid within paths.
std::wstring ConfigFileReader::trim(
const std::wstring& toTrim)
{
static PCWSTR WHITESPACE_CHARS = L" \n\r\t";
std::wstring copyToTrim = toTrim;
std::size_t first = copyToTrim.find_first_not_of(WHITESPACE_CHARS);
if (first == std::wstring::npos)
{
// No non-whitespace found
return std::wstring(L"");
}
// Result not checked for std::wstring::npos because it is guaranteed to have a value if the first pass succeeded.
copyToTrim.erase(copyToTrim.find_last_not_of(WHITESPACE_CHARS)+1);
return copyToTrim.substr(first);
}
// Parses the specified line for the given tag. Tags are assumed to include
// the "=" separator character.
std::wstring ConfigFileReader::getValueFromLine(
const std::wstring& line, // Passed by value to preserve the original line.
std::wstring& tagToFind)
{
std::wstring trimmedLine = this->trim(line);
std::size_t index = trimmedLine.find(tagToFind);
if (std::string::npos != index)
{
std::wstring value = trimmedLine.substr(index + tagToFind.size()); // Everything else after the tag
return this->trim(value);
}
else
{
return std::wstring(L"");
}
}
}