YAZ 5.38.1
atoin.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
5
10
11#if HAVE_CONFIG_H
12#include <config.h>
13#endif
14
15#include <string.h>
16#include <limits.h>
17#include <yaz/yaz-iconv.h>
18#include <yaz/atoi.h>
19
20int atoi_n(const char *buf, int len)
21{
22 int val, ret;
23 ret = atoi_n_check(buf, len, &val);
24 return ret ? val : 0;
25}
26
27int atoi_n_check(const char *buf, int size, int *val)
28{
29 int ret = yaz_atoi(10, buf, size, val);
30 return ret != size ? 0 : 1;
31}
32
33int yaz_atoi(int base, const char *ptr, int len, int *val)
34{
35 unsigned long uval;
36 int ret = yaz_atoul(base, ptr, len, &uval);
37 if (ret == -1)
38 return -1;
39 if (uval > INT_MAX)
40 return -1;
41 *val = (int) uval;
42 return ret;
43}
44
45int yaz_atoul(int base, const char *ptr, int len, unsigned long *val)
46{
47 int i;
48
49 if (base < 2 || base > 36)
50 return -1;
51 *val = 0;
52 for (i = 0; i < len; i++)
53 {
54 int digit;
55 if (yaz_isdigit(ptr[i]))
56 digit = ptr[i] - '0';
57 else if (yaz_isupper(ptr[i]))
58 digit = ptr[i] - ('A' - 10);
59 else if (yaz_islower(ptr[i]))
60 digit = ptr[i] - ('a' - 10);
61 else
62 break;
63 if (digit >= base)
64 break;
65 if (*val > (ULONG_MAX - digit) / base) /* overflow */
66 return -1;
67 *val = *val * base + digit;
68 }
69 return i;
70}
71
72/*
73 * Local variables:
74 * c-basic-offset: 4
75 * c-file-style: "Stroustrup"
76 * indent-tabs-mode: nil
77 * End:
78 * vim: shiftwidth=4 tabstop=8 expandtab
79 */
80
ASCII to integer conversion.
int atoi_n_check(const char *buf, int size, int *val)
like atoi_n but checks for proper formatting
Definition atoin.c:27
int yaz_atoi(int base, const char *ptr, int len, int *val)
string to int conversion, with base
Definition atoin.c:33
int yaz_atoul(int base, const char *ptr, int len, unsigned long *val)
string to unsigned long conversion, with base
Definition atoin.c:45
int atoi_n(const char *buf, int len)
like atoi(3) except that it reads exactly len characters
Definition atoin.c:20
Header for YAZ iconv interface.
#define yaz_islower(x)
Definition yaz-iconv.h:91
#define yaz_isdigit(x)
Definition yaz-iconv.h:86
#define yaz_isupper(x)
Definition yaz-iconv.h:89