/* Weather station library module.
   Rick Smereka, Copyright (C) 2002.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, get a copy via the Internet at
   http://gnu.org/copyleft/gpl.html or write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
   MA 02111-1307 USA

   You can contact the author via email at rsmereka@future-lab.com
   
   Original Linux version. Aug/2002, Rick Smereka

   Added function 'wlib_wlink_pack_datime' and include of
   'weather.h'. Dec/2002, Rick Smereka */

#include "stdhead.h"
#include "weather.h"
#include "wlib.h"

/* module data */

static char *wdesc[] = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE",
                         "SSE", "S", "SSW", "SW", "WSW", "W", "WNW",
                         "NW", "NNW" };
static char *wdeg[] = { "0.0", "22.5", "45.0", "67.5", "90.0", "112.5",
                        "135.0", "157.5", "180.0", "202.5", "225.0",
                        "247.5", "270.0", "292.5", "315.0", "337.5" };

int wlib_wdir_desc_2_deg(char *ddesc, struct fxp **ddeg)
{
   /* Convert a wind direction as an alpha description to
      degrees. There are sixteen possible alpha descriptions.
      Function returns 'TRUE' upon success with the wind
      direction in degrees loaded into the newly allocated
      fxp structure 'ddeg' (caller is responsible to
      de-allocate this). Function returns 'FALSE' otherwise. */

   struct fxp *result;
   int i;

   if (ddesc == (char *)NULL || !strlen(ddesc))
      return(FALSE);

   for(i = 0; i < 16; i++)
      {
      if (!stricmp(ddesc, wdesc[i]))
         {
         if ((result = fxp_iconv(wdeg[i])) == FXP_OT_NULL)
            return(FALSE);

         *ddeg = result;
         return(TRUE);
         }
      }

   return(FALSE);
}

int wlib_get_field(char *line, char *outfield, int which, char delim)
{
   /* Obtain a 'delim' delimited field within the text 'line' of a
      'WeatherLink' report. Upon success, the result field will be
      loaded into 'outfield' which must already be allocated to
      sufficient size by the caller. Function returns a 'wlib_get_field'
      code which are defined in 'wlib.h'. */

   char *tfield;

   if (line == (char *)NULL || !strlen(line))
      return(WGF_PARM_ERROR);

   if (outfield == (char *)NULL)
      return(WGF_PARM_ERROR);

   outfield[0] = EOS;

   if (which > ll_words(line, delim))
      return(WGF_NO_SUCH_FIELD);

   if ((tfield = initstring(line)) == (char *)NULL)
      return(WGF_MEMORY_FAIL);

   if (!ll_word(line, tfield, which, delim))
      {
      free(tfield);
      return(WGF_INTERNAL_ERROR);
      }

   /* an empty field consists of two hyphens in a row */

   if (!strcmp(tfield, "--"))
      {
      free(tfield);
      return(WGF_BLANK);
      }

   trim(tfield, outfield);
   free(tfield);
   return(WGF_OK);
}

int wlib_report_trailer(FILE *out, char *apname, char *ver, int do_copy)
{
   /* Place a standard trailer on a weather report with or
      without a copyright notice. If the copyright notice
      is turned off, the program platform will not be 
      reported. Function returns 'TRUE'
      upon success, 'FALSE' otherwise. */

   char tbuf[25];
   int ryear;

   get_ftime(tbuf);
   
   if (out == (FILE *)NULL)
      return(FALSE);

   fprintf(out, "\nLast Updated %s EDT/EST.\n", tbuf);
   fprintf(out, "This report was generated by %s", apname);

   if (ver != (char *)NULL && strlen(ver))
      fprintf(out, " version %s", ver);

   if (do_copy)
      {
      fprintf(out, " for ");
   
#ifdef OS_UNIX
      fprintf(out, "%s.\n", SUB_PLATFORM_STRING);
#else
      fprintf(out, "%s.\n", PLATFORM_STRING);
#endif

      ryear = run_year();
      fprintf(out, "Copyright (C) %d, Rick Smereka.\n", ryear);
      }
   else
      fprintf(out, ".\n");

   return(TRUE);
}

int wlib_wlink_pack_datime(char *line, char *datime, char delim)
{
   /* Take the date and time in the WeatherLink report and pack
      them into a string in the form yyyymmddhhmm. This is the
      form used by VWS. VWS uses the Windoze 'regional settings'
      to determine the date and time format. My Windoze machine
      uses 'yy-mm-dd' and 'hh:mm[a|p]'. Function 
      returns 'TRUE' upon success with the packed date/time
      loaded into 'datime' (which must already be allocated by
      the user), 'FALSE' otherwise. */

   char tmp[20], wlinktime[15], vwstime[15];
   char wlinkdate[9];
   int ret, nwords, year, month, day;

   if (datime == (char *)NULL)
      return(FALSE);

   /* get date first */

   if ((ret = wlib_get_field(line, wlinkdate, WLINK_FIELD_DATE, delim)) !=
       WGF_OK)
      return(FALSE);

   if (ll_words(wlinkdate, '-') != 3)
      return(FALSE);

   if (!ll_word(wlinkdate, tmp, 1, '-'))
      return(FALSE);

   if (!qatoi(tmp, &year))
      return(FALSE);

   if (!ll_word(wlinkdate, tmp, 2, '-'))
      return(FALSE);

   if (!qatoi(tmp, &month))
      return(FALSE);

   if (!ll_word(wlinkdate, tmp, 3, '-'))
      return(FALSE);

   if (!qatoi(tmp, &day))
      return(FALSE);

   /* get time next */

   if ((ret = wlib_get_field(line, wlinktime, WLINK_FIELD_TIME, delim)) !=
       WGF_OK)
      return(FALSE);

   /* convert from 12 hour to 24 hour */

   if (!datime_hour12_2_hour24(wlinktime, vwstime, FALSE))
      return(FALSE);

   sprintf(datime, "%d%02d%02d%s", year + 2000, month, day, vwstime);
   return(TRUE);
}
