Back to index

radiance  4R0+20100331
object.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: object.c,v 1.6 2003/02/28 20:11:29 greg Exp $";
00003 #endif
00004 /*
00005  * Header file for tracking hierarchical object names
00006  */
00007 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include "parser.h"
00012 
00013 
00014 int    obj_nnames;                 /* depth of name hierarchy */
00015 char   **obj_name;                 /* name list */
00016 
00017 static int    obj_maxname;         /* allocated list size */
00018 
00019 #define ALLOC_INC    16            /* list increment ( > 1 ) */
00020 
00021 
00022 int
00023 obj_handler(ac, av)         /* handle an object entity statement */
00024 int    ac;
00025 char   **av;
00026 {
00027        if (ac == 1) {                            /* just pop top object */
00028               if (obj_nnames < 1)
00029                      return(MG_ECNTXT);
00030               free((MEM_PTR)obj_name[--obj_nnames]);
00031               obj_name[obj_nnames] = NULL;
00032               return(MG_OK);
00033        }
00034        if (ac != 2)
00035               return(MG_EARGC);
00036        if (!isname(av[1]))
00037               return(MG_EILL);
00038        if (obj_nnames >= obj_maxname-1) { /* enlarge array */
00039               if (!obj_maxname)
00040                      obj_name = (char **)malloc(
00041                             (obj_maxname=ALLOC_INC)*sizeof(char *));
00042               else
00043                      obj_name = (char **)realloc((MEM_PTR)obj_name,
00044                             (obj_maxname+=ALLOC_INC)*sizeof(char *));
00045               if (obj_name == NULL)
00046                      return(MG_EMEM);
00047        }
00048                                           /* allocate new entry */
00049        obj_name[obj_nnames] = (char *)malloc(strlen(av[1])+1);
00050        if (obj_name[obj_nnames] == NULL)
00051               return(MG_EMEM);
00052        strcpy(obj_name[obj_nnames++], av[1]);
00053        obj_name[obj_nnames] = NULL;
00054        return(MG_OK);
00055 }
00056 
00057 
00058 void
00059 obj_clear()                 /* clear object stack */
00060 {
00061        while (obj_nnames)
00062               free((MEM_PTR)obj_name[--obj_nnames]);
00063        if (obj_maxname) {
00064               free((MEM_PTR)obj_name);
00065               obj_maxname = 0;
00066        }
00067 }