home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / csso0301.zip / SAMPLES / C-API / SOURCE / KCTSTLIB.C < prev    next >
Encoding:
C/C++ Source or Header  |  2000-02-21  |  3.3 KB  |  124 lines

  1. /*  Copyright (c) 2000 IBK-Landquart-Switzerland. All rights reserved.
  2.  *
  3.  *  Module      :  KcTstLib.C
  4.  *  Application :  CSS Sample Library in plain C
  5.  *  Author      :  Peter Koch, IBK
  6.  *
  7.  *  NOTES:
  8.  *
  9.  *  (1) Within C/C++ implementions of CSS functions, as well as within
  10.  *      initialize and cleanup, there is no need to handle errors since
  11.  *      that will be done by the caller. Just return in case of an error.
  12.  *
  13.  *  (2) These functions will allways return a value of 0, so there is no
  14.  *      need to check for errors.
  15.  *
  16.  *  Date        Description                                 Who
  17.  *  --------------------------------------------------------------------------
  18.  *  Feb 2000    First release                               P.Koch, IBK
  19.  */
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23.  
  24. #define CSS_LIBRARY
  25. #include <KCssC.h>
  26.  
  27. /*
  28.  * c h e c k N u m b e r
  29.  *
  30.  * Check if string represents a number
  31.  */
  32. static int checkNumber(char *s)
  33. {
  34.    int any;
  35.    any = 0;
  36.    if (*s=='-' || *s=='+') s++;
  37.    while ('0'<=*s && *s<='9') { s++; any = 1; }
  38.    if (*s=='.') s++;
  39.    while ('0'<=*s && *s<='9') s++;
  40.    return any && *s == 0;
  41. } /* checkNumber */
  42.  
  43. /*
  44.  * a v e r a g e
  45.  *
  46.  * Sample CSS function calculating the average of up 5 numbers
  47.  */
  48. KCssFunction average(KCssHandle aCss)
  49. {
  50.    double sum;
  51.    long bufsiz;
  52.    int argCount, i;
  53.    char buf[40], name[4];
  54.  
  55.    /* get actual # of arguments */
  56.    bufsiz = sizeof(buf);
  57.    KCssGet(aCss, "argCount", buf, &bufsiz);
  58.    argCount = atoi(buf);
  59.  
  60.    /* calculate sum of all arguments */
  61.    sum = 0.0;
  62.    for (i = 0; i < argCount; i++) {
  63.       /* create name of parameter */
  64.       sprintf(name, "p%d", i+1);
  65.  
  66.       /* get argument */
  67.       bufsiz = sizeof(buf);
  68.       if ( KCssGet(aCss, name, buf, &bufsiz) ) return; /* (1) */
  69.  
  70.       /* check for number */
  71.       if (!checkNumber(buf)) {
  72.          sprintf(buf, "argument %d is no number!", i);
  73.          KCssSetError(aCss, buf, -1); /* (2) */
  74.       } /* if */
  75.  
  76.       sum += atof(buf);
  77.    } /* for */
  78.  
  79.    /* return result */
  80.    sprintf(buf, "%f", sum / argCount);
  81.    KCssSetResult(aCss, buf, -1); /* (2) */
  82. } /* average */
  83.  
  84. /*
  85.  * i n i t i a l i z e
  86.  *
  87.  * initialize css library at load time
  88.  */
  89. KCssExport initialize(KCssHandle css)
  90. {
  91.    static char* module = "KcTstLib.dll"; /* module name */
  92.    long errs;
  93.  
  94.    /* define a global constant by loading a script */
  95.    errs = KCssLoadScriptMem(
  96.       css,                               /* css handle */
  97.       module,                            /* module name */
  98.       "const tstVersion = 1.0;\n"        /* script source */
  99.    );
  100.    if (errs) return; /* (1) */
  101.  
  102.    /* load a function (no errs check since returning hereafter anyway) */
  103.    KCssAddFunc(
  104.       css,                               /* handle */
  105.       module,                            /* module name */
  106.       "average(const p1, "               /* declaration */
  107.               "const p2, "
  108.              "[const p3, "
  109.               "const p4, "
  110.               "const p5])",
  111.       average);                          /* function address */
  112. } /* initialize */
  113.  
  114. /*
  115.  * c l e a n u p
  116.  *
  117.  * clean up css library before unloading
  118.  */
  119. KCssExport cleanup(KCssHandle css)
  120. {
  121.    /* nothing to clean up in our sample */
  122. } /* cleanup */
  123.  
  124.