Consultor Eletrônico



Kbase 6152: SAMPLE CODE to pass data between PROGRESS and C funcs
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   10/05/1998
SAMPLE CODE to pass data between PROGRESS and C funcs

920317-gss01This notebook entry contains examples of C functions and Progress
statements that pass data between the Progress program and the C
functions using shared buffers and shared variables.

The C programs included are:
addsbuf.c -- Contains functions that read in data from the
Progress program hlctest.p through shared
buffers, modify that data, and pass it back
out to hlctest.p through shared buffers.

addsvar.c -- Contains functions that read in data from the
Progress program hlctest.p through shared
variables, modify that data, and pass it back
out to hlctest.p through shared buffers.

hlcprodsp.c -- The dispatch function file that contains
definitions for the function identifiers and
their corresponding C function names.

The Progress program included is hlctest.p. This program makes
calls to the C functions and runs against a copy of the demo
data base.

=====================================================================


/* HLC example program addsbuf.c */

#include "hlc.h"
#include <math.h>

#define BUFLEN 100

/********************************************************************/
/* Shared buffer data transfer - decimal field. Define a shared */
/* buffer, CUSTBUF, for the customer file in the demo database. */
/* Read the curr-bal field into this c function, add 100 to that */
/* value and write curr-bal back out to CUSTBUF. */
/********************************************************************/
sbufdec()
{
char buffer[BUFLEN];
int fldpos, ret, unknown = 0, varlen = BUFLEN, actlen;
double currbal;

/* get the field handle for the curr-bal field in the shared */
/* buffer CUSTBUF */
fldpos = profldix("custbuf","curr-bal");

/* read the decimal numeric field from CUSTBUF and place in */
/* buffer */
ret = prordbn("custbuf",fldpos,0,buffer,&unknown,varlen,&actlen);
buffer[actlen] = '\0';

/* convert buffer to float and assign to currbal, add 100 to */
/* currbal */
currbal = atof(buffer) + 100.0;

/* copy new currbal back into buffer */
sprintf(buffer,"%.2f",currbal);

/* write currbal to the decimal field in shared buffer CUSTBUF */
ret = prowtbn("custbuf",fldpos,0,buffer,unknown);
}

/********************************************************************/
/* Shared buffer data transfer - integer field. Define a shared */
/* buffer, CUSTBUF, for the customer file in the demo database. */
/* Read the discount field into this c function, add 10 to that */
/* value and write discount back out to CUSTBUF. */
/********************************************************************/
sbufint()
{
long int intbuffer;
int fldpos, ret, unknown = 0;

/* get the field handle for the discount field in the shared */
/* buffer CUSTBUF */
fldpos = profldix("custbuf","discount");

/* read the integer numeric field from CUSTBUF */
ret = prordbi("custbuf",fldpos,0,&intbuffer,&unknown);

/* add 10 to value of discount */
intbuffer += 10L;

/* write discount to the integer field in shared buffer CUSTBUF */
ret = prowtbi("custbuf",fldpos,0,intbuffer,unknown);
}

/********************************************************************/
/* Shared buffer data transfer - character field. Define a shared */
/* buffer CUSTBUF, for the customer file in the demo database. Read */
/* the name field into this c function, append 'X' to the end of */
/* the name and write name back out to CUSTBUF. */
/********************************************************************/
sbufchar()
{
char buffer[BUFLEN];
int fldpos, ret, unknown = 0, varlen = BUFLEN, actlen;

/* get the field handle for the name field in the shared buffer */
/* CUSTBUF */
fldpos = profldix("custbuf","name");

/* read the character name field from CUSTBUF and place in buffer*/
ret = prordbc("custbuf",fldpos,0,buffer,&unknown,varlen,&actlen);

/* add the character 'X' to the buffer */
sprintf(buffer,"%sX",buffer);

/* write the new name back out to the name field in the CUSTBUF */
/* buffer */
ret = prowtbc("custbuf",fldpos,0,buffer,unknown);
}

/********************************************************************/
/* Shared buffer data transfer - date field. Define a shared buffer,*/
/* ORDERBUF, for the order file in the demo database. Read the */
/* odate field into this c function, add 1 to the year and then */
/* write odate back out to ORDERBUF. */
/********************************************************************/
sbufdate()
{
int fldpos, ret, unknown = 0, month, day, year;

/* get the field handle for the name field in the shared buffer */
/* ORDERBUF */
fldpos = profldix("orderbuf","odate");

/* read the date odate field from ORDERBUF */
ret = prordbd("orderbuf",fldpos,0,&year,&month,&day,&unknown);

/* add 1 year to odate */
year +=1;

/* write the new year back out to the odate field in the ORDERBUF*/
/* buffer */
ret = prowtbd("orderbuf",fldpos,0,year,month,day,unknown);
}

/********************************************************************/
/* Shared buffer data transfer - logical field. Define a shared */
/*buffer, SYSCBUF, for the syscontrol file in the demo database. */
/* Read the printr field into this c function, change the logical */
/* value from yes to no and write the printr field back out to */
/* SYSCBUF */
/********************************************************************/
sbuflog()
{
int fldpos, ret, unknown = 0, logical;

/* get the field handle for the printr field in the shared buffer*/
/* SYSCBUF */
fldpos = profldix("syscbuf","printr");

/* read the printr logical field from SYSCBUF */
ret = prordbl("syscbuf",fldpos,0,&logical,&unknown);

/* set printr value to no */
logical = 0;

/* write the new printr value back out to the printr field in the*/
/* SYSCBUF buffer */
ret = prowtbl("syscbuf",fldpos,0,logical,unknown);
}


=====================================================================


/* HLC example program addsvar.c */
#include "hlc.h"
#include <math.h>

#define BUFLEN 100

/********************************************************************/
/* Shared variable data transfer - decimal field. Define a shared */
/* var s_curr-bal, for the curr-bal field in the customer file of */
/* the demo database. Read the value of s_curr-bal into this C */
/* function, add 100 to it and write it back out to the s_curr-bal */
/* shared variable */
/********************************************************************/
svardec()
{
char buffer[BUFLEN];
int ret, unknown = 0, varlen = BUFLEN, actlen;
double currbal;

/* read the decimal numeric field from s_curr-bal */
ret = prordn("s_curr-bal",0,buffer,&unknown,varlen,&actlen);
buffer[actlen] = '\0';

/* convert buffer to float and assign to currbal, add 100 to */
/* currbal */
currbal = atof(buffer) + 100.0;

/* copy new currbal back into buffer */
sprintf(buffer,"%.2f",currbal);

/* write currbal back out to s_curr-bal shared variable */
ret = prowtn("s_curr-bal",0,buffer,unknown);
}

/********************************************************************/
/* Shared variable data transfer - integer field. Define a shared */
/* var s_discount, for the discount field in the customer file of */
/* the demo database. Read the value of s_discount into this C */
/* function, add 10 to it and write it back out to the s_discount */
/* shared variable */
/********************************************************************/
svarint()
{
int ret, unknown = 0;
long int discount;

/* read the integer numeric field from s_discount */
ret = prordi("s_discount",0,&discount,&unknown);

/* add 10 to s_discount */
discount += 10;

/* write discount back out to s_discount shared variable */
ret = prowti("s_discount",0,discount,unknown);
}

/********************************************************************/
/* Shared variable data transfer - character field. Define a shared */
/* var s_name, for the name field in the customer file of the demo */
/* database. Read the value of s_name into this c function, add 'X' */
/* to the end of it and write it back out to the s_name shared */
/* variable */
/********************************************************************/
svarchar()
{
char buffer[BUFLEN];
int ret, unknown = 0, varlen = BUFLEN, actlen;

/* read the name character field from s_name */
ret = prordc("s_name",0,buffer,&unknown,varlen,&actlen);
buffer[actlen] = '\0';

/* add 'X' to the end of the name */
sprintf(buffer,"%sX",buffer);

/* write buffer back out to s_name shared variable */
ret = prowtc("s_name",0,buffer,unknown);
}

/********************************************************************/
/* Shared variable data transfer - date field. Define a shared var */
/* s_odate, for the odate field in the order file of the demo */
/* database. Read the value of s_odate into this C function, add 1 */
/* to the year write it back out to the s_odate shared variable */
/********************************************************************/
svardate()
{
int ret, unknown = 0, year, month, day;

/* read the date field from s_odate */
ret = prordd("s_odate",0,&year,&month,&day,&unknown);

/* add 1 to year */
year += 1;

/* write year back out to s_odate shared variable */
ret = prowtd("s_odate",0,year,month,day,unknown);
}

/********************************************************************/
/* Shared variable data transfer - logical field. Define a shared */
/* var s_printr, for the printr field in the syscontrol file of the */
/* demo database. Read the value of s_printr into this c function, */
/* and change the value to no and write it back out to the s_printr */
/* shared variable */
/********************************************************************/
svarlog()
{
int ret, unknown = 0, logical;

/* read the logical field from s_printr */
ret = prordl("s_printr",0,&logical,&unknown);

/* change value of printr to no */
logical = 0;

/* write logical back out to s_printr shared variable */
ret = prowtl("s_printr",0,logical,unknown);
}

======================================================================


/* File: hlprodsp.c */

#define FUNCTEST(nam, rout) if (strcmp(nam, pfunnam) == 0) return rout(argc,argv);

/* PROGRAM: PRODSP
*
* This is the interface to all C routines that
* PROGRESS has associated 'call' statements to.
*/

long
PRODSP(pfunnam, argc, argv)

char *pfunnam; /* Name of function to call */
int argc;
char *argv[];

{
/* Interface to installation test */
FUNCTEST ("SBUFDEC", sbufdec);
FUNCTEST ("SBUFINT", sbufint);
FUNCTEST ("SBUFCHAR",sbufchar);
FUNCTEST ("SBUFDATE",sbufdate);
FUNCTEST ("SBUFLOG",sbuflog);
FUNCTEST ("SVARDEC",svardec);
FUNCTEST ("SVARINT",svarint);
FUNCTEST ("SVARCHAR",svarchar);
FUNCTEST ("SVARDATE",svardate);
FUNCTEST ("SVARLOG",svarlog);

return 1;
}


=====================================================================


/********************************************************************/
/* Shared buffer data transfer between PROGRESS programs and c */
/* functions */
/********************************************************************/

def new shared buffer custbuf for customer.
def new shared buffer orderbuf for order.
def new shared buffer syscbuf for syscontrol.

find first custbuf.

curr-bal = 1050.
discount = 100.
name = "First Customer".

display custbuf.curr-bal with frame a row 1
title " Curr-Bal before adding 100 ".
call SBUFDEC.
display custbuf.curr-bal with frame b row 8
title " Curr-Bal after adding 100 ".

display custbuf.discount with frame c row 1
title " Discount before adding 10 ".
call SBUFINT.
display custbuf.discount with frame d row 8
title " Discount after adding 10 ".

display custbuf.name with frame e row 1
title " Name before appending X ".
call SBUFCHAR.
display custbuf.name with frame f row 8
title " Name after appending X ".
custbuf.name = substr(custbuf.name,1,((length(custbuf.name)) - 1)).

find first orderbuf.
odate = 10/10/90.
display orderbuf.odate with frame g row 1
title " Odate before adding 1 to year ".
call SBUFDATE.
display orderbuf.odate with frame h row 8
title " Odate after adding 1 to year ".

find first syscbuf.
printr = yes.
display syscbuf.printr with frame i row 1
title " Printr is now yes ".
call SBUFLOG.
display syscbuf.printr with frame j row 8
title " Printr is now no ".
printr = yes.

/********************************************************************/
/* Shared variable data transfer between PROGRESS programs and c */
/* functions */
/********************************************************************/

def new shared variable s_curr-bal as dec.
def new shared variable s_discount as int.
def new shared variable s_name as char format "x(20)".
def new shared variable s_odate as date.
def new shared variable s_printr as logical.

find first customer.
s_curr-bal = customer.curr-bal.
display s_curr-bal with frame k row 1
title " S_curr-bal before adding 100 ".
CALL SVARDEC.
display s_curr-bal with frame l row 8
title " S_curr-bal after adding 100 ".

s_discount = customer.discount.
display s_discount with frame m row 1
title " S_discount before adding 10 ".
CALL SVARINT.
display s_discount with frame n row 8
title " S_discount after adding 10 ".

s_name = customer.name.
display s_name with frame o row 1 title " S_name before adding X ".
CALL SVARCHAR.
display s_name with frame p row 8 title " S_name after adding X ".

find first order.
s_odate = order.odate.
display s_odate with frame q row 1
title " S_odate before adding 1 to year ".
CALL SVARDATE.
display s_odate with frame r row 8
title " S_odate after adding 1 to year ".

find first syscontrol.
s_printr = syscontrol.printr.
display s_printr with frame s row 1
title " S_printr before changing to no ".
CALL SVARLOG.
display s_printr with frame t row 8
title " S_printr after changing to no ".

Progress Software Technical Support Note # 6152