1 // Copyright 2023 The gRPC Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "test/cpp/util/windows/manifest_file.h"
16
17 #include <grpc/support/port_platform.h>
18
19 #ifdef GPR_WINDOWS
20
21 #include <Windows.h>
22
23 #include <fstream>
24 #include <string>
25 #include <vector>
26
27 #include "absl/strings/str_format.h"
28 #include "absl/strings/str_replace.h"
29 #include "absl/strings/str_split.h"
30
31 #include <grpc/support/log.h>
32
33 #include "src/core/lib/gprpp/crash.h"
34
35 namespace grpc {
36 namespace testing {
37
NormalizeFilePath(const std::string & filepath)38 std::string NormalizeFilePath(const std::string& filepath) {
39 return absl::StrReplaceAll(filepath, {{"/", "\\"}});
40 }
41
ManifestFile(const std::string & filepath)42 ManifestFile::ManifestFile(const std::string& filepath)
43 : filestream_(filepath, std::ios::in | std::ios::binary) {
44 if (!filestream_.is_open()) {
45 grpc_core::Crash(absl::StrFormat("Failed to open: %s, error code: %d",
46 filepath, GetLastError()));
47 }
48 }
49
Get(const std::string & key)50 std::string ManifestFile::Get(const std::string& key) {
51 auto iter = cache_.find(key);
52 if (iter != cache_.end()) {
53 return iter->second;
54 }
55 do {
56 std::string line;
57 std::getline(filestream_, line);
58 if (!line.empty()) {
59 std::vector<std::string> kv = absl::StrSplit(line, " ");
60 GPR_ASSERT(kv.size() == 2);
61 cache_.emplace(kv[0], kv[1]);
62 if (kv[0] == key) {
63 return kv[1];
64 }
65 }
66 } while (!filestream_.eof() && !filestream_.fail());
67 grpc_core::Crash(
68 absl::StrFormat("Failed to find key: %s in MANIFEST file", key));
69 }
70
71 } // namespace testing
72 } // namespace grpc
73
74 #endif // GPR_WINDOWS
75