home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / gfx / 3d / irit / misc_lib / miscattr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-14  |  24.1 KB  |  541 lines

  1. /*****************************************************************************
  2. * Setting attributes for objects.                         *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.2, Mar. 1990   *
  5. *****************************************************************************/
  6.  
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include "irit_sm.h"
  11. #include "imalloc.h"
  12. #include "miscattr.h"
  13.  
  14. /*****************************************************************************
  15. * DESCRIPTION:                                                               M
  16. *   Routine to set an integer attribute.                     M
  17. *                                                                            *
  18. * PARAMETERS:                                                                M
  19. *   Attrs:     Attribute list where to place new attribute.                  M
  20. *   Name:      Name of thely introducedattribute                             M
  21. *   Data:      Ingeter attribute to save.                                    M
  22. *                                                                            *
  23. * RETURN VALUE:                                                              M
  24. *   void                                                                     M
  25. *                                                                            *
  26. * KEYWORDS:                                                                  M
  27. *   AttrSetIntAttrib, attributes                                             M
  28. *****************************************************************************/
  29. void AttrSetIntAttrib(IPAttributeStruct **Attrs, char *Name, int Data)
  30. {
  31.     IPAttributeStruct
  32.         *Attr = AttrFindAttribute(*Attrs, Name);
  33.     
  34.     if (Attr) {
  35.     _AttrFreeAttributeData(Attr);
  36.     Attr -> U.I = Data;
  37.     Attr -> Type = IP_ATTR_INT;
  38.     }
  39.     else {
  40.     Attr = _AttrMallocAttribute(Name, IP_ATTR_INT);
  41.     Attr -> U.I = Data;
  42.     Attr -> Pnext = *Attrs;
  43.     *Attrs = Attr;
  44.     }
  45. }
  46.  
  47. /*****************************************************************************
  48. * DESCRIPTION:                                                               M
  49. *   Routine to get an integer attribute.                     M
  50. *                                                                            *
  51. * PARAMETERS:                                                                M
  52. *   Attrs:    Attribute list to search for requested attribute.              M
  53. *   Name:     Name of requested attribute.                                   M
  54. *                                                                            *
  55. * RETURN VALUE:                                                              M
  56. *   int:      Found attribute, or IP_ATTR_BAD_INT if not found.              M
  57. *                                                                            *
  58. * KEYWORDS:                                                                  M
  59. *   AttrGetIntAttrib, attributes                                             M
  60. *****************************************************************************/
  61. int AttrGetIntAttrib(IPAttributeStruct *Attrs, char *Name)
  62. {
  63.     IPAttributeStruct
  64.     *Attr = AttrFindAttribute(Attrs, Name);
  65.  
  66.     if (Attr != NULL) {
  67.     if (Attr -> Type == IP_ATTR_INT)
  68.         return Attr -> U.I;
  69.     else if (Attr -> Type == IP_ATTR_REAL)
  70.         return (int) Attr -> U.R;
  71.     else if (Attr -> Type == IP_ATTR_STR)
  72.         return atoi(Attr -> U.Str);
  73.     }
  74.  
  75.     return IP_ATTR_BAD_INT;
  76. }
  77.  
  78. /*****************************************************************************
  79. * DESCRIPTION:                                                               M
  80. *   Routine to set a pointer attribute.                          M
  81. *                                                                            *
  82. * PARAMETERS:                                                                M
  83. *   Attrs:     Attribute list where to place new attribute.                  M
  84. *   Name:      Name of thely introducedattribute                             M
  85. *   Data:      Pointer attribute to save.                                    M
  86. *                                                                            *
  87. * RETURN VALUE:                                                              M
  88. *   void                                                                     M
  89. *                                                                            *
  90. * KEYWORDS:                                                                  M
  91. *   AttrSetPtrAttrib, attributes                                             M
  92. *****************************************************************************/
  93. void AttrSetPtrAttrib(IPAttributeStruct **Attrs, char *Name, VoidPtr Data)
  94. {
  95.     IPAttributeStruct
  96.         *Attr = AttrFindAttribute(*Attrs, Name);
  97.  
  98.     if (Attr) {
  99.     _AttrFreeAttributeData(Attr);
  100.     Attr -> U.Ptr = Data;
  101.     Attr -> Type = IP_ATTR_PTR;
  102.     }
  103.     else {
  104.     Attr = _AttrMallocAttribute(Name, IP_ATTR_PTR);
  105.     Attr -> U.Ptr = Data;
  106.     Attr -> Pnext = *Attrs;
  107.     *Attrs = Attr;
  108.     }
  109. }
  110. /*****************************************************************************
  111. * DESCRIPTION:                                                               M
  112. *   Routine to get a pointer attribute.                         M
  113. *                                                                            *
  114. * PARAMETERS:                                                                M
  115. *   Attrs:    Attribute list to search for requested attribute.              M
  116. *   Name:     Name of requested attribute.                                   M
  117. *                                                                            *
  118. * RETURN VALUE:                                                              M
  119. *   VoidPtr: Found attribute, or NULL if not found.                      M
  120. *                                                                            *
  121. * KEYWORDS:                                                                  M
  122. *   AttrGetPtrAttrib, attributes                                             M
  123. *****************************************************************************/
  124. VoidPtr AttrGetPtrAttrib(IPAttributeStruct *Attrs, char *Name)
  125. {
  126.     IPAttributeStruct
  127.     *Attr = AttrFindAttribute(Attrs, Name);
  128.  
  129.     if (Attr != NULL && Attr -> Type == IP_ATTR_PTR)
  130.         return Attr -> U.Ptr;
  131.     else
  132.         return NULL;
  133. }
  134.  
  135. /*****************************************************************************
  136. * DESCRIPTION:                                                               M
  137. *   Routine to set a RealType attribute.                     M
  138. *                                                                            *
  139. * PARAMETERS:                                                                M
  140. *   Attrs:     Attribute list where to place new attribute.                  M
  141. *   Name:      Name of thely introducedattribute                             M
  142. *   Data:      RealType attribute to save.                                   M
  143. *                                                                            *
  144. * RETURN VALUE:                                                              M
  145. *   void                                                                     M
  146. *                                                                            *
  147. * KEYWORDS:                                                                  M
  148. *   AttrSetRealAttrib, attributes                                            M
  149. *****************************************************************************/
  150. void AttrSetRealAttrib(IPAttributeStruct **Attrs, char *Name, RealType Data)
  151. {
  152.     IPAttributeStruct
  153.         *Attr = AttrFindAttribute(*Attrs, Name);
  154.     
  155.     if (Attr) {
  156.     _AttrFreeAttributeData(Attr);
  157.     Attr -> U.R = Data;
  158.     Attr -> Type = IP_ATTR_REAL;
  159.     }
  160.     else {
  161.     Attr = _AttrMallocAttribute(Name, IP_ATTR_REAL);
  162.     Attr -> U.R = Data;
  163.     Attr -> Pnext = *Attrs;
  164.     *Attrs = Attr;
  165.     }
  166. }
  167.  
  168. /*****************************************************************************
  169. * DESCRIPTION:                                                               M
  170. *   Routine to get a RealType attribute.                     M
  171. *                                                                            *
  172. * PARAMETERS:                                                                M
  173. *   Attrs:    Attribute list to search for requested attribute.              M
  174. *   Name:     Name of requested attribute.                                   M
  175. *                                                                            *
  176. * RETURN VALUE:                                                              M
  177. *   RealType: Found attribute, or IP_ATTR_BAD_REAL if not found.             M
  178. *                                                                            *
  179. * KEYWORDS:                                                                  M
  180. *   AttrGetRealAttrib, attributes                                            M
  181. *****************************************************************************/
  182. RealType AttrGetRealAttrib(IPAttributeStruct *Attrs, char *Name)
  183. {
  184.     IPAttributeStruct
  185.     *Attr = AttrFindAttribute(Attrs, Name);
  186.  
  187.     if (Attr != NULL) {
  188.     if (Attr -> Type == IP_ATTR_REAL)
  189.         return Attr -> U.R;
  190.     else if (Attr -> Type == IP_ATTR_INT)
  191.         return (RealType) Attr -> U.I;
  192.     else if (Attr -> Type == IP_ATTR_STR)
  193.     {
  194.         RealType r;
  195.  
  196.         sscanf("%lf", Attr -> U.Str, &r);
  197.         return r;
  198.     }
  199.     }
  200.  
  201.     return IP_ATTR_BAD_REAL * 10;
  202. }
  203.  
  204. /*****************************************************************************
  205. * DESCRIPTION:                                                               M
  206. *   Routine to set a string attribute.                         M
  207. *                                                                            *
  208. * PARAMETERS:                                                                M
  209. *   Attrs:     Attribute list where to place new attribute.                  M
  210. *   Name:      Name of thely introducedattribute                             M
  211. *   Data:      String attribute to save.                                     M
  212. *                                                                            *
  213. * RETURN VALUE:                                                              M
  214. *   void                                                                     M
  215. *                                                                            *
  216. * KEYWORDS:                                                                  M
  217. *   AttrSetStrAttrib, attributes                                             M
  218. *****************************************************************************/
  219. void AttrSetStrAttrib(IPAttributeStruct **Attrs, char *Name, char *Data)
  220. {
  221.     IPAttributeStruct
  222.         *Attr = AttrFindAttribute(*Attrs, Name);
  223.  
  224.     if (Attr) {
  225.     _AttrFreeAttributeData(Attr);
  226.     Attr -> U.Str = IritStrdup(Data);
  227.     Attr -> Type = IP_ATTR_STR;
  228.     }
  229.     else {
  230.     Attr = _AttrMallocAttribute(Name, IP_ATTR_STR);
  231.     Attr -> U.Str = IritStrdup(Data);
  232.     Attr -> Pnext = *Attrs;
  233.     *Attrs = Attr;
  234.     }
  235. }
  236.  
  237. /*****************************************************************************
  238. * DESCRIPTION:                                                               M
  239. *   Routine to get a string attribute.                         M
  240. *                                                                            *
  241. * PARAMETERS:                                                                M
  242. *   Attrs:    Attribute list to search for requested attribute.              M
  243. *   Name:     Name of requested attribute.                                   M
  244. *                                                                            *
  245. * RETURN VALUE:                                                              M
  246. *   char *:   Found attribute, or NULL if not found.                        M
  247. *                                                                            *
  248. * KEYWORDS:                                                                  M
  249. *   AttrGetStrAttrib, attributes                                             M
  250. *****************************************************************************/
  251. char *AttrGetStrAttrib(IPAttributeStruct *Attrs, char *Name)
  252. {
  253.     IPAttributeStruct
  254.     *Attr = AttrFindAttribute(Attrs, Name);
  255.  
  256.     if (Attr != NULL && Attr -> Type == IP_ATTR_STR)
  257.         return Attr -> U.Str;
  258.     else
  259.         return NULL;
  260. }
  261.  
  262. /*****************************************************************************
  263. * DESCRIPTION:                                                               M
  264. * Routine to aid in scanning a list of attributes.                           M
  265. *   If TraceAttrs != NULL, a ptr to its attribute list is saved and the      M
  266. * next attribute is returned every call until the end of the list is         M
  267. * reached, in which NULL is returned.                                        M
  268. *   FirstAttrs should be NULL in all but the first call in the sequence.     M
  269. *   Attributes with names starting with an underscore '_' are assumed to be  M
  270. * temporary or internal and are skipped.                     M 
  271. *                                                                            *
  272. * PARAMETERS:                                                                M
  273. *   TraceAttrs:    If not NULL, contains the previously returned attribute.  M
  274. *   FirstAttrs:    First attribute in list, usually NULL in all but the      M
  275. *                  first invokation in a psuence.                 M
  276. *                                                                            *
  277. * RETURN VALUE:                                                              M
  278. *   IPAttributeStruct *:  Next attribute in list.                            M
  279. *                                                                            *
  280. * KEYWORDS:                                                                  M
  281. *   AttrTraceAttributes, attributes                                          M
  282. *****************************************************************************/
  283. IPAttributeStruct *AttrTraceAttributes(IPAttributeStruct *TraceAttrs,
  284.                        IPAttributeStruct *FirstAttrs)
  285. {
  286.     if (FirstAttrs != NULL)
  287.     TraceAttrs = FirstAttrs;
  288.     else if (TraceAttrs != NULL)
  289.     TraceAttrs = TraceAttrs -> Pnext;
  290.     else
  291.     return NULL;
  292.  
  293.     while (TraceAttrs) {
  294.     if (TraceAttrs -> Name[0] != '_') {
  295.         return TraceAttrs;
  296.     }
  297.     else
  298.         TraceAttrs = TraceAttrs -> Pnext;
  299.     }
  300.  
  301.     return NULL;
  302. }
  303.  
  304. /*****************************************************************************
  305. * DESCRIPTION:                                                               M
  306. *   Routine to convert an attribute to a string.                 M
  307. *                                                                            *
  308. * PARAMETERS:                                                                M
  309. *   Attr:      To convert to a string.                                       M
  310. *                                                                            *
  311. * RETURN VALUE:                                                              M
  312. *   char *:    A pointer to a static string representing/describing the      M
  313. *              given attribute.                             M
  314. *                                                                            *
  315. * KEYWORDS:                                                                  M
  316. *   Attr2String, attributes                                                  M
  317. *****************************************************************************/
  318. char *Attr2String(IPAttributeStruct *Attr)
  319. {
  320.     static char Str[LINE_LEN_LONG];
  321.  
  322.     Str[0] = 0;
  323.  
  324.     switch (Attr -> Type) {
  325.     case IP_ATTR_INT:
  326.         sprintf(Str, "[%s %d]", Attr -> Name, Attr -> U.I);
  327.         break;
  328.     case IP_ATTR_REAL:
  329.         sprintf(Str, "[%s %g]", Attr -> Name, Attr -> U.R);
  330.         break;
  331.     case IP_ATTR_STR:
  332.         if (strchr(Attr -> U.Str, '"') != NULL) {
  333.         /* Need to escape the quotes. */
  334.             int i, j;
  335.  
  336.         sprintf(Str, "[%s \"", Attr -> Name);
  337.  
  338.         /* Need to quote the string or escape the internal " */
  339.         for (i = j = 0; i < strlen(Attr -> U.Str); i++) {
  340.             if (Attr -> U.Str[i] == '"')
  341.             Str[j++] = '\\';
  342.             Str[j++] = Attr -> U.Str[i];
  343.         }
  344.         Str[j] = 0;
  345.         strcat(Str, "\"]");
  346.         }
  347.         else if (strlen(Attr -> U.Str) > 0)
  348.             sprintf(Str, "[%s \"%s\"]", Attr -> Name, Attr -> U.Str);
  349.         else
  350.         sprintf(Str, "[%s]", Attr -> Name);
  351.         break;
  352.     case IP_ATTR_OBJ:
  353.         sprintf(Str, "[%s _OBJ_ATTR_NOT_CNVRTED_]", Attr -> Name);
  354.         break;
  355.     case IP_ATTR_PTR:
  356.         sprintf(Str, "[%s _PTR_ATTR_NOT_CNVRTED_]", Attr -> Name);
  357.         break;
  358.     default:
  359.         IritFatalError("Undefined attribute type");
  360.         break;
  361.     }
  362.  
  363.     return Str;
  364. }
  365.  
  366. /*****************************************************************************
  367. * DESCRIPTION:                                                               M
  368. *   Routine to initialize the attributes of the given Attr list.         M
  369. *                                                                            *
  370. * PARAMETERS:                                                                M
  371. *   Attrs:   To initialize.                                                  M
  372. *                                                                            *
  373. * RETURN VALUE:                                                              M
  374. *   void                                                                     M
  375. *                                                                            *
  376. * KEYWORDS:                                                                  M
  377. *   AttrResetAttributes, attributes                                          M
  378. *****************************************************************************/
  379. void AttrResetAttributes(IPAttributeStruct **Attrs)
  380. {
  381.     *Attrs = NULL;
  382. }
  383.  
  384. /*****************************************************************************
  385. * DESCRIPTION:                                                               M
  386. *   Routine to search for an attribute by Name.                     M
  387. *                                                                            *
  388. * PARAMETERS:                                                                M
  389. *   Attrs:    Attribute list to search.                                      M
  390. *   Name:     Attribute to search by this name.                              M
  391. *                                                                            *
  392. * RETURN VALUE:                                                              M
  393. *   IPAttributeStruct *:  Attribute if found, otherwise NULL.                M
  394. *                                                                            *
  395. * KEYWORDS:                                                                  M
  396. *   AttrFindAttribute, attributes                                            M
  397. *****************************************************************************/
  398. IPAttributeStruct *AttrFindAttribute(IPAttributeStruct *Attrs, char *Name)
  399. {
  400.     for (; Attrs != NULL; Attrs = Attrs -> Pnext)
  401.     if (stricmp(Name, Attrs -> Name) == 0)
  402.         return Attrs;
  403.  
  404.     return NULL;
  405. }
  406.  
  407. /*****************************************************************************
  408. * DESCRIPTION:                                                               *
  409. * Allocated a new attribute structure.                                       *
  410. *                                                                            *
  411. * PARAMETERS:                                                                *
  412. *   Name:   Name of newly created attribute.                                 *
  413. *   Type:   Type of newly created attribute.                                 *
  414. *                                                                            *
  415. * RETURN VALUE:                                                              *
  416. *   IPAttributeStruct *:  The newly created attribute.                       *
  417. *****************************************************************************/
  418. IPAttributeStruct *_AttrMallocAttribute(char *Name, IPAttributeType Type)
  419. {
  420.     IPAttributeStruct
  421.     *Attr = (IPAttributeStruct *) IritMalloc(sizeof(IPAttributeStruct));
  422.  
  423.     Attr -> Type = Type;
  424.     Attr -> Name = IritStrdup(Name);
  425.     Attr -> Pnext = NULL;
  426.  
  427.     return Attr;
  428. }
  429.  
  430. /*****************************************************************************
  431. * DESCRIPTION:                                                               M
  432. * Routine to remove and delete the attribute named Name from the given       M
  433. * Attr list.                                                                 M
  434. *                                                                            *
  435. * PARAMETERS:                                                                M
  436. *   Attrs:   To search for an attribute named Name and remove and delete it. M
  437. *   Name:    Name of attribute to remove and delete.                         M
  438. *                                                                            *
  439. * RETURN VALUE:                                                              M
  440. *   void                                                                     M
  441. *                                                                            *
  442. * KEYWORDS:                                                                  M
  443. *   AttrFreeOneAttribute, attributes                                         M
  444. *****************************************************************************/
  445. void AttrFreeOneAttribute(IPAttributeStruct **Attrs, char *Name)
  446. {
  447.     IPAttributeStruct *TmpAttr,
  448.     *Attr = *Attrs;
  449.  
  450.     if (Attr) {
  451.     if (stricmp(Name, Attr -> Name) == 0) {
  452.         /* It is the first one - delete first in list. */
  453.         *Attrs = Attr -> Pnext;
  454.         Attr -> Pnext = NULL;
  455.         AttrFreeAttributes(&Attr);
  456.     }
  457.     else {
  458.         /* Search the rest of the list and delete if found. */
  459.         while (Attr -> Pnext != NULL) {
  460.         if (stricmp(Name, Attr -> Pnext -> Name) == 0) {
  461.             TmpAttr = Attr -> Pnext;
  462.             Attr -> Pnext = TmpAttr -> Pnext;
  463.             TmpAttr -> Pnext = NULL;
  464.             AttrFreeAttributes(&TmpAttr);
  465.         }
  466.         else
  467.             Attr = Attr -> Pnext;
  468.         }
  469.     }
  470.     }
  471. }
  472.  
  473. /*****************************************************************************
  474. * DESCRIPTION:                                                               M
  475. * Routine to remove anddelete all attributes of the given Attr list.         M
  476. *                                                                            *
  477. * PARAMETERS:                                                                M
  478. *   Attrs:   To remove and delete.                                           M
  479. *                                                                            *
  480. * RETURN VALUE:                                                              M
  481. *   void                                                                     M
  482. *                                                                            *
  483. * KEYWORDS:                                                                  M
  484. *   AttrFreeAttributes, attributes                                           M
  485. *****************************************************************************/
  486. void AttrFreeAttributes(IPAttributeStruct **Attrs)
  487. {
  488.     IPAttributeStruct *Attr;
  489.  
  490.     if (*Attrs == NULL)
  491.     return;
  492.  
  493.     for (Attr = *Attrs; Attr != NULL; ) {
  494.         IPAttributeStruct
  495.         *Next = Attr -> Pnext;
  496.  
  497.     _AttrFreeAttributeData(Attr);
  498.     IritFree((VoidPtr) Attr -> Name);
  499.     IritFree((VoidPtr) Attr);
  500.     Attr = Next;
  501.     }
  502.  
  503.     *Attrs = NULL;
  504. }
  505.  
  506. /*****************************************************************************
  507. * DESCRIPTION:                                                               M
  508. *   Routine to copy an attribute list.                         M
  509. *                                                                            *
  510. * PARAMETERS:                                                                M
  511. *   Src:       Attribute list to duplicate.                                  M
  512. *                                                                            *
  513. * RETURN VALUE:                                                              M
  514. *   IPAttributeStruct *:   Duplicated attribute list.                        M
  515. *                                                                            *
  516. * KEYWORDS:                                                                  M
  517. *   AttrCopyAttributes,attributes                                            M
  518. *****************************************************************************/
  519. IPAttributeStruct *AttrCopyAttributes(IPAttributeStruct *Src)
  520. {
  521.     IPAttributeStruct
  522.     *DestAttr = NULL,
  523.     *Dest = NULL;
  524.  
  525.     for ( ; Src != NULL; Src = Src -> Pnext) {
  526.     if (Src -> Name[0] == '_')       /* Do not copy internal attributes. */
  527.         continue;
  528.  
  529.     if (Dest == NULL) {    
  530.         Dest = DestAttr = AttrCopyOneAttribute(Src);
  531.     }
  532.     else {
  533.         DestAttr -> Pnext = AttrCopyOneAttribute(Src);
  534.  
  535.         DestAttr = DestAttr -> Pnext;
  536.     }
  537.     }
  538.  
  539.     return Dest;
  540. }
  541.