Embedding CSS

C Styled Script
Reference Manual

<< Back  End  Next >>
 
 
INDEX
Introduction
Installation
Using The CSS Executive
Language
Directives
System Library
String Library
Regular Expression Lib.
File Library
Database Library
C API
   Embedding CSS
   Writing Libraries
   API Reference
C++ API
CSS Links
  

Embedding CSS in your application is quite easy: After opening a CSS handle the API functions can be used to compile scripts, define and call functions, query and set variables and so on.

Have a look at this example making use of various API functions. (You will find the more complete examples Embedd.c andEmbedd.css in the samples\c-api subdirectories):

#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <KCssC.h>
 
static char* module = "First.exe";       // module name
static KCssHandle css;                   // css handle
static long errs;                        // css api return code
 
/*
 * c h e c k E r r s
 *
 * This function ckecks for errors. Error messages are in case displayed
 * and program is terminated to keep the sample as simple as possible.
 */
static void checkErrs(void)
{
   long size, errs2;
   int i;
   char *msg;
 
   if (errs) {
      fprintf(stderr, "KCss error found:\r\n");
      for (i = 0; i < errs; i++) {
         /* First we get the size of the message only so a buffer */
         /* large enough for the string can be allocated: */
         errs2 = KCssGetError(css, i, 0, &size); /* get size of message */
         if (errs2) { errs = errs2; checkErrs();  }
 
         /* Now a buffer is allocated and the message is retrieved */
         /* and displayed:
         msg = (char*)malloc(size);
         errs2 = KCssGetError(css, i, msg, &size); /* get message */
         if (errs2) { errs = errs2; checkErrs();  }
         fprintf(stderr, "%s\r\n");
 
         /* the buffer is no longer required and gets discarded: */
         free(msg);
      } /* for */
      exit(1);
   } /* if */
} /* checkErrs */
 
/*
 * c h e c k N u m b e r
 *
 * Check if string represents a number
 */
static int checkNumber(char *s)
{
   int any;
 
   any = 0;
   if (*s=='-' || *s=='+') s++;
   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)
{
   int argCount, i;
   long bufsiz;
   double sum;
   char buf[40], name[4];
 
   /* get actual # of arguments */
   bufsiz = sizeof(buf);
   if ( KCssGet(aCss, "argCount", buf, &bufsiz) ) return; /* (1) */
   argCount = atoi(buf);
 
   // calculate sum of all arguments
   sum = 0.0;
   for (i = 0; i < argCount; i++) {
      /* create name of parameter */
      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 p%d is no number!", i+1);
         KCssSetError(aCss, buf, -1); /* (2) */
      } /* if */
 
      sum += atof(buf);
   } /* for */
 
   /* return result */
   sprintf(buf, "%f", sum / argCount);
   KCssSetResult(aCss, buf, -1); /* (2) */
} /* average */
 
int main()
{
   printf("opening css\r\n");
   errs = KCssOpen(&css,0);
   checkErrs();
 
   printf("\r\nload C function 'average'\r\n");
   errs = KCssAddFunc(
      css,                               /* handle */
      module,                            /* module name */
      "average(const p1, "               /* declaration */
             "[const p2,"
              "const p3,"
              "const p4,"
              "const p5])",
      average);                          /* function address */
   checkErrs();
 
   printf("\r\ncompile a script from memory\r\n");
   errs = KCssLoadScriptMem(
      css,                               /* handle */
      module,                            /* module name */
 
      "#loadLibrary 'KcSysLib'\n"        /* the script */
      ""
      "check()\n"
      "{\n"
      "   sysLog('the average of 3, 5, 12 and 7 is '\n"
      "          |average(3,5,12,7)\n"
      "   );\n"
      "}\n"
   );
   checkErrs();
 
   printf("\r\ncall 'check' within compiled script\r\n");
   errs = KCssCall(css, module, "check", 0, 0);
   checkErrs();
 
   printf("\r\nclosing css\r\n");
   errs = KCssClose(css);
   checkErrs();
 
   return 0;
} /* main */

Notes:

  1. Within C/C++ implementions of CSS functions 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.
 Copyright © IBK LandquartLast revised by Peter Koch, 24.02.00<< Back  Top  Next >>