libcopp  2.2.0
cmd_option_value.cpp
Go to the documentation of this file.
1 /*
2  * cmd_option_value.cpp
3  *
4  * Created on: 2011-12-29
5  * Author: OWenT
6  *
7  * 应用程序命令处理
8  *
9  */
10 
11 #include "cli/cmd_option_value.h"
12 #include <algorithm>
13 
14 namespace util {
15 namespace cli {
16 
17 namespace detail {
18 static char tolower(char c) {
19  if (c >= 'A' && c <= 'Z') {
20  return c - 'A' + 'a';
21  }
22 
23  return c;
24 }
25 } // namespace detail
26 
27 cmd_option_value::cmd_option_value(const char *str_data) : data_(str_data) {}
28 cmd_option_value::cmd_option_value(const char *begin, const char *end) { data_.assign(begin, end); }
29 cmd_option_value::cmd_option_value(const std::string &str_data) { data_ = str_data; }
30 
31 const std::string &cmd_option_value::to_cpp_string() const { return data_; }
32 
33 bool cmd_option_value::to_bool() const { return to<bool>(); }
34 
35 char cmd_option_value::to_char() const { return to<char>(); }
36 
37 short cmd_option_value::to_short() const { return to<short>(); }
38 
39 int cmd_option_value::to_int() const { return to<int>(); }
40 
41 long cmd_option_value::to_long() const { return to<long>(); }
42 
43 long long cmd_option_value::to_longlong() const { return to<long long>(); }
44 
45 double cmd_option_value::to_double() const { return to<double>(); }
46 
47 float cmd_option_value::to_float() const { return to<float>(); }
48 
49 const char *cmd_option_value::to_string() const { return data_.c_str(); }
50 
51 // ============ unsigned ============
52 unsigned char cmd_option_value::to_uchar() const { return to<unsigned char>(); }
53 
54 unsigned short cmd_option_value::to_ushort() const { return to<unsigned short>(); }
55 
56 unsigned int cmd_option_value::to_uint() const { return to<unsigned int>(); }
57 
58 unsigned long cmd_option_value::to_ulong() const { return to<unsigned long>(); }
59 
60 unsigned long long cmd_option_value::to_ulonglong() const { return to<unsigned long long>(); }
61 
62 int8_t cmd_option_value::to_int8() const { return static_cast<int8_t>(to_int()); }
63 
64 uint8_t cmd_option_value::to_uint8() const { return static_cast<uint8_t>(to_uint()); }
65 
66 int16_t cmd_option_value::to_int16() const { return to<int16_t>(); }
67 
68 uint16_t cmd_option_value::to_uint16() const { return to<uint16_t>(); }
69 
70 int32_t cmd_option_value::to_int32() const { return to<int32_t>(); }
71 
72 uint32_t cmd_option_value::to_uint32() const { return to<uint32_t>(); }
73 
74 int64_t cmd_option_value::to_int64() const { return to<int64_t>(); }
75 
76 uint64_t cmd_option_value::to_uint64() const { return to<uint64_t>(); }
77 
79  std::string lowercase_content = data_;
80  std::transform(lowercase_content.begin(), lowercase_content.end(), lowercase_content.begin(), detail::tolower);
81 
82  if (lowercase_content.empty()) {
83  return false;
84  }
85 
86  if ("no" == lowercase_content || "false" == lowercase_content || "disabled" == lowercase_content ||
87  "disable" == lowercase_content || "0" == lowercase_content) {
88  return false;
89  }
90 
91  return true;
92 }
93 
94 void cmd_option_value::split(char delim, std::vector<cmd_option_value> &out) {
95  size_t len = 1;
96  for (size_t i = 0; i < data_.size(); ++i) {
97  if (delim == data_[i]) {
98  ++len;
99  }
100  }
101 
102  out.reserve(len);
103  size_t begin_pos = 0;
104  size_t end_pos = 0;
105  while (end_pos != std::string::npos && begin_pos < data_.size()) {
106  end_pos = data_.find(delim, begin_pos);
107  if (end_pos == std::string::npos && begin_pos < data_.size()) {
108  out.push_back(cmd_option_value(&data_[begin_pos]));
109  begin_pos = end_pos;
110  } else {
111  out.push_back(cmd_option_value(&data_[begin_pos], &data_[end_pos]));
112  begin_pos = end_pos + 1;
113  }
114  }
115 }
116 } // namespace cli
117 } // namespace util
unsigned short to_ushort() const
void split(char delim, std::vector< cmd_option_value > &out)
cmd_option_value(const char *str_data)
unsigned char to_uchar() const
unsigned int to_uint() const
unsigned long long to_ulonglong() const
const char * to_string() const
unsigned long to_ulong() const
const std::string & to_cpp_string() const
static char tolower(char c)