microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
debug_i

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

base/env_string.h

105lines · modeblame

22340011cao lei2 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3#pragma once
4#include <locale>
5#include <optional>
6#include <string>
7#include <sstream>
97ee9eb5Wenbing Li2 years ago8
22340011cao lei2 years ago9#include "string_utils.h"
10#ifdef _WIN32
11#include <Windows.h>
12#endif
13
14template <typename T>
15bool TryParseStringWithClassicLocale(std::string_view str, T& value) {
16if constexpr (std::is_integral<T>::value && std::is_unsigned<T>::value) {
17// if T is unsigned integral type, reject negative values which will wrap
18if (!str.empty() && str[0] == '-') {
19return false;
20}
21}
22
23// don't allow leading whitespace
24if (!str.empty() && std::isspace(str[0], std::locale::classic())) {
25return false;
26}
27
28std::istringstream is{std::string{str}};
29is.imbue(std::locale::classic());
30T parsed_value{};
31
32const bool parse_successful =
33is >> parsed_value &&
34is.get() == std::istringstream::traits_type::eof(); // don't allow trailing characters
35if (!parse_successful) {
36return false;
37}
38
39value = std::move(parsed_value);
40return true;
41}
42
43inline bool TryParseStringWithClassicLocale(std::string_view str, std::string& value) {
44value = str;
45return true;
46}
47
48inline bool TryParseStringWithClassicLocale(std::string_view str, bool& value) {
49if (str == "0" || str == "False" || str == "false") {
50value = false;
51return true;
52}
53
54if (str == "1" || str == "True" || str == "true") {
55value = true;
56return true;
57}
58
59return false;
60}
61
62template <typename T>
63std::optional<T> ParseEnvironmentVariable(const std::string& name) {
64std::string buffer;
65#ifdef _WIN32
66constexpr size_t kBufferSize = 32767;
67
68// Create buffer to hold the result
69buffer.resize(kBufferSize, '\0');
70
71// The last argument is the size of the buffer pointed to by the lpBuffer parameter, including the null-terminating character, in characters.
72// If the function succeeds, the return value is the number of characters stored in the buffer pointed to by lpBuffer, not including the terminating null character.
73// Therefore, If the function succeeds, kBufferSize should be larger than char_count.
74auto char_count = GetEnvironmentVariableA(name.c_str(), buffer.data(), kBufferSize);
75
76if (kBufferSize > char_count) {
77buffer.resize(char_count);
78} else {
79// Else either the call was failed, or the buffer wasn't large enough.
80// TODO: Understand the reason for failure by calling GetLastError().
81// If it is due to the specified environment variable being found in the environment block,
82// GetLastError() returns ERROR_ENVVAR_NOT_FOUND.
83// For now, we assume that the environment variable is not found.
84buffer.clear();
85}
86#else
87char* val = getenv(name.c_str());
88buffer = (val == nullptr) ? std::string() : std::string(val);
89#endif
90T parsed_value;
91if (!TryParseStringWithClassicLocale(buffer, parsed_value)) {
92OrtW::Exception(MakeString("Failed to parse environment variable - name: ", name, ", value: ", buffer), OrtErrorCode::ORT_FAIL);
93}
94return parsed_value;
95}
96
97template <typename T>
98T ParseEnvironmentVariableWithDefault(const std::string& name, const T& default_value) {
99const auto parsed = ParseEnvironmentVariable<T>(name);
100if (parsed.has_value()) {
101return *parsed;
102}
103
104return default_value;
105}