2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/stdlib/lib_checkbase.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_checkbase
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is part of the strol() family implementation. This function checks
|
|
|
|
* the initial part of a string to see if it can determine the numeric
|
|
|
|
* base that is represented.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* *ptr points to the first, non-whitespace character in the string.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2016-06-13 15:29:05 +02:00
|
|
|
* - if base is valid, the actual base to use, and pptr is updated to point
|
|
|
|
* at the first digit.
|
|
|
|
* - if base is invalid (<2 or >36), return -1.
|
|
|
|
*
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2014-01-16 15:03:26 +01:00
|
|
|
int lib_checkbase(int base, FAR const char **pptr)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2016-06-13 16:21:06 +02:00
|
|
|
FAR const char *ptr = *pptr;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Check for unspecified base */
|
|
|
|
|
|
|
|
if (!base)
|
|
|
|
{
|
|
|
|
/* Assume base 10 */
|
|
|
|
|
|
|
|
base = 10;
|
|
|
|
|
2021-03-02 14:33:27 +01:00
|
|
|
/* Check for leading '0' - that would signify octal
|
|
|
|
* or hex (or binary)
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
if (*ptr == '0')
|
|
|
|
{
|
|
|
|
/* Assume octal */
|
|
|
|
|
|
|
|
base = 8;
|
|
|
|
ptr++;
|
|
|
|
|
2014-01-16 15:03:26 +01:00
|
|
|
/* Check for hexadecimal */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2014-04-13 22:32:20 +02:00
|
|
|
if ((*ptr == 'X' || *ptr == 'x') &&
|
2012-11-10 17:06:01 +01:00
|
|
|
lib_isbasedigit(ptr[1], 16, NULL))
|
|
|
|
{
|
|
|
|
base = 16;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 14:33:27 +01:00
|
|
|
/* If it a hexadecimal representation,
|
|
|
|
* than discard any leading "0X" or "0x"
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
else if (base == 16)
|
|
|
|
{
|
|
|
|
if (ptr[0] == '0' && (ptr[1] == 'X' || ptr[1] == 'x'))
|
|
|
|
{
|
|
|
|
ptr += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 15:29:05 +02:00
|
|
|
/* Check for incorrect bases. */
|
2016-06-13 16:21:06 +02:00
|
|
|
|
2016-06-13 15:29:05 +02:00
|
|
|
else if (base < 2 || base > 26)
|
|
|
|
{
|
2016-06-13 16:21:06 +02:00
|
|
|
return -1; /* Means incorrect base */
|
2016-06-13 15:29:05 +02:00
|
|
|
}
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/* Return the updated pointer and base */
|
|
|
|
|
|
|
|
*pptr = ptr;
|
|
|
|
return base;
|
|
|
|
}
|