245 lines
5.3 KiB
C
245 lines
5.3 KiB
C
/*-
|
|
* Copyright (c) 2017-2021 Carsten Sonne Larsen <cs@innolan.net>
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
*/
|
|
|
|
#include "config.h"
|
|
#include "conv.h"
|
|
#include "text.h"
|
|
|
|
static const char *alphaNumerics = "0123456789";
|
|
static const int base = 10;
|
|
|
|
bool TryParseBoolean(char *string, bool *value)
|
|
{
|
|
if (string == NULL || ((const char *)string) == '\0')
|
|
{
|
|
return false;
|
|
}
|
|
else if (Stricmp((CONST_STRPTR)noValueString, (CONST_STRPTR)value) == 0 ||
|
|
Stricmp((CONST_STRPTR)falseValueString, (CONST_STRPTR)value) == 0 ||
|
|
Stricmp((CONST_STRPTR) "0", (CONST_STRPTR)value) == 0)
|
|
{
|
|
*value = false;
|
|
return true;
|
|
}
|
|
else if (Stricmp((CONST_STRPTR)yesValueString, (CONST_STRPTR)value) == 0 ||
|
|
Stricmp((CONST_STRPTR)trueValueString, (CONST_STRPTR)value) == 0 ||
|
|
Stricmp((CONST_STRPTR) "1", (CONST_STRPTR)value) == 0)
|
|
{
|
|
*value = true;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
* Try to parse a null terminated string to a long.
|
|
*/
|
|
bool TryParseLong(char *string, long *value)
|
|
{
|
|
LONG characters, parsedValue;
|
|
characters = StrToLong((STRPTR)string, &parsedValue);
|
|
if (characters != -1)
|
|
{
|
|
*value = parsedValue;
|
|
return true;
|
|
}
|
|
|
|
*value = 0;
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
* Try to parse a null terminated string to a long.
|
|
*/
|
|
bool TryParseLongLong(char *string, long long *value)
|
|
{
|
|
unsigned long long parsedValue;
|
|
long characters;
|
|
characters = StrToLongLong(string, &parsedValue);
|
|
if (characters != -1)
|
|
{
|
|
*value = parsedValue;
|
|
return true;
|
|
}
|
|
|
|
*value = 0;
|
|
return false;
|
|
}
|
|
|
|
/*
|
|
* Convert a boolean to a null terminated string.
|
|
*/
|
|
int BoolToStr(bool value, char *string)
|
|
{
|
|
int count;
|
|
if (value)
|
|
{
|
|
StrCopy(string, yesValueString);
|
|
count = StrLen(yesValueString);
|
|
}
|
|
else
|
|
{
|
|
StrCopy(string, noValueString);
|
|
count = StrLen(noValueString);
|
|
}
|
|
return count;
|
|
}
|
|
|
|
/*
|
|
* Convert a boolean to a null terminated numeral string.
|
|
*/
|
|
int BoolToStrNum(bool value, char *string)
|
|
{
|
|
char *s = string;
|
|
|
|
if (s == NULL)
|
|
return 0;
|
|
|
|
if (value)
|
|
*s++ = '1';
|
|
else
|
|
*s++ = '0';
|
|
|
|
*s = '\0';
|
|
return 1;
|
|
}
|
|
|
|
/*
|
|
* Convert a long to a null terminated string.
|
|
*/
|
|
int LongToStr(signed long value, char *string)
|
|
{
|
|
unsigned int len, count = 0;
|
|
unsigned long current = value;
|
|
char chars[12];
|
|
char *p = chars;
|
|
char *q = string;
|
|
bool negative = false;
|
|
|
|
if (value < 0)
|
|
{
|
|
current = -value;
|
|
negative = true;
|
|
}
|
|
|
|
do
|
|
{
|
|
unsigned long remainder = current % base;
|
|
*p++ = alphaNumerics[remainder];
|
|
current /= base;
|
|
count++;
|
|
} while (current >= 1);
|
|
|
|
p--;
|
|
len = count;
|
|
|
|
if (negative)
|
|
{
|
|
*q++ = '-';
|
|
len++;
|
|
}
|
|
|
|
while (count-- != 0)
|
|
*q++ = *p--;
|
|
|
|
*q = '\0';
|
|
return len;
|
|
}
|
|
|
|
/*
|
|
* Convert a long long to a null terminated string.
|
|
*/
|
|
int LongLongToStr(signed long long value, char *string)
|
|
{
|
|
unsigned int len, count = 0;
|
|
unsigned long long current = value;
|
|
char chars[MAXLONGLONGCHARSIZE];
|
|
char *p = chars;
|
|
char *q = string;
|
|
bool negative = false;
|
|
|
|
if (value < 0)
|
|
{
|
|
current = -value;
|
|
negative = true;
|
|
}
|
|
|
|
do
|
|
{
|
|
unsigned long long remainder = current % base;
|
|
*p++ = alphaNumerics[remainder];
|
|
current /= base;
|
|
count++;
|
|
} while (current >= 1);
|
|
|
|
p--;
|
|
len = count;
|
|
|
|
if (negative)
|
|
{
|
|
*q++ = '-';
|
|
len++;
|
|
}
|
|
|
|
while (count-- != 0)
|
|
*q++ = *p--;
|
|
|
|
*q = '\0';
|
|
return len;
|
|
}
|
|
|
|
/*
|
|
* Convert a null terminated string to a long long.
|
|
*/
|
|
int StrToLongLong(char *string, unsigned long long *value)
|
|
{
|
|
signed long long result = 0;
|
|
bool negative = false;
|
|
char *p = string;
|
|
int count = 0;
|
|
|
|
if (*p == '-')
|
|
{
|
|
negative = true;
|
|
p++;
|
|
}
|
|
|
|
while (*p != '\0')
|
|
{
|
|
if (*p < '0' || *p > '9' || count > 20)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
result *= 10;
|
|
result += (*p++) - '0';
|
|
}
|
|
|
|
*value = negative ? -result : result;
|
|
return count;
|
|
}
|