home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) 2000 IBK-Landquart-Switzerland. All rights reserved.
- *
- * Module : KcTstLib.CPP
- * Application : CSS Sample Library using C-API
- * Author : Peter Koch, IBK
- *
- * NOTES:
- *
- * (1) Within C/C++ implementions of CSS functions, as well as within
- * initialize and cleanup, 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.
- *
- * Date Description Who
- * --------------------------------------------------------------------------
- * Feb 2000 First release P.Koch, IBK
- */
-
- #include <stdlib.h>
- #include <stdio.h>
-
- #define CSS_LIBRARY
- #include <KCssC.h>
-
- /*
- * 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 %d is no number!", i);
- KCssSetError(aCss, buf, -1); // (2)
- } // if
-
- sum += atof(buf);
- } // for
-
- // return result
- sprintf(buf, "%f", sum / argCount);
- KCssSetResult(aCss, buf, -1); // (2)
- } // average
-
- /*
- * i n i t i a l i z e
- *
- * initialize css library at load time
- */
- KCssExport initialize(KCssHandle css)
- {
- static char* module = "KcTstLib.dll"; // module name
- long errs;
-
- // define a global constant by loading a script
- errs = KCssLoadScriptMem(
- css, // css handle
- module, // module name
- "const tstVersion = 1.0;\n" // script source
- );
- if (errs) return; // (1)
-
- // load a function (no errs check since returning hereafter anyway)
- KCssAddFunc(
- css, // handle
- module, // module name
- "average(const p1, " // declaration
- "const p2, "
- "[const p3, "
- "const p4, "
- "const p5])",
- average); // function address
- } // initialize
-
- /*
- * c l e a n u p
- *
- * clean up css library before unloading
- */
- KCssExport cleanup(KCssHandle css)
- {
- // nothing to clean up in our sample
- } // cleanup
-