home *** CD-ROM | disk | FTP | other *** search
- /* cfortex.c */
- /* Burkhard Burow, burow%13313.hepnet@csa3.lbl.gov, U. of Toronto, 1991. */
-
- #include <stdio.h>
- #include "cfortran.h"
-
- #define EQ_SELECT 1 /* To see the various examples select one of:
- EASY_SELECT, ST_SELECT, FT_SELECT, S1_SELECT, ABC_SELECT, R_SELECT,
- REV_SELECT, FCB_SELECT, EQ_SELECT, F0_SELECT, FA_SELECT, FB_SELECT,
- FC_SELECT, FD_SELECT, FE_SELECT, FF_SELECT, FG_SELECT, FH_SELECT,
- FI_SELECT, FJ_SELECT. */
-
- #ifdef EASY_SELECT
- PROTOCCALLSFSUB2(easy,PINT,INT)
- #define EASY(A,B) CCALLSFSUB2(easy,PINT,INT, A,B)
-
- main() {
- int a;
- printf("\nEASY EXAMPLE\n");
- EASY(a,7);
- printf("The FORTRAN routine easy(a,7) returns a = %d\n", a);
- }
- #endif
-
- #ifdef ST_SELECT
- PROTOCCALLSFSUB3(st,PSTRINGV,STRINGV,FLOAT)
- #define ST(A,B,C) CCALLSFSUB3(st,PSTRINGV,STRINGV,FLOAT,A,B,C)
-
- int main() {
- static char v[][5] = {"000 ", "1", "22", " "};
- static char w[][9] = {" ", "bb","ccc ","dddd"};
- ST(v, w, 10.);
- printf("main:v=%s,%s,%s,%s. PSTRINGV => Has had trailing blanks stripped.\n",
- v[0],v[1],v[2],v[3]);
- printf("main:w=%s,%s,%s,%s. STRINGV => malloc'd copy for FORTRAN=> C intact.\n"
- ,w[0],w[1],w[2],w[3]);
- }
- #endif
-
- #ifdef FT_SELECT
- PROTOCCALLSFFUN3(STRING,ft,PSTRINGV,STRINGV,FLOAT)
- #define FT(A,B,C) CCALLSFFUN3(ft,PSTRINGV,STRINGV,FLOAT,A,B,C)
-
- main() {
- static char v[][5] = {"000 ", "1", "22", " "};
- static char w[][9] = {" ", "bb","ccc ","dddd"};
- float a = 10.0;
- printf("FT(v, w, a); returns:%s.\n",FT(v, w, a));
- printf("main:v=%s,%s,%s,%s. PSTRINGV => Has had trailing blanks stripped.\n",
- v[0],v[1],v[2],v[3]);
- printf("main:w=%s,%s,%s,%s. STRINGV => malloc'd copy for FORTRAN=> C intact.\n"
- ,w[0],w[1],w[2],w[3]);
- }
- #endif
-
- #ifdef S1_SELECT
- PROTOCCALLSFSUB1(s1,PSTRING)
- #define S1(A1) CCALLSFSUB1(s1,PSTRING,A1)
- PROTOCCALLSFSUB1(forstr1,PSTRING)
- #define FORSTR1(A1) CCALLSFSUB1(forstr1,PSTRING,A1)
-
- main() {
- static char b[] = "abcdefghij", forb[13] = "abcdefghijkl";
- S1(b); FORSTR1(forb);
- printf("s1(b) returns b = %s; forstr1(forb) = returns forb = %s;\n", b, forb);
- }
- #endif
-
- #ifdef ABC_SELECT
- PROTOCCALLSFSUB3(abc,STRING,PSTRING,PSTRING)
- #define ABC(A1,A2,A3) CCALLSFSUB3(abc,STRING,PSTRING,PSTRING,A1,A2,A3)
-
- main() {
- static char aa[] = "one ", bb[] = "two ", cc[] = "three"; int i;
- for (i=0; i<10; i++) {printf("%s;%s;%s;\n",aa,bb,cc); ABC(aa,bb,cc);}
- }
- #endif
-
- #ifdef R_SELECT
- PROTOCCALLSFFUN1(FLOAT,r,INT)
- #define R(A1) CCALLSFFUN1(r,INT,A1)
- PROTOCCALLSFFUN0(STRING,forstr2)
- #define FORSTR2() CCALLSFFUN0(forstr2)
- PROTOCCALLSFFUN1(STRING,forstr,STRING)
- #define FORSTR(A1) CCALLSFFUN1(forstr,STRING,A1)
-
- main() {
- static char aa[] = "one";
- int rrr = 333;
- printf("R(rrr=%d) returns int arg. as float:%f\n",rrr,R(rrr));
- printf("FORSTR(aa=%s) returns the string arg. as:%s<-end here\n",aa,FORSTR(aa));
- printf("FORSTR2() returns the string constant:%s<-end here\n",FORSTR2());
- }
- #endif
-
- #ifdef REV_SELECT
- PROTOCCALLSFFUN1(INT,frev,INTV)
- #define FREV(A1) CCALLSFFUN1(frev,INTV,A1)
- PROTOCCALLSFSUB1(rev,INTV)
- #define REV(A1) CCALLSFSUB1(rev,INTV,A1)
-
- main() {
- static int a[] = {1,2};
- printf("REV(a[0,1]=%d,%d) returns:",a[0],a[1]);
- REV(a); printf("a[0,1]=%d,%d\n",a[0],a[1]);
- printf("FREV(a[0,1]=%d,%d) returns:",a[0],a[1]);
- printf("%d",FREV(a)); printf(" with a[0,1]=%d,%d\n",a[0],a[1]);
- }
- #endif
-
- #ifdef FCB_SELECT
- PROTOCCALLSFSUB0(ffcb)
- #define FFCB() CCALLSFSUB0(ffcb)
-
- typedef struct { char v[13],w[4][13],x[2][3][13]; } FCB_DEF;
- #define FCB COMMON_BLOCK(fcb)
- extern FCB_DEF FCB;
-
- main() {
- char cv[14];
- static char cw[4][14] = {"C's w[0]", "C's w[1]", "C's w[2]", "C's w[3]"};
- static char cx[2][3][14] = {"C's x[0][0]", "C's x[0][1]", "C's x[0][2]",
- "C's x[1][0]", "C's x[1][1]", "C's x[1][2]"};
- C2FCBSTR("C's V" ,FCB.v,0);
- C2FCBSTR(cw ,FCB.w,1);
- C2FCBSTR(cx ,FCB.x,2);
- FFCB();
- FCB2CSTR(FCB.v ,cv ,0);
- FCB2CSTR(FCB.w ,cw ,1);
- FCB2CSTR(FCB.x ,cx ,2);
- printf("FCB returns v = %s.\n",cv);
- printf("FCB returns w[1,2,3,4] = %s,%s,%s,%s.\n",cw[0],cw[1],cw[2],cw[3]);
- printf("FCB returns x[0,(1,2,3)] = %s,%s,%s.\n",cx[0][0],cx[0][1],cx[0][2]);
- printf("FCB returns x[1,(1,2,3)] = %s,%s,%s.\n",cx[1][0],cx[1][1],cx[1][2]);
- }
- #endif
-
- #ifdef EQ_SELECT
- PROTOCCALLSFSUB0(feq)
- #define FEQ() CCALLSFSUB0(feq)
-
- #define KWBANK 690
- typedef struct {
- int nzebra; float gversn,zversn; int ixstor,ixdiv,ixcons; float fendq[16];
- union {
- struct {
- int Lmain,Lr1;
- union {float Ws[KWBANK]; int Iws[2];}u;
- }s;
- union {
- int Lq[80];
- struct {
- int dummy[8];
- union {float Q[2]; int Iq[2];}u;
- }s;
- }u;
- }u;
- } GCBANK_DEF;
- #define lmain u.s.Lmain
- #define lr1 u.s.Lr1
- #define ws u.s.u.Ws
- #define iws u.s.u.Iws
- #define lq u.u.Lq
- #define q u.u.s.u.Q
- #define iq u.u.s.u.Iq
- #define GCBANK COMMON_BLOCK(gcbank)
- GCBANK_DEF GCBANK;
-
- main() {
- FEQ();
- printf("GCBANK.nzebra = %d.\n", GCBANK.nzebra);
- printf("GCBANK.gversn = %f.\n", GCBANK.gversn);
- printf("GCBANK.zversn = %f.\n", GCBANK.zversn);
- printf("GCBANK.ixstor = %d.\n", GCBANK.ixstor);
- printf("GCBANK.ixcons = %d.\n", GCBANK.ixcons);
- printf("GCBANK.fendq[15] = %f.\n", GCBANK.fendq[15]);
- printf("GCBANK.lmain = %d.\n", GCBANK.lmain);
- printf("GCBANK.lr1 = %d.\n", GCBANK.lr1);
- printf("GCBANK.ws[KWBANK-1] = %f.\n", GCBANK.ws[KWBANK-1]);
- printf("GCBANK.iq[0] = %d.\n", GCBANK.iq[0]);
- }
- #endif
-
- /* The following functions are called by FORTRAN functions, as shown by the
- remaining examples.
- We redefine the C name of each routine called from FORTRAN so that the
- FORTRAN call may use the C name, e.g. the next line. */
- #define EXIST ccallsc(exist)
- void EXIST() {printf("EXIST: was called.\n");}
- FCALLSCSUB0(exist)
-
- #define CA ccallsc(ca)
- void CA(int i) {printf("CA: had integer argument:%d.\n",i);}
- FCALLSCSUB1(ca,INT)
-
- #define CB ccallsc(cb)
- void CB(int *i) {
- printf("CB: had pointer argument to integer:%d.\n",*i); *i*=2;}
- FCALLSCSUB1(cb,PINT)
-
- #define CC ccallsc(cc)
- void CC(char *s) {printf("CC: had string argument:%s.\n",s);}
- FCALLSCSUB1(cc,STRING)
-
- #define CD ccallsc(cd)
- void CD(char *s)
- {printf("CD: had string argument:%s.\n",s); strcpy(s,"to you 12345678");}
- FCALLSCSUB1(cd,PSTRING)
-
- #define CE ccallsc(ce)
- void CE(char v[][5])
- {printf("CE: had string vector argument:%s,%s,%s.\n",v[0],v[1],v[2]);}
- #define ce_STRV_A1 TERM_CHARS(' ',1)
- FCALLSCSUB1(ce,STRINGV)
-
- #define CF ccallsc(cf)
- void CF(char v[][5], int n)
- {int i;
- printf("CF: had %d string vector argument:",n);
- for (i=0; i<n-1; i++) printf("%s,",v[i]);
- printf("%s.\n",v[i]);
- }
- #define cf_STRV_A1 NUM_ELEM_ARG(2)
- FCALLSCSUB2(cf,STRINGV,INT)
-
-
- #define CG ccallsc(cg)
- int CG() {return 1;}
- FCALLSCFUN0(INT,cg)
-
- #define CH ccallsc(ch)
- char *CH() {return "hello";}
- FCALLSCFUN0(STRING,ch)
-
- #define CI ccallsc(ci)
- char *CI(char v[][5]) {return v[3];}
- #define ci_STRV_A1 NUM_ELEMS(6)
- FCALLSCFUN1(STRING,ci,STRINGV)
-
- #define CJ ccallsc(cj)
- char *CJ(int v) {printf("CJ:v=%d\n",v);return "hello";}
- FCALLSCFUN1(STRING,cj,INT)
-
- #ifdef F0_SELECT
- PROTOCCALLSFSUB0(fexist)
- #define FEXIST() CCALLSFSUB0(fexist)
-
- main() {FEXIST();}
- #endif
-
- #ifdef FA_SELECT
- PROTOCCALLSFSUB1(fa,INT)
- #define FA(A1) CCALLSFSUB1(fa,INT,A1)
-
- main() {FA(1234);}
- #endif
-
- #ifdef FB_SELECT
- PROTOCCALLSFSUB1(fb,PINT)
- #define FB(A1) CCALLSFSUB1(fb,PINT,A1)
-
- main()
- {int i,ii; i=ii=1234;
- FB(ii); printf("MAIN: FB(i=%d) returns with i=%d.\n",i,ii);}
- #endif
-
- #ifdef FC_SELECT
- PROTOCCALLSFSUB1(fc,STRING)
- #define FC(A1) CCALLSFSUB1(fc,STRING,A1)
-
- main() {FC("hello");}
- #endif
-
- #ifdef FD_SELECT
- PROTOCCALLSFSUB1(fd,PSTRING)
- #define FD(A1) CCALLSFSUB1(fd,PSTRING,A1)
-
- main()
- {static char i[] = "happy "; static char ii[] = "happy ";
- FD(ii); printf("MAIN: FD(i=%s) returns with i=%s.\n",i,ii);}
- #endif
-
- #ifdef FE_SELECT
- PROTOCCALLSFSUB1(fe,STRINGV)
- #define FE(A1) CCALLSFSUB1(fe,STRINGV,A1)
-
- main()
- {static char v[][5] = {"0000", "1", "22", ""}; FE(v);}
- #endif
-
- #ifdef FF_SELECT
- PROTOCCALLSFSUB2(ff,STRINGV,INT)
- #define FF(A1,A2) CCALLSFSUB2(ff,STRINGV,INT, A1,A2)
-
- main()
- {static char v[][5] = {"0000", "1", "22", ""};
- FF(v,sizeof(v)/sizeof v[0]);}
- #endif
-
- #ifdef FG_SELECT
- PROTOCCALLSFFUN0(INT,fg)
- #define FG() CCALLSFFUN0(fg)
-
- main()
- {printf("FG() returns %d.\n",FG());}
- #endif
-
- #ifdef FH_SELECT
- PROTOCCALLSFFUN0(STRING,fh)
- #define FH() CCALLSFFUN0(fh)
-
- main()
- {printf("FH() returns %s.\n",FH());}
- #endif
-
- #ifdef FI_SELECT
- PROTOCCALLSFFUN1(STRING,fi,STRINGV)
- #define FI(A1) CCALLSFFUN1(fi,STRINGV,A1)
-
- main()
- {static char v[][5] = {"0000", "1", "22", "333", "8", "9"};
- printf("FI(v) returns %s.\n",FI(v));}
- #endif
-
- #ifdef FJ_SELECT
- PROTOCCALLSFFUN1(STRING,fj,INT)
- #define FJ(A1) CCALLSFFUN1(fj,INT,A1)
-
- main()
- { printf("FJ(2) returns %s.\n",FJ(2));}
- #endif
-