home *** CD-ROM | disk | FTP | other *** search
- PRODUCT : TURBO PROLOG / TURBO C NUMBER : 366
- VERSION : 1.0
- OS : PC-DOS
- DATE : August 20, 1987
-
- TITLE : TURBO PROLOG & TURBO C INTERFACE / STRINGS"
-
- When linking Turbo C modules with Turbo Prolog, it is sometimes
- desirable to pass strings between the two languages. Here is an
- example program which shows how strings are passed from Turbo
- Prolog to Turbo C and then back from Turbo C to Turbo Prolog.
-
- Type the Turbo Prolog program and compile the code to an .OBJ
- file. After this is done, type the Turbo C program and compile
- it to a .OBJ file as well. Using the following DOS command:
-
- TCC -u- -r- -ml -c -O mailbox
-
- Once both source code files are successfully compiled into object
- code, issue the following link command from the DOS command line
- to create the executable file MAIL.EXE.
-
- TLINK CPINIT+INIT+MAILMAN+MAILBOX+MAILMAN.SYM,MAIL,,PROLOG+CL
-
- Here is the Turbo Prolog source code:
-
- /*---------------------------------------------------------
- * MAILMAN.PRO -- demonstrates how strings are passed
- * between Turbo Prolog and Turbo C
- *---------------------------------------------------------
- */
-
- GLOBAL PREDICATES
- cpinit - language c
- send(integer, string) - (i,i) language c
- receive(integer,string) - (i,o) language c
-
- PREDICATES
- nondeterm repeat
- send_em(integer)
- recieve_em
-
- CLAUSES
- repeat.
- repeat :- repeat.
- send_em(5) :- !. /* Example of a FOR loop in Prolog */
- send_em(X) :-
- Y = X + 1 ,
- write(Y," -> ") ,
- readln(Pro_string) ,
- shiftwindow(2) ,
- send(Y, Pro_string) ,
- shiftwindow(1) ,
- send_em(Y).
-
- receive_em :-
- repeat ,
- clearwindow ,
- write("Enter an integer between 1 and 5 which\n" ,
- "corresponds with one of your previously\n" ,
- "written messages.\n", ,
- " Enter '0' to quit.\n") ,
- readint(Int) ,
- shiftwindow(2) ,
- receive(Int, Message) ,
- shiftwindow(1) ,
- write(Message, "\n Press any key to continue: ") ,
- readchar(_) ,
- Int = 0.
-
- GOAL
- cpinit ,
- makewindow(2,7,7," Turbo C Window ",13,0,12,80) ,
- makewindow(1,7,7," Turbo Prolog Window ",0,0,13,80) ,
- write("Please enter five lines to send to Turbo C.\n",
- "These will be numbered from one to five.\n\n") ,
- send_em(0) ,
- write("\n\nPress any key to continue: "), readchar(_) ,
- receive_em.
-
- /* END MAILMAN.PRO */
-
- Here is the Turbo C source code:
-
- /*---------------------------------------------------------
- * MAILBOX.C -- demonstrates how strings are passed
- * between Turbo Prolog and Turbo C
- *---------------------------------------------------------
- */
-
- /*
- prototype _strcpy and define strcpy so we don't have to
- write our code with underbars
- */
-
- char *_strcpy(char *dest, char *source);
- #define strcpy _strcpy
-
- /*
- prototype zwf and writef for readability
- */
-
- void zwf(char *format, ...);
- #define writef zwf
-
- char *bad_box = "Invalid Box Number"; /* error message */
- char buffer[5][80]; /* message buffer */
-
- /*---------------------------------------------------------
- * send_0 - store a message passed from prolog
- *---------------------------------------------------------
- */
- void send_0(int indx, char *string) {
-
- if ((indx < 1) || (indx > 5))
- writef(bad_box);
-
- strcpy(&buffer[indx-1][0],string);
- writef("Storing -> %d: %s\n",indx,&buffer[indx-1][0]);
-
- }
- /*---------------------------------------------------------
- * receive_0 - pass a message to Prolog
- *---------------------------------------------------------
- */
- void receive_0(int indx, char **string) {
-
- if ( (indx < 0) || (indx > 5) )
- *string = bad_box;
- else
- if ( (indx == 0) )
- *string = "";
- else {
- *string = buffer[indx-1];
- writef("Fetching -> %d: %s\n",indx,*string);
- }
-
- }
- /* END MAILBOX.C */
-