/******************************************************************************
*
* Copyright (C) 2019  Brian Berlin
* 
* 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 3 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, see <http://www.gnu.org/licenses/>
*
******************************************************************************
*
* FILENAME: media.c
*
* DESCRIPTION:  This contains media handling functions
*
* AUTHORS:  bberlin
*
* CHANGE LOG
*
*  VER     DATE     CHANGED BY   DESCRIPTION OF CHANGE
* ------  --------  ----------   ------------------------------------
* 0.9.0   12/25/19  bberlin      Creation
******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "cart.h"
#include "media.h"
#include "console.h"

/******************************************************************************
**  Function   :  media_init / exit
**                            
**  Objective  :  These functions handle initialization and exit.  For init,
**                pointers are intitialized to zero and defaults for other 
**                values are set.  For exit, free any allocated memory
**
**  Parameters :  p_media - pointer to media struct
**                                                
**  return     :  NONE
**      
******************************************************************************/ 
void media_init ( t_media *p_media ) {

    p_media->title = 0;
    p_media->file = 0;
    p_media->image_size = 0;
    p_media->mapping = CART_MIN;
    p_media->media_type = CART_CART;
    p_media->machine_type = MACHINE_TYPE_5200;
    p_media->write_protect = 1;
    p_media->crc = 0;
    p_media->collections = 0;
    p_media->md5 = 0;
    p_media->sha1 = 0;

} /* end media_init */

void media_exit ( t_media *p_media ) {

    free(p_media->title);
    free(p_media->file);
    free(p_media->md5);
    free(p_media->sha1);

    /* NULL so a second media_exit (e.g. unmount then shutdown) is a no-op */
    p_media->title = 0;
    p_media->file  = 0;
    p_media->md5   = 0;
    p_media->sha1  = 0;

} /* end media_exit */

/******************************************************************************
**  Function   :  media_copy
**                            
**  Objective  :  This function copies the input struct while freeing 
**                currently used memory in the to struct
**
**  Parameters :  p_from - pointer to media struct to copy from
**                p_to   - pointer to media struct to copy to
**                                                
**  return     :  NONE
**      
******************************************************************************/ 
void media_copy ( t_media *p_from, t_media *p_to ) {

    free ( p_to->title );
    free ( p_to->file );
    free ( p_to->md5 );
    free ( p_to->sha1 );

    *p_to = *p_from;

    if ( p_from->title && strlen(p_from->title) ) {
        p_to->title = malloc(strlen(p_from->title) + 1);
        strcpy ( p_to->title, p_from->title );
    }
    else
        p_to->title = 0;

    if ( p_from->file && strlen(p_from->file) ) {
        p_to->file = malloc(strlen(p_from->file) + 1);
        strcpy ( p_to->file, p_from->file );
    }
    else
        p_to->file = 0;

    if ( p_from->md5 && strlen(p_from->md5) ) {
        p_to->md5 = malloc(strlen(p_from->md5) + 1);
        strcpy ( p_to->md5, p_from->md5 );
    }
    else
        p_to->md5 = 0;

    if ( p_from->sha1 && strlen(p_from->sha1) ) {
        p_to->sha1 = malloc(strlen(p_from->sha1) + 1);
        strcpy ( p_to->sha1, p_from->sha1 );
    }
    else
        p_to->sha1 = 0;

} /* end media_copy */
