MPD  0.20.6
NumberParser.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009-2013 Max Kellermann <max.kellermann@gmail.com>
3  * http://www.musicpd.org
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef NUMBER_PARSER_HXX
32 #define NUMBER_PARSER_HXX
33 
34 #include <assert.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 
38 static inline unsigned
39 ParseUnsigned(const char *p, char **endptr=nullptr, int base=10)
40 {
41  assert(p != nullptr);
42 
43  return (unsigned)strtoul(p, endptr, base);
44 }
45 
46 static inline int
47 ParseInt(const char *p, char **endptr=nullptr, int base=10)
48 {
49  assert(p != nullptr);
50 
51  return (int)strtol(p, endptr, base);
52 }
53 
54 static inline uint64_t
55 ParseUint64(const char *p, char **endptr=nullptr, int base=10)
56 {
57  assert(p != nullptr);
58 
59  return strtoull(p, endptr, base);
60 }
61 
62 static inline int64_t
63 ParseInt64(const char *p, char **endptr=nullptr, int base=10)
64 {
65  assert(p != nullptr);
66 
67  return strtoll(p, endptr, base);
68 }
69 
70 static inline double
71 ParseDouble(const char *p, char **endptr=nullptr)
72 {
73  assert(p != nullptr);
74 
75  return (double)strtod(p, endptr);
76 }
77 
78 static inline float
79 ParseFloat(const char *p, char **endptr=nullptr)
80 {
81  return (float)ParseDouble(p, endptr);
82 }
83 
84 #endif
static int ParseInt(const char *p, char **endptr=nullptr, int base=10)
static float ParseFloat(const char *p, char **endptr=nullptr)
static uint64_t ParseUint64(const char *p, char **endptr=nullptr, int base=10)
static double ParseDouble(const char *p, char **endptr=nullptr)
static unsigned ParseUnsigned(const char *p, char **endptr=nullptr, int base=10)
static int64_t ParseInt64(const char *p, char **endptr=nullptr, int base=10)