]> icculus.org git repositories - divverent/nexuiz.git/blob - data/menuqc/util/property.qc
rename menu directories
[divverent/nexuiz.git] / data / menuqc / util / property.qc
1 // NG Menu
2 // util/property.qc
3
4 #ifdef DEBUG
5         #define DebugValidate(str)      Property_Validate( str )
6 #else
7         #define DebugValidate(str)      true
8 #endif
9
10 /*
11 ===================
12 Property_Create
13 ===================
14 */
15 string() Property_Create =
16 {
17         return String_Create();
18 };
19
20 string( string pString ) Property_Zone =
21 {
22         return String_Zone( pString );
23 };
24
25 string( string pString ) Propery_Free =
26 {
27         return String_Free( pString );
28 };
29
30 /*
31 ===================
32 Property_Validate
33 ===================
34 */
35 bool( string pString ) Property_Validate =
36 {
37         local float lCount;
38
39         lCount = Util_GetAltStringCount( pString );
40         // check whether this is a valid property string
41         if( rint( lCount / 2 ) != lCount / 2 )
42 #ifdef STRICTRULES
43                 error( __FUNC__, "Invalid property string \"", pString , "\"!\n" );
44 #else
45         {
46                 dprint( __FUNC__, "Invalid property string \"", pString, "\"!\n" );
47                 return false;
48         }
49 #endif
50         return true;
51 };
52
53 /*
54 ===================
55 Property_Exists
56 ===================
57 */
58 bool( string pString, string pName ) Property_Exists =
59 {
60         local float lCount;
61         local float lCounter;
62
63         if( !DebugValidate( pString ) )
64                 return false;
65
66         lCount = Util_GetAltStringCount( pString );
67         for( lCounter = 0 ; lCounter < lCount ; lCounter += 2 )
68                 if( String_Normal( Util_GetAltStringItem( pString, lCounter ) ) == pName )
69                         return true;
70         return false;
71 };
72
73 /*
74 ===================
75 Property_Register
76 ===================
77 */
78 string( string pString, string pName, string pInitValue ) Property_Register =
79 {
80         local float lCount;
81         local float lCounter;
82
83         if( !DebugValidate( pString ) )
84                 return pString;
85
86         // If the entry already exists, do nothing (thus exit the function)
87         lCount = Util_GetAltStringCount( pString );
88         for( lCounter = 0 ; lCounter < lCount ; lCounter += 2 )
89                 if( String_Normal( Util_GetAltStringItem( pString, lCounter ) ) == pName )
90                         return pString;
91
92         // If it hasnt been added yet, create it
93         pString = Util_AltStringPush( pString, pInitValue );
94         pString = Util_AltStringPush( pString, pName );
95
96         return pString;
97 }
98
99 /*
100 ===================
101 Property_Set
102 ===================
103 */
104 string( string pString, string pName, string pValue ) Property_Set =
105 {
106         local float lCount;
107         local float lCounter;
108
109         if( !DebugValidate( pString ) )
110                 return pString;
111
112         // Found the entry and set it to the new value
113         lCount = Util_GetAltStringCount( pString );
114         for( lCounter = 0 ; lCounter < lCount ; lCounter += 2 )
115                 if( String_Normal( Util_GetAltStringItem( pString, lCounter ) ) == pName )
116                         return Util_SetAltStringItem( pString, lCounter + 1, pValue );
117
118         // If the property isnt found, it depends on DEBUG and STRICT
119 #ifdef STRICTRULES
120         error( __FUNC__, "The property \"", pName, "\" hasn't been declared!\npString = \"", pString, "\"" );
121 #else
122         dprint( __FUNC__, "The property \"", pName, "\" hasn't been declared!\npString = \"", pString, "\"" );
123 #endif
124
125         return pString;
126 };
127
128 string( string pString, string pOldName, string pNewName ) Property_Rename =
129 {
130         local float lCount;
131         local float lCounter;
132
133         if( !DebugValidate( pString ) )
134                 return pString;
135
136         // Found the entry if it already exists
137         lCount = Util_GetAltStringCount( pString );
138         for( lCounter = 0 ; lCounter < lCount ; lCounter += 2 )
139                 if( String_Normal( Util_GetAltStringItem( pString, lCounter ) ) == pOldName )
140                         return Util_SetAltStringItem( pString, lCounter, pNewName );
141
142         // If the property isnt found, it depends on DEBUG and STRICT
143 #ifdef STRICTRULES
144         error( __FUNC__, "The property \"", pOldName, "\" hasn't been declared!\npString = \"", pString, "\"" );
145 #else
146         dprint( __FUNC__, "The property \"", pOldName, "\" hasn't been declared!\npString = \"", pString, "\"" );
147 #endif
148         return pString;
149 };
150
151 string( string pString, string pName )  Property_Delete =
152 {
153         local float lCount;
154         local float lCounter;
155
156         if( !DebugValidate( pString ) )
157                 return pString;
158
159         // Found the entry if it already exists
160         lCount = Util_GetAltStringCount( pString );
161         for( lCounter = 0 ; lCounter < lCount ; lCounter += 2 )
162                 if( String_Normal( Util_GetAltStringItem( pString, lCounter ) ) == pName ) {
163                         pString = Util_DelAltStringItem( pString, lCounter );
164                         pString = Util_DelAltStringItem( pString, lCounter );
165                         return pString;
166                 }
167
168         // If the property isnt found, it depends on DEBUG and STRICT
169 #ifdef STRICTRULES
170         error( __FUNC__, "The property \"", pName, "\" hasn't been declared!\npString = \"", pString, "\"" );
171 #else
172         dprint( __FUNC__, "The property \"", pName, "\" hasn't been declared!\npString = \"", pString, "\"" );
173 #endif
174         return pString;
175 };
176
177 /*
178 ===================
179 Property_Set
180 ===================
181 */
182 /*
183 string( string pString, string pName, string pValue )     Property_Set =
184 {
185         local float lCount;
186         local float lCounter;
187         local float lProp;
188
189         // save pValue
190         pValue = strzone( pValue );
191
192         if( !DebugValidate( pString ) )
193                 return pString;
194
195         // Found the entry if it already exists
196         lCount = Util_GetAltStringCount( pString );
197         lProp = -1;
198         for( lCounter = 0 ; lCounter < lCount ; lCounter += 2 )
199                 if( Util_GetAltStringItem( pString, lCounter ) == pName ) {
200                         lProp = lCounter;
201                         break;
202                 }
203
204         // If it hasnt been added yet, create it
205         if( lProp == -1 ) {
206                 pString = Util_AltStringPush( pString, pValue );
207                 pString = Util_AltStringPush( pString, pName );
208         } else
209                 pString = Util_SetAltStringItem( pString, lProp + 1, pValue );
210
211         strunzone( pValue );
212         return pString;
213 };
214 */
215
216 /*
217 ===================
218 Property_Get
219 ===================
220 */
221 string( string pString, string pName )  Property_Get =
222 {
223         local float lCount;
224         local float lCounter;
225
226         if( !DebugValidate( pString ) )
227                 return pString;
228
229         // Found the entry and return its value
230         lCount = Util_GetAltStringCount( pString );
231         for( lCounter = 0 ; lCounter < lCount ; lCounter += 2 )
232                 if( String_Normal( Util_GetAltStringItem( pString, lCounter ) ) == pName )
233                         return Util_GetAltStringItem( pString, lCounter + 1);
234
235         // If the property isnt found, it depends on DEBUG and STRICT
236 #ifdef STRICTRULES
237         error( "The property \"", pName, "\" hasn't been declared!\npString = \"", pString, "\"" );
238 #else
239         dprint( "The property \"", pName, "\" hasn't been declared!\npString = \"", pString, "\"" );
240 #endif
241
242         return String_Zone( "" );
243 };
244
245 /*
246 ===================
247 Property_GetString
248 ===================
249 */
250 string( string pString, string pName )  Property_GetString =
251 {
252         return Property_Get( pString, pName );
253 };
254
255 /*
256 ===================
257 Property_GetFloatProperty
258 ===================
259 */
260 float( string pString, string pName )   Property_GetFloat =
261 {
262         return stof( String_Normal( Property_Get( pString, pName ) ) );
263 };
264
265 vector( string pString, string pName )  Property_GetVector =
266 {
267         return stov( String_Normal( Property_Get( pString, pName ) ) );
268 };
269
270 entity( string pString, string pName )  Property_GetEntity =
271 {
272         return ftoe( Property_GetFloat( pString, pName ) );
273 };
274
275 #undef DebugValidate()