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

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