]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Parsing.cc
Updated documentation
[thirdparty/squid.git] / src / Parsing.cc
CommitLineData
c8f4eac4 1/*
262a0e14 2 * $Id$
c8f4eac4 3 *
4 * DEBUG: section 3 Configuration File Parsing
5 * AUTHOR: Harvest Derived
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
26ac0430 23 *
c8f4eac4 24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
26ac0430 28 *
c8f4eac4 29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
27bc2077
AJ
35#include "config.h"
36#include "compat/strtoll.h"
c8f4eac4 37#include "Parsing.h"
38
39/*
40 * These functions is the same as atoi/l/f, except that they check for errors
41 */
42
0e656b69 43double
44xatof(const char *token)
c8f4eac4 45{
46 char *end;
0e656b69 47 double ret = strtod(token, &end);
c8f4eac4 48
49 if (ret == 0 && end == token)
50 self_destruct();
51
52 return ret;
53}
54
55int
56xatoi(const char *token)
57{
58 return xatol(token);
59}
60
0e656b69 61long
62xatol(const char *token)
63{
64 char *end;
65 long ret = strtol(token, &end, 10);
66
67 if (end == token || *end)
68 self_destruct();
69
70 return ret;
71}
72
73unsigned short
74xatos(const char *token)
75{
76 long port = xatol(token);
77
78 if (port & ~0xFFFF)
79 self_destruct();
80
81 return port;
82}
83
b1fb3348
AJ
84int64_t
85GetInteger64(void)
86{
87 char *token = strtok(NULL, w_space);
88 int i;
89
90 if (token == NULL)
91 self_destruct();
92
93 i = strtoll(token, NULL, 10);
94
95 return i;
96}
97
c8f4eac4 98int
99GetInteger(void)
100{
101 char *token = strtok(NULL, w_space);
102 int i;
103
104 if (token == NULL)
105 self_destruct();
106
60b522a9
AR
107 // %i honors 0 and 0x prefixes, which are important for things like umask
108 if (sscanf(token, "%i", &i) != 1)
c8f4eac4 109 self_destruct();
110
111 return i;
112}
113
0e656b69 114u_short
115GetShort(void)
116{
117 char *token = strtok(NULL, w_space);
118
119 if (token == NULL)
120 self_destruct();
121
122 return xatos(token);
123}
124
053b1f59 125bool
126StringToInt(const char *s, int &result, const char **p, int base)
127{
128 if (s) {
129 char *ptr = 0;
130 const int h = (int) strtol(s, &ptr, base);
131
132 if (ptr != s && ptr) {
133 result = h;
134
135 if (p)
136 *p = ptr;
137
138 return true;
139 }
140 }
141
142 return false;
143}
47f6e231 144
145bool
146StringToInt64(const char *s, int64_t &result, const char **p, int base)
147{
148 if (s) {
149 char *ptr = 0;
150 const int64_t h = (int64_t) strtoll(s, &ptr, base);
151
152 if (ptr != s && ptr) {
153 result = h;
154
155 if (p)
156 *p = ptr;
157
158 return true;
159 }
160 }
161
162 return false;
163}
82b7abe3
AJ
164
165bool
b7ac5457 166GetHostWithPort(char *token, Ip::Address *ipa)
82b7abe3
AJ
167{
168 char *t;
169 char *host;
170 char *tmp;
171 unsigned short port;
172
173 host = NULL;
174 port = 0;
175
176#if USE_IPV6
177 if (*token == '[') {
178 /* [host]:port */
179 host = token + 1;
180 t = strchr(host, ']');
181 if (!t)
182 return false;
183 *t++ = '\0';
184 if (*t != ':')
185 return false;
186 port = xatos(t + 1);
187 } else
188#endif
189 if ((t = strchr(token, ':'))) {
190 /* host:port */
191 host = token;
192 *t = '\0';
193 port = xatos(t + 1);
194
195 if (0 == port)
196 return false;
197 } else if ((port = strtol(token, &tmp, 10)), !*tmp) {
198 /* port */
199 } else {
200 host = token;
201 port = 0;
202 }
203
204 if (NULL == host)
205 ipa->SetAnyAddr();
206 else if ( ipa->GetHostByName(host) ) /* dont use ipcache. Accept either FQDN or IPA. */
207 (void) 0;
208 else
209 return false;
210
211 /* port MUST be set after the IPA lookup/conversion is performed. */
212 ipa->SetPort(port);
213
214 return true;
215}