/src/cyad/CommandlineParser/HumanReadableParser.cpp

https://gitlab.com/github-cloud-corporation/cynara · C++ · 65 lines · 33 code · 11 blank · 21 comment · 9 complexity · 72c4d26e7400177d39d2c843adb00b9b MD5 · raw file

  1. /*
  2. * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file src/cyad/CommandlineParser/HumanReadableParser.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Helper functions to parse human readable values from command-line
  21. */
  22. #include <algorithm>
  23. #include <string>
  24. #include <unordered_set>
  25. #include <cynara-policy-types.h>
  26. #include <cyad/CommandlineParser/PolicyParsingException.h>
  27. #include "HumanReadableParser.h"
  28. namespace Cynara {
  29. namespace HumanReadableParser {
  30. std::unordered_set<std::string> yeses = {
  31. "y", "1", "yes", "true", "on", "yeah", "yep", "of course", "sure thing"
  32. };
  33. bool isYes(const std::string &value) {
  34. std::string lower_value(value);
  35. std::transform(value.begin(), value.end(), lower_value.begin(), ::tolower);
  36. return yeses.find(value) != yeses.end();
  37. }
  38. PolicyType policyType(const std::string &rawPolicy) {
  39. if (rawPolicy == "DENY")
  40. return CYNARA_ADMIN_DENY;
  41. if (rawPolicy == "NONE")
  42. return CYNARA_ADMIN_NONE;
  43. if (rawPolicy == "BUCKET")
  44. return CYNARA_ADMIN_BUCKET;
  45. if (rawPolicy == "ALLOW")
  46. return CYNARA_ADMIN_ALLOW;
  47. try {
  48. return std::stoi(rawPolicy, nullptr, 0); // base of 0 means autodetection
  49. } catch (const std::logic_error &) {
  50. throw PolicyParsingException();
  51. }
  52. }
  53. } /* namespace HumanReadableParser */
  54. } /* namespace Cynara */