#if !NO_SCCS_STRINGS
static char sccs_dow_c[] = "@(#)dow.c	128.1 1/20/93 19:39:01";
#endif /* NO_SCCS_STRINGS */
/***********************************************************************\

   (c) Copyright 1991 by Lotus Development Corporation
   All Rights Reserved

   Module:  dow.c

   Description:   This module defines the add-in @function DOW.
   DOW returns the current day of the week.

\***********************************************************************/

#define  LPI_MAIN_MODULE    /* identify this module as the primary one */

#define USE_REGISTER    /* incorporate routines to register @functions */
#define USE_ATDATE        /* incorporate routines to access  functions */

#include "lpixport.h"          /* incorporate Lotus header information */

#define  DOW_NAME  "DOW"
#define  DAY0      "Saturday"
#define  DAY1      "Sunday"
#define  DAY2      "Monday"
#define  DAY3      "Tuesday"
#define  DAY4      "Wedneday"
#define  DAY5      "Thursday"
#define  DAY6      "Friday"

#define  FIRST_DAY  0.0
#define  LAST_DAY   73051.0

/***********************************************************************\

   Day or the week function.  Accepts a 1-2-3 date number and returns
   the corresponding day of the week label.

\**********************************************************************/

lsshort dow (RegContext, arg_count, LpiContext)
   LpiRegContextType   RegContext;   /* required registration context */
   lushort             arg_count;        /* number of input arguments */
   lptr(CONTEXT)       LpiContext;            /* required Lpi Context */
{
   lfloat8      theInputFloat;         /* storage for float arguments */
   lslong       theDay, theDayNumber;
   lsshort      retval;
   char *       theDayNames[7];

   theDayNames[0] = DAY0;
   theDayNames[1] = DAY1;
   theDayNames[2] = DAY2;
   theDayNames[3] = DAY3;
   theDayNames[4] = DAY4;
   theDayNames[5] = DAY5;
   theDayNames[6] = DAY6;

   /* get the input date number */

   retval = LpiRegGetArgNumFloat(0,&theInputFloat);
   if (retval != LPI_SUCCESS) {
      LpiRegAtSetRetType(LPI_T_REG_AT_TYPE_ERR);
      return LPI_SUCCESS;
   }
   
   /* convert to a day of the week number */

   if ((theInputFloat <= FIRST_DAY) || (theInputFloat >= LAST_DAY)) {
      LpiRegAtSetRetType(LPI_T_REG_AT_TYPE_ERR);
      return LPI_SUCCESS;
   }
   LpiPushLfloat8(&theInputFloat);
   LpiInt();
   theDayNumber = LpiPopLsLong() % 7;

   /* return the corresponding day name */

   LpiRegAtSetRetType(LPI_T_REG_AT_TYPE_STR_LMBCS);
   strcpy ((char *) LpiRegAtAccessRetData(), theDayNames[theDayNumber]); 
   return LPI_SUCCESS;
}
 
/************************************************************************\

   The following function, LpiBind, is called when 1-2-3 loads our add-in.
   In this function we load the Register module and register our @function.
   Use this function to perform any setup, such as allocating memory,
   opening files, etc.

\************************************************************************/

LpiBind(dow_b)
{
   lsshort retval;                              /* function return code */
   lushort arg_types[1];

   arg_types[0] = LPI_T_ARG_TYPE_NUM;

   LPI_SETUP_LOAD;      /* required before loading zero or more modules */

   if (LPI_LOAD_REGISTER != LPI_SUCCESS)        /* load Register module */
      LpiSignal (LPI_FAILURE);
   if (LPI_LOAD_ATDATE != LPI_SUCCESS)            /* load AtDate module */
      LpiSignal (LPI_FAILURE);

   retval = LpiRegAtfunc(
      DOW_NAME,                    /* @function name in a #define above */
      dow,                /* name of function that implements @function */
      1,                                   /* number of input arguments */
      arg_types,                       /* array of input argument types */ 
      LNULL,                                    /* registration context */
      LPI_T_REG_AT_NO_FLAGS,              /* normal argument processing */
      LPI_T_REG_AT_TYPE_STR_LMBCS);                 /* returns a string */

   /* if registration fails, indicate an error */
   if (retval != LPI_SUCCESS) LpiSignal (LPI_FAILURE); 

   LpiSignal (LPI_SUCCESS);             /* otherwise indicate no errors */
}

/************************************************************************\

   The following function, LpiUnbind, is called when 1-2-3 unloads our 
   add-in.  This function simply executes the macro LPI_UNBIND and
   signals no errors.  Use this function to perform any cleanup, such
   as deallocating memory you allocated in LpiBind, closing opened files,
   etc.

   Note: You do not have to explicitly unregister an @function.  When you
   unload the add-in that defines the function, 1-2-3 automatically
   unregisters any @functions the add-in defined.  If you attempt to enter
   an @function that has not yet been registered, 1-2-3 beeps. All existing
   formulas that contain add-in @functions that are currently 
   unregistered resolve to ERR.  The @functions will resolve
   properly the next time the add-in registers the @function.

/************************************************************************/

LpiUnbind(dow_u)
{
   LPI_UNLOAD;                                    /* macro for clean-up */

   LpiSignal(LPI_SUCCESS);                        /* indicate no errors */
}

