home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) 1998-2000 IBK-Landquart-Switzerland. All rights reserved.
- *
- * Module : Embedd.cpp
- * Application : Sample to embedd CSS as macro language with C-API
- * Author : Peter Koch, IBK
- *
- * NOTES:
- *
- * (1) Within C/C++ implementions of CSS functions there is no need to
- * handle errors since that will be done by the caller. Just return
- * in case of an error.
- *
- * (2) These functions will allways return a value of 0, so there is no
- * need to check for errors.
- *
- * (3) In real applications you could first call KCssGet to get the
- * required size and then allocate the buffer dynamicly (like in
- * checkErrs).
-
- * Date Description Who
- * --------------------------------------------------------------------------
- * Feb 2000 First release P.Koch, IBK
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream.h>
- #include <KCssC.h>
-
- static char* module = "Embedd.exe"; // module name
- static KCssHandle css; // css handle
- static long errs; // css api return code
-
- /*
- * c h e c k E r r s
- *
- * This function ckecks for errors. Error messages are in case displayed
- * and program is terminated to keep the sample as simple as possible.
- */
- static void checkErrs()
- {
- if (errs) {
- cout << "KCss error found:" << endl;
- for (int i = 0; i < errs; i++) {
- // First we get the size of the message only so a buffer
- // large enough for the string can be allocated:
- long size, errs2;
- errs2 = KCssGetError(css, i, 0, &size); // get size of message
- if (errs2) { errs = errs2; checkErrs(); }
-
- // Now a buffer is allocated and the message is retrieved
- // and displayed:
- char* msg = new char[size];
- errs2 = KCssGetError(css, i, msg, &size); // get message
- if (errs2) { errs = errs2; checkErrs(); }
- cout << msg << endl;
-
- // the buffer is no longer required and gets discarded:
- delete [] msg;
- } // for
- exit(1);
- } // if
- } // checkErrs
-
- /*
- * c h e c k N u m b e r
- *
- * Check if string represents a number
- */
- static int checkNumber(char *str)
- {
- char *s = str;
- if (*s=='-' || *s=='+') s++;
- int any(0);
- while ('0'<=*s && *s<='9') { s++; any = 1; }
- if (*s=='.') s++;
- while ('0'<=*s && *s<='9') s++;
- return any && *s == 0;
- } // checkNumber
-
- /*
- * a v e r a g e
- *
- * Sample CSS function calculating the average of up 5 numbers
- */
- KCssFunction average(KCssHandle aCss)
- {
- char buf[40];
- long bufsiz;
-
- // get actual # of arguments
- bufsiz = sizeof(buf);
- KCssGet(aCss, "argCount", buf, &bufsiz);
- int argCount = atoi(buf);
-
- // calculate sum of all arguments
- double sum(0.0);
- for (int i = 0; i < argCount; i++) {
- // create name of parameter
- char name[4];
- sprintf(name, "p%d", i+1);
-
- // get argument
- bufsiz = sizeof(buf);
- if ( KCssGet(aCss, name, buf, &bufsiz) ) return; // (1)
-
- // check for number
- if (!checkNumber(buf)) {
- sprintf(buf, "argument p%d is no number!", i+1);
- KCssSetError(aCss, buf, -1); // (2)
- } // if
-
- sum += atof(buf);
- } // for
-
- // return result
- sprintf(buf, "%f", sum / argCount);
- KCssSetResult(aCss, buf, -1); // (2)
- } // average
-
- int main()
- {
- cout << "opening css" << endl;
- errs = KCssOpen(&css,0);
- checkErrs();
-
- cout << endl << "get css version" << endl;
- char buf[1024];
- long bufsize(sizeof(buf));
- errs = KCssGet(css, "cssVersion", buf, &bufsize); // (1)
- checkErrs();
- cout << " css version is " << buf << endl;
-
- cout << endl << "load c++ function 'average'" << endl;
- errs = KCssAddFunc(
- css, // handle
- module, // module name
- "average(const p1, " // declaration
- "[const p2, "
- "const p3, "
- "const p4, "
- "const p5])",
- average); // function address
- checkErrs();
-
- cout << endl << "call 'average' from c++" << endl;
- static char *vals[] = {
- "3", "12", "17"
- };
- errs = KCssCall(
- css, // handle
- module, // module name
- "average", // function to call
- sizeof(vals)/sizeof(char*), // # of arguments
- vals); // arguments
- checkErrs();
-
- cout << endl << "get result" << endl;
- bufsize = sizeof(buf);
- errs = KCssGetResult(css, buf, &bufsize); // (1)
- checkErrs();
- cout << " result = " << buf << endl;
-
- cout << endl << "compile a script from memory" << endl;
- errs = KCssLoadScriptMem(
- css, // handle
- module, // module name
-
- "#loadLibrary 'KcSysLib'\n" // the script
- ""
- "check()\n"
- "{\n"
- " sysLog('the average of 3, 5, 12 and 7 is '\n"
- " |average(3,5,12,7)\n"
- " );\n"
- "}\n"
- );
- checkErrs();
-
- cout << endl << "call 'check' within compiled script" << endl;
- errs = KCssCall(css, module, "check", 0, 0);
- checkErrs();
-
- cout << endl << "closing css" << endl;
- errs = KCssClose(css);
- checkErrs();
-
- return 0;
- } // main
-