-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute.c
More file actions
executable file
·194 lines (180 loc) · 5.64 KB
/
attribute.c
File metadata and controls
executable file
·194 lines (180 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/****************************************************************************
* attribute.c
* Author Joel Welling
* Copyright 1990, Pittsburgh Supercomputing Center, Carnegie Mellon University
*
* Permission use, copy, and modify this software and its documentation
* without fee for personal use or use within your organization is hereby
* granted, provided that the above copyright notice is preserved in all
* copies and that that copyright and this permission notice appear in
* supporting documentation. Permission to redistribute this software to
* other organizations or individuals is not granted; that must be
* negotiated with the PSC. Neither the PSC nor Carnegie Mellon
* University make any representations about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*****************************************************************************/
/*
This module provides methods for the manipulation of attribute lists.
*/
#include <stdio.h>
#include "p3dgen.h"
#include "pgen_objects.h"
#include "indent.h"
#include "ge_error.h"
/* Macro to help allocate space */
#define GET_STORAGE( ptr, type ) \
if ( !( ptr= (type *)malloc( sizeof(type) ) ) ) \
ger_fatal("attribute: add_attr: unable to allocate %d bytes!", sizeof(type))
P_Attrib_List *add_attr( P_Attrib_List *oldfirst, char *attribute,
int type, P_Void_ptr value)
/* This routine adds a cell to an attribute list */
{
P_Symbol symbol;
P_Attrib_List *thiscell;
P_Color *clr;
P_Point *pt;
P_Vector *vec;
int *intp;
float *floatp;
ger_debug("attribute: add_attr");
if ( !(thiscell= (P_Attrib_List *)malloc(sizeof(P_Attrib_List))) )
ger_fatal("transform: add_attr: unable to allocate %d bytes!\n",
sizeof(P_Attrib_List));
thiscell->next= oldfirst;
thiscell->prev= (P_Attrib_List *)0;
if (oldfirst) oldfirst->prev= thiscell;
thiscell->attribute= create_symbol(attribute);
thiscell->type= type;
switch (type) {
case P3D_INT:
case P3D_BOOLEAN:
GET_STORAGE( intp, int );
*intp= *(int *)value;
thiscell->value= (P_Void_ptr)intp;
break;
case P3D_FLOAT: /* Clumsy notation because DEC cc can't handle casts */
GET_STORAGE( floatp, float );
*floatp= *(float *)value;
thiscell->value= (P_Void_ptr)floatp;
break;
case P3D_STRING:
if ( !(thiscell->value= (P_Void_ptr)malloc(strlen((char *)value)+1) ) )
ger_fatal("attribute: add_attr: unable to allocate %d bytes for string!",
strlen((char *)value)+1);
strcpy((char *)thiscell->value, (char *)value);
break;
case P3D_COLOR:
thiscell->value= (P_Void_ptr)duplicate_color( (P_Color *)value );
break;
case P3D_POINT:
GET_STORAGE( pt, P_Point );
thiscell->value= (P_Void_ptr)pt;
pt->x= ((P_Point *)value)->x;
pt->y= ((P_Point *)value)->y;
pt->z= ((P_Point *)value)->z;
break;
case P3D_VECTOR:
GET_STORAGE( vec, P_Vector );
thiscell->value= (P_Void_ptr)vec;
vec->x= ((P_Vector *)value)->x;
vec->y= ((P_Vector *)value)->y;
vec->z= ((P_Vector *)value)->z;
break;
case P3D_TRANSFORM:
thiscell->value= (P_Void_ptr)duplicate_trans( (P_Transform *)value );
break;
case P3D_MATERIAL:
thiscell->value= (P_Void_ptr)duplicate_material( (P_Material *)value );
break;
case P3D_OTHER:
/* Can't do much but copy the pointer and hope it isn't deallcoated */
thiscell->value= value;
break;
}
return(thiscell);
}
void print_attr(P_Attrib_List *attr)
/* This routine dumps an attribute list */
{
P_Point *pt;
P_Vector *vec;
P_Color *clr;
ger_debug("gob_mthd: print_attr");
ind_push();
while (attr) {
ind_write("%s: ",symbol_string(attr->attribute));
switch (attr->type) {
case P3D_INT: ind_write("int %d",*(int *)(attr->value)); ind_eol();
break;
case P3D_BOOLEAN:
ind_write("boolean ");
if (*(int *)(attr->value)) ind_write("TRUE"); else ind_write("FALSE");
ind_eol();
break;
case P3D_FLOAT:
ind_write("float %f",*(float *)(attr->value)); ind_eol();
break;
case P3D_STRING: ind_write("string <%s>",(char *)(attr->value)); ind_eol();
break;
case P3D_COLOR:
dump_color( (P_Color *)attr->value );
break;
case P3D_POINT:
pt= (P_Point *)attr->value;
ind_write( "point ( %f %f %f )", pt->x, pt->y, pt->z );
ind_eol();
break;
case P3D_VECTOR:
vec= (P_Vector *)attr->value;
ind_write( "vector ( %f %f %f )", vec->x, vec->y, vec->z );
ind_eol();
break;
case P3D_TRANSFORM:
ind_write("transform"); ind_eol();
dump_trans( (P_Transform *)attr->value );
break;
case P3D_MATERIAL:
ind_write("material: "); dump_material( (P_Material *)attr->value );
break;
case P3D_OTHER:
ind_write("(unprintable)"); ind_eol();
break;
}
attr= attr->next;
}
ind_pop();
}
void destroy_attr(P_Attrib_List *attr)
/* This routine walks an attribute list, destroying it. */
{
P_Attrib_List *this;
ger_debug("attribute: destroy_attr");
while (attr) {
this= attr;
attr= attr->next;
switch (this->type) {
case P3D_INT:
case P3D_BOOLEAN:
case P3D_FLOAT:
case P3D_STRING:
case P3D_POINT:
case P3D_VECTOR:
free( this->value );
break;
case P3D_COLOR:
destroy_color( (P_Color *)this->value );
break;
case P3D_TRANSFORM:
destroy_trans( (P_Transform *)this->value );
break;
case P3D_MATERIAL:
destroy_material( (P_Material *)this->value );
break;
case P3D_OTHER:
/* Wasn't allocated, so do nothing */
break;
}
free( (P_Void_ptr)this );
}
}