/src/cyad/PolicyTypeTranslator.cpp

https://gitlab.com/admin-github-cloud/cynara · C++ · 96 lines · 53 code · 22 blank · 21 comment · 15 complexity · 7c5093596a2557a9157f2db495a137fd 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/PolicyTypeTranslator.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief A helper class to translate strings to PolicyType
  21. */
  22. #include <algorithm>
  23. #include <stdexcept>
  24. #include <cynara-error.h>
  25. #include <cyad/CommandlineParser/PolicyParsingException.h>
  26. #include "PolicyTypeTranslator.h"
  27. namespace Cynara {
  28. namespace {
  29. const std::string BUCKET_LITERAL = "bucket";
  30. const std::string NONE_LITERAL = "none";
  31. } /* namespace anonymous */
  32. PolicyTypeTranslator::PolicyTypeTranslator() {}
  33. PolicyTypeTranslator::~PolicyTypeTranslator() {}
  34. void PolicyTypeTranslator::setDescriptions(cynara_admin_policy_descr **descs) {
  35. for (int i = 0; descs[i]; ++i) {
  36. std::string name(descs[i]->name);
  37. std::transform(name.begin(), name.end(), name.begin(), ::tolower);
  38. m_descs.push_back(std::make_pair(descs[i]->result, name));
  39. }
  40. }
  41. PolicyType PolicyTypeTranslator::translate(const std::string &rawPolicy) {
  42. std::string policy(rawPolicy);
  43. std::transform(policy.begin(), policy.end(), policy.begin(), ::tolower);
  44. auto descComp = [policy] (const PolicyDescriptions::value_type &it) -> bool {
  45. return it.second == policy;
  46. };
  47. const auto it = std::find_if(m_descs.begin(), m_descs.end(), descComp);
  48. if (it != m_descs.end())
  49. return it->first;
  50. if (policy == NONE_LITERAL)
  51. return CYNARA_ADMIN_NONE;
  52. if (policy == BUCKET_LITERAL)
  53. return CYNARA_ADMIN_BUCKET;
  54. try {
  55. return std::stoi(policy, nullptr, 0);
  56. } catch (const std::logic_error &) {
  57. throw PolicyParsingException();
  58. }
  59. }
  60. std::string PolicyTypeTranslator::humanize(int policy) {
  61. auto descComp = [policy] (const PolicyDescriptions::value_type &it) -> bool {
  62. return it.first == policy;
  63. };
  64. const auto it = std::find_if(m_descs.begin(), m_descs.end(), descComp);
  65. if (it != m_descs.end()) {
  66. return it->second;
  67. }
  68. if (policy == CYNARA_ADMIN_NONE)
  69. return NONE_LITERAL;
  70. if (policy == CYNARA_ADMIN_BUCKET)
  71. return BUCKET_LITERAL;
  72. return std::to_string(policy);
  73. }
  74. } /* namespace Cynara */