// Copyright 2019 The Shaderc Authors. All rights reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#include "libshaderc_util/args.h"1516#include <iomanip>17#include <sstream>1819namespace shaderc_util {2021bool GetOptionArgument(int argc, char** argv, int* index,22const std::string& option,23string_piece* option_argument) {24const string_piece arg = argv[*index];25assert(arg.starts_with(option));26if (arg.size() != option.size()) {27*option_argument = arg.substr(option.size());28return true;29}3031if (option.back() == '=') {32*option_argument = "";33return true;34}3536if (++(*index) >= argc) return false;37*option_argument = argv[*index];38return true;39}4041bool ParseUint32(const std::string& str, uint32_t* value) {42std::istringstream iss(str);4344iss >> std::setbase(0);45iss >> *value;4647// We should have read something.48bool ok = !str.empty() && !iss.bad();49// It should have been all the text.50ok = ok && iss.eof();51// It should have been in range.52ok = ok && !iss.fail();5354// Work around a bugs in various C++ standard libraries.55// Count any negative number as an error, including "-0".56ok = ok && (str[0] != '-');5758return ok;59}6061} // namespace shaderc_util626364