PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cyad/AdminPolicyParser.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 84 lines | 48 code | 15 blank | 21 comment | 5 complexity | b9f50c4c5b9b063f64b5cd65d039525d MD5 | raw file
  1. /*
  2. * Copyright (c) 2014-2016 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/AdminPolicyParser.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Parses policies from input stream
  21. */
  22. #include <config/PathConfig.h>
  23. #include <exceptions/BucketRecordCorruptedException.h>
  24. #include <storage/BucketDeserializer.h>
  25. #include "AdminPolicyParser.h"
  26. namespace Cynara {
  27. namespace AdminPolicyParser {
  28. CynaraAdminPolicies parse(std::istream &input,
  29. std::function<PolicyType(const std::string &)> translatePolicy) {
  30. CynaraAdminPolicies policies;
  31. auto nextToken = [] (const std::string &line, std::size_t &beginToken) -> std::string {
  32. auto endToken = line.find(PathConfig::StoragePath::fieldSeparator, beginToken);
  33. if (endToken != std::string::npos) {
  34. auto token = line.substr(beginToken, endToken - beginToken);
  35. beginToken = endToken + 1;
  36. return token;
  37. }
  38. throw BucketRecordCorruptedException(line);
  39. };
  40. auto lastToken = [] (const std::string &line, std::size_t &beginToken) -> std::string {
  41. if (beginToken < line.size()) {
  42. auto ret = line.substr(beginToken);
  43. beginToken = line.size();
  44. return ret;
  45. }
  46. return std::string();
  47. };
  48. std::string line;
  49. std::size_t lineNum = 1;
  50. while (std::getline(input, line, PathConfig::StoragePath::recordSeparator)) {
  51. if (line.empty())
  52. break;
  53. try {
  54. std::size_t beginToken = 0;
  55. auto bucketId = nextToken(line, beginToken);
  56. auto policyKey = BucketDeserializer::parseKey(line, beginToken);
  57. auto policyType = translatePolicy(nextToken(line, beginToken));
  58. auto metadata = lastToken(line, beginToken);
  59. policies.add(bucketId, PolicyResult(policyType, metadata), policyKey);
  60. } catch (const BucketRecordCorruptedException &ex) {
  61. throw ex.withLineNumber(lineNum);
  62. }
  63. lineNum++;
  64. }
  65. policies.seal();
  66. return policies;
  67. }
  68. } /* namespace AdminPolicyParser */
  69. } /* namespace Cynara */