From: Leandro Lucarella Date: Wed, 21 Jun 2006 16:17:54 +0000 (+0000) Subject: Se ponen mejor los tipos de ipaddr y se reemplaza el constructor desde char* por X-Git-Tag: svn_import~38 X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/commitdiff_plain/474e7d1c9c79d5f252c89fddc02058bc65668693 Se ponen mejor los tipos de ipaddr y se reemplaza el constructor desde char* por el de un std::string. --- diff --git a/practicas/pipi/src/ip.cpp b/practicas/pipi/src/ip.cpp index f3fed6e..b131524 100644 --- a/practicas/pipi/src/ip.cpp +++ b/practicas/pipi/src/ip.cpp @@ -132,7 +132,7 @@ void add_routes(RouteTable& rt, std::istream& is, Dev& dev) iss >> net >> gw >> mtu >> metric; if (net == "0") net = "0.0.0.0"; if (gw == "0") gw = "0.0.0.0"; - rt.add(net.c_str(), gw.c_str(), metric, mtu, dev); + rt.add(net, gw, metric, mtu, dev); } } diff --git a/practicas/pipi/src/ipaddr.cpp b/practicas/pipi/src/ipaddr.cpp index 7b153b2..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,15 +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) -//{ -// IPAddr(ip.c_str()); -//} +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]; } diff --git a/practicas/pipi/src/ipaddr.h b/practicas/pipi/src/ipaddr.h index 0c0c549..b5c9cea 100644 --- a/practicas/pipi/src/ipaddr.h +++ b/practicas/pipi/src/ipaddr.h @@ -4,13 +4,14 @@ #include #include #include +#include /// Dirección IP struct IPAddr { /// Átomo de dirección IP - typedef unsigned char atom; + typedef uint8_t atom; /// Representación interna atom atoms[4]; @@ -22,16 +23,16 @@ struct IPAddr IPAddr(atom a1, atom a2, atom a3, atom a4); /// Constructor - IPAddr(int ip); + IPAddr(uint32_t ip); /// Constructor - IPAddr(const char* ip) throw (std::logic_error); + //IPAddr(const char* ip) throw (std::logic_error); /// Constructor - //IPAddr(const std::string& ip) throw (std::logic_error); + IPAddr(const std::string& ip) throw (std::logic_error); /// Operador de casteo a unsigned - operator unsigned () const; + operator uint32_t () const; /// Operador de casteo a std::string operator std::string () const; diff --git a/practicas/pipi/src/test_ipout.cpp b/practicas/pipi/src/test_ipout.cpp index 255416e..1f11fc1 100644 --- a/practicas/pipi/src/test_ipout.cpp +++ b/practicas/pipi/src/test_ipout.cpp @@ -69,7 +69,7 @@ void add_routes(RouteTable& rt, std::istream& is, size_t mtu, Dev& dev) iss >> net >> gw >> metric; if (net == "0") net = "0.0.0.0"; if (gw == "0") gw = "0.0.0.0"; - rt.add(net.c_str(), gw.c_str(), mtu, metric, dev); + rt.add(net, gw, mtu, metric, dev); } }