1 // Copyright 2016 The Draco Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #include "draco/core/options.h"
16
17 #include <cstdlib>
18 #include <string>
19 #include <utility>
20
21 namespace draco {
22
Options()23 Options::Options() {}
24
MergeAndReplace(const Options & other_options)25 void Options::MergeAndReplace(const Options &other_options) {
26 for (const auto &item : other_options.options_) {
27 options_[item.first] = item.second;
28 }
29 }
30
SetInt(const std::string & name,int val)31 void Options::SetInt(const std::string &name, int val) {
32 options_[name] = std::to_string(val);
33 }
34
SetFloat(const std::string & name,float val)35 void Options::SetFloat(const std::string &name, float val) {
36 options_[name] = std::to_string(val);
37 }
38
SetBool(const std::string & name,bool val)39 void Options::SetBool(const std::string &name, bool val) {
40 options_[name] = std::to_string(val ? 1 : 0);
41 }
42
SetString(const std::string & name,const std::string & val)43 void Options::SetString(const std::string &name, const std::string &val) {
44 options_[name] = val;
45 }
46
GetInt(const std::string & name) const47 int Options::GetInt(const std::string &name) const { return GetInt(name, -1); }
48
GetInt(const std::string & name,int default_val) const49 int Options::GetInt(const std::string &name, int default_val) const {
50 const auto it = options_.find(name);
51 if (it == options_.end()) {
52 return default_val;
53 }
54 return std::atoi(it->second.c_str());
55 }
56
GetFloat(const std::string & name) const57 float Options::GetFloat(const std::string &name) const {
58 return GetFloat(name, -1);
59 }
60
GetFloat(const std::string & name,float default_val) const61 float Options::GetFloat(const std::string &name, float default_val) const {
62 const auto it = options_.find(name);
63 if (it == options_.end()) {
64 return default_val;
65 }
66 return static_cast<float>(std::atof(it->second.c_str()));
67 }
68
GetBool(const std::string & name) const69 bool Options::GetBool(const std::string &name) const {
70 return GetBool(name, false);
71 }
72
GetBool(const std::string & name,bool default_val) const73 bool Options::GetBool(const std::string &name, bool default_val) const {
74 const int ret = GetInt(name, -1);
75 if (ret == -1) {
76 return default_val;
77 }
78 return static_cast<bool>(ret);
79 }
80
GetString(const std::string & name) const81 std::string Options::GetString(const std::string &name) const {
82 return GetString(name, "");
83 }
84
GetString(const std::string & name,const std::string & default_val) const85 std::string Options::GetString(const std::string &name,
86 const std::string &default_val) const {
87 const auto it = options_.find(name);
88 if (it == options_.end()) {
89 return default_val;
90 }
91 return it->second;
92 }
93
94 } // namespace draco
95