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

  1. /*  Copyright (c) 2000 IBK-Landquart-Switzerland. All rights reserved.
  2.  *
  3.  *  Module      :  KcTstLib.CPP
  4.  *  Application :  CSS Sample Library using C-API
  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 *str)
  33. {
  34.    char *s = str;
  35.    if (*s=='-' || *s=='+') s++;
  36.    int any(0);
  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.    char buf[40];
  51.    long bufsiz;
  52.  
  53.    // get actual # of arguments
  54.    bufsiz = sizeof(buf);
  55.    KCssGet(aCss, "argCount", buf, &bufsiz);
  56.    int argCount = atoi(buf);
  57.  
  58.    // calculate sum of all arguments
  59.    double sum(0.0);
  60.    for (int i = 0; i < argCount; i++) {
  61.       // create name of parameter
  62.       char name[4];
  63.       sprintf(name, "p%d", i+1);
  64.  
  65.       // get argument
  66.       bufsiz = sizeof(buf);
  67.       if ( KCssGet(aCss, name, buf, &bufsiz) ) return; // (1)
  68.  
  69.       // check for number
  70.       if (!checkNumber(buf)) {
  71.          sprintf(buf, "argument %d is no number!", i);
  72.          KCssSetError(aCss, buf, -1); // (2)
  73.       } // if
  74.  
  75.       sum += atof(buf);
  76.    } // for
  77.  
  78.    // return result
  79.    sprintf(buf, "%f", sum / argCount);
  80.    KCssSetResult(aCss, buf, -1); // (2)
  81. } // average
  82.  
  83. /*
  84.  * i n i t i a l i z e
  85.  *
  86.  * initialize css library at load time
  87.  */
  88. KCssExport initialize(KCssHandle css)
  89. {
  90.    static char* module = "KcTstLib.dll";  // module name
  91.    long errs;
  92.  
  93.    // define a global constant by loading a script
  94.    errs = KCssLoadScriptMem(
  95.       css,                               // css handle
  96.       module,                            // module name
  97.       "const tstVersion = 1.0;\n"        // script source
  98.    );
  99.    if (errs) return; // (1)
  100.  
  101.    // load a function (no errs check since returning hereafter anyway)
  102.    KCssAddFunc(
  103.       css,                               // handle
  104.       module,                            // module name
  105.       "average(const p1, "               // declaration
  106.               "const p2, "
  107.              "[const p3, "
  108.               "const p4, "
  109.               "const p5])",
  110.       average);                          // function address
  111. } // initialize
  112.  
  113. /*
  114.  * c l e a n u p
  115.  *
  116.  * clean up css library before unloading
  117.  */
  118. KCssExport cleanup(KCssHandle css)
  119. {
  120.    // nothing to clean up in our sample
  121. } // cleanup
  122.  
  123.