home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / PASCAL / MISCTI10.ZIP / TI366.ASC < prev    next >
Encoding:
Text File  |  1988-05-02  |  4.0 KB  |  140 lines

  1. PRODUCT : TURBO PROLOG / TURBO C     NUMBER : 366
  2. VERSION : 1.0
  3. OS      : PC-DOS
  4. DATE    : August 20, 1987
  5.  
  6.   TITLE : TURBO PROLOG & TURBO C INTERFACE / STRINGS"
  7.  
  8. When linking Turbo C modules with Turbo Prolog, it is sometimes
  9. desirable to pass strings between the two languages.  Here is an
  10. example program which shows how strings are passed from Turbo
  11. Prolog to Turbo C and then back from Turbo C to Turbo Prolog.
  12.  
  13. Type  the Turbo Prolog program and compile the code to an .OBJ
  14. file.  After this is done, type  the Turbo C program and compile
  15. it to a .OBJ file as well. Using the following DOS command:  
  16.  
  17. TCC -u- -r- -ml -c -O mailbox
  18.  
  19. Once both source code files are successfully compiled into object
  20. code, issue the following link command from the DOS command line
  21. to create the executable file MAIL.EXE.
  22.  
  23. TLINK CPINIT+INIT+MAILMAN+MAILBOX+MAILMAN.SYM,MAIL,,PROLOG+CL
  24.  
  25. Here is the Turbo Prolog source code:
  26.  
  27. /*---------------------------------------------------------
  28.  *  MAILMAN.PRO -- demonstrates how strings are passed
  29.  *                 between Turbo Prolog and Turbo C
  30.  *---------------------------------------------------------
  31.  */
  32.  
  33. GLOBAL PREDICATES
  34.    cpinit - language c
  35.    send(integer, string) - (i,i) language c
  36.    receive(integer,string) - (i,o) language c
  37.  
  38. PREDICATES
  39.    nondeterm repeat
  40.    send_em(integer)
  41.    recieve_em 
  42.    
  43. CLAUSES
  44.    repeat.
  45.    repeat :- repeat.
  46.    send_em(5) :- !.         /* Example of a FOR loop in Prolog */
  47.    send_em(X) :- 
  48.        Y = X + 1 ,
  49.        write(Y," -> ") ,
  50.        readln(Pro_string) ,
  51.        shiftwindow(2) ,
  52.        send(Y, Pro_string) ,
  53.        shiftwindow(1) ,
  54.        send_em(Y).
  55.  
  56.    receive_em :-
  57.           repeat ,
  58.           clearwindow ,
  59.           write("Enter an integer between 1 and 5 which\n" ,
  60.                 "corresponds with one of your previously\n" ,
  61.                 "written messages.\n", ,
  62.                 "    Enter '0' to quit.\n") ,
  63.           readint(Int) ,
  64.           shiftwindow(2) ,
  65.           receive(Int, Message) ,
  66.           shiftwindow(1) ,
  67.           write(Message, "\n Press any key to continue: ") ,
  68.           readchar(_) ,
  69.           Int = 0.
  70.        
  71. GOAL   
  72.    cpinit ,
  73.    makewindow(2,7,7," Turbo C Window ",13,0,12,80) ,
  74.    makewindow(1,7,7," Turbo Prolog Window ",0,0,13,80) ,
  75.    write("Please enter five lines to send to Turbo C.\n",
  76.          "These will be numbered from one to five.\n\n") ,
  77.    send_em(0) ,
  78.    write("\n\nPress any key to continue: "), readchar(_) ,
  79.    receive_em.
  80.  
  81. /*   END MAILMAN.PRO   */
  82.  
  83. Here is the Turbo C source code:
  84.  
  85. /*---------------------------------------------------------
  86.  *  MAILBOX.C -- demonstrates how strings are passed
  87.  *               between Turbo Prolog and Turbo C
  88.  *---------------------------------------------------------
  89.  */
  90.  
  91. /*
  92.    prototype _strcpy and define strcpy so we don't have to
  93.    write our code with underbars
  94. */
  95.  
  96. char *_strcpy(char *dest, char *source); 
  97. #define strcpy _strcpy
  98.  
  99. /*
  100.    prototype zwf and writef for readability
  101. */
  102.  
  103. void zwf(char *format, ...);
  104. #define writef zwf
  105.  
  106. char *bad_box = "Invalid Box Number";  /* error message */
  107. char buffer[5][80];                   /* message buffer */
  108.  
  109. /*---------------------------------------------------------
  110.  *  send_0 - store a message passed from prolog
  111.  *---------------------------------------------------------
  112.  */
  113. void send_0(int indx, char *string) {
  114.  
  115.      if ((indx < 1) || (indx > 5))   
  116.          writef(bad_box);
  117.  
  118.      strcpy(&buffer[indx-1][0],string);
  119.     writef("Storing -> %d: %s\n",indx,&buffer[indx-1][0]);
  120.  
  121. }
  122. /*---------------------------------------------------------
  123.  *  receive_0 - pass a message to Prolog
  124.  *---------------------------------------------------------
  125.  */
  126. void receive_0(int indx, char **string) {
  127.  
  128.      if ( (indx < 0) || (indx > 5) )
  129.          *string = bad_box;
  130.      else
  131.        if ( (indx == 0) )
  132.          *string = "";
  133.      else {
  134.        *string = buffer[indx-1];
  135.        writef("Fetching -> %d: %s\n",indx,*string);
  136.     }
  137.  
  138. }
  139. /*   END MAILBOX.C   */
  140.