Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / feature_validator.cc
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/feature_validator.h"
6
7 #include <system_info.h>
8
9 #include <memory>
10 #include <string>
11
12 namespace common_installer {
13
14 FeatureValidator::FeatureValidator(
15     const std::map<std::string, std::string>& features)
16     : features_(features) {
17 }
18
19 bool FeatureValidator::Validate(std::string* error) {
20   for (auto& pair : features_) {
21     const auto& feature = pair.first;
22
23     system_info_type_e type;
24     int ret = system_info_get_platform_type(feature.c_str(), &type);
25     if (ret != SYSTEM_INFO_ERROR_NONE) {
26       *error = std::string("Unknown feature: ") + feature;
27       return false;
28     }
29     bool ok = false;
30     switch (type) {
31     case SYSTEM_INFO_BOOL:
32       ok = ValidateBoolean(feature, error);
33       break;
34     case SYSTEM_INFO_INT:
35       ok = ValidateInteger(feature, error);
36       break;
37     case SYSTEM_INFO_STRING:
38       ok = ValidateString(feature, error);
39       break;
40     case SYSTEM_INFO_DOUBLE:
41       // There is no double typed feature on platform, no way to interpret it
42       ok = true;
43       break;
44     default:
45       ok = false;
46       *error = "Unknown type of feature";
47     }
48     if (!ok)
49       return false;
50   }
51   return true;
52 }
53
54 bool FeatureValidator::ValidateBoolean(
55     const std::string& feature, std::string* error) {
56   bool supported = false;
57   int ret = system_info_get_platform_bool(feature.c_str(), &supported);
58   if (ret != SYSTEM_INFO_ERROR_NONE) {
59     *error = std::string("Failed to call system_info_get_platform_bool()") +
60         " for " + feature + ", error code: " + std::to_string(ret);
61     return false;
62   }
63   return true;
64 }
65
66 bool FeatureValidator::ValidateInteger(
67     const std::string& feature, std::string* error) {
68   int platform_value = 0;
69   int ret = system_info_get_platform_int(feature.c_str(), &platform_value);
70   if (ret != SYSTEM_INFO_ERROR_NONE) {
71     *error = std::string("Failed to call system_info_get_platform_int()") +
72         " for " + feature + ", error code: " + std::to_string(ret);
73     return false;
74   }
75   return true;
76 }
77
78 bool FeatureValidator::ValidateString(
79     const std::string& feature, std::string* error) {
80   char* text = nullptr;
81   std::unique_ptr<char, decltype(&std::free)> text_deleter(text, std::free);
82   int ret = system_info_get_platform_string(feature.c_str(), &text);
83   if (ret != SYSTEM_INFO_ERROR_NONE) {
84     *error = std::string("Failed to call system_info_get_platform_string()") +
85         " for " + feature + ", error code: " + std::to_string(ret);
86     return false;
87   }
88   return true;
89 }
90
91 }  // namespace common_installer