mirror of https://git.48k.eu/ogserver
60 lines
1.2 KiB
C
60 lines
1.2 KiB
C
/*
|
|
* Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
* the terms of the GNU Affero General Public License as published by the
|
|
* Free Software Foundation; either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*/
|
|
|
|
|
|
#include <ctype.h>
|
|
#include <errno.h>
|
|
#include <assert.h>
|
|
#include <limits.h>
|
|
#include <stdlib.h>
|
|
#include "utils.h"
|
|
|
|
void str_toupper(char *str)
|
|
{
|
|
char *c = str;
|
|
|
|
while (*c) {
|
|
*c = toupper(*c);
|
|
c++;
|
|
}
|
|
}
|
|
|
|
void str_tolower(char *str)
|
|
{
|
|
char *c = str;
|
|
|
|
while (*c) {
|
|
*c = tolower(*c);
|
|
c++;
|
|
}
|
|
}
|
|
|
|
int safe_strtoull(const char *str, uint64_t *out_value, int base, uint64_t max)
|
|
{
|
|
char *endptr = NULL;
|
|
uint64_t result;
|
|
errno = 0;
|
|
|
|
assert(str != NULL && out_value != NULL);
|
|
|
|
if (str[0] == '-')
|
|
return -1;
|
|
|
|
result = strtoull(str, &endptr, base);
|
|
|
|
if (endptr == str ||
|
|
*endptr != '\0' ||
|
|
(errno == ERANGE && result == ULLONG_MAX) ||
|
|
result > max)
|
|
return -1;
|
|
|
|
*out_value = result;
|
|
return 0;
|
|
}
|