This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
pair.C
168 lines (150 loc) · 3.07 KB
/
pair.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* $Id$ */
/*
*
* Copyright (C) 2002-2004 Maxwell Krohn (max@okcupid.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
#include "pair.h"
void pair_dump1 (const pair_t &p) { p.dump1 (); }
void pair_encode (encode_t *e, str sep, const pair_t &p)
{ p.encode (e, sep); }
void pair_trav (callback<void, const pair_t &>::ref cb, pair_t *p)
{ (*cb) (*p); }
void
pair_t::dump1 () const
{
warnx << key;
if (vals.size () == 0) {
warnx << "\n";
return;
}
warnx << " --> ";
if (vals[0])
warnx << vals[0];
warnx << "\n";
}
//
// XXX - todo -- fold this and the following into one function
//
vec<u_int64_t> *
pair_t::to_uint64 () const
{
vec<u_int64_t> *ret = NULL;
const char *nptr;
char *endptr;
size_t s;
switch (uis) {
case IV_ST_NONE:
s = vals.size ();
uivals.setsize (s);
for (u_int i = 0; i < s; i++) {
nptr = vals[i].cstr();
endptr = NULL;
errno = 0;
uivals[i] = strtoull (nptr, &endptr, 0);
if (*endptr || endptr == nptr) {
uis = IV_ST_FAIL;
break;
}
}
if (uis != IV_ST_FAIL) {
uis = IV_ST_OK;
ret = &uivals;
}
break;
case IV_ST_OK:
ret = &uivals;
break;
case IV_ST_FAIL:
default:
break;
}
return ret;
}
vec<int64_t> *
pair_t::to_int () const
{
vec<int64_t> *ret = NULL;
const char *nptr;
char *endptr;
size_t s;
switch (is) {
case IV_ST_NONE:
{
s = vals.size ();
ivals.setsize (s);
for (u_int i = 0; i < s; i++) {
nptr = vals[i].cstr();
endptr = NULL;
errno = 0;
ivals[i] = strtoll (nptr, &endptr, 0);
if (*endptr || endptr == nptr || errno == ERANGE) {
is = IV_ST_FAIL;
break;
}
}
if (is != IV_ST_FAIL) {
is = IV_ST_OK;
ret = &ivals;
}
break;
}
case IV_ST_OK:
ret = &ivals;
break;
case IV_ST_FAIL:
default:
break;
}
return ret;
}
bool
pair_t::to_int (int64_t *v) const
{
vec<int64_t> *p = to_int ();
if (p && p->size () > 0) {
*v = (*p)[0];
return true;
}
return false;
}
bool
pair_t::to_uint64 (u_int64_t *v) const
{
vec<u_int64_t> *p = to_uint64 ();
if (p && p->size () > 0) {
*v = (*p)[0];
return true;
}
return false;
}
bool
pair_t::to_double (double *dp) const
{
bool ret = false;
if (vals.size() > 0) {
const char *start = vals[0].cstr ();
char *ep = NULL;
double d = strtod(start, &ep);
if (ep && *ep == '\0') {
ret = true;
*dp = d;
}
}
return ret;
}