Convert to new Doxygen style.
[bertos.git] / mware / strtol10.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief Poor man's hex arrays (implementation).
9  *
10  * \version $Id$
11  * \author Bernardo Innocenti <bernie@develer.com>
12  */
13
14 /*#*
15  *#* $Log$
16  *#* Revision 1.3  2006/07/19 12:56:28  bernie
17  *#* Convert to new Doxygen style.
18  *#*
19  *#* Revision 1.2  2005/04/11 18:10:45  bernie
20  *#* Doxygen fixes.
21  *#*
22  *#* Revision 1.1  2005/03/15 00:06:30  bernie
23  *#* Simpler, smaller, faster.
24  *#*
25  *#*/
26
27 #include "strtol10.h"
28
29 /**
30  * Convert a formatted base-10 ASCII number to unsigned long binary representation.
31  *
32  * Unlike the standard strtoul(), this function has an interface
33  * that makes it better suited for protocol parsers.  It's also
34  * much simpler and smaller than a full featured strtoul().
35  *
36  * \param first  Pointer to first byte of input range (STL-style).
37  * \param last   Pointer to end of input range (STL-style).
38  *               Pass NULL to parse up to the first \\0.
39  * \param val    Pointer to converted value.
40  *
41  * \return true for success, false for failure.
42  *
43  * \see strtol10()
44  */
45 bool strtoul10(const char *first, const char *last, unsigned long *val)
46 {
47         // Check for no input
48         if (*first == '\0')
49                 return false;
50
51         *val = 0;
52         for(/*nop*/; first != last && *first != '\0'; ++first)
53         {
54                 if ((*first < '0') || (*first > '9'))
55                         return false;
56
57                 *val = (*val * 10L) + (*first - '0');
58         }
59
60         return true;
61 }
62
63
64 /**
65  * Convert a formatted base-10 ASCII number to signed long binary representation.
66  *
67  * \see strtoul10()
68  */
69 bool strtol10(const char *first, const char *last, long *val)
70 {
71         bool negative = false;
72
73         if (*first == '+')
74                 ++first; /* skip unary plus sign */
75         else if (*first == '-')
76         {
77                 negative = true;
78                 ++first;
79         }
80
81         bool result = strtoul10(first, last, (unsigned long *)val);
82
83         if (negative)
84                 *val = - *val;
85
86         return result;
87 }
88