This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
strtol10.c
88 lines (75 loc) · 1.88 KB
/
strtol10.c
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
85
86
87
/**
* \file
* <!--
* Copyright 2005 Develer S.r.l. (http://www.develer.com/)
* This file is part of DevLib - See README.devlib for information.
* -->
*
* \brief Poor man's hex arrays (implementation).
*
* \version $Id$
* \author Bernardo Innocenti <bernie@develer.com>
*/
/*#*
*#* $Log$
*#* Revision 1.3 2006/07/19 12:56:28 bernie
*#* Convert to new Doxygen style.
*#*
*#* Revision 1.2 2005/04/11 18:10:45 bernie
*#* Doxygen fixes.
*#*
*#* Revision 1.1 2005/03/15 00:06:30 bernie
*#* Simpler, smaller, faster.
*#*
*#*/
#include "strtol10.h"
/**
* Convert a formatted base-10 ASCII number to unsigned long binary representation.
*
* Unlike the standard strtoul(), this function has an interface
* that makes it better suited for protocol parsers. It's also
* much simpler and smaller than a full featured strtoul().
*
* \param first Pointer to first byte of input range (STL-style).
* \param last Pointer to end of input range (STL-style).
* Pass NULL to parse up to the first \\0.
* \param val Pointer to converted value.
*
* \return true for success, false for failure.
*
* \see strtol10()
*/
bool strtoul10(const char *first, const char *last, unsigned long *val)
{
// Check for no input
if (*first == '\0')
return false;
*val = 0;
for(/*nop*/; first != last && *first != '\0'; ++first)
{
if ((*first < '0') || (*first > '9'))
return false;
*val = (*val * 10L) + (*first - '0');
}
return true;
}
/**
* Convert a formatted base-10 ASCII number to signed long binary representation.
*
* \see strtoul10()
*/
bool strtol10(const char *first, const char *last, long *val)
{
bool negative = false;
if (*first == '+')
++first; /* skip unary plus sign */
else if (*first == '-')
{
negative = true;
++first;
}
bool result = strtoul10(first, last, (unsigned long *)val);
if (negative)
*val = - *val;
return result;
}