All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ip_address.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009 zooml.com
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 #include "ip_address.h"
23 #include <sstream>
24 #include <stdexcept>
25 namespace te
26 {
27  namespace common
28  {
29  namespace uri
30  {
31  const char ip_address::SEPARATOR_CHAR = '.';
33  octets_[0] = 0;
34  octets_[1] = 0;
35  octets_[2] = 0;
36  octets_[3] = 0;
37  }
38  ip_address::ip_address(const std::string& v) {
39  std::string::const_iterator first = v.begin();
40  if (!parse(first, v.end(), *this) || first != v.end())
41  throw std::invalid_argument("invalid IP address: \"" + v + "\"");
42  }
43  std::string ip_address::string() const {
44  std::ostringstream oss;
45  operator <<(oss);
46  return oss.str();
47  }
48  std::ostream& ip_address::operator <<(std::ostream& os) const {
49  return os << (int)octets_[0] << SEPARATOR_CHAR << (int)octets_[1] << SEPARATOR_CHAR << (int)octets_[2] << SEPARATOR_CHAR << (int)octets_[3];
50  }
51  bool parse(std::string::const_iterator& first, std::string::const_iterator last, ip_address& v) {
52  std::string::const_iterator f = first;
53  ip_address ip;
54  for (size_t i = 0; i < 4; ++i) {
55  if (f == last)
56  return false;
57  if (i && (*f++ != ip_address::SEPARATOR_CHAR || f == last))
58  return false;
59  int n;
60  unsigned int c = (unsigned int)*f;
61  if (c == '0') {
62  n = 0;
63  ++f;
64  } else {
65  if (c < '1' || '9' < c) // 1st dig.
66  return false;
67  n = c - '0';
68  if (++f != last) {
69  c = (unsigned int)*f;
70  if ('0' <= c && c <= '9') { // 2nd dig.
71  n = n * 10 + (c - '0');
72  if (++f != last) {
73  c = (unsigned int)*f;
74  if ('0' <= c && c <= '9') { // 3rd dig.
75  n = n * 10 + (c - '0');
76  if (255 < n)
77  return false;
78  ++f;
79  }
80  }
81  }
82  }
83  }
84  ip.octets_[i] = n;
85  }
86  v = ip;
87  first = f;
88  return true;
89  }
90  }
91  }
92 }
ip_address()
Construct null.
Definition: ip_address.cpp:32
std::string string() const
Calculate string.
Definition: ip_address.cpp:43
friend bool TECOMMONEXPORT parse(std::string::const_iterator &first, std::string::const_iterator last, ip_address &v)
Parse IP address, returning whether found or not and advancing first and setting address if found...
Definition: ip_address.cpp:51
unsigned char octets_[4]
Definition: ip_address.h:61
bool parse(std::string::const_iterator &first, std::string::const_iterator last, authority &v)
Parse URI authority, returning whether found or not and advancing first and setting authority if foun...
Definition: authority.cpp:156
std::ostream & operator<<(std::ostream &os) const
Stream out.
Definition: ip_address.cpp:48
static const char SEPARATOR_CHAR
separator ('.')
Definition: ip_address.h:57