#ifndef INCLUDED_BOBCAT_HOSTENT_
#define INCLUDED_BOBCAT_HOSTENT_

#include <netdb.h>
#include <string>

namespace FBB
{

class Hostent: private hostent
{
    union PTR
    {
        char const * const *p2;
        char const *p1;
    };

    size_t d_nAliases;
    size_t d_nAddresses;

    public:
        Hostent();
        Hostent(Hostent const &other);                      // hostent1.f
        Hostent(Hostent &&tmp);
        explicit Hostent(hostent const *other);

        ~Hostent();                                         // destructor.f

        Hostent &operator=(Hostent const &other);
        Hostent &operator=(Hostent &&tmp);                  // opis.f

        char const *alias(size_t nr) const;                 // .f
        char const *binaryAddress(size_t nr) const;         // .f length bytes!
        char const * const *beginAlias() const;             // .f

        std::string dottedDecimalAddress(size_t nr) const;

        char const * const *endAlias() const;               // .f
        char const *hostname() const;                       // .f
        size_t nAliases() const;                            // .f
        size_t nAddresses() const;                          // .f
        size_t addressLength() const;                       // .f
        size_t addressType() const;                         // .f

        void swap(Hostent &other);

    private:
        static char *xstrdup(char const *src);

        void copy(hostent const *other, size_t n_aliases,
                                         size_t n_addresses);
        void destroy();
                                            // the count functions also count
                                            // the final 0-values.
        size_t countAliases(char const * const *alias) const;
        size_t countAddresses(char const * const *addresses,
                                            size_t length) const;
};

inline size_t Hostent::addressLength() const
{
    return h_length;
}
inline size_t Hostent::addressType() const
{
    return h_addrtype;
}
inline char const *Hostent::alias(size_t nr) const
{
    return nr >= d_nAliases ? 0 : h_aliases[nr];
}
inline char const * const *Hostent::beginAlias() const
{
    return h_aliases;
}
inline char const *Hostent::binaryAddress(size_t nr) const
{
    return nr >= nAddresses() ? 0 : h_addr_list[nr];
}
inline Hostent::~Hostent()
{
    destroy();
}
inline char const * const *Hostent::endAlias() const
{
    return h_aliases + nAliases();
}
inline Hostent::Hostent(Hostent const &other)
{
    copy(&other, other.d_nAliases, other.d_nAddresses);
}
inline char const *Hostent::hostname() const
{
    return h_name;
}
inline size_t Hostent::nAddresses() const
{
    return d_nAddresses - 1;
}
inline size_t Hostent::nAliases() const
{
    return d_nAliases - 1;
}
inline Hostent &Hostent::operator=(Hostent &&tmp)
{
    swap(tmp);
    return *this;
}

}   // FBB

#endif
