1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
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
16 #include "cm_util.h"
17
18 #include "cm_type.h"
19 #include "cm_log.h"
20
21 #define CARRY 10
22 #define STR_MAX_LEN 10
23
CmIsNumeric(const char * str,const size_t length,uint32_t * value)24 int32_t CmIsNumeric(const char *str, const size_t length, uint32_t *value)
25 {
26 if (str == NULL || length == 0 || length > STR_MAX_LEN || value == NULL) {
27 CM_LOG_D("input parameter error");
28 return CMR_ERROR_INVALID_ARGUMENT;
29 }
30
31 for (size_t i = 0; i < length; i++) {
32 if (str[i] == '\0') {
33 break;
34 }
35 if (i == length - 1) {
36 CM_LOG_D("the string does not have an terminator");
37 return CMR_ERROR_INVALID_ARGUMENT;
38 }
39 }
40
41 char *endptr = NULL;
42 unsigned long num = strtoul(str, &endptr, CARRY);
43 if (endptr == NULL || *endptr != '\0') {
44 CM_LOG_D("str is not numeric string");
45 return CMR_ERROR_INVALID_ARGUMENT;
46 } else {
47 *value = (uint32_t)num;
48 return CM_SUCCESS;
49 }
50 }
51