/************************************************************
**
** Application: UnitConv
**
** Title:       c.defaultdata
**
*************************************************************/

/*
*
* Copyright (c) 2015, Chris Johnson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the copyright holder nor the names of their
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

/* Include files */
/* from standard clib */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* from oslib: */
#include "OSLib:toolbox.h"
#include "OSLib:wimp.h"


/* from oslib support */
#include "OSLibSupport:Err.h"
#include "OSLibSupport:x.h"
#include "OSLibSupport:message.h"

/* from CJLib */
#include "CJLib:message.h"

/* from application */
#include "cjoslib.h"
#include "res.h"
#include "msglink.h"
#include "main.h"
#include "defaultdata.h"

/* global vars */


/* The functions */

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

osbool defaultdata_fill_special_arrays ( void )
{
  conv_group *ptr ;
  int s, i;

  ptr = ( conv_group * ) malloc ( sizeof ( struct conv_group ) ) ;
    if ( ptr == NULL )
    {
      //flag error and return
      Msg_Warn ( DATANOMEM ) ;
      return (FALSE);
    }


  strcpy ( ptr->group_name, "Special");
  /* first set */
  s=0;
  strcpy ( ptr->set[s].set_name, "Temperature");
  ptr->set[s].no_of_items = 4;
  i=0;
  strcpy(ptr->set[s].item[i].unit_name, "Kelvin");
  ptr->set[s].item[i++].factor = 1;
  strcpy(ptr->set[s].item[i].unit_name, "Centigrade");
  ptr->set[s].item[i++].factor = 1;
  strcpy(ptr->set[s].item[i].unit_name, "Fahrenheit");
  ptr->set[s].item[i++].factor = 1;
  strcpy(ptr->set[s].item[i].unit_name, "'Gas mark'");
  ptr->set[s].item[i++].factor = 1;
  s++;
  /* now more sets, if any */

  ptr->no_of_sets = s;

  uc_data.group [ uc_data.no_of_groups ] = ptr;
  uc_data.no_of_groups +=1 ;

  return (TRUE);
}





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

osbool defaultdata_fill_default_arrays ( void )
{
  conv_group *ptr ;
  int s, i;

  /* Got here because of a problem with the unitsdata file */
  /* Has any memory been allocated? */
  /* If so, release it before carrying on */

  for ( i = 0; i <= MAX_UC_MENU_ENTRIES; i++ )
  {
    if (uc_data.group [i] != NULL)
    {
      free (uc_data.group [i]);
      uc_data.group [i] = NULL;
    }
  }

  /* Now we can allocate memory for default arrays */
  ptr = ( conv_group * ) malloc ( sizeof ( struct conv_group ) ) ;
  if ( ptr == NULL )
  {
    //flag error and return
    Msg_Warn ( DATANOMEM ) ;
    return (FALSE);
  }

  strcpy ( ptr->group_name, "Defaults");
  ptr->no_of_sets = 8;
  /* first set */
  s=0;
  strcpy ( ptr->set[s].set_name, "Length");
  ptr->set[s].no_of_items = 6;
  i=0;
  strcpy(ptr->set[s].item[i].unit_name, "cm");
  ptr->set[s].item[i++].factor = 100;
  strcpy(ptr->set[s].item[i].unit_name, "metre");
  ptr->set[s].item[i++].factor = 1;
  strcpy(ptr->set[s].item[i].unit_name, "km");
  ptr->set[s].item[i++].factor = 1E-3;
  strcpy(ptr->set[s].item[i].unit_name, "in");
  ptr->set[s].item[i++].factor = 39.3700787;
  strcpy(ptr->set[s].item[i].unit_name, "ft");
  ptr->set[s].item[i++].factor = 3.28083989;
  strcpy(ptr->set[s].item[i].unit_name, "mile");
  ptr->set[s].item[i++].factor = 6.21371192E-4;
  /* second set */
  s++;
  strcpy ( ptr->set[s].set_name, "Volume");
  ptr->set[s].no_of_items = 5;
  i=0;
  strcpy(ptr->set[s].item[i].unit_name, "litre (L)");
  ptr->set[s].item[i++].factor = 1;
  strcpy(ptr->set[s].item[i].unit_name, "pint");
  ptr->set[s].item[i++].factor = 1.759804;
  strcpy(ptr->set[s].item[i].unit_name, "gallon");
  ptr->set[s].item[i++].factor = 0.2199755;
  strcpy(ptr->set[s].item[i].unit_name, "cubic metre");
  ptr->set[s].item[i++].factor = 1E-3;
  strcpy(ptr->set[s].item[i].unit_name, "cubic foot");
  ptr->set[s].item[i++].factor = 0.03531566;
  /* set 3 */
  s++;
  strcpy ( ptr->set[s].set_name, "Area (large)");
  ptr->set[s].no_of_items = 5;
  i=0;
  strcpy(ptr->set[s].item[i].unit_name, "square m");
  ptr->set[s].item[i++].factor = 10000;
  strcpy(ptr->set[s].item[i].unit_name, "hectare");
  ptr->set[s].item[i++].factor = 1;
  strcpy(ptr->set[s].item[i].unit_name, "square km");
  ptr->set[s].item[i++].factor = 0.01;
  strcpy(ptr->set[s].item[i].unit_name, "acre");
  ptr->set[s].item[i++].factor = 0.404685644;
  strcpy(ptr->set[s].item[i].unit_name, "square mile");
  ptr->set[s].item[i++].factor = 3.8610216E-3;
  /* set 4 */
  s++;
  strcpy ( ptr->set[s].set_name, "Area (small)");
  ptr->set[s].no_of_items = 5;
  i=0;
  strcpy(ptr->set[s].item[i].unit_name, "square cm");
  ptr->set[s].item[i++].factor = 10000;
  strcpy(ptr->set[s].item[i].unit_name, "square m");
  ptr->set[s].item[i++].factor = 1;
  strcpy(ptr->set[s].item[i].unit_name, "square in");
  ptr->set[s].item[i++].factor = 1550.0031;
  strcpy(ptr->set[s].item[i].unit_name, "square ft");
  ptr->set[s].item[i++].factor = 10.76391;
  strcpy(ptr->set[s].item[i].unit_name, "square yd");
  ptr->set[s].item[i++].factor = 1.19599;
  /* set 5 */
  s++;
  strcpy ( ptr->set[s].set_name, "Mass");
  ptr->set[s].no_of_items = 4;
  i=0;
  strcpy(ptr->set[s].item[i].unit_name, "g");
  ptr->set[s].item[i++].factor = 1;
  strcpy(ptr->set[s].item[i].unit_name, "oz");
  ptr->set[s].item[i++].factor = 0.035273962;
  strcpy(ptr->set[s].item[i].unit_name, "kg");
  ptr->set[s].item[i++].factor = 1E-3;
  strcpy(ptr->set[s].item[i].unit_name, "lb");
  ptr->set[s].item[i++].factor = 0.0022046226;
  /* set 6 */
  s++;
  strcpy ( ptr->set[s].set_name, "Pressure");
  ptr->set[s].no_of_items = 6;
  i=0;
  strcpy(ptr->set[s].item[i].unit_name, "atmosphere");
  ptr->set[s].item[i++].factor = 1;
  strcpy(ptr->set[s].item[i].unit_name, "Pa or N/m");
  ptr->set[s].item[i++].factor = 101325;
  strcpy(ptr->set[s].item[i].unit_name, "torr or mm Hg");
  ptr->set[s].item[i++].factor = 760;
  strcpy(ptr->set[s].item[i].unit_name, "mbar");
  ptr->set[s].item[i++].factor = 1013.25;
  strcpy(ptr->set[s].item[i].unit_name, "psi");
  ptr->set[s].item[i++].factor = 14.696;
  strcpy(ptr->set[s].item[i].unit_name, "kg/cm");
  ptr->set[s].item[i++].factor = 1.03323;
  /* set 7 */
  s++;
  strcpy ( ptr->set[s].set_name, "Velocity");
  ptr->set[s].no_of_items = 4;
  i=0;
  strcpy(ptr->set[s].item[i].unit_name, "m/s");
  ptr->set[s].item[i++].factor = 1;
  strcpy(ptr->set[s].item[i].unit_name, "ft/s");
  ptr->set[s].item[i++].factor = 3.28083989;
  strcpy(ptr->set[s].item[i].unit_name, "km/hr");
  ptr->set[s].item[i++].factor = 3.6;
  strcpy(ptr->set[s].item[i].unit_name, "miles/hr");
  ptr->set[s].item[i++].factor = 2.23693629;
  /* set 8 */
  s++;
  strcpy ( ptr->set[s].set_name, "Time");
  ptr->set[s].no_of_items = 4;
  i=0;
  strcpy(ptr->set[s].item[i].unit_name, "seconds");
  ptr->set[s].item[i++].factor = 86400;
  strcpy(ptr->set[s].item[i].unit_name, "minutes");
  ptr->set[s].item[i++].factor = 1440;
  strcpy(ptr->set[s].item[i].unit_name, "Hours");
  ptr->set[s].item[i++].factor = 24;
  strcpy(ptr->set[s].item[i].unit_name, "Days");
  ptr->set[s].item[i++].factor = 1;

  uc_data.group [0] = ptr;
  uc_data.no_of_groups =1 ;

  return (TRUE);
}



