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/log/check.h"
28 #include "absl/strings/str_format.h"
29 #include "absl/strings/str_replace.h"
30 #include "absl/strings/str_split.h"
31 #include "src/core/util/crash.h"
32
33 namespace grpc {
34 namespace testing {
35
NormalizeFilePath(const std::string & filepath)36 std::string NormalizeFilePath(const std::string& filepath) {
37 return absl::StrReplaceAll(filepath, {{"/", "\\"}});
38 }
39
ManifestFile(const std::string & filepath)40 ManifestFile::ManifestFile(const std::string& filepath)
41 : filestream_(filepath, std::ios::in | std::ios::binary) {
42 if (!filestream_.is_open()) {
43 grpc_core::Crash(absl::StrFormat("Failed to open: %s, error code: %d",
44 filepath, GetLastError()));
45 }
46 }
47
Get(const std::string & key)48 std::string ManifestFile::Get(const std::string& key) {
49 auto iter = cache_.find(key);
50 if (iter != cache_.end()) {
51 return iter->second;
52 }
53 do {
54 std::string line;
55 std::getline(filestream_, line);
56 if (!line.empty()) {
57 std::vector<std::string> kv = absl::StrSplit(line, " ");
58 CHECK_EQ(kv.size(), 2u);
59 cache_.emplace(kv[0], kv[1]);
60 if (kv[0] == key) {
61 return kv[1];
62 }
63 }
64 } while (!filestream_.eof() && !filestream_.fail());
65 grpc_core::Crash(
66 absl::StrFormat("Failed to find key: %s in MANIFEST file", key));
67 }
68
69 } // namespace testing
70 } // namespace grpc
71
72 #endif // GPR_WINDOWS
73