/* ---------------------------------------------------------------------
   (c) ED 2004
   Project      : CLIB
   Function     : IPV4
   Module       : IPV4
   File         : ipv4.c
   Created      : 09-09-2004
   Modified     : 26-08-2008
   --------------------------------------------------------------------- */

/* ---------------------------------------------------------------------
   Log

   1.1 26-08-2008 ipv4_str2num() accepts a NULL pointer as second parameter
   1.0 09-09-2004 Initial version
   0.0 09-09-2004 Created

   --------------------------------------------------------------------- */

#ifdef __cplusplus
#error This is not C++. Please use a C compiler.
#endif

#include "ed/inc/ipv4.h"
#include "ed/inc/sys.h"
#include "ed/inc/sysalloc.h"
#include <stdio.h>

/* macros ============================================================== */
/* constants =========================================================== */
/* types =============================================================== */
/* structures ========================================================== */
/* private data ======================================================== */
/* private functions =================================================== */
/* internal public data ================================================ */
/* internal public functions =========================================== */
/* entry points ======================================================== */

/* ---------------------------------------------------------------------
   ipv4_num2str()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
int ipv4_num2str (char *s, size_t size, ipv4_s const *pip)
{
   int err;

   if (size >= IPV4_STR_SIZE)
   {
      sprintf (s, IPV4_FMT, pip->ip1, pip->ip2, pip->ip3, pip->ip4);
      err = 0;
   }
   else
   {
      err = 1;
   }

   return err;
}

/* ---------------------------------------------------------------------
   ipv4_str2num()
   ---------------------------------------------------------------------

   ---------------------------------------------------------------------
   I:
   O:
   --------------------------------------------------------------------- */
int ipv4_str2num (char const *s, ipv4_s * pip)
{
   int err = 1;

   if (s != NULL)
   {
      int dyn = 0;
      if (pip == NULL)
      {
         pip = malloc (sizeof *pip);
         dyn = pip != NULL;
      }

      if (pip != NULL)
      {
         CLR (pip, ipv4_s);

         {
            int n =
               sscanf (s, IPV4_FMT, &pip->ip1, &pip->ip2, &pip->ip3,
                       &pip->ip4);

            err = n == 0;
         }

         if (dyn)
         {
            free (pip), pip = NULL;
         }
      }
   }
   return err;
}

/* public data ========================================================= */
