X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/blobdiff_plain/0a2739fe97762f57a80599ef170df98e07422a3d..61db0fa798876deeb90fc27f4ebdc207d3a53457:/practicas/pipi/src/ipaddr.cpp diff --git a/practicas/pipi/src/ipaddr.cpp b/practicas/pipi/src/ipaddr.cpp index 1cdd573..5d9bfdf 100644 --- a/practicas/pipi/src/ipaddr.cpp +++ b/practicas/pipi/src/ipaddr.cpp @@ -21,7 +21,7 @@ IPAddr::IPAddr(atom a1, atom a2, atom a3, atom a4) } /// Constructor -IPAddr::IPAddr(int ip) +IPAddr::IPAddr(uint32_t ip) { atoms[0] = ip >> 24; atoms[1] = ip >> 16; @@ -29,6 +29,7 @@ IPAddr::IPAddr(int ip) atoms[3] = ip; } +#if 0 /// Constructor IPAddr::IPAddr(const char* ip) throw (std::logic_error) { @@ -41,9 +42,23 @@ IPAddr::IPAddr(const char* ip) throw (std::logic_error) atoms[i] = std::atoi(ips.c_str()); } } +#endif + +/// Constructor +IPAddr::IPAddr(const std::string& ip) throw (std::logic_error) +{ + std::istringstream iss(ip); + std::string ips; + for (int i = 0; i < 4; ++i) + { + if (!std::getline(iss, ips, '.')) + throw std::logic_error("Dirección IP inválida"); + atoms[i] = std::atoi(ips.c_str()); + } +} /// Operador de casteo a unsigned -IPAddr::operator unsigned () const +IPAddr::operator uint32_t () const { return (atoms[0] << 24) + (atoms[1] << 16) + (atoms[2] << 8) + atoms[3]; }