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

/*
*
* 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:os.h"
#include "OSLib:osfile.h"
#include "OSLib:toolbox.h"
#include "OSLib:wimp.h"

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

/* private headers */
#include "main.h"
#include "msglink.h"
#include "cjoslib.h"
#include "defaultdata.h"
#include "unitconv.h"
#include "loaddata.h"





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

static osbool Loaddata_LoadFile ( char *filename, int length, unitconv_data *uc_data )
{
  fileswitch_object_type type;
  bits load, exec;
  fileswitch_attr attr;
  char *filebuffer;
  osbool load_file;
  int len;


  /* allocate some memory */
  filebuffer = (char *)malloc ( length ) ;
  if ( filebuffer !=0 )
  {
    /* load the file into memory */
    type = osfile_load_stamped_no_path ( filename, (byte *)filebuffer,
                                         &load, &exec, &len, &attr ) ;
    /* now process the file */
    load_file = Unitconv_ProcessDatafile ( filebuffer, uc_data ) ;

    // free the buffer
    free ( filebuffer ) ;
  }
  else
  {
    // memory allocation failed
    load_file = FALSE;
    Msg_Warn ( LOADNOMEM ) ;
  }

  return load_file;
}




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

static osbool Loaddata_FindFile ( char *filename, int *length, osbool load_custom )
{
  fileswitch_object_type type;
  bits load, exec, filetype;
  fileswitch_attr attr;


  if ( load_custom )
  {
    strcpy ( filename, CUSTOM_FILE );
    type = osfile_read_stamped_no_path ( filename, &load, &exec, length,
                                         &attr, &filetype ) ;
    if ( type == 1 )
      return TRUE;
  }
  strcpy ( filename, DATA_FILE );
  type = osfile_read_stamped_no_path ( filename, &load, &exec, length,
                                       &attr, &filetype ) ;
  if ( type == 1 )
    return TRUE;

  return FALSE;
}




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

osbool Loaddata_Setup ( void )
{
  int length ;
  char filename [256];
  osbool load_file;


  /* Was ctrl held down when launched? */

  load_file = Loaddata_FindFile ( filename, &length, !CJL_ControlPressed() );
  if ( load_file )
  {
    /* file exists so load and process it */
    load_file = Loaddata_LoadFile ( filename, length, &uc_data );
  }

  if ( !load_file )
  {
    /* use hard coded default */
    if (!defaultdata_fill_default_arrays ())
    {
      Msg_Warn ( LOADNOMEM2 ) ;
      Quit = TRUE;
      return (FALSE);
    }
  }
  /* now set up special conversions */
  defaultdata_fill_special_arrays();
  return (TRUE);
}




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





/*************  End of c.loaddata  ***********************/

