SmartSnippets DA1459x SDK
cli_utils.h
Go to the documentation of this file.
1 
44 #ifndef CLI_UTILS_H
45 #define CLI_UTILS_H
46 
47 #if dg_configUSE_CLI
48 
49 #include <errno.h>
50 #include <limits.h>
51 #include <stdint.h>
52 #include <stdbool.h>
53 #include <stdlib.h>
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 /*
60  * \brief Verify if given argument is a number or not
61  *
62  * \param [in] arg Argument to check
63  * \param [out] v Argument parsed to a number
64  *
65  * \return true if argument was properly parsed to a number, false otherwise
66  */
67 __STATIC_INLINE bool verify_num(const char *arg, long *v)
68 {
69  char *check_ptr;
70  errno = 0;
71 
72  *v = strtol(arg, &check_ptr, 0);
73 
74  if (errno == ERANGE) {
75  return false;
76  }
77 
78  return (*arg != '\0' && *check_ptr == '\0');
79 }
80 
81 /*
82  * \brief Verify if given argument is a non-negative number or not
83  *
84  * \param [in] arg Argument to check
85  * \param [out] v Argument parsed to a number
86  *
87  * \return true if argument was properly parsed to a non-negative number, false otherwise
88  */
89 __STATIC_INLINE bool verify_non_neg_num(const char *arg, unsigned long long *v)
90 {
91  char *check_ptr;
92  errno = 0;
93 
94  /*
95  * Check if the argument doesn't include '-' character at the first position, that informs
96  * that this is a negative number
97  */
98  if (arg[0] == '-') {
99  return false;
100  }
101 
102  *v = strtoull(arg, &check_ptr, 0);
103 
104  if (errno == ERANGE) {
105  return false;
106  }
107 
108  return (*arg != '\0' && *check_ptr == '\0');
109 }
110 
111 /*
112  * \brief Parse argument to uint64_t
113  *
114  * \param [in] arg Argument to parse
115  * \param [out] val Argument parsed to a number
116  *
117  * \return true if parsed correctly, false otherwise
118  */
119 __STATIC_INLINE bool parse_u64(const char *arg, uint64_t *val)
120 {
121  unsigned long long buf;
122 
123  if (!verify_non_neg_num(arg, &buf)) {
124  return false;
125  }
126 
127  *val = (uint64_t)buf;
128  return true;
129 }
130 
131 /*
132  * \brief Parse argument to uint32_t
133  *
134  * \param [in] arg Argument to parse
135  * \param [out] val Argument parsed to a number
136  *
137  * \return true if parsed correctly, false otherwise
138  */
139 __STATIC_INLINE bool parse_u32(const char *arg, uint32_t *val)
140 {
141  unsigned long long buf;
142 
143  if (!verify_non_neg_num(arg, &buf)) {
144  return false;
145  }
146 
147  if (buf > ULONG_MAX) {
148  return false;
149  }
150 
151  *val = (uint32_t)buf;
152  return true;
153 }
154 
155 /*
156  * \brief Parse argument to uint16_t
157  *
158  * \param [in] arg Argument to parse
159  * \param [out] val Argument parsed to a number
160  *
161  * \return true if parsed correctly, false otherwise
162  */
163 __STATIC_INLINE bool parse_u16(const char *arg, uint16_t *val)
164 {
165  unsigned long long buf;
166 
167  if (!verify_non_neg_num(arg, &buf)) {
168  return false;
169  }
170 
171  if (buf > USHRT_MAX) {
172  return false;
173  }
174 
175  *val = (uint16_t)buf;
176  return true;
177 }
178 
179 /*
180  * \brief Parse argument to uint8_t
181  *
182  * \param [in] arg Argument to parse
183  * \param [out] val Argument parsed to a number
184  *
185  * \return true if parsed correctly, false otherwise
186  */
187 __STATIC_INLINE bool parse_u8(const char *arg, uint8_t *val)
188 {
189  unsigned long long buf;
190 
191  if (!verify_non_neg_num(arg, &buf)) {
192  return false;
193  }
194 
195  if (buf > UCHAR_MAX) {
196  return false;
197  }
198 
199  *val = (uint8_t)buf;
200  return true;
201 }
202 
203 /*
204  * \brief Parse argument to int16_t
205  *
206  * \param [in] arg Argument to parse
207  * \param [out] val Argument parsed to a number
208  *
209  * \return true if parsed correctly, false otherwise
210  */
211 __STATIC_INLINE bool parse_16(const char *arg, int16_t *val)
212 {
213  long buf = 0;
214 
215  if (!verify_num(arg, &buf)) {
216  return false;
217  }
218 
219  if (buf > SHRT_MAX) {
220  return false;
221  }
222 
223  *val = (int16_t)buf;
224  return true;
225 }
226 
227 /*
228  * \brief Parse argument to bool
229  *
230  * \param [in] arg Argument to parse
231  * \param [out] val Argument parsed to a boolean value
232  *
233  * \return true if parsed correctly and number has proper value, false otherwise
234  */
235 __STATIC_INLINE bool parse_bool(const char *arg, bool *val)
236 {
237  unsigned long long buf;
238 
239  if (!verify_non_neg_num(arg, &buf)) {
240  return false;
241  }
242 
243  /* Valid values are only 0 (false) or 1 (true) */
244  if (buf > 1) {
245  return false;
246  }
247 
248  *val = (bool)buf;
249  return true;
250 }
251 
252 /*
253  * \brief Parse argument to size_t
254  *
255  * \param [in] arg Argument to parse
256  * \param [out] val Argument parsed to a number
257  *
258  * \return true if parsed correctly, false otherwise
259  */
260 __STATIC_INLINE bool parse_size_t(const char *arg, size_t *val)
261 {
262  unsigned long long buf;
263 
264  if (!verify_non_neg_num(arg, &buf)) {
265  return false;
266  }
267 
268  if (sizeof(size_t) == 4 && buf > ULONG_MAX) {
269  return false;
270  }
271 
272  *val = (size_t)buf;
273  return true;
274 }
275 
276 #ifdef __cplusplus
277 }
278 #endif
279 
280 #endif /* dg_configUSE_CLI */
281 
282 #endif /* CLI_UTILS_H */
283