slang-users mailing list

[2007 Date Index] [2007 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] C Structure help ...


Ben Duncan <linux4ms@xxxxxxx> wrote:
>I have an Appgen  structure like this:
>
>
>/*
>***************************************************************
>   The Appgen Structure
>***************************************************************
>*/
>
>typedef struct
>{
>
>   DB * di_apgn;           /* OUR Appgen DB Structure Pointer */
>   int di_size;            /* -1 means recompute */
>   char *file_name;        /* What is OUR file name for this instance? */
>   char *rec_key;          /* Appgen has a key size limit of 132 */
>   int is_locked;          /* IS the file locked ? */
>   int Attribute;          /* What is the LAST Attribute we dealt with? */
>   int Sub_Val;            /* WHat is the LAST Multi Value ? */
>   int MV_CNT;             /* Keep up with our Looping on MV's */
>
>   char *MV_BUFFER ;       /* On AppGen MV Extract, this is WHERE   */
>                           /* We keep up with the returned malloc   */
>                           /* IT is OUR duty to keep with it since  */
>                           /* Appgen just allocates it and pushes   */
>                           /* the MV string onto it ....            */
>
>} APGN_Type
>
>I need to know how Do I pass that back and forth to/from  the Slang/SLAG
>  program.

I think that the easiest way to do this is to use Mike Noble's slirp
package to automatically generate the code.  Basically you point slirp
at the header file and it performs its magic.  You can read more about
slirp at <http://space.mit.edu/cxc/software/slang/modules/slirp/>.

If you want to hand-wrap it you will have to create an initializer and
a destructor for the object and then make use of slang's MMT functions.
For examples of this approach see the code in the modules directory of
the slang distribution.  As a quick overview, you will first need to
register the type using, e.g.,

int APGN_Type_Id;
static int register_apgn_type (void)
{
   SLang_Class_Type *cl;

   if (NULL == (cl = SLclass_allocate_class ("APGN_Type")))
     return -1;

   if (-1 == SLclass_set_destroy_function (cl, destroy_apgen))
     return -1;

   /* By registering as SLANG_VOID_TYPE, slang will dynamically allocate a
    * type.
    */
   if (-1 == SLclass_register_class (cl, SLANG_VOID_TYPE, sizeof (APGN_Type), 
                                     SLANG_CLASS_TYPE_MMT))
     return -1;

   APGN_Type_Id = SLclass_get_class_id (cl);

   return 0;
}

Here destroy_apgen is a callback function that slang will call when
an APGN_Type instance is nolonger referenced:

static void destroy_apgen (SLtype type, VOID_STAR f)
{
   APGN_Type *apgn = (APGN_Type *)f;
   apgn_delete (apgn); /* code to free apgn */
}

You will need a constructor for this object.  You hinted at something
to be used in a slang script as

  APGN_FILE = appgen_open("MyFile", "r");

So create a slang-intrinsic function such as:

  static void intrin_appgen_open (char *file, char *mode)
  {
     SLang_MMT_Type *mmt;
     APGN_Type *apgn;
     
     /* Add code to create an instance of APGN_Type from file/mode */
     apgn = some_code (file, mode);
     
     if (NULL == (mmt = SLang_creat_mmt (APGN_Type_Id, (VOID_STAR)apgn)))
       {
          /* failed */
	  delete_apgn (apgn);
	  return;
       }
     if (-1 == SLang_push_mmt (mmt))
       SLang_free_mmt (mmt);
   }

Then register that function using the SLns_add_intrinsic_function
routine:
    
      .
      .
    register_apgn_type ();
    SLns_add_intrinsic_function (NULL, "appgen_open", 
                                 (FVOID_STAR) intrin_appgen_open,
				 SLANG_VOID_TYPE, 2,
				 SLANG_STRING_TYPE, SLANG_STRING_TYPE);

The above function says that when calling appgen_open from slang, the
C function intrin_appgen_open should be called, which returns void and
takes 2 char * arguments.

I hope this helps.
--John








[2007 date index] [2007 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]