| Version 1.9 Build 803
|
|
Next: Appendix: MakeOtt
Up: NOTE 257 -
Previous: Appendix: ottClient.g
The C++ wrapper main program to the C functions is listed below.
The functions performing the calculations are in ottFluxes.c.
/* File ottClient.cc, version 1.2, released 02/12/02 at 11:42:52
retrieved by SCCS 02/12/02 at 11:43:21
C++ client code wrapper for C functions to calculate the Ott et al fluxes.
HISTORY
021202 GIL remove un-used segments of the code.
021118 GIL add some comments
021115 GIL Initial version based on ex_client.cc
021114 GIL Removed support for complex functions
970803 JRF Initial version, well documented at
http://www.gb.nrao.edu/~rfisher/Glish/ex_client.html
DESCRIPTION
Rick Fisher created a very nice set of example code that allows an
observer to quickly create a interface from C to glish.
This function impliments the C++ wrapper to a simple C program to calculate
the Ott et al 1994 flux densities for a set of reference sources.
There are two steps in the process: First is setting the source name
for which the values are calculated. The second step is providing
an array of frequencies (Hz), for which the flux densities are required.
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "Glish/Client.h"
// Declare all of the C/C++ functions that you are going to use or put
// the main() function at the end.
void setFluxes(Client &c, GlishEvent *e);
void setSource(Client &c, GlishEvent *e);
extern "C" {
char * setOttSourceName( char * source);
char * setOttSourceFluxes( long n, double values[]);
} /* end of C declares */
int main (int argc, char **argv) {
// This creates a required client object.
Client c(argc, argv);
// The client can be invoked with arguments, but we`ll bypass that
// complication.
if (argc > 1) { /* if any argument, explain usage */
printf ("Usage: cl := client('ex_client')\n");
return 1;
} /* end if an argument */
// Create a pointer to be assigned to a received glish event.
GlishEvent *e;
// Stay in this loop until the client is terminated. The c.NextEvent()
// function blocks until it receives an event from glish. It then
// returns an event pointer that is used to access the values passed
// from glish.
while ((e = c.NextEvent())) {
// Search for an expected event name and execute the appropriate
// function when found.
// vvvvvvvvvvvv Your code substituted below here. vvvvvvvvvvvv
if (!strcmp(e->Name(), "ottSetSource")) {
setSource( c, e);
} else if (!strcmp(e->Name(), "ottSetFlux")) {
setFluxes( c, e);
// ^^^^^^^^^^^^ Your code substituted above here. ^^^^^^^^^^^^
} else {
// Report an error if an event name is not recognized.
c.Unrecognized();
}
}
return 0;
} /* end of client main() */
void setFluxes(Client &c, GlishEvent *e)
{
// See do_int_squared() function for comments on statements which
// are common to all functions.
Value *val = e->Val();
long array_length = val->Length(), i = 0;
double *return_value = new double[array_length];
for (i = 0; i < array_length; i++) /* default value is 1. */
return_value[i] = 1.0;
if (val->Type() != TYPE_DOUBLE || array_length <= 1) {
printf("Double type array expected from `setFluxes'\n");
} else {
double * dataValues = val->DoublePtr();
double * freqFluxes = new double[array_length];
// vvvvvvvvvvvv Your code substituted below here. vvvvvvvvvvvv
for (i = 0; i < array_length; i++)
freqFluxes[i] = dataValues[i];
setOttSourceFluxes( array_length, freqFluxes);
for (i = 0; i < array_length; i++) /* transfer out */
return_value[i] = freqFluxes[i];
// ^^^^^^^^^^^^ Your code substituted above here. ^^^^^^^^^^^^
delete freqFluxes;
} /* end else if data of expected type */
Value *rep = new Value(return_value, array_length, COPY_ARRAY);
c.Reply(rep);
delete return_value;
Unref(rep);
} /* end of setFluxes() */
#define MAXSIZE 100 /* set string max size */
void setSource(Client &c, GlishEvent *e) {
// setSource() takes the source name event and transfers the name to the C
// functions.
Value *val = e->Val();
char return_value[MAXSIZE] = "", sourceName[MAXSIZE] = "", * errMsg = '\0';
if (val->Type() != TYPE_STRING) {
printf("String type value expected from `reverse_string'\n");
} else {
char *received_value = val->StringVal();
strncpy( sourceName, received_value, MAXSIZE);
sourceName[MAXSIZE-1] = '\0';
// The call of val->StringVal() implicitly allocates memory
// for the string which must be deleted to avoid a memory leak.
delete received_value;
// vvvvvvvvvvvv Your code substituted below here. vvvvvvvvvvvv
errMsg = setOttSourceName( sourceName);
if (errMsg != '\0') /* check for error strings */
strcpy( return_value, "!!!! ");
strcat( return_value, sourceName);
// ^^^^^^^^^^^^ Your code substituted above here. ^^^^^^^^^^^^
}
Value *rep = new Value(return_value);
c.Reply(rep);
Unref(rep);
} /* end of setSource() */
Next: Appendix: MakeOtt
Up: NOTE 257 -
Previous: Appendix: ottClient.g
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc.,
Washington, D.C.
Return to AIPS++ Home Page
2004-08-28