Shared Samples

Try out source code samples stored in Bitbucket repositories.
Showing results for 
Search instead for 
Do you mean 

Job template

by Technical Evangelist on ‎09-08-2015 10:09 AM - edited on ‎03-29-2016 11:34 AM by Anonymous (733 Views)

Example Type

Job

 

 

Purpose

            To provide users a template for setting up command line processes (Jobs) in IMAGINE.

 

 

Files

            jobTemplate.c              (C source code)

           

 

Description

The jobTemplate example is intended to provide users with a simple, but extensible, template of the necessary structure to create a job under IMAGINE. The template contains the necessary functions and structure to create a three option JOB. The user can modify or add additional options to accommodate almost any command line process that is needed.

The two main elements to be aware of belong to the earg package.  Earg_Cmd is the main structure that defines the command line options and syntax. Each option on the command line is defined in its own function. The earg_DoArgs function, usually located in the main, invokes the arguments as defined in the Earg_Cmd structure.

 

 

Command Line Options

 

jobtemplate [-in[put] <inputfilename>] [-out[put] <outputfilename>] [-f[lag] [0/1]]

 

-in[put]

 Any input filename. This program doesn’t actually use this filename.

 

-out[put]

 Any output filename. This program doesn’t actually use this filename.

 

-f[lag]

 A limited option flag that the user can specify. This option has a limited number of available inputs. The programmer is responsible for setting a default value and making sure that the user specifies a valid argument. If the user doesn’t specify a valid argument, the job uses the default.

 

Example: job jobTemplate -in"" -out"" -f 1

 

 

Output

            Using the above options, "You selected the optional flag!" is printed to the Session Log.

Notes

Jobs are usually used for large, long running, processes that can be invoked using command line parameters. This type of program is usually used in data processing routines whereby the output is usually a data file of some type.

 

Source code:

https://bitbucket.org/HGDN/hgdn_erdas_imagine/src/e1ade3b7d148/Job_jobTemplate/?at=master

 

/************************************************************
*************************************************************

jobTemplate.c

This is a simple template meant to illustrate the a minimum
of code necessary to set up a proper job. The input and 
output options are not used but are included as a standard
commandline that a programmer would want to set up.

By: Adam Estrada

Calling syntax:

jobtemplate -in <dummyinputfile> -out <dummyoutputfile>
-f <0 or 1>

**************************************************************
*************************************************************/

/*
**	Standard Includes
*/
#include <stdio.h>

/*
**	Toolkit Includes
*/
#include <earg.h>
#include <eerr.h>
#include <eint.h>
#include <esmg.h>
#include <emsc.h>

/*
**	Error checking Macros
*/

#define EFS0( msg ) EERR_FATAL_SET_0( __LINE__, msg )
#define ES0(err, msg) EERR_SHOW_0(err, EERR_DEBUG, __LINE__, msg)

/*
**	Functions
*/
static void	jobMain(int, char **);
static void	set_input(int, char **);
static void	set_output(int, char **);
static void	set_optionalflag(int, char **);

/* 
** Command Argument Structure
*/

Earg_Cmd command[] =
{
    set_input,        "-in[put] %s",  "", NULL,
    set_output,       "-out[put] %s", "", NULL,
    set_optionalflag, "-f[lag] [%d]", "", NULL,
    jobMain,          "jobTemplate",  "", NULL,
    EARG_END
};

/*
**	Global Variables
*/
static char *input = NULL;		
static char *output = NULL;			
static Emsc_Boolean	optionalflag = EMSC_FALSE; 
static int	rc = -1; 

/*
**	Execution entry point
*/
int
main(
	int argc, 
	char **argv
)
{
    EERR_INIT("main", &lclerr, lclerr);
    
    (void)eint_InitToolkit((Emsc_Opaque**)&lclerr);
    EERR_CHECK(lclerr, EFS0("Error initializing the Toolkit"));
    
    esmg_JobInit("jobTemplate", &lclerr);
    EERR_CHECK(lclerr, EFS0("Error connecting to the Session Manager"));
    
    earg_DoArgs(command, argc, argv, &lclerr);
    ES0(lclerr, "An error has been found in the command line syntax!");
    
    esmg_JobEnd(&lclerr);
    ES0(lclerr, "An error was encountered while disconnecting from the session!");
    
cleanup:
    ES0(lclerr, "Errors reported by main");
    
    return rc;
}

/*
**	Input file
*/
static void
set_input(
	int argc, 
	char **argv
)
{
    input = estr_Duplicate(argv[1]);
    return;
}

/*
**	output file
*/
static void
set_output(
	int argc,
	char **argv
)
{
    output = estr_Duplicate(argv[1]);
    return;
}

/*
**	Optional flag command can be used like this for setting variable arguments
*/
static void
set_optionalflag(
	int argc, 
	char **argv
)
{
    if (argc == 1)
    {
        optionalflag = EMSC_FALSE;
    }
    
    else
    {
        if (estr_Eqic(argv[1], "1"))
        {
            optionalflag = EMSC_TRUE;
        }
        else if (estr_Eqic(argv[1], "0"))
        {
            optionalflag = EMSC_FALSE;
        }
        else 
        {
            optionalflag = EMSC_TRUE;
        }
    }
    
    return;
}


/*
**	The heart of the processing and where most of the action takes
**  place since it may be the only required argument on the command line
*/
static void
jobMain(
	int argc,
	char **argv
)
{
    Eint_InitToolkitData *erdInit = eint_GetInit();
    
    EERR_INIT("jobMain", &lclerr, lclerr);
    
    if (optionalflag)
    {
        esmg_LogMessage(ESMG_LOG_TERSE, &lclerr,
        "You selected the optional flag!", NULL);
        EERR_CHECK(lclerr, EFS0("Error creating Session Log message"));
    }
        
    rc = 0;
    
cleanup:
    ES0(lclerr, "Errors reported by jobMain");

	emsc_Free(input);
	emsc_Free(output);
    
    return;
}

 

Overview
Contributors