1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2015 Couchbase, Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #ifndef SRC_COMMON_H
19 #define SRC_COMMON_H 1
20 
21 #include "config.h"
22 
23 #include <ctype.h>
24 #include <errno.h>
25 #include <inttypes.h>
26 #include <limits.h>
27 #include <math.h>
28 #include <memcached/engine.h>
29 #include <platform/platform.h>
30 
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include <list>
36 #include <sstream>
37 #include <utility>
38 #include <vector>
39 
40 #include "ep_time.h"
41 
42 
43 /* Linux' limits don't bring this in in c++ mode without doing weird
44    stuff.  It's a known constant, so we'll just make it if we don't
45    have it. */
46 #ifndef UINT16_MAX
47 #define UINT16_MAX 65535
48 #endif /* UINT16_MAX */
49 
parseUint16(const char *in, uint16_t *out)50 inline bool parseUint16(const char *in, uint16_t *out) {
51     if (out == nullptr) {
52         return false;
53     }
54 
55     errno = 0;
56     *out = 0;
57     char *endptr;
58     long num = strtol(in, &endptr, 10);
59     if (errno == ERANGE || num < 0 || num > (long)UINT16_MAX) {
60         return false;
61     }
62     if (isspace(*endptr) || (*endptr == '\0' && endptr != in)) {
63         *out = static_cast<uint16_t>(num);
64         return true;
65     }
66     return false;
67 }
68 
parseUint32(const char *str, uint32_t *out)69 inline bool parseUint32(const char *str, uint32_t *out) {
70     char *endptr = NULL;
71     if (out == nullptr || str == nullptr) {
72         return false;
73     }
74     *out = 0;
75     errno = 0;
76 
77     unsigned long l = strtoul(str, &endptr, 10);
78     if (errno == ERANGE) {
79         return false;
80     }
81 
82     if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
83         if ((long) l < 0) {
84             /* only check for negative signs in the uncommon case when
85              * the unsigned number is so big that it's negative as a
86              * signed number. */
87             if (strchr(str, '-') != NULL) {
88                 return false;
89             }
90         }
91         *out = l;
92         return true;
93     }
94 
95     return false;
96 }
97 
parseInt64(const char *str, int64_t *out)98 inline bool parseInt64(const char *str, int64_t *out) {
99     if (out == nullptr) {
100         return false;
101     }
102     errno = 0;
103     *out = 0;
104     char *endptr;
105 
106     int64_t ll = strtoll(str, &endptr, 10);
107     if (errno == ERANGE) {
108         return false;
109     }
110 
111     if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
112         *out = static_cast<int64_t>(ll);
113         return true;
114     }
115 
116     return false;
117 }
118 
119 #define xisspace(c) isspace((unsigned char)c)
parseUint64(const char *str, uint64_t *out)120 inline bool parseUint64(const char *str, uint64_t *out) {
121     if (out == nullptr) {
122         return false;
123     }
124     errno = 0;
125     *out = 0;
126     char *endptr;
127     uint64_t ull = strtoull(str, &endptr, 10);
128     if (errno == ERANGE)
129         return false;
130     if (xisspace(*endptr) || (*endptr == '\0' && endptr != str)) {
131         if ((int64_t) ull < 0) {
132             /* only check for negative signs in the uncommon case when
133              * the unsigned number is so big that it's negative as a
134              * signed number. */
135             if (strchr(str, '-') != NULL) {
136                 return false;
137             }
138         }
139         *out = ull;
140         return true;
141     }
142     return false;
143 }
144 
145 #endif  // SRC_COMMON_H_
146