Exemple #1
0
//---------------------------------------------------------
bool CCMD_Module::_Load_Input(CSG_Parameter *pParameter)
{
    wxString	FileName;

    if(	!pParameter->is_Input() || !pParameter->is_Enabled() )
    {
        return( true );
    }

    if( !m_CMD.Found(_Get_ID(pParameter), &FileName) )
    {
        return( pParameter->is_Optional() );
    }

    if( pParameter->is_DataObject() )
    {
        if( !SG_Get_Data_Manager().Find(&FileName) && !SG_Get_Data_Manager().Add(&FileName) && !pParameter->is_Optional() )
        {
            CMD_Print_Error(_TL("input file"), &FileName);

            return( false );
        }

        return( pParameter->Set_Value(SG_Get_Data_Manager().Find(&FileName)) );
    }

    else if( pParameter->is_DataObject_List() )
    {
        pParameter->asList()->Del_Items();

        wxString	FileNames(FileName);

        do
        {
            FileName	= FileNames.BeforeFirst	(';');
            FileNames	= FileNames.AfterFirst	(';');

            if( !SG_Get_Data_Manager().Find(&FileName) )
            {
                SG_Get_Data_Manager().Add(&FileName);
            }

            pParameter->asList()->Add_Item(SG_Get_Data_Manager().Find(&FileName));
        }
        while( FileNames.Length() > 0 );
    }

    return( true );
}
Exemple #2
0
//---------------------------------------------------------
bool CCMD_Module::_Get_Parameters(CSG_Parameters *pParameters)
{
    if( !pParameters || m_CMD.Parse(false) != 0 )
    {
        return( false );
    }

    //-----------------------------------------------------
    int		i;

    for(i=0; i<pParameters->Get_Count(); i++)
    {
        CSG_Parameter	*pParameter	= pParameters->Get_Parameter(i);

        if( pParameter->is_Input() )
        {
            // nop now, loading options first
        }

        else if( pParameter->is_Output() )
        {
            if( pParameter->is_DataObject() && pParameter->is_Optional() && !pParameter->asDataObject() && m_CMD.Found(_Get_ID(pParameter)) )
            {
                pParameter->Set_Value(DATAOBJECT_CREATE);
            }
        }

        else if( pParameter->is_Option() && !pParameter->is_Information() )
        {
            long		l;
            double		d;
            wxString	s;

            switch( pParameter->Get_Type() )
            {
            default:
                break;

            case PARAMETER_TYPE_Parameters:
                _Get_Parameters(pParameter->asParameters());
                break;

            case PARAMETER_TYPE_Bool:
                pParameter->Set_Value(m_CMD.Found(_Get_ID(pParameter)) ? 1 : 0);
                break;

            case PARAMETER_TYPE_Int:
                if( m_CMD.Found(_Get_ID(pParameter), &l) )
                {
                    pParameter->Set_Value((int)l);
                }
                break;

            case PARAMETER_TYPE_Choice:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    if( s.ToLong(&l) )
                    {
                        pParameter->Set_Value((int)l);
                    }
                    else
                    {
                        pParameter->Set_Value(CSG_String(&s));
                    }
                }
                break;

            case PARAMETER_TYPE_Double:
            case PARAMETER_TYPE_Degree:
                if( m_CMD.Found(_Get_ID(pParameter), &s) && s.ToDouble(&d) )
                {
                    pParameter->Set_Value(d);
                }
                break;

            case PARAMETER_TYPE_Range:
                if( m_CMD.Found(_Get_ID(pParameter, wxT("MIN")), &s) && s.ToDouble(&d) )
                {
                    pParameter->asRange()->Set_LoVal(d);
                }

                if( m_CMD.Found(_Get_ID(pParameter, wxT("MAX")), &s) && s.ToDouble(&d) )
                {
                    pParameter->asRange()->Set_HiVal(d);
                }
                break;

            case PARAMETER_TYPE_String:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    pParameter->Set_Value(CSG_String(&s));
                }
                break;

            case PARAMETER_TYPE_Text:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    CSG_File	Stream;

                    if( Stream.Open(CSG_String(&s)) )
                    {
                        CSG_String	t;

                        Stream.Read(t, Stream.Length());

                        pParameter->Set_Value(t.c_str());
                    }
                    else
                    {
                        pParameter->Set_Value(CSG_String(&s));
                    }
                }
                break;

            case PARAMETER_TYPE_FilePath:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    if( pParameter->asFilePath()->is_Multiple() )
                    {
                        s.Prepend(wxT("\""));
                        s.Replace(wxT(";"), wxT("\" \""));
                        s.Append (wxT("\""));
                    }

                    pParameter->Set_Value(CSG_String(&s));
                }
                break;

            case PARAMETER_TYPE_FixedTable:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    CSG_Table	Table(&s);
                    pParameter->asTable()->Assign_Values(&Table);
                }
                break;

            case PARAMETER_TYPE_Grid_System:
                if( pParameter->Get_Children_Count() == 0 )
                {
                    long	nx, ny;
                    double	d, x, y;

                    if(	!m_CMD.Found(_Get_ID(pParameter, wxT("NX")), &nx)
                            ||	!m_CMD.Found(_Get_ID(pParameter, wxT("NY")), &ny)
                            ||	!m_CMD.Found(_Get_ID(pParameter, wxT( "X")), &s) || !s.ToDouble(&x)
                            ||	!m_CMD.Found(_Get_ID(pParameter, wxT( "Y")), &s) || !s.ToDouble(&y)
                            ||	!m_CMD.Found(_Get_ID(pParameter, wxT( "D")), &s) || !s.ToDouble(&d) )
                    {
                        pParameter->asGrid_System()->Assign(-1, 0.0, 0.0, 0, 0);
                    }
                    else
                    {
                        pParameter->asGrid_System()->Assign(d, x, y, (int)nx, (int)ny);
                    }
                }
                break;
            }
        }
    }

    m_pModule->Update_Parameter_States();

    //-----------------------------------------------------
    for(i=0; i<pParameters->Get_Count(); i++)
    {
        CSG_Parameter	*pParameter	= pParameters->Get_Parameter(i);

        if( pParameter->is_Input() )
        {
            if( !_Load_Input(pParameters->Get_Parameter(i)) )
            {
                CMD_Print_Error(pParameters->Get_Parameter(i)->Get_Name());

                return( false );
            }
        }

        else if( pParameter->is_Option() && !pParameter->is_Information() )
        {
            long		l;
            wxString	s;

            switch( pParameter->Get_Type() )
            {
            case PARAMETER_TYPE_Table_Field:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    if( s.ToLong(&l) )
                    {
                        pParameter->Set_Value((int)l);
                    }
                    else
                    {
                        pParameter->Set_Value(CSG_String(&s));
                    }
                }
                break;

            case PARAMETER_TYPE_Table_Fields:
                if( m_CMD.Found(_Get_ID(pParameter), &s) )
                {
                    pParameter->Set_Value(CSG_String(&s));
                }
                break;
            }
        }
    }

    //-----------------------------------------------------
    return( true );
}
Exemple #3
0
//---------------------------------------------------------
bool CCMD_Module::Execute(CSG_String sLibName, int argc, char *argv[])
{
    int		i;

    //-----------------------------------------------------
    if( !m_pModule )
    {
        return( false );
    }

    if( argc <= 1 )
    {
        wxString sUsage = m_CMD.GetUsageString();
        sUsage = wxString::Format(SG_T("Usage: saga_cmd %s %d %s"), sLibName.c_str(), m_pModule->Get_ID(), sUsage.AfterFirst(' ').AfterFirst(' '));
        SG_PRINTF(sUsage);

        return( false );
    }

    //-----------------------------------------------------
    // m_CMD.SetCmdLine(argc, argv);
    //
    // We can't do it this way (passing argv as char**) because then we use an
    // overload of the method which (re-)sets the locale from the current
    // enviromment; in order to prevent this, we use wxString overload

    wxString	sCmdLine;

    for(i=1; i<argc; i++)
    {
        wxString	sTmp = argv[i];

        sCmdLine += wxString::Format(SG_T("\"%s\" "), sTmp.c_str());
    }

    m_CMD.SetCmdLine(sCmdLine);

    //-----------------------------------------------------
    bool	bResult	= _Get_Parameters(m_pModule->Get_Parameters());

    for(i=0; bResult && i<m_pModule->Get_Parameters_Count(); i++)
    {
        _Get_Parameters(m_pModule->Get_Parameters(i));
    }

    if( !bResult )
    {
        CMD_Print("");

        wxString sUsage = m_CMD.GetUsageString();
        sUsage = wxString::Format(SG_T("Usage: saga_cmd %s %d %s"), sLibName.c_str(), m_pModule->Get_ID(), sUsage.AfterFirst(' ').AfterFirst(' '));
        SG_PRINTF(sUsage);
    }

    //-----------------------------------------------------
    CMD_Set_Module(this);

    if( bResult && m_pModule->On_Before_Execution() )
    {
        bResult	= m_pModule->Execute();

        m_pModule->On_After_Execution();
    }

    CMD_Set_Module(NULL);

    //-----------------------------------------------------
    if( bResult )
    {
        _Save_Output(m_pModule->Get_Parameters());

        for(i=0; i<m_pModule->Get_Parameters_Count(); i++)
        {
            _Save_Output(m_pModule->Get_Parameters(i));
        }

        SG_Get_Data_Manager().Delete_Unsaved();	// remove temporary data to save memory resources
    }
    else
    {
        CMD_Print_Error(_TL("executing tool"), m_pModule->Get_Name());
    }

    return( bResult );
}
//---------------------------------------------------------
int		Callback(TSG_UI_Callback_ID ID, CSG_UI_Parameter &Param_1, CSG_UI_Parameter &Param_2)
{
	static int		iBuisy		= 0;
	static int		iPercent	= -1;
	const SG_Char	Buisy[4]	= {	'|', '/', '-', '\\'	};

	int		Result	= 1;

	//-----------------------------------------------------
	switch( ID )
	{
	default:

		Result	= 0;

		break;


	///////////////////////////////////////////////////////
	//                                                   //
	///////////////////////////////////////////////////////

	//-----------------------------------------------------
	case CALLBACK_PROCESS_GET_OKAY:

		if( g_bShow_Progress && Param_1.True )
		{
			SG_PRINTF(SG_T("\r%c   "), Buisy[iBuisy++]);

			iBuisy	%= 4;
		}

		break;


	//-----------------------------------------------------
	case CALLBACK_PROCESS_SET_OKAY:

		break;


	//-----------------------------------------------------
	case CALLBACK_PROCESS_SET_PROGRESS:

		if( g_bShow_Progress )
		{
			int	i	= Param_2.Number != 0.0 ? 1 + (int)(100.0 * Param_1.Number / Param_2.Number) : 100;

			if( i != iPercent )
			{
				if( g_bXML )
				{
					SG_PRINTF(SG_T("<%s>%d</%s>\n"), SG_XML_PROGRESS, iPercent = i, SG_XML_PROGRESS);
				}
				else
				{
					if( iPercent < 0 || i < iPercent )
					{
						SG_PRINTF(SG_T("\n"));
					}

					SG_PRINTF(SG_T("\r%3d%%"), iPercent = i);
				}
			}
		}

		break;

	//-----------------------------------------------------
	case CALLBACK_PROCESS_SET_READY:

		iPercent	= -1;

		break;


	//-----------------------------------------------------
	case CALLBACK_PROCESS_SET_TEXT:

		if( g_bShow_Messages )
		{
			CMD_Print(Param_1.String, SG_XML_MESSAGE_PROC);
		}

		break;


	///////////////////////////////////////////////////////
	//                                                   //
	///////////////////////////////////////////////////////

	//-----------------------------------------------------
	case CALLBACK_MESSAGE_ADD:

		if( g_bShow_Messages )
		{
			CMD_Print(Param_1.String, SG_XML_MESSAGE);
		}

		break;


	//-----------------------------------------------------
	case CALLBACK_MESSAGE_ADD_ERROR:

		CMD_Print_Error(Param_1.String);

		break;


	//-----------------------------------------------------
	case CALLBACK_MESSAGE_ADD_EXECUTION:

		if( g_bShow_Messages )
		{
			CMD_Print(Param_1.String, SG_XML_MESSAGE_EXEC);
		}

		break;


	///////////////////////////////////////////////////////
	//                                                   //
	///////////////////////////////////////////////////////

	//-----------------------------------------------------
	case CALLBACK_DLG_MESSAGE:

		if( g_bShow_Messages )
		{
			CMD_Print(Param_2.String + ": " + Param_1.String, NULL);
		}

		break;


	//-----------------------------------------------------
	case CALLBACK_DLG_CONTINUE:

		Result	= CMD_Get_YesNo(Param_2.String.c_str(), Param_1.String.c_str());

		break;


	//-----------------------------------------------------
	case CALLBACK_DLG_ERROR:

		Result	= CMD_Get_YesNo(Param_2.String.c_str(), Param_1.String.c_str());

		break;


	///////////////////////////////////////////////////////
	//                                                   //
	///////////////////////////////////////////////////////

	//-----------------------------------------------------
	case CALLBACK_DATAOBJECT_ADD:

		Result	= SG_Get_Data_Manager().Add((CSG_Data_Object *)Param_1.Pointer) ? 1 : 0;

		break;


	//-----------------------------------------------------
	case CALLBACK_DATAOBJECT_UPDATE:

		break;


	//-----------------------------------------------------
	case CALLBACK_DATAOBJECT_SHOW:

		break;


	//-----------------------------------------------------
	case CALLBACK_DATAOBJECT_COLORS_GET:

		break;


	//-----------------------------------------------------
	case CALLBACK_DATAOBJECT_COLORS_SET:

		break;


	//-----------------------------------------------------
	case CALLBACK_DATAOBJECT_PARAMS_GET:

		break;


	//-----------------------------------------------------
	case CALLBACK_DATAOBJECT_PARAMS_SET:

		break;

	//-----------------------------------------------------
	case CALLBACK_DLG_PARAMETERS:

		Result	= g_pCMD_Module && g_pCMD_Module->Get_Parameters((CSG_Parameters *)Param_1.Pointer) ? 1 : 0;

		break;


	///////////////////////////////////////////////////////
	//                                                   //
	///////////////////////////////////////////////////////

	//-----------------------------------------------------
	}

	return( Result );
}
//---------------------------------------------------------
void			CMD_Print_Error		(const CSG_String &Error, const CSG_String &Info)
{
	CMD_Print_Error(CSG_String::Format(SG_T("%s [%s]"), Error.c_str(), Info.c_str()));
}
//---------------------------------------------------------
bool CCMD_Module::Execute(int argc, char *argv[])
{
	//-----------------------------------------------------
	if( !m_pLibrary || !m_pModule )
	{
		return( false );
	}

	if( argc <= 1 )
	{
		Usage();

		return( false );
	}

	//-----------------------------------------------------
	// m_CMD.SetCmdLine(argc, argv);
	//
	// We can't do it this way (passing argv as char**) because then we use an
	// overload of the method which (re-)sets the locale from the current
	// enviromment; in order to prevent this, we use wxString overload
	{
		wxString	sCmdLine;

		for(int i=1; i<argc; i++)
		{
			sCmdLine	+= wxString(i == 1 ? "\"" : " \"") + argv[i] + "\"";
		}

		m_CMD.SetCmdLine(sCmdLine);
	}

	if( m_CMD.Parse(false) != 0 )
	{
		Usage();

		return( false );
	}

	//-----------------------------------------------------
	int		i;

	bool	bResult	= _Get_Parameters(m_pModule->Get_Parameters(), true);

	for(i=0; bResult && i<m_pModule->Get_Parameters_Count(); i++)
	{
		bResult	= _Get_Parameters(m_pModule->Get_Parameters(i), true);
	}

	if( !bResult )
	{
		Usage();

		return( false );
	}

	//-----------------------------------------------------
	CMD_Set_Module(this);

	if( m_pModule->On_Before_Execution() )
	{
		bResult	= m_pModule->Execute();

		m_pModule->On_After_Execution();
	}

	CMD_Set_Module(NULL);

	//-----------------------------------------------------
	if( bResult )
	{
		_Save_Output(m_pModule->Get_Parameters());

		for(i=0; i<m_pModule->Get_Parameters_Count(); i++)
		{
			_Save_Output(m_pModule->Get_Parameters(i));
		}

		SG_Get_Data_Manager().Delete_Unsaved();	// remove temporary data to save memory resources
	}
	else
	{
		CMD_Print_Error(_TL("executing tool"), m_pModule->Get_Name());
	}

	return( bResult );
}