|
Writing libraries |
|
|
Writing a CSL library is easy and gives you the opportunity to provide a professional script interface to your application. You should make yourself familiar with the concept of DLL's or shared libraries in your compiler documentation. For your convenience you can consult the files build.bat / build.cmd / build in the Samples\Class subdirectories to see what compiler and linker switches are required. Your library must export 2 entries: ZCslInitLib and ZCslCleanupLib. ZCslInitLib is called when the DLL gets loaded. You use the API to define global var's and const's and load functions at startup. ZCslCleanupLib will be called when the DLL is unloaded so you can perform any tidy up before the CSL handle is closed. You will find this sample library MyLib.cpp in the Samples\Class\Source directory. Use the sample as a starting point for your own DLL's:
#include <ZBase.h> // load ZC_.... defines
#if ZC_GNU
#include <strstream.h>
#else
#include <strstrea.h>
#endif
#if ZC_WIN
#include <ZCslWrap.hpp>
#else
#include <ZCsl.hpp>
#endif
static ZString myStrStrip(ZCsl* csl)
{
return csl->get("string").strip();
} // myStrStrip
static ZString mySubString(ZCsl* csl)
{
int argc = csl->get("argCount").asInt();
switch (argc) {
case 2:
return csl->get("string").subString(
csl->get("start").asInt()
);
case 3:
return csl->get("string").subString(
csl->get("start").asInt(),
csl->get("count").asInt()
);
default:
return csl->get("string").subString(
csl->get("start").asInt(),
csl->get("count").asInt(),
csl->get("padchar")[1]
);
} // switch
} // mySubString
ZCslInitLib(csl)
{
ZString iFile("MyLib");
istrstream init("const myVersion = 0.1;\n");
csl->loadScript(iFile, &init);
(*csl)
.addFunc(
iFile,
"myStrStrip(const string)",
myStrStrip)
.addFunc(
iFile,
"mySubString(const string, const start, [const count, const padchar])",
mySubString);
} // ZCslInitLib
ZCslCleanupLib(aCsl)
{
// nothing to cleanup in this sample
} // ZCslCleanupLib
|
||||||||
| Copyright © IBK Landquart | Last revision: 27.05.2002 | << Back Top Next >> |