#include "SmallObj.h"
#include <time.h>
//  [9/9/2004]
//-------------------------------------------------------------------------------------
//          Time Str
//-------------------------------------------------------------------------------------
namespace  TimeStr
{
    char _ProcessBegin[32];
    char _Now_Date[32];
    char _Now_Time[32];
    char _Now[32];
	

    void  Update()
    {
        static int  NextProc = 0;  
        int Now = clock();
        if( NextProc - Now > 0 )
            return;

        NextProc = Now + 1000;

        time_t NowTime;
        time( &NowTime );
        struct tm *gmt;
        gmt = localtime( &NowTime );

        sprintf( _Now_Date , "%d/%02d/%02d" , 1900+ gmt->tm_year , 1 + gmt->tm_mon , gmt->tm_mday );
        sprintf( _Now_Time , "%02d:%02d:%02d" , gmt->tm_hour , gmt->tm_min, gmt->tm_sec );
        sprintf( _Now , "%s %s" , _Now_Date , _Now_Time );
    }
    char* ProcessBegin()
    {
        static bool IsInit = false;

        if( IsInit == false )
        {
            IsInit = true;
            time_t NowTime;
            time( &NowTime );
            struct tm gmt;
            gmt = *localtime( &NowTime );
            gmt.tm_sec -= clock() / 1000;
            mktime( &gmt);    

            sprintf( _ProcessBegin , "%d/%02d/%02d %02d:%02d:%02d " 
                , 1900+ gmt.tm_year , 1 + gmt.tm_mon , gmt.tm_mday 
                , gmt.tm_hour , gmt.tm_min, gmt.tm_sec);

        }
        return _ProcessBegin;
    }
    char* Now_Date()
    {
        Update();
        return _Now_Date;
    }
    char* Now_Time()
    {
        Update();
        return _Now_Time;
    }
    char* Now()
    {
        Update();
        return _Now;
    }

	int  Now_Value( )
	{
		__time32_t NowTime;
		_time32( &NowTime );

		return (int)NowTime;
	}

	char* DateTimeStr( int Time )
	{
		static char DataTime[256];
		__time32_t NowTime = Time;

		struct tm *gmt;
		gmt = _localtime32( &NowTime );

		char DStr[256];
		char TStr[256];
		sprintf( DStr , "%d/%02d/%02d" , 1900+ gmt->tm_year , 1 + gmt->tm_mon , gmt->tm_mday );
		sprintf( TStr , "%02d:%02d:%02d" , gmt->tm_hour , gmt->tm_min, gmt->tm_sec );
		sprintf( DataTime , "%s %s" , DStr , TStr );
		return DataTime;
	}
};

//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
bool    Map_StrAndID::Add( char* Str , int ID )
{
    if( _StrToIDList.find( Str ) != _StrToIDList.end() )
        return false;

    if( _Max_Number_ <= (unsigned)ID )
        return false;

    while( ID >= (int)_IDToStrList.size() )
        _IDToStrList.push_back( "" );

    if( _IDToStrList[ ID ].c_str()[0] != 0 ) 
        return false;

    _StrToIDList[ Str ]  = ID; 
    _IDToStrList[ ID  ]  = Str;

    return true;  
}
bool    Map_StrAndID::Del( char* Str )
{
    int ID = GetID( Str );
    if( ID == -1 )
        return false;

    _IDToStrList[ID] = "";
    if( _StrToIDList.erase( Str ) == 0 )
        return false;


    return true;
}
bool    Map_StrAndID::Del( int ID )
{
    if( (unsigned)ID >= _IDToStrList.size() )
        return false;

    string Str = _IDToStrList[ ID ];

    if( Str.c_str()[0] == 0 )
        return false;

    _IDToStrList[ID] = "";

    if( _StrToIDList.erase( Str ) == 0 )
        return false;

    return true;
}
int     Map_StrAndID::GetID( char* Str )
{
    map< string , int >::iterator Iter;
    Iter = _StrToIDList.find( Str );
    if( Iter == _StrToIDList.end() )
        return -1;
    return Iter->second;
}
char*    Map_StrAndID::GetStr( int ID )
{
    if( (unsigned)ID > _IDToStrList.size() )
        return NULL;

    string Str = _IDToStrList[ ID ];
    return (char*)Str.c_str();
}
//-------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
//讀取參數(數字)
//----------------------------------------------------------------------------------
char*		ArgTransferStruct::GetString( string Name )
{
    map<string,string>::iterator Iter;

    Iter = _Arg.find( Name );
    if( Iter == _Arg.end() )
        return NULL;		

    return (char*)Iter->second.c_str();
}
//----------------------------------------------------------------------------------
//讀取參數(字串)
//----------------------------------------------------------------------------------
int			ArgTransferStruct::GetNumber( string Name )
{
    map<string,string>::iterator Iter;

    Iter = _Arg.find( Name );
    if( Iter == _Arg.end() )
        return 0;		

    return atol( Iter->second.c_str() );
}
//----------------------------------------------------------------------------------
//資料設定
//----------------------------------------------------------------------------------
void		ArgTransferStruct::PushValue( string Name , string Value )
{
    _Arg[ Name ] = Value;
}
void		ArgTransferStruct::PushValue( string Name , int Value )
{

	_Arg[ Name ] = IntToStr( Value );
}
//----------------------------------------------------------------------------------
void BaseLinkList::AddTo( BaseLinkList* FrontObj )
{
    assert( FrontObj != this );
    assert( FrontObj->Next != this );

    Front = FrontObj;

    Next = Front->Next;
    Front->Next = this;
    if( Next != NULL )
        Next->Front = this;

    assert( Next != this );
    assert( Front != this );
}

void BaseLinkList::Del( )
{
    assert( Front->Next == this );
   
    Front->Next = Next;

    if( Next )
        Next->Front = Front;

    assert( Next != Front );
}

//----------------------------------------------------------------------------------------------------------