home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / icon / Source / Iconx / C / Lrec < prev    next >
Encoding:
Text File  |  1990-07-19  |  2.2 KB  |  106 lines

  1. /*
  2.  * File: lrec.c
  3.  *  Contents: field, mkrec
  4.  */
  5.  
  6. #include "../h/config.h"
  7. #include "../h/rt.h"
  8. #include "rproto.h"
  9.  
  10.  
  11. /*
  12.  * x.y - access field y of record x.
  13.  */
  14.  
  15. LibDcl(field,2,".")
  16.    {
  17.    register word fnum;
  18.    register struct b_record *rp;
  19.    register dptr dp;
  20.    extern word *ftabp, *records;
  21.  
  22. #if MACINTOSH
  23. #if MPW
  24. /* #pragma unused(nargs) */
  25. #endif                    /* MPW */
  26. #endif                    /* MACINTOSH */
  27.  
  28.    if (DeRef(Arg1) == Error) 
  29.       RunErr(0, NULL);
  30.  
  31.    /*
  32.     * Arg1 must be a record and Arg2 must be a field number.
  33.     */
  34.    if (Arg1.dword != D_Record) 
  35.       RunErr(107, &Arg1);
  36.  
  37.    /*
  38.     * Map the field number into a field number for the record x.
  39.     */
  40.    rp = (struct b_record *) BlkLoc(Arg1);
  41.    fnum = ftabp[IntVal(Arg2) * *records + rp->recdesc->proc.recnum - 1];
  42.    /*
  43.     * If fnum < 0, x doesn't contain the specified field.
  44.     */
  45.    if (fnum < 0) 
  46.       RunErr(207, &Arg1);
  47.  
  48.    /*
  49.     * Return a pointer to the descriptor for the appropriate field.
  50.     */
  51.    dp = &rp->fields[fnum];
  52.    Arg0.dword = D_Var + ((word *)dp - (word *)rp);
  53.    VarLoc(Arg0) = (dptr)rp;
  54.    Return;
  55.    }
  56.  
  57.  
  58. /*
  59.  * mkrec - create a record.
  60.  */
  61.  
  62. LibDcl(mkrec,-1,"mkrec")
  63.    {
  64.    register int i;
  65.    register struct b_proc *bp;
  66.    register struct b_record *rp;
  67.  
  68.    /*
  69.     * Be sure that call is from a procedure.
  70.     */
  71.  
  72.    /*
  73.     * Ensure space for the record to be created.
  74.     */
  75.    if (blkreq((uword)sizeof(struct b_record) +
  76.          BlkLoc(Arg(0))->proc.nfields*sizeof(struct descrip)) == Error) 
  77.       RunErr(0, NULL);
  78.  
  79.    /*
  80.     * Get a pointer to the record constructor procedure and allocate
  81.     *  a record with the appropriate number of fields.
  82.     */
  83.    bp = (struct b_proc *) BlkLoc(Arg(0));
  84.    rp = alcrecd((int)bp->nfields, (union block **)bp);
  85.    rp->id = (bp->recid)++;
  86.  
  87.    /*
  88.     * Set all fields in the new record to null value.
  89.     */
  90.    for (i = (int)bp->nfields; i > nargs; i--)
  91.       rp->fields[i-1] = nulldesc;
  92.  
  93.    /*
  94.     * Assign each argument value to a record element and dereference it.
  95.     */
  96.    for ( ; i > 0; i--) {
  97.       rp->fields[i-1] = Arg(i);
  98.       if (DeRef(rp->fields[i-1]) == Error) 
  99.          RunErr(0, NULL);
  100.       }
  101.  
  102.    ArgType(0) = D_Record;
  103.    Arg(0).vword.bptr = (union block *)rp;
  104.    Return;
  105.    }
  106.