Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Base - Type [IO].inl
    »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".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
#if !defined(TGS_COMMON_BASE_TYPES_IO_INL)
#define TGS_COMMON_BASE_TYPES_IO_INL
#pragma once


/* == Common ============================================================================================================================================================ */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Public Functions                                                                                                                                                      */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

/* ---- tgIO_Read_Line -------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgSIZE tgIO_Read_Line( PC_STg2_Input psIN, C_TgSIZE uiOffset_Start, PC_TgCHAR pszDest, C_TgSIZE nuiDest )
{
    TgUINT32                            iIdx = 0;
    TgSIZE                              uiOffset;

    if (nuiDest <= 1)
    {
        return (0);
    };

    uiOffset = uiOffset_Start;
    for (; iIdx < nuiDest - 1 && !(*psIN->m_pfnEnd_Of_File)( psIN, uiOffset ); ++iIdx)
    {
        TgCHAR                              uiChar;

        uiOffset += psIN->m_pfnRead( psIN, uiOffset, (P_TgUINT08)(&uiChar), sizeof( TgCHAR ) );

        pszDest[iIdx] = uiChar;

        if (TgT('\r') == uiChar || TgT('\n') == uiChar || 0 == uiChar)
        {
            break;
        };
    };

    pszDest[iIdx] = 0;
    return (uiOffset - uiOffset_Start);
}


/* ---- tgIO_Print ------------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgSINT32 tgIO_Print( PC_STg2_Output psOUT, CPC_TgCHAR pszText )
{
    psOUT->m_pfnWrite( psOUT, KTgMAX_SIZE, (CP_TgUINT08)pszText, (tgSZ_Length( pszText ) + 1)*sizeof( TgCHAR ) );
    return (0);
}


/* ---- tgIO_IM_Open ---------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgIO_IM_Open( PC_STg2_Input_MEM psIN, CP_TgVOID pData, C_TgSIZE uiSize, TgFCN_MEM_FREE pfnFree )
{
    psIN->m_sInput.m_pfnRead = tgIO_IM_Read;
    psIN->m_sInput.m_pfnEnd_Of_File = tgIO_IM_End_Of_File;
    psIN->m_sInput.m_pfnClose = tgIO_IM_Close;
    psIN->m_uiSize = uiSize;
    psIN->m_pData = pData;
    psIN->m_pfnFree = pfnFree;
}


/* ---- tgIO_IM_Close --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
CLANG_WARN_DISABLE_PUSH(cast-qual)
TgINLINE TgVOID tgIO_IM_Close( PC_STg2_Input psIN )
{
    union
    {
        P_STg2_Input_MEM                    psIN_MEM;
        P_STg2_Input                        psIN;
    }                                   sPM;

    sPM.psIN = psIN;

    if (sPM.psIN_MEM->m_pfnFree)
    {
        sPM.psIN_MEM->m_pfnFree( (P_TgVOID)(sPM.psIN_MEM->m_pData) );
    };

    sPM.psIN_MEM->m_uiSize = 0;
    sPM.psIN_MEM->m_pData = 0;
    sPM.psIN_MEM->m_pfnFree = 0;
}
CLANG_WARN_DISABLE_POP(cast-qual)


/* ---- tgIO_IM_Size ---------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgSIZE tgIO_IM_Size( PC_STg2_Input_MEM psIN )
{
    return (psIN->m_uiSize);
}


/* ---- tgIO_IM_Read ---------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgSIZE tgIO_IM_Read( PC_STg2_Input psIN, C_TgSIZE uiOffset, P_TgVOID pData, C_TgSIZE uiSize )
{
    union
    {
        P_STg2_Input_MEM                    psIN_MEM;
        P_STg2_Input                        psIN;
    }                                   sPM;

    sPM.psIN = psIN;

    if (uiOffset < sPM.psIN_MEM->m_uiSize)
    {
        TgSIZE                              uiData;

        uiData = tgCM_MIN_UXX( uiSize, sPM.psIN_MEM->m_uiSize - uiOffset );
        TgERROR(sPM.psIN_MEM->m_uiSize >= uiOffset );
        TgMEMCPY( pData, uiSize, (CP_TgUINT08)sPM.psIN_MEM->m_pData + uiOffset, uiData );
        return (uiData);
    };

    return (0);
}


/* ---- tgIO_IM_End_Of_File --------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgBOOL tgIO_IM_End_Of_File( PC_STg2_Input psIN, C_TgSIZE uiOffset )
{
    union
    {
        P_STg2_Input_MEM                    psIN_MEM;
        P_STg2_Input                        psIN;
    }                                   sPM;

    sPM.psIN = psIN;

    return (uiOffset >= sPM.psIN_MEM->m_uiSize ? TgTRUE : TgFALSE);
}


/* ---- tgIO_OM_Open ---------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgIO_OM_Open( PC_STg2_Output_MEM psOUT, P_TgVOID pData, C_TgSIZE uiSize, TgFCN_MEM_FREE pfnFree )
{
    psOUT->m_sOutput.m_pfnWrite = tgIO_OM_Write;
    psOUT->m_sOutput.m_pfnClose = tgIO_OM_Close;
    psOUT->m_uiSize_Current = 0;
    psOUT->m_uiSize = uiSize;
    psOUT->m_pData = (P_TgUINT08)pData;
    psOUT->m_pfnFree = pfnFree;
}


/* ---- tgIO_OM_Close --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgVOID tgIO_OM_Close( PC_STg2_Output psOUT )
{
    union
    {
        P_STg2_Output_MEM                   psOUT_MEM;
        P_STg2_Output                       psOUT;
    }                                   sPM;

    sPM.psOUT = psOUT;

    if (sPM.psOUT_MEM->m_pfnFree)
    {
        sPM.psOUT_MEM->m_pfnFree( sPM.psOUT_MEM->m_pData );
    };

    sPM.psOUT_MEM->m_uiSize_Current = 0;
    sPM.psOUT_MEM->m_uiSize = 0;
    sPM.psOUT_MEM->m_pData = 0;
    sPM.psOUT_MEM->m_pfnFree = 0;
}


/* ---- tgIO_OM_Query_Data ---------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE CP_TgUINT08 tgIO_OM_Query_Data( PC_STg2_Output_MEM psOUT )
{
    return (psOUT->m_pData);
}


/* ---- tgIO_OM_Write --------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgINLINE TgSIZE tgIO_OM_Write( PC_STg2_Output psOUT, C_TgSIZE uiOffset_Start, CP_TgVOID pData, C_TgSIZE uiSize )
{
    union
    {
        P_STg2_Output_MEM                   psOUT_MEM;
        P_STg2_Output                       psOUT;
    }                                   sPM;
    TgSIZE                              uiOffset;

    sPM.psOUT = psOUT;

    uiOffset = KTgMAX_SIZE == uiOffset_Start ? sPM.psOUT_MEM->m_uiSize_Current : uiOffset_Start;
    if (uiOffset < sPM.psOUT_MEM->m_uiSize)
    {
        TgSIZE                              uiData;

        uiData = tgCM_MIN_UXX( uiSize, sPM.psOUT_MEM->m_uiSize - uiOffset );
        TgERROR(sPM.psOUT_MEM->m_uiSize >= uiOffset );
        TgMEMCPY( sPM.psOUT_MEM->m_pData + uiOffset, uiData, pData, uiData );
        sPM.psOUT_MEM->m_uiSize_Current = tgCM_MAX_UXX( sPM.psOUT_MEM->m_uiSize_Current, uiOffset + uiData );

        return (uiData);
    };

    return (0);
}


/* ====================================================================================================================================================================== */
#endif