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

  1. /*  Copyright (c) 1998-2000 IBK-Landquart-Switzerland. All rights reserved.
  2.  *
  3.  *  Module      :  Embedd.cpp
  4.  *  Application :  Sample to embedd CSS as macro language with C-API
  5.  *  Author      :  Peter Koch, IBK
  6.  *
  7.  *  NOTES:
  8.  *
  9.  *  (1) Within C/C++ implementions of CSS functions there is no need to
  10.  *      handle errors since that will be done by the caller. Just return
  11.  *      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.  *  (3) In real applications you could first call KCssGet to get the
  17.  *      required size and then allocate the buffer dynamicly (like in
  18.  *      checkErrs).
  19.  
  20.  *  Date        Description                                 Who
  21.  *  --------------------------------------------------------------------------
  22.  *  Feb 2000    First release                               P.Koch, IBK
  23.  */
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <iostream.h>
  27. #include <KCssC.h>
  28.  
  29. static char* module = "Embedd.exe";      // module name
  30. static KCssHandle css;                   // css handle
  31. static long errs;                        // css api return code
  32.  
  33. /*
  34.  * c h e c k E r r s
  35.  *
  36.  * This function ckecks for errors. Error messages are in case displayed
  37.  * and program is terminated to keep the sample as simple as possible.
  38.  */
  39. static void checkErrs()
  40. {
  41.    if (errs) {
  42.       cout << "KCss error found:" << endl;
  43.       for (int i = 0; i < errs; i++) {
  44.          // First we get the size of the message only so a buffer
  45.          // large enough for the string can be allocated:
  46.          long size, errs2;
  47.          errs2 = KCssGetError(css, i, 0, &size); // get size of message
  48.          if (errs2) { errs = errs2; checkErrs();  }
  49.  
  50.          // Now a buffer is allocated and the message is retrieved
  51.          // and displayed:
  52.          char* msg = new char[size];
  53.          errs2 = KCssGetError(css, i, msg, &size); // get message
  54.          if (errs2) { errs = errs2; checkErrs();  }
  55.          cout << msg << endl;
  56.  
  57.          // the buffer is no longer required and gets discarded:
  58.          delete [] msg;
  59.       } // for
  60.       exit(1);
  61.    } // if
  62. } // checkErrs
  63.  
  64. /*
  65.  * c h e c k N u m b e r
  66.  *
  67.  * Check if string represents a number
  68.  */
  69. static int checkNumber(char *str)
  70. {
  71.    char *s = str;
  72.    if (*s=='-' || *s=='+') s++;
  73.    int any(0);
  74.    while ('0'<=*s && *s<='9') { s++; any = 1; }
  75.    if (*s=='.') s++;
  76.    while ('0'<=*s && *s<='9') s++;
  77.    return any && *s == 0;
  78. } // checkNumber
  79.  
  80. /*
  81.  * a v e r a g e
  82.  *
  83.  * Sample CSS function calculating the average of up 5 numbers
  84.  */
  85. KCssFunction average(KCssHandle aCss)
  86. {
  87.    char buf[40];
  88.    long bufsiz;
  89.  
  90.    // get actual # of arguments
  91.    bufsiz = sizeof(buf);
  92.    KCssGet(aCss, "argCount", buf, &bufsiz);
  93.    int argCount = atoi(buf);
  94.  
  95.    // calculate sum of all arguments
  96.    double sum(0.0);
  97.    for (int i = 0; i < argCount; i++) {
  98.       // create name of parameter
  99.       char name[4];
  100.       sprintf(name, "p%d", i+1);
  101.  
  102.       // get argument
  103.       bufsiz = sizeof(buf);
  104.       if ( KCssGet(aCss, name, buf, &bufsiz) ) return; // (1)
  105.  
  106.       // check for number
  107.       if (!checkNumber(buf)) {
  108.          sprintf(buf, "argument p%d is no number!", i+1);
  109.          KCssSetError(aCss, buf, -1); // (2)
  110.       } // if
  111.  
  112.       sum += atof(buf);
  113.    } // for
  114.  
  115.    // return result
  116.    sprintf(buf, "%f", sum / argCount);
  117.    KCssSetResult(aCss, buf, -1); // (2)
  118. } // average
  119.  
  120. int main()
  121. {
  122.    cout << "opening css" << endl;
  123.    errs = KCssOpen(&css,0);
  124.    checkErrs();
  125.  
  126.    cout << endl << "get css version" << endl;
  127.    char buf[1024];
  128.    long bufsize(sizeof(buf));
  129.    errs = KCssGet(css, "cssVersion", buf, &bufsize); // (1)
  130.    checkErrs();
  131.    cout << "  css version is " << buf << endl;
  132.  
  133.    cout << endl << "load c++ function 'average'" << endl;
  134.    errs = KCssAddFunc(
  135.       css,                               // handle
  136.       module,                            // module name
  137.       "average(const p1, "               // declaration
  138.              "[const p2, "
  139.               "const p3, "
  140.               "const p4, "
  141.               "const p5])",
  142.       average);                          // function address
  143.    checkErrs();
  144.  
  145.    cout << endl << "call 'average' from c++" << endl;
  146.    static char *vals[] = {
  147.       "3", "12", "17"
  148.    };
  149.    errs = KCssCall(
  150.       css,                               // handle
  151.       module,                            // module name
  152.       "average",                         // function to call
  153.       sizeof(vals)/sizeof(char*),        // # of arguments
  154.       vals);                             // arguments
  155.    checkErrs();
  156.  
  157.    cout << endl << "get result" << endl;
  158.    bufsize = sizeof(buf);
  159.    errs = KCssGetResult(css, buf, &bufsize); // (1)
  160.    checkErrs();
  161.    cout << "  result = " << buf << endl;
  162.  
  163.    cout << endl << "compile a script from memory" << endl;
  164.    errs = KCssLoadScriptMem(
  165.       css,                               // handle
  166.       module,                            // module name
  167.  
  168.       "#loadLibrary 'KcSysLib'\n"        // the script
  169.       ""
  170.       "check()\n"
  171.       "{\n"
  172.       "   sysLog('the average of 3, 5, 12 and 7 is '\n"
  173.       "          |average(3,5,12,7)\n"
  174.       "   );\n"
  175.       "}\n"
  176.    );
  177.    checkErrs();
  178.  
  179.    cout << endl << "call 'check' within compiled script" << endl;
  180.    errs = KCssCall(css, module, "check", 0, 0);
  181.    checkErrs();
  182.  
  183.    cout << endl << "closing css" << endl;
  184.    errs = KCssClose(css);
  185.    checkErrs();
  186.  
  187.    return 0;
  188. } // main
  189.  
  190.