Embedding CSS |
|
Embedding CSS in your application is quite easy. All you have to do is create an object from the class KCss. Then you may use member functions to manipulate CSS. When processing is done the KCss object can be deleted. Have a look at the example below defining and running a CSS function. (You will find a more sophisticated example Embedd.css in theSAMPLES subdirectories): #include <stdio.h> #include <strstrea.h> #include <KCSS.hpp> main() { try { IString module("MyTest.exe"); KCss css; // create css object css.loadLibrary("KcSysLib"); // load system library istrstream str( // define css function "test()" "{" " sysLog('hello world');" " return 0;" "}" ); css.loadScript(module, &str); // compile the script IString ret(css.call(module,"test")); // execute test return ret.asInt(); } catch (IException exc) { for (int i=exc.textCount(); i>=0; i--) cerr << exc.text(i) << endl; } } // main Of course you would not hard-code the css function in a real application. You would provide some way for editing the function to the user. So far this example is of little use, so let's extend the example by an application specific C++ function callable from the script: #include <stdio.h> #include <strstrea.h> #include <KCSS.hpp> /* * countDigs: a C++ function callable from CSS */ static IString countDigs(KCss* css) { IString str(css->get("string")); // get argument char* s(str); // pointer to string buffer int cnt(0); while (s) { if (*s >= '0' && *s <= '9') cnt++; s++; } return IString(cnt); } // countDigs main() { try { IString module("MyTest.exe"); KCss css; // create css object css.addFunc( // add c++ function module, // module name "countDigs(const string)", // fct header countDigs); // fct address istrstream str( // define css function "test2()" "{" " return countDigs('+41 (81) 322 83 84');" "}" ); css.loadScript(module, &str); // compile the script IString cnt(css.call(module,"test2")); // call test2 cout << "count = " << cnt << endl; // will show: count = 11 return 0; } // try catch (IException exc) { for (int i=exc.textCount(); i>=0; i--) cerr << exc.text(i) << endl; } // catch } // main Instead of loading the script from memory as in the examples above you could also load the script from a file of course: ... css.loadScript("User.css"); ... For test purpose you may also call your function without a CSS script envolved: ... IString cnt = css.call(module,"countDigs",1,"++123-456-778899"); ... | ||||||||
Copyright © IBK Landquart | Last revised by Peter Koch, 24.02.00 | << Back Top Next >> |