#pragma warning (disable:4949)
#pragma warning(disable : 4996)

#include "BaseFunction.h"
#include <windows.h>
#include <stdio.h>
#include <direct.h>
#include <io.h>
#include <assert.h>

#pragma unmanaged
//-------------------------------------------------------------------------------------
//檔案所在路徑與檔名結合
//-------------------------------------------------------------------------------------
char* CombineFilePath( const char* _Path , const char* File )
{
	static char Buf[512];
    char*   Path =( char* )_Path;
	for( int i= int(strlen(Path)-1) ; i >=0 ; i-- )
	{
		if( Path[i] != ' ' && Path[i] != '\t')
			break;

		Path[i] = '\0';
	}

	if(		Path[strlen(Path)-1] == '\\' 
		|| 	Path[strlen(Path)-1] == '/')
		sprintf( Buf , "%s%s",Path ,File );
	else
		sprintf( Buf , "%s\\%s",Path ,File );

	return Buf;
}

#define _SMATH_EPSILON			1.0e-4f						//1E-6f

bool FloatEQU ( float X , float Y )
{
    return (((X - _SMATH_EPSILON) < Y) && (Y < ( X + _SMATH_EPSILON)));
}

bool CheckFloatAvlid( float X )
{
    struct 
    {
        union
        {
            float Value;
            struct
            {
                unsigned int     Fraction : 23;
                unsigned int     Exponent : 8;
                unsigned int     Sign :1;
            };
        };
    }Float;

    Float.Value = X;
    if( Float.Exponent == 0 && Float.Fraction != 0 )
        return false;
    if( Float.Exponent == 255 )
        return false;
    return true;
}

//資料轉換
char*   IntToStr( int Value )
{
    static char Buf[32];
    sprintf( Buf , "%d" , Value );
    return Buf;
}

unsigned int MytimeGetTime( )
{
    static unsigned int  BeginTime = timeGetTime();
    return timeGetTime() - BeginTime;
}
//-------------------------------------------------------------------------------------
vector<string>*	AllPathList( string SrcPath )
{
    static vector<string> AllPath;
    string Path;
    vector<string> OpenPath;
    struct _finddata_t c_file;
    long	hFile;

    char Findmask[512];
    char Buf[512];

    AllPath.clear();
    OpenPath.clear();

    const char *P = SrcPath.c_str();
    if( P[ SrcPath.length() - 1] != '\\' && SrcPath.length() != 0 )
        SrcPath += "\\";


    OpenPath.push_back( "" );


    while( OpenPath.size() )
    {
        Path = OpenPath.back();

        AllPath.push_back( Path );

        OpenPath.pop_back();
        sprintf( Findmask , "%s%s*." , SrcPath.c_str() , Path.c_str() );

        if( (hFile = (long)_findfirst( (char*)Findmask , &c_file )) != -1L )
        {

            do
            {
                if( !(c_file.attrib & _A_SUBDIR) )
                    continue;

                if( c_file.name[0] == '.' )
                    continue;

                //TempPath = Path + c_file.name + "\\";
                sprintf( Buf , "%s%s\\", Path.c_str() , c_file.name );
                OpenPath.push_back( Buf );


            }while( _findnext( hFile, &c_file ) == 0 );


            _findclose( hFile );
        }
    }

    return &AllPath;
}
vector<string>*	AllPathList_Pure( string Path )
{
    vector<string>*	Ret;
    char CurPath[ _MAX_PATH ];

    if( _getcwd( CurPath, _MAX_PATH ) == NULL )
        return NULL;

    _chdir( Path.c_str() );

    Ret = AllPathList( "" );

    _chdir( CurPath );

    return Ret;
}

vector<string>*	AllFileList_Sub( string Path , string Sub )
{
    vector<string>*	Ret;
    char CurPath[ _MAX_PATH ];

    if( _getcwd( CurPath, _MAX_PATH ) == NULL )
        return NULL;

    _chdir( Path.c_str() );

    Ret = AllFileList( "" , Sub );

    _chdir( CurPath );

    return Ret;
}
//-------------------------------------------------------------------------------------
bool	CreateAllPath( string Path , vector<string>& PathList )
{
    char CurPath[ _MAX_PATH ];

    if( _getcwd( CurPath, _MAX_PATH ) == NULL )
        return false;

    _mkdir( Path.c_str() );
    _chdir( Path.c_str() );
    for( int i = 0 ; i < (int)PathList.size() ; i++ )
    {
        _mkdir( PathList[i].c_str() );
    }

    _chdir( CurPath );

    return true;

}
//-------------------------------------------------------------------------------------
vector<string>*	AllFileList( string Path , string Sub )
{
    static vector<string> AllFilePath;
    vector<string>* AllPath = AllPathList( Path );

    if(		Path.c_str()[ Path.length() - 1 ] != '\\' 
        &&	Path.c_str()[ Path.length() - 1 ] != '/'	)
        Path += "\\";

    AllFilePath.clear();
    string Temp;
    for( int i = 0 ; i < (int)AllPath->size() ; i++ )
    {
        Path = (*AllPath)[i] + Sub;

        WIN32_FIND_DATA  file_data;
        HANDLE	hfile = FindFirstFile(Path.c_str(), &file_data);
        if(hfile == INVALID_HANDLE_VALUE)
            continue;
            //return &AllFilePath;

        AllFilePath.push_back( (*AllPath)[i] + file_data.cFileName );


        while(FindNextFile(hfile, &file_data))
        {
            AllFilePath.push_back( (*AllPath)[i] + file_data.cFileName );
        }

    }

    return &AllFilePath;

}
char*   StringToSqlX( char* Str , int Size , bool IsNullEnd )
{    
    //bool    NullEnd = false;
    int     i;
    if( Str == NULL )
        return "";
    if( Size == 0)
    {
        Size = 0x10000 / 2 -1;
//        NullEnd = true;
    }

	assert( Size*2+2 <= 0x10000 );
    //static char Ret[ 2 ] = { '0' , 'x' };
    static char OutStr[0x10000 ] = { '0' , 'x' };
    
    for( i = 0 ; i < Size ; i++ )
    {
        if( Str[i] == 0 && IsNullEnd == true )
            break;

        sprintf( OutStr+i*2+2 , "%02x" , (unsigned char)Str[i] );

    }
    if( i == 0  )
    {
        sprintf( OutStr+i*2+2 , "00" );
    }
	else
	{
		OutStr[ i*2+2 ] = 0;
	}
    return OutStr;
}

//-------------------------------------------------------------------------------------
//正規化檔案路徑
void  FilePathnormalize( char* FilePath , char* OutPath )
{
    char Buf[ 512 ];
    strcpy( Buf , FilePath );

    _strlwr( Buf );

    int i , j;

    //1 把多的空白去除
    for( i = (int)strlen( Buf ) - 1 ; i >= 0 ; i-- )
    {
        if( Buf[i] != ' ' )
            break;

        Buf[i] = 0;
    }

    //去除前面多的斜線與空白
    for( i = 0 ; Buf[i] != 0; i++ )
    {
        if( Buf[i] != ' ' )
            break;
    }

    if( Buf[i] == '\\' )
    {
        if( Buf[i+1] == '\\' )
        {
        }
        else
            i++;
    }
    else if( Buf[i] == '/')
    {
        i++;
    }

//    int StartPoint = i;

    //2 把斜線轉成相同  
    for( j = 0 ; Buf[i] ; i++ )
    {
        if( Buf[i] == '/' || Buf[i] == '\\')
        {
            OutPath[j++] = '/';
            for( ; Buf[i+1] != ' ' ; i++ )
                break;
        }
        else
        {
            OutPath[j++] = Buf[i];
        }
    }
    OutPath[ j ] = 0;
}

int     Random( int Value )
{
    if( Value <= 1 )
        return 0;

    if( Value < 65536 )
    {
        return rand()%Value;
    }
    
    int R = rand() + rand()* 0x10000 ;

    return R % Value;
}

int     Random( int Min , int Max )
{
	int Range = Max - Min;
	if( Range == 0  )
	{
		return Min;
	}
	else if( Range > 0 )
	{
		return Min + Random( Range );
	}
	else
	{
		return Max + Random( Range * -1 );
	}
}

#pragma managed