Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Base - Type [File].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".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* == Common ============================================================================================================================================================ */

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

/* ---- tgCM_Path_Add_Seperator ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Add_Seperator( PC_TgCHAR pszDest, C_TgSIZE uiLength )
{
    C_TgSIZE                                nuiDest = tgSZ_Length( pszDest );

    TgERROR(0 != pszDest);

    if ((0 == nuiDest) || (nuiDest >= uiLength - 2))
    {
        return (0);
    };

    if (TgCHAR_PATH_SEPERATOR == pszDest[nuiDest - 1])
    {
        return (pszDest);
    };

    pszDest[nuiDest + 0] = TgCHAR_PATH_SEPERATOR;
    pszDest[nuiDest + 1] = 0;
    return (pszDest);
}


/* ---- tgCM_Path_Add_Extension ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Add_Extension( PC_TgCHAR pszDest, C_TgSIZE uiLength, CPC_TgCHAR pszExt )
{
    C_TgSIZE                                nuiDest = tgSZ_Length( pszDest );
    C_TgSIZE                                nuiExt = tgSZ_Length( pszExt );

    TgERROR((0 != pszDest) && (0 != pszExt));

    if (nuiDest + nuiExt + 1 >= uiLength - 1)
    {
        return (0);
    };

    if ('.' != pszExt[0])
    {
        tgSZ_Append( pszDest, uiLength, TgT(".") );
    };

    tgSZ_Append( pszDest, uiLength, pszExt );

    return (pszDest);
}


/* ---- tgCM_Path_Copy -------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Copy( PC_TgCHAR pszDest, C_TgSIZE uiLength, CPC_TgCHAR pszAdd )
{
    TgERROR((0 != pszDest) && (uiLength > 1));
    pszDest[0] = 0;
    return (tgCM_Path_Append( pszDest, uiLength, pszAdd ));
}


/* ---- tgCM_Path_Append ------------------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Append( PC_TgCHAR pszDest, C_TgSIZE uiLength, CPC_TgCHAR pszAdd )
{
    C_TgSIZE                                nuiDest = tgSZ_Length( pszDest );
    C_TgSIZE                                nuiAdd = tgSZ_Length( pszAdd );
    TgBOOL                                  bPrefix_Sep;

    TgERROR((0 != pszDest) && (uiLength > 1));

    if (nuiDest + nuiAdd + 2 > uiLength)
    {
        return (0);
    };

    bPrefix_Sep = TgCHAR_PATH_SEPERATOR == pszAdd[0];

    if (nuiDest > 0)
    {
        tgCM_Path_Add_Seperator( pszDest, uiLength );
        tgSZ_Append( pszDest, uiLength, pszAdd + (bPrefix_Sep ? 1 : 0) );
    }
    else
    {
        tgSZ_Copy( pszDest, uiLength, pszAdd );
    };

    return (pszDest);
}


/* ---- tgCM_Path_Common_Prefix_Size ------------------------------------------------------------------------------------------------------------------------------------ */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
TgUINT32 tgCM_Path_Common_Prefix_Size( CPC_TgCHAR pszPath0, CPC_TgCHAR pszPath1 )
{
    TgUINT32                                uiIndex = 0;

    TgERROR((0 != pszPath0) && (0 != pszPath1));

    for (; (pszPath0[uiIndex] == pszPath1[uiIndex]) && (0 != pszPath0[uiIndex]) && (0 != pszPath1[uiIndex]) ; ++uiIndex)
    {
    };

    return (uiIndex);
}


/* ---- tgCM_Path_Common_Prefix ----------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Common_Prefix( PC_TgCHAR pszDest, C_TgSIZE uiLength, CPC_TgCHAR pszPath0, CPC_TgCHAR pszPath1 )
{
    TgUINT32                                uiIndex = 0;

    TgERROR((0 != pszDest) && (0 != pszPath0) && (0 != pszPath1));

    for (; (pszPath0[uiIndex] == pszPath1[uiIndex]) && (0 != pszPath0[uiIndex]) && (0 != pszPath1[uiIndex]) && uiIndex < uiLength; ++uiIndex)
    {
        pszDest[uiIndex] = pszPath0[uiIndex];
    };

    pszDest[uiIndex < uiLength ? uiIndex : 0] = 0;
    return (pszDest);
}


/* ---- tgCM_Path_Remove_Seperator -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Remove_Seperator( PC_TgCHAR pszDest )
{
    C_TgSIZE                                nuiDest = tgSZ_Length( pszDest );

    TgERROR(0 != pszDest);

    if (nuiDest < 1)
    {
        return (pszDest);
    };

    if (TgCHAR_PATH_SEPERATOR != pszDest[nuiDest - 1])
    {
        return (pszDest);
    };

    pszDest[nuiDest - 1] = 0;
    return (pszDest);
}


/* ---- tgCM_Path_Remove_Extension -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Remove_Extension( PC_TgCHAR pszDest )
{
    PC_TgCHAR                               pszExt = tgCM_Path_Find_Extension( pszDest );

    if (0 != pszExt)
    {
        pszExt[0] = 0;
        return (pszDest);
    }

    return (pszDest);
}


/* ---- tgCM_Path_Remove_File_Name -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Remove_File_Name( PC_TgCHAR pszDest )
{
    C_TgSIZE                                nuiDest = tgSZ_Length( pszDest );
    TgSIZE                                  uiIndex;

    TgERROR(0 != pszDest);

    for (uiIndex = nuiDest; uiIndex > 0; --uiIndex)
    {
        if (TgCHAR_PATH_SEPERATOR == pszDest[uiIndex - 1])
        {
            pszDest[uiIndex] = 0;
            return (pszDest);
        };
    };

    return (pszDest);
}


/* ---- tgCM_Path_Rename_Extension -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Rename_Extension( PC_TgCHAR pszDest, C_TgSIZE nuiDest, CPC_TgCHAR pszExt )
{
    C_TgSIZE                                uiLength = tgSZ_Length( pszDest );
    C_TgSIZE                                nuiExt = tgSZ_Length( pszExt );
    TgSIZE                                  uiIndex = nuiDest;

    TgERROR(0 != pszDest);

    for (uiIndex = uiLength; uiIndex > 0; --uiIndex)
    {
        if (TgT('.') == pszDest[uiIndex - 1])
        {
            if (uiIndex + nuiExt + 2 > nuiDest)
            {
                return (0);
            };

            pszDest[uiIndex] = 0;

            if ('.' == pszExt[0])
            {
                tgSZ_Copy( pszDest + uiIndex, nuiDest - uiIndex, pszExt + 1 );
            }
            else
            {
                tgSZ_Copy( pszDest + uiIndex, nuiDest - uiIndex, pszExt + 0 );
            };

            return (pszDest);
        };
    };

    return (pszDest);

}


/* ---- tgCM_Path_Remove_Directory -------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Remove_Directory( PC_TgCHAR pszDest )
{
    C_TgSIZE                                nuiDest = tgSZ_Length( pszDest );
    TgSIZE                                  uiIndex;

    TgERROR(0 != pszDest);

    for (uiIndex = nuiDest; uiIndex > 0; --uiIndex)
    {
        if (TgCHAR_PATH_SEPERATOR == pszDest[uiIndex - 1])
        {
            TgUINT32                                uiCoffee;

            for (uiCoffee = 0; uiIndex <= nuiDest; ++uiIndex, ++uiCoffee)
            {
                pszDest[uiCoffee] = pszDest[uiIndex];
            };

            return (pszDest);
        };
    };

    return (pszDest);
}


/* ---- tgCM_Path_Init -------------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
CLANG_WARN_DISABLE_PUSH(cast-align)
P_TgCHAR tgCM_Path_Init( PC_TgCHAR pszDest, C_TgSIZE nuiDest, ... )
{
    CP_TgCHAR                               pszPath;
    va_list                                 vaList;

    TgERROR(0 != pszDest && 0 < nuiDest);

    va_start( vaList, nuiDest );

    pszPath = (CP_TgCHAR)va_arg( vaList, TgSINTPTR );

    *pszDest = 0;

    for (; 0 != *pszPath; pszPath = (CP_TgCHAR)va_arg( vaList, TgSINTPTR ))
    {
        C_TgSIZE                                nuiPathAppend = tgSZ_Length( pszPath );
        C_TgSIZE                                nuiPath = tgSZ_Length( pszDest );

        if (nuiPath + nuiPathAppend >= nuiDest)
        {
            return (0);
        };

        tgSZ_Append( pszDest, nuiDest, pszPath );

        if (0 == tgCM_Path_Add_Seperator( pszDest, nuiDest ))
        {
            return (0);
        };
    };

    va_end( vaList );

    return (pszDest);
}
CLANG_WARN_DISABLE_POP(cast-align)


/* ---- tgCM_Path_Find_Extension ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Find_Extension( PC_TgCHAR pszDest )
{
    C_TgSIZE                                nuiDest = tgSZ_Length( pszDest );
    TgSIZE                                  uiIndex;

    TgERROR(0 != pszDest);

    for (uiIndex = nuiDest; uiIndex > 0; --uiIndex)
    {
        if ('.' == pszDest[uiIndex - 1])
        {
            return (pszDest + uiIndex - 1);
        };
    };

    return (0);
}


/* ---- tgCM_Path_Find_File_Name ---------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
P_TgCHAR tgCM_Path_Find_File_Name( PC_TgCHAR pszDest )
{
    C_TgSIZE                                nuiDest = tgSZ_Length( pszDest );
    TgSIZE                                  uiIndex;

    TgERROR(0 != pszDest);

    if (TgCHAR_PATH_SEPERATOR == pszDest[nuiDest - 1])
    {
        return (0);
    };

    for (uiIndex = nuiDest; uiIndex > 0; --uiIndex)
    {
        if (TgCHAR_PATH_SEPERATOR == pszDest[uiIndex - 1])
        {
            return (pszDest + uiIndex);
        };
    };

    return (0);
}



/* ---- tgCM_Path_IsAbsolute -------------------------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
#if TgCOMPILE_BASE_API_PLATFORM
TgBOOL tgCM_Path_IsAbsolute( PC_TgCHAR UNUSED_PARAM pszPath )
{
    return (TgFALSE);
}
/*# TgCOMPILE_BASE_API_PLATFORM */
#endif


/* ---- tgCM_Path_Query_Application_Data_Directory ---------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
#if TgCOMPILE_BASE_API_PLATFORM
TgVOID tgCM_Path_Query_Application_Data_Directory( P_TgCHAR pszPath, C_TgSIZE nuiPath )
{
    TgPARAM_CHECK( pszPath && nuiPath );
    pszPath[0] = 0;
}
/*# TgCOMPILE_BASE_API_PLATFORM */
#endif


/* ---- tgCM_Path_Query_Temporary_Directory ----------------------------------------------------------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
#if TgCOMPILE_BASE_API_PLATFORM
TgVOID tgCM_Path_Query_Temporary_Directory( P_TgCHAR pszPath, C_TgSIZE nuiPath )
{
    TgPARAM_CHECK( pszPath && nuiPath );
    pszPath[0] = 0;
}
/*# TgCOMPILE_BASE_API_PLATFORM */
#endif