File: Proplists.c

package info (click to toggle)
openclonk 8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 169,656 kB
  • sloc: cpp: 180,484; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 148; sh: 101; javascript: 34
file content (74 lines) | stat: -rw-r--r-- 2,152 bytes parent folder | download | duplicates (5)
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
/**
	Proplists.c
	General helper functions that create or work with proplists.
		
	@author Guenther, Maikel, Marky
*/

/*
 Adds the contents of one proplist to the other.
 Usually you can do this by using a prototype, but 
 this is not possible when you receive a second proplist
 from another function.
 
 @par original This proplist will be expanded.
 @par additional These are the additional contents.
 @par no_overwrite By default the function overwrites
      existing properties in the original proplist,
      as it would happen if you derive from a prototype.
      If this parameter is 'true' the function will report
      an error instead of overwriting a parameter.
 
 @return proplist The original proplist. This is in fact the same
         proplist that was passed as an argument, the functions just
         returns it for convenience.
 */
global func AddProperties(proplist original, proplist additional, bool no_overwrite)
{
	if (original == nil)
	{
		FatalError("Cannot add properties to 'nil'");
	}
	if (additional == nil)
	{
		FatalError("Adding proplist 'nil' to another proplist makes no sense.");
	}
	
	for (var property in GetProperties(additional))
	{
		if (no_overwrite && (original[property] != nil)) // about to overwrite?
		{
			FatalError(Format("Cancelling overwrite of property '%s', original value %v, with new value %v", property, original[property], additional[property]));
		}
		else
		{
			original[property] = additional[property];
		}
	}
	
	return original;
}


// Turns a property into a writable one. This for example useful if an object
// inherits a property from a definition which should not stay read-only.
global func MakePropertyWritable(name, obj)
{
	obj = obj ?? this;
	if (GetType(obj[name]) == C4V_Array)
	{
		obj[name] = obj[name][:];
		var len = GetLength(obj[name]);
		for (var i = 0; i < len; ++i)
			MakePropertyWritable(i, obj[name]);
	}
	if (GetType(obj[name]) == C4V_PropList)
	{
		obj[name] = new obj[name] {};
		var properties = GetProperties(obj[name]);
		var len = GetLength(properties);
		for (var i = 0; i < len; ++i)
			MakePropertyWritable(properties[i], obj[name]);
	}
  	return;
}