Functions

gs_info.cpp File Reference

#include <oct.h>
#include <stdio.h>
#include <stdlib.h>
#include "grpc.h"
#include "gs_oct.h"
Include dependency graph for gs_info.cpp:

Go to the source code of this file.

Functions

 DEFUN_DLD (gs_info, args, nargout,"Octave client for GridSolve")

Function Documentation

DEFUN_DLD ( gs_info  ,
args  ,
nargout  ,
"Octave client for GridSolve"   
)

Definition at line 23 of file gs_info.cpp.

                                                                  {
    octave_value_list bad_retval;   //bad return value
    gs_problem_t* pd;               //GridSolve problem description
    gs_argument_t* argp;            //GridRPC argument pointer
    grpc_function_handle_t *handle; //GridRPC function handle
    grpc_error_t status;            //GridRPC status/error code

        //meaningless return value for error condition
    //or those calls that do not make grpc function call
    for (int i = 0; i < nargout; i++) {
        bad_retval(i) = Matrix(0, 0);
    }

    //initialize the GridSolve environment
    status = grpc_initialize(NULL);
    if (status != GRPC_NO_ERROR && status != GRPC_ALREADY_INITIALIZED) {
        fprintf(stderr, "%s\n", grpc_error_string(status));
        return bad_retval;
    }

    //number of input arguments
    int nargin = args.length();
    
    //input error checking
    if (nargin <= 0) {
        fprintf(stderr, "No input service name found. Aborted.\n");
        return bad_retval;
    }

    if (nargin > 1) {
        fprintf(stderr, "Exactly one argument (service name) is expected. Aborted.\n");
        return bad_retval;
    }

    if (!args(0).is_string()) {
        fprintf(stderr, "Invalid first parameter: should be a string. Aborted.\n");
        grpc_finalize();
        return bad_retval;
    }
    
    //otherwise, the input is correct

    int strlen = args(0).string_value().length();

    //get the problem name
    char *pname = new char[strlen + 1];
    for (int i = 0; i < strlen; i++)
        pname[i] = (args(0).string_value())[i];
    pname[strlen] = '\0';

    //initialize the GridRPC function handle
    handle = (grpc_function_handle_t *) malloc(sizeof(grpc_function_handle_t));
    if (handle == NULL) {
        perror("malloc");
        delete pname;
        grpc_finalize();
        return bad_retval;
    }

    status = grpc_function_handle_default(handle, pname);
    if (status != GRPC_NO_ERROR) {
        fprintf(stderr, "%s\n", grpc_error_string(status));
        delete pname;
        grpc_function_handle_destruct(handle);
        if (handle != NULL) free(handle);
        grpc_finalize();
        return bad_retval;
    }

    //get the GridSolve problem description
    pd = handle->problem_desc;

    if (pd == NULL) {
        fprintf(stderr, "Empty GridSolve problem description. Aborted.\n");
        delete pname;
        grpc_function_handle_destruct(handle);
        if (handle != NULL) free(handle);
        grpc_finalize();
        return bad_retval;
    }

    //print out the problem description
    gs_problem_dump(pd);
    
    //generate the function call prototype
    printf("\nOctave call prototype:\n");
    printf("[");
    bool first_arg = true;
    //output arguments first (right hand side)
    for (argp = pd->arglist; argp != NULL; argp = argp->next) {
        if (argp->inout == GS_INOUT ||
            argp->inout == GS_OUT || 
            argp->inout == GS_VAROUT) {
            if (first_arg) {
                printf("%s", argp->name);
                first_arg = false;
            } else printf(", %s", argp->name);
        }
    }
    printf("] = gs_call(");
    first_arg = true;
    //and then input arguments (left hand side)
    for (argp = pd->arglist; argp != NULL; argp = argp->next) {
        if (argp->inout == GS_IN ||
            argp->inout == GS_INOUT) {
            if (first_arg) {
                printf("%s", argp->name);
                first_arg = false;
            } else printf(", %s", argp->name);
        }
    }
    printf(")\n\n");
    
    //finalize and clean up
    grpc_function_handle_destruct(handle);
    if (handle != NULL) free(handle);
    delete pname;
    grpc_finalize();
}

Here is the call graph for this function: