1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
// =============================================================================
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2008 - DIGITEO - Sylvestre LEDRU
//
// This file is distributed under the same license as the Scilab package.
// =============================================================================
//Here with give a complete example on adding new primitive to Scilab
//create the procedure filese
//creating the interface file
i=['#include <string>'
'extern ""C"" {'
'#include ""stack-c.h""'
'int sci_cppfind(char *fname) {'
' int m1 = 0, n1 = 0, l1;'
' char *inputString1, *inputString2;'
' int m2 = 0, n2 = 0, l2;'
' int m3 = 0, n3 = 0;'
' double *position = NULL; /* Where we will store the position */'
' CheckRhs(2,2); /* Check the number of input argument */'
' CheckLhs(1,1); /* Check the number of output argument */'
' GetRhsVar(1, ""c"", &m1, &n1, &l1); /* Retrieve the first input argument */'
' inputString1=cstk(l1);'
' GetRhsVar(2, ""c"", &m2, &n2, &l2); /* Retrieve the second input argument */'
' inputString2=cstk(l2);'
' std::string myMessage (inputString1);'
' std::string search (inputString2);'
' m3=1;n3=1;'
' position = new double[1];'
' if (myMessage.find(search) != std::string::npos) {'
' position[0] = myMessage.find(search); /* The actual operation */'
' } else {'
' position[0] = -1; /* Substring not found */'
' }'
' CreateVarFromPtr(Rhs+1,""d"",&m3,&n3,&position); /* Create the output argument */'
' LhsVar(1) = Rhs+1;'
' delete[] position;'
' return 0;'
'}'
'}'];
mputl(i,'sci_cppfind.cxx');
//creating the shared library (a gateway, a Makefile and a loader are
//generated.
files=['sci_cppfind.cxx'];
ilib_build('foo',['cppfind','sci_cppfind'],files,[]);
Generate a gateway file
Generate a loader file
Generate a Makefile: Makelib
Running the makefile
Compilation of sci_cppfind.cxx
Building shared library (be patient)
Generate a cleaner file
// load the shared library
exec loader.sce
// ------------------------------------------------------
// generated by builder.sce: Please do not edit this file
// ------------------------------------------------------
foo_path = get_file_path('loader.sce');
list_functions = [ 'cppfind';
];
addinter(foo_path+'/foo.dll','foo',list_functions);
Shared archive loaded.
Link done.
// remove temp. variables on stack
clear foo_path;
clear list_functions;
clear get_file_path;
// ------------------------------------------------------
// Small test to see if the function is actually working.
if cppfind("my very long string","long") <> 8 bugmes();quit;end
if cppfind("my very long string","very") <> 3 bugmes();quit;end
if cppfind("my very long string","short") <> -1 bugmes();quit;end
|