Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Kernel - File IO.c
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, Andrew Aye.  All Rights Reserved.
    This software is free for non-commercial use.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
      following conditions are met:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution in binary form must reproduce this copyright notice, this list of conditions and the following disclaimers in the documentation and other materials
          provided with the distribution.
    The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
    The intellectual property rights of the algorithms used reside with Andrew Aye.
    You may not use this software, in whole or in part, in support of any commercial product without the express written consent of the author.
    There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Kernel ============================================================================================================================================================ */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Private Functions                                                                                                                                                     */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgKN_IF__Open --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgKN_IF__Open( PCU_STg2_Input_File psIN_File, C_TgKN_FS_MOUNT_ID tiFS_Mount, C_TgUINT32 uiPath, C_TgUINT32 uiFile )
{
    STg2_KN_File_Open_Command           sOpen_Cmd;

    memset( psIN_File, 0, sizeof( STg2_Input_File ) );

    sOpen_Cmd.m_tiFS_Mount = tiFS_Mount;
    sOpen_Cmd.m_uiPath = uiPath;
    sOpen_Cmd.m_uiFile = uiFile;
    sOpen_Cmd.m_enMode = ETgFILE_IO_ACCESS__READ | ETgFILE_IO_ACCESS__SHARED;
    sOpen_Cmd.m_iCache = 0;
    sOpen_Cmd.m_pszFile_Name = nullptr;

    psIN_File->m_tiFile = tgKN_FS_BL_Open( &sOpen_Cmd );

    if (TgTRUE != tgKN_FILE_ID_Is_Valid( psIN_File->m_tiFile ))
    {
        return (KTgE_FAIL);
    };

    /* Assign the base structure (v-table) */
    psIN_File->m_sInput.m_pfnRead = tgKN_IF__Read;
    psIN_File->m_sInput.m_pfnEnd_Of_File = tgKN_IF__End_Of_File;
    psIN_File->m_sInput.m_pfnClose = tgKN_IF__Close;

    return (KTgS_OK);
}


/* ---- tgKN_IF__Read --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgSIZE tgKN_IF__Read( PC_STg2_Input psIN, C_TgSIZE UNUSED_PARAM uiOffset, PC_TgVOID pbyData, C_TgSIZE nuiData )
{
    union
    {
        P_STg2_Input_File                   psIN_File;
        P_STg2_Input                        psIN;
    }                                   sIN;
    STg2_KN_File_Read_Command           sRead_Cmd;

    sIN.psIN = psIN;

    sRead_Cmd.m_tiFile = sIN.psIN_File->m_tiFile;
    sRead_Cmd.m_pfnAllocator = 0;
    sRead_Cmd.m_pBuffer = pbyData;
    sRead_Cmd.m_nuiBuffer = nuiData;
    sRead_Cmd.m_nuiResult = 0;
    sRead_Cmd.m_pfnFinish = 0;
    sRead_Cmd.m_uiParam = 0;

    tgKN_FS_BL_Read( &sRead_Cmd );

    return (sRead_Cmd.m_nuiResult);
}


/* ---- tgKN_IF__End_Of_File -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgBOOL tgKN_IF__End_Of_File( PC_STg2_Input psIN, C_TgSIZE uiOffset )
{
    union
    {
        P_STg2_Input_File                   psIN_File;
        P_STg2_Input                        psIN;
    }                                   sIN;

    sIN.psIN = psIN;

    if (TgTRUE != tgEQ_KN_FILE_ID( g_asKN_File_Info[sIN.psIN_File->m_tiFile.m.iI].m_tiFile, sIN.psIN_File->m_tiFile ))
    {
        return (TgTRUE);
    };

    return (uiOffset < g_asKN_File_Info[sIN.psIN_File->m_tiFile.m.iI].m_uiSize ? TgFALSE : TgTRUE);
}


/* ---- tgKN_IF__Close -------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgKN_IF__Close( PC_STg2_Input psIN )
{
    union
    {
        P_STg2_Input_File                   psIN_File;
        P_STg2_Input                        psIN;
    }                                   sIN;

    sIN.psIN = psIN;
    tgKN_FS_BL_Close( sIN.psIN_File->m_tiFile );
    sIN.psIN_File->m_tiFile = KTgKN_FILE_ID__INVALID;
}


/* ---- tgKN_OF__Open --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgRESULT tgKN_OF__Open( PCU_STg2_Output_File psOUT_File, C_TgKN_FS_MOUNT_ID tiFS_Mount, C_TgUINT32 uiPath, C_TgUINT32 uiFile )
{
    STg2_KN_File_Open_Command           sOpen_Cmd;

    /* Close down the file if it has a valid close function */
    if (nullptr != psOUT_File->m_sOutput.m_pfnClose)
    {
        psOUT_File->m_sOutput.m_pfnClose( &psOUT_File->m_sOutput );
    };

    /* Clear the structure */
    memset( psOUT_File, 0, sizeof( STg2_Output_File ) );

    /* Open the file at the given path for write exclusive */
    sOpen_Cmd.m_tiFS_Mount = tiFS_Mount;
    sOpen_Cmd.m_uiPath = uiPath;
    sOpen_Cmd.m_uiFile = uiFile;
    sOpen_Cmd.m_enMode = ETgFILE_IO_ACCESS__WRITE | ETgFILE_IO_ACCESS__SHARED;
    sOpen_Cmd.m_iCache = 0;
    sOpen_Cmd.m_pszFile_Name = nullptr;

    psOUT_File->m_tiFile = tgKN_FS_BL_Open( &sOpen_Cmd );

    if (!tgKN_FILE_ID_Is_Valid( psOUT_File->m_tiFile ))
    {
        return (KTgE_FAIL);
    };

    /* Assign the base structure (v-table) */
    psOUT_File->m_sOutput.m_pfnWrite = tgKN_OF__Write;
    psOUT_File->m_sOutput.m_pfnClose = tgKN_OF__Close;

    return (KTgS_OK);
}


/* ---- tgKN_OF__Write -------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgSIZE tgKN_OF__Write( PC_STg2_Output psOUT, C_TgSIZE UNUSED_PARAM uiOffset, CPC_TgVOID pbyData, C_TgSIZE nuiData )
{
    union
    {
        P_STg2_Output_File                  psOUT_File;
        P_STg2_Output                       psOUT;
    }                                   sOUT;
    STg2_KN_File_Write_Command          sWrite_Cmd;

    sOUT.psOUT = psOUT;

    sWrite_Cmd.m_tiFile = sOUT.psOUT_File->m_tiFile;
    sWrite_Cmd.m_pBuffer = pbyData;
    sWrite_Cmd.m_nuiBuffer = nuiData;
    sWrite_Cmd.m_nuiResult = 0;
    sWrite_Cmd.m_pfnFinish = 0;
    sWrite_Cmd.m_uiParam = 0;

    tgKN_FS_BL_Write( &sWrite_Cmd );

    return (sWrite_Cmd.m_nuiResult);
}


/* ---- tgKN_OF__Close -------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgVOID tgKN_OF__Close( PC_STg2_Output psOUT )
{
    union
    {
        P_STg2_Output_File                  psOUT_File;
        P_STg2_Output                       psOUT;
    }                                   sOUT;

    sOUT.psOUT = psOUT;
    tgKN_FS_BL_Close( sOUT.psOUT_File->m_tiFile );
    sOUT.psOUT_File->m_tiFile = KTgKN_FILE_ID__INVALID;
}