simple_foreign.c

00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <alloca.h>
00004 #include <math.h>
00005 
00006 #include "cinterf.h"
00007 
00008 /* call as: change_char(+String,+CharPos,+ReplacementString,-ResultString)
00009    Will take String and replace the character at position CharPos with the
00010    first character in ReplacementString. Both String and ReplacementString must
00011    be atoms.
00012 */
00013 DllExport int call_conv change_char(CTXTdecl)
00014 {
00015    char *str_in; 
00016    int  pos;
00017    char *c, *str_out;
00018 
00019         str_in = (char *) extern_ptoc_string(1);
00020         str_out = (char *) alloca(strlen(str_in)+1);
00021         strcpy(str_out, str_in);
00022         pos = extern_ptoc_int(2);
00023         c = (char *) extern_ptoc_string(3);
00024         str_out[pos-1] = c[0];
00025 
00026         /* Now that we have constructed a new symbol, we must ensure that it
00027            appears in the symbol table.  This can be done using function
00028            string_find() that searches the symbol table for the symbol, and
00029            if the symbol does not appear there, it inserts it.  If we are 
00030            sure that the symbol already appeared in the symbol table there
00031            is no need to use string_find(). 
00032          */
00033 
00034         //      extern_ctop_string(4, (char *) string_find(str_out,4));
00035                 extern_ctop_string(CTXTc 4, str_out);
00036 
00037         return TRUE;
00038 }
00039 
00040 int my_sqrt(CTXTdecl)
00041 {
00042    int i = ptoc_int(1);
00043 
00044    extern_ctop_float(2, (float) pow((double)i, 0.5));
00045    return TRUE;
00046 }
00047 
00048 DllExport int call_conv minus_one(CTXTdecl)
00049 {
00050   int i = extern_ptoc_int(1);
00051   extern_ctop_int(2,i-1);
00052   return TRUE;
00053 }
00054 

Generated on Wed Jul 26 13:30:43 2006 for XSB by  doxygen 1.4.5