File: strtonum.c

package info (click to toggle)
mtools 4.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,568 kB
  • sloc: ansic: 18,911; sh: 3,457; makefile: 248; sed: 7
file content (84 lines) | stat: -rw-r--r-- 2,220 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*  Copyright 2018 Alain Knaff.
 *  This file is part of mtools.
 *
 *  Mtools is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Mtools is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include "sysincludes.h"
#include "mtools.h"

unsigned int strtoui(const char *nptr, char **endptr, int base) {
    unsigned long l = strtoul(nptr, endptr, base);
    if(l > UINT_MAX) {
	fprintf(stderr, "%lu too large\n", l);
	exit(1);
    }
    return (unsigned int) l;
}

unsigned int atoui(const char *str) {
    return strtoui(str, 0, 0);
}

int strtoi(const char *nptr, char **endptr, int base) {
    long l = strtol(nptr, endptr, base);
    if(l > INT_MAX || l < INT_MIN) {
	fprintf(stderr, "%lu out ob bounds\n", l);
	exit(1);
    }
    return (int) l;
}

unsigned long atoul(const char *str) {
    return strtoul(str, 0, 0);
}

uint8_t strtou8(const char *nptr, char **endptr, int base) {
    unsigned long l = strtoul(nptr, endptr, base);
    if(l > UINT8_MAX) {
	fprintf(stderr, "%lu too large\n", l);
	exit(1);
    }
    return (uint8_t) l;
}

uint8_t atou8(const char *str) {
    return strtou8(str, 0, 0);
}

uint16_t strtou16(const char *nptr, char **endptr, int base) {
    unsigned long l = strtoul(nptr, endptr, base);
    if(l > UINT16_MAX) {
	fprintf(stderr, "%lu too large\n", l);
	exit(1);
    }
    return (uint16_t) l;
}

uint16_t atou16(const char *str) {
    return strtou16(str, 0, 0);
}

uint32_t strtou32(const char *nptr, char **endptr, int base) {
    unsigned long l = strtoul(nptr, endptr, base);
    if(l > UINT32_MAX) {
	fprintf(stderr, "%lu too large\n", l);
	exit(1);
    }
    return (uint32_t) l;
}

uint32_t atou32(const char *str) {
    return strtou32(str, 0, 0);
}